artdaq_mfextensions  v1_05_00
LogReader_receiver.cc
1 #include "mfextensions/Receivers/LogReader_receiver.hh"
2 #include <iostream>
3 #include "mfextensions/Receivers/ReceiverMacros.hh"
4 
5 mfviewer::LogReader::LogReader(const fhicl::ParameterSet& pset)
6  : MVReceiver(pset), pos_(0), filename_(pset.get<std::string>("filename")), counter_(0), metadata_1(R"(\%MSG-([wide])\s([^:]*):\s\s([^\s]*)\s*(\d\d-[^-]*-\d{4}\s\d+:\d+:\d+)\s.[DS]T\s\s(\w+))")
7 //, metadata_2
8 // ( "([^\\s]*)\\s([^\\s]*)\\s([^\\s]*)\\s(([^\\s]*)\\s)?([^:]*):(\\d*)" )
9 {
10  std::cout << "LogReader_receiver Constructor" << std::endl;
11 }
12 
14 {
15  try
16  {
17  log_.close();
18  }
19  catch (...)
20  {
21  // IGNORED
22  }
23 }
24 
26 {
27  while (!stopRequested_)
28  {
29  log_.open(filename_.c_str(), std::fstream::in);
30  log_.seekg(pos_);
31 
32  if (log_.is_open() && log_.good())
33  {
34  bool msgFound = false;
35  while (!msgFound)
36  {
37  if (log_.eof())
38  {
39  break;
40  }
41 
42  std::string line;
43  pos_ = log_.tellg();
44  getline(log_, line);
45  if (line.find("%MSG") != std::string::npos)
46  {
47  msgFound = true;
48  log_.seekg(pos_);
49  }
50  }
51 
52  if (msgFound)
53  {
54  std::cout << "Message found, emitting!" << std::endl;
55  emit NewMessage(read_next());
56  ++counter_;
57  }
58  log_.clear();
59  pos_ = log_.tellg();
60  }
61 
62  log_.clear();
63  log_.close();
64  usleep(500000);
65  // sleep(1);
66  }
67 
68  std::cout << "LogReader_receiver shutting down!" << std::endl;
69 }
70 
71 #include <ctime>
72 #include <iostream>
73 
75 {
76  std::string line;
77 
78  // meta data 1
79  getline(log_, line);
80 
81  std::string category, application, eventID;
82  timeval tv = {0, 0};
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':
100  sev = SERROR;
101  break;
102  case 'w':
103  sev = SWARNING;
104  break;
105  case 'i':
106  sev = SINFO;
107  break;
108  case 'd':
109  sev = SDEBUG;
110  break;
111  default:
112  break;
113  }
114 
115  struct tm tm;
116  time_t t;
117 
118  value = std::string(what_[4].first, what_[4].second);
119  strptime(value.c_str(), "%d-%b-%Y %H:%M:%S", &tm);
120 
121  tm.tm_isdst = -1;
122  t = mktime(&tm);
123  tv.tv_sec = t;
124  tv.tv_usec = 0;
125 
126  category = std::string(what_[2].first, what_[2].second);
127  application = std::string(what_[3].first, what_[3].second);
128 
129  eventID = std::string(what_[5].first, what_[5].second);
130  // msg.setHostname ( std::string(what_[4].first, what_[4].second) );
131  // msg.setHostaddr ( std::string(what_[5].first, what_[5].second) );
132  }
133  std::string body;
134  getline(log_, line);
135 
136  msg_ptr_t msg = std::make_shared<qt_mf_msg>("", category, application, 0, tv);
137  msg->setSeverityLevel(sev);
138  msg->setEventID(eventID);
139 
140  while (!log_.eof() && line.find("%MSG") == std::string::npos)
141  {
142  body += line;
143  getline(log_, line);
144  }
145 
146  msg->setMessage(filename_, counter_, body);
147  msg->updateText();
148 
149  return msg;
150 }
151 
152 #include "moc_LogReader_receiver.cpp"
153 
154 DEFINE_MFVIEWER_RECEIVER(mfviewer::LogReader)
LogReader(const fhicl::ParameterSet &pset)
LogReader Constructor
virtual ~LogReader()
LogReader Destructor
void run()
Receiver loop method. Reads messages from file and emits newMessage signal
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
msg_ptr_t read_next()
Read the next message from the input stream