artdaq_mfextensions  v1_03_01
LogReader_receiver.cc
1 #include "mfextensions/Receivers/LogReader_receiver.hh"
2 #include "mfextensions/Receivers/ReceiverMacros.hh"
3 #include <iostream>
4 
5 mfviewer::LogReader::LogReader(fhicl::ParameterSet pset)
6  : MVReceiver(pset)
7  , log_()
8  , pos_(0)
9  , filename_(pset.get<std::string>("filename"))
10  , counter_(0)
11  , metadata_1
12  ("\\%MSG-([wide])\\s([^:]*):\\s\\s([^\\s]*)\\s*(\\d\\d-[^-]*-\\d{4}\\s\\d+:\\d+:\\d+)\\s.[DS]T\\s\\s(\\w+)")
13  //, metadata_2
14  // ( "([^\\s]*)\\s([^\\s]*)\\s([^\\s]*)\\s(([^\\s]*)\\s)?([^:]*):(\\d*)" )
15 {
16  std::cout << "LogReader_receiver Constructor" << std::endl;
17 }
18 
19 mfviewer::LogReader::~LogReader()
20 {
21  log_.close();
22 }
23 
24 void mfviewer::LogReader::run()
25 {
26  while (!stopRequested_)
27  {
28  log_.open(filename_.c_str(), std::fstream::in);
29  log_.seekg(pos_);
30 
31  if (log_.is_open() && log_.good())
32  {
33  bool msgFound = false;
34  while (!msgFound)
35  {
36  if (log_.eof())
37  {
38  break;
39  }
40 
41  std::string line;
42  pos_ = log_.tellg();
43  getline(log_, line);
44  if (line.find("%MSG") != std::string::npos)
45  {
46  msgFound = true;
47  log_.seekg(pos_);
48  }
49  }
50 
51  if (msgFound)
52  {
53  std::cout << "Message found, emitting!" << std::endl;
54  emit NewMessage(read_next());
55  ++counter_;
56  }
57  log_.clear();
58  pos_ = log_.tellg();
59  }
60 
61  log_.clear();
62  log_.close();
63  usleep(500000);
64  //sleep(1);
65  }
66 
67  std::cout << "LogReader_receiver shutting down!" << std::endl;
68 }
69 
70 
71 #include <iostream>
72 #include <time.h>
73 
74 qt_mf_msg mfviewer::LogReader::read_next()
75 {
76  std::string line;
77 
78  // meta data 1
79  getline(log_, line);
80 
81  std::string category, application, eventID;
82  timeval tv;
83  sev_code_t sev = SERROR;
84 
85  if (boost::regex_search(line, what_, metadata_1))
86  {
87 #if 0
88  std::cout << ">> " << std::string(what_[1].first, what_[1].second) << "\n";
89  std::cout << ">> " << std::string(what_[2].first, what_[2].second) << "\n";
90  std::cout << ">> " << std::string(what_[3].first, what_[3].second) << "\n";
91  std::cout << ">> " << std::string(what_[4].first, what_[4].second) << "\n";
92  std::cout << ">> " << std::string(what_[5].first, what_[5].second) << "\n";
93 #endif
94 
95  std::string value = std::string(what_[1].first, what_[1].second);
96 
97  switch (value[0])
98  {
99  case 'e': sev = SERROR;
100  break;
101  case 'w':sev = SWARNING;
102  break;
103  case 'i': sev = SINFO;
104  break;
105  case 'd': sev = SDEBUG;
106  break;
107  default: break;
108  }
109 
110  struct tm tm;
111  time_t t;
112 
113  value = std::string(what_[4].first, what_[4].second);
114  strptime(value.c_str(), "%d-%b-%Y %H:%M:%S", &tm);
115 
116  tm.tm_isdst = -1;
117  t = mktime(&tm);
118  tv.tv_sec = t;
119  tv.tv_usec = 0;
120 
121  category = std::string(what_[2].first, what_[2].second);
122  application = std::string(what_[3].first, what_[3].second);
123 
124  eventID = std::string(what_[5].first, what_[5].second);
125  //msg.setHostname ( std::string(what_[4].first, what_[4].second) );
126  //msg.setHostaddr ( std::string(what_[5].first, what_[5].second) );
127  }
128  std::string body;
129  getline(log_, line);
130 
131  qt_mf_msg msg("", category, application, 0, tv);
132  msg.setSeverityLevel(sev);
133  msg.setEventID(eventID);
134 
135 
136  while (!log_.eof() && line.find("%MSG") == std::string::npos)
137  {
138  body += line;
139  getline(log_, line);
140  }
141 
142  msg.setMessage(filename_, counter_, body);
143  msg.updateText();
144 
145  return msg;
146 }
147 
148 #include "moc_LogReader_receiver.cpp"
149 
150 DEFINE_MFVIEWER_RECEIVER(mfviewer::LogReader)
Qt wrapper around MessageFacility message
Definition: qt_mf_msg.hh:37
MessageFacility Log Reader Read messagefacility log archive and reemit as messagefacility messages ...