artdaq_mfextensions  v1_02_02
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 mf::MessageFacilityMsg mfviewer::LogReader::read_next()
75 {
76  mf::MessageFacilityMsg msg;
77  std::string line;
78 
79  // meta data 1
80  getline(log_, line);
81 
82  if (boost::regex_search(line, what_, metadata_1))
83  {
84 #if 0
85  std::cout << ">> " << std::string(what_[1].first, what_[1].second) << "\n";
86  std::cout << ">> " << std::string(what_[2].first, what_[2].second) << "\n";
87  std::cout << ">> " << std::string(what_[3].first, what_[3].second) << "\n";
88  std::cout << ">> " << std::string(what_[4].first, what_[4].second) << "\n";
89  std::cout << ">> " << std::string(what_[5].first, what_[5].second) << "\n";
90 #endif
91 
92  std::string value = std::string(what_[1].first, what_[1].second);
93 
94  switch (value[0])
95  {
96  case 'e': msg.setSeverity("ERROR");
97  break;
98  case 'w': msg.setSeverity("WARNING");
99  break;
100  case 'i': msg.setSeverity("INFO");
101  break;
102  case 'd': msg.setSeverity("DEBUG");
103  break;
104  default: break;
105  }
106 
107  struct tm tm;
108  time_t t;
109  timeval tv;
110 
111  value = std::string(what_[4].first, what_[4].second);
112  strptime(value.c_str(), "%d-%b-%Y %H:%M:%S", &tm);
113 
114  tm.tm_isdst = -1;
115  t = mktime(&tm);
116  tv.tv_sec = t;
117  tv.tv_usec = 0;
118 
119  msg.setCategory(std::string(what_[2].first, what_[2].second));
120  msg.setTimestamp(tv);
121  msg.setApplication(std::string(what_[3].first, what_[3].second));
122 # if MESSAGEFACILITY_HEX_VERSION >= 0x20002 // an indication of a switch from s48 to s50
123  msg.setModule(std::string(what_[5].first, what_[5].second));
124 # else
125  msg.setProcess(std::string(what_[5].first, what_[5].second));
126 # endif
127  //msg.setHostname ( std::string(what_[4].first, what_[4].second) );
128  //msg.setHostaddr ( std::string(what_[5].first, what_[5].second) );
129  }
130  std::string body;
131  getline(log_, line);
132 
133  while (!log_.eof() && line.find("%MSG") == std::string::npos)
134  {
135  body += line;
136  getline(log_, line);
137  }
138 
139  msg.setMessage(filename_, std::to_string(counter_), body);
140 
141  std::cout <<
142  "Time: " << msg.timestr() <<
143  ", Severity: " << msg.severity() <<
144  ", Category: " << msg.category() <<
145  ", Hostname: " << msg.hostname() <<
146  ", Hostaddr: " << msg.hostaddr() <<
147 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
148  ", Process: " << msg.process() <<
149 # endif
150  ", Application: " << msg.application() <<
151  ", Module: " << msg.module() <<
152  ", Context: " << msg.context() <<
153  ", File: " << msg.file() <<
154  ", Message: " << msg.message() << std::endl;
155 
156  return msg;
157 }
158 
159 #include "moc_LogReader_receiver.cpp"
160 
161 DEFINE_MFVIEWER_RECEIVER(mfviewer::LogReader)
MessageFacility Log Reader Read messagefacility log archive and reemit as messagefacility messages ...