artdaq_mfextensions  v1_07_02
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  this->setObjectName("viewer Log");
12 }
13 
15 {
16  try
17  {
18  log_.close();
19  }
20  catch (...)
21  {
22  // IGNORED
23  }
24 }
25 
27 {
28  while (!stopRequested_)
29  {
30  log_.open(filename_.c_str(), std::fstream::in);
31  log_.seekg(pos_);
32 
33  if (log_.is_open() && log_.good())
34  {
35  bool msgFound = false;
36  while (!msgFound)
37  {
38  if (log_.eof())
39  {
40  break;
41  }
42 
43  std::string line;
44  pos_ = log_.tellg();
45  getline(log_, line);
46  if (line.find("%MSG") != std::string::npos)
47  {
48  msgFound = true;
49  log_.seekg(pos_);
50  }
51  }
52 
53  if (msgFound)
54  {
55  std::cout << "Message found, emitting!" << std::endl;
56  emit NewMessage(read_next());
57  ++counter_;
58  }
59  log_.clear();
60  pos_ = log_.tellg();
61  }
62 
63  log_.clear();
64  log_.close();
65  usleep(500000);
66  // sleep(1);
67  }
68 
69  std::cout << "LogReader_receiver shutting down!" << std::endl;
70 }
71 
72 #include <ctime>
73 #include <iostream>
74 
76 {
77  std::string line;
78 
79  // meta data 1
80  getline(log_, line);
81 
82  std::string category, application, eventID;
83  timeval tv = {0, 0};
84  sev_code_t sev = SERROR;
85 
86  if (boost::regex_search(line, what_, metadata_1))
87  {
88 #if 0
89  std::cout << ">> " << std::string(what_[1].first, what_[1].second) << "\n";
90  std::cout << ">> " << std::string(what_[2].first, what_[2].second) << "\n";
91  std::cout << ">> " << std::string(what_[3].first, what_[3].second) << "\n";
92  std::cout << ">> " << std::string(what_[4].first, what_[4].second) << "\n";
93  std::cout << ">> " << std::string(what_[5].first, what_[5].second) << "\n";
94 #endif
95 
96  std::string value = std::string(what_[1].first, what_[1].second);
97 
98  switch (value[0])
99  {
100  case 'e':
101  sev = SERROR;
102  break;
103  case 'w':
104  sev = SWARNING;
105  break;
106  case 'i':
107  sev = SINFO;
108  break;
109  case 'd':
110  sev = SDEBUG;
111  break;
112  default:
113  break;
114  }
115 
116  struct tm tm;
117  time_t t;
118 
119  value = std::string(what_[4].first, what_[4].second);
120  strptime(value.c_str(), "%d-%b-%Y %H:%M:%S", &tm);
121 
122  tm.tm_isdst = -1;
123  t = mktime(&tm);
124  tv.tv_sec = t;
125  tv.tv_usec = 0;
126 
127  category = std::string(what_[2].first, what_[2].second);
128  application = std::string(what_[3].first, what_[3].second);
129 
130  eventID = std::string(what_[5].first, what_[5].second);
131  // msg.setHostname ( std::string(what_[4].first, what_[4].second) );
132  // msg.setHostaddr ( std::string(what_[5].first, what_[5].second) );
133  }
134  std::string body;
135  getline(log_, line);
136 
137  msg_ptr_t msg = std::make_shared<qt_mf_msg>("", category, application, 0, tv);
138  msg->setSeverityLevel(sev);
139  msg->setEventID(eventID);
140 
141  while (!log_.eof() && line.find("%MSG") == std::string::npos)
142  {
143  body += line;
144  getline(log_, line);
145  }
146 
147  msg->setMessage(filename_, counter_, body);
148  msg->updateText();
149 
150  return msg;
151 }
152 
153 #include "moc_LogReader_receiver.cpp"
154 
155 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