artdaq_mfextensions  v1_03_05
LogReader_receiver.cc
1 #include "mfextensions/Receivers/LogReader_receiver.hh"
2 #include <iostream>
3 #include "mfextensions/Receivers/ReceiverMacros.hh"
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 
20 
22  while (!stopRequested_) {
23  log_.open(filename_.c_str(), std::fstream::in);
24  log_.seekg(pos_);
25 
26  if (log_.is_open() && log_.good()) {
27  bool msgFound = false;
28  while (!msgFound) {
29  if (log_.eof()) {
30  break;
31  }
32 
33  std::string line;
34  pos_ = log_.tellg();
35  getline(log_, line);
36  if (line.find("%MSG") != std::string::npos) {
37  msgFound = true;
38  log_.seekg(pos_);
39  }
40  }
41 
42  if (msgFound) {
43  std::cout << "Message found, emitting!" << std::endl;
44  emit NewMessage(read_next());
45  ++counter_;
46  }
47  log_.clear();
48  pos_ = log_.tellg();
49  }
50 
51  log_.clear();
52  log_.close();
53  usleep(500000);
54  // sleep(1);
55  }
56 
57  std::cout << "LogReader_receiver shutting down!" << std::endl;
58 }
59 
60 #include <time.h>
61 #include <iostream>
62 
64  std::string line;
65 
66  // meta data 1
67  getline(log_, line);
68 
69  std::string category, application, eventID;
70  timeval tv = {0, 0};
71  sev_code_t sev = SERROR;
72 
73  if (boost::regex_search(line, what_, metadata_1)) {
74 #if 0
75  std::cout << ">> " << std::string(what_[1].first, what_[1].second) << "\n";
76  std::cout << ">> " << std::string(what_[2].first, what_[2].second) << "\n";
77  std::cout << ">> " << std::string(what_[3].first, what_[3].second) << "\n";
78  std::cout << ">> " << std::string(what_[4].first, what_[4].second) << "\n";
79  std::cout << ">> " << std::string(what_[5].first, what_[5].second) << "\n";
80 #endif
81 
82  std::string value = std::string(what_[1].first, what_[1].second);
83 
84  switch (value[0]) {
85  case 'e':
86  sev = SERROR;
87  break;
88  case 'w':
89  sev = SWARNING;
90  break;
91  case 'i':
92  sev = SINFO;
93  break;
94  case 'd':
95  sev = SDEBUG;
96  break;
97  default:
98  break;
99  }
100 
101  struct tm tm;
102  time_t t;
103 
104  value = std::string(what_[4].first, what_[4].second);
105  strptime(value.c_str(), "%d-%b-%Y %H:%M:%S", &tm);
106 
107  tm.tm_isdst = -1;
108  t = mktime(&tm);
109  tv.tv_sec = t;
110  tv.tv_usec = 0;
111 
112  category = std::string(what_[2].first, what_[2].second);
113  application = std::string(what_[3].first, what_[3].second);
114 
115  eventID = std::string(what_[5].first, what_[5].second);
116  // msg.setHostname ( std::string(what_[4].first, what_[4].second) );
117  // msg.setHostaddr ( std::string(what_[5].first, what_[5].second) );
118  }
119  std::string body;
120  getline(log_, line);
121 
122  qt_mf_msg msg("", category, application, 0, tv);
123  msg.setSeverityLevel(sev);
124  msg.setEventID(eventID);
125 
126  while (!log_.eof() && line.find("%MSG") == std::string::npos) {
127  body += line;
128  getline(log_, line);
129  }
130 
131  msg.setMessage(filename_, counter_, body);
132  msg.updateText();
133 
134  return msg;
135 }
136 
137 #include "moc_LogReader_receiver.cpp"
138 
139 DEFINE_MFVIEWER_RECEIVER(mfviewer::LogReader)
void updateText()
Parse fields and create HTML string representing message
Definition: qt_mf_msg.cc:59
void setEventID(std::string eventID)
Set the Event ID of the message
Definition: qt_mf_msg.hh:133
LogReader(fhicl::ParameterSet pset)
LogReader Constructor
qt_mf_msg read_next()
Read the next message from the input stream
virtual ~LogReader()
LogReader Destructor
Qt wrapper around MessageFacility message
Definition: qt_mf_msg.hh:30
void run()
Receiver loop method. Reads messages from file and emits newMessage signal
void setSeverityLevel(sev_code_t sev)
Set the severity code of the message (Viewer levels)
Definition: qt_mf_msg.hh:101
void setMessage(std::string prefix, int iteration, std::string msg)
Set the message
Definition: qt_mf_msg.cc:53
MessageFacility Log Reader Read messagefacility log archive and reemit as messagefacility messages ...
A MVReceiver class listens for messages and raises a signal when one arrives
Definition: MVReceiver.hh:17