00001 #include "mfextensions/Receivers/LogReader_receiver.hh"
00002 #include "mfextensions/Receivers/ReceiverMacros.hh"
00003 #include <iostream>
00004
00005 mfviewer::LogReader::LogReader(fhicl::ParameterSet pset)
00006 : MVReceiver(pset)
00007 , log_()
00008 , pos_(0)
00009 , filename_(pset.get<std::string>("filename"))
00010 , counter_(0)
00011 , metadata_1
00012 ("\\%MSG-([wide])\\s([^:]*):\\s\\s([^\\s]*)\\s*(\\d\\d-[^-]*-\\d{4}\\s\\d+:\\d+:\\d+)\\s.[DS]T\\s\\s(\\w+)")
00013
00014
00015 {
00016 std::cout << "LogReader_receiver Constructor" << std::endl;
00017 }
00018
00019 mfviewer::LogReader::~LogReader()
00020 {
00021 log_.close();
00022 }
00023
00024 void mfviewer::LogReader::run()
00025 {
00026 while (!stopRequested_)
00027 {
00028 log_.open(filename_.c_str(), std::fstream::in);
00029 log_.seekg(pos_);
00030
00031 if (log_.is_open() && log_.good())
00032 {
00033 bool msgFound = false;
00034 while (!msgFound)
00035 {
00036 if (log_.eof())
00037 {
00038 break;
00039 }
00040
00041 std::string line;
00042 pos_ = log_.tellg();
00043 getline(log_, line);
00044 if (line.find("%MSG") != std::string::npos)
00045 {
00046 msgFound = true;
00047 log_.seekg(pos_);
00048 }
00049 }
00050
00051 if (msgFound)
00052 {
00053 std::cout << "Message found, emitting!" << std::endl;
00054 emit NewMessage(read_next());
00055 ++counter_;
00056 }
00057 log_.clear();
00058 pos_ = log_.tellg();
00059 }
00060
00061 log_.clear();
00062 log_.close();
00063 usleep(500000);
00064
00065 }
00066
00067 std::cout << "LogReader_receiver shutting down!" << std::endl;
00068 }
00069
00070
00071 #include <iostream>
00072 #include <time.h>
00073
00074 mf::MessageFacilityMsg mfviewer::LogReader::read_next()
00075 {
00076 mf::MessageFacilityMsg msg;
00077 std::string line;
00078
00079
00080 getline(log_, line);
00081
00082 if (boost::regex_search(line, what_, metadata_1))
00083 {
00084 #if 0
00085 std::cout << ">> " << std::string(what_[1].first, what_[1].second) << "\n";
00086 std::cout << ">> " << std::string(what_[2].first, what_[2].second) << "\n";
00087 std::cout << ">> " << std::string(what_[3].first, what_[3].second) << "\n";
00088 std::cout << ">> " << std::string(what_[4].first, what_[4].second) << "\n";
00089 std::cout << ">> " << std::string(what_[5].first, what_[5].second) << "\n";
00090 #endif
00091
00092 std::string value = std::string(what_[1].first, what_[1].second);
00093
00094 switch (value[0])
00095 {
00096 case 'e': msg.setSeverity("ERROR");
00097 break;
00098 case 'w': msg.setSeverity("WARNING");
00099 break;
00100 case 'i': msg.setSeverity("INFO");
00101 break;
00102 case 'd': msg.setSeverity("DEBUG");
00103 break;
00104 default: break;
00105 }
00106
00107 struct tm tm;
00108 time_t t;
00109 timeval tv;
00110
00111 value = std::string(what_[4].first, what_[4].second);
00112 strptime(value.c_str(), "%d-%b-%Y %H:%M:%S", &tm);
00113
00114 tm.tm_isdst = -1;
00115 t = mktime(&tm);
00116 tv.tv_sec = t;
00117 tv.tv_usec = 0;
00118
00119 msg.setCategory(std::string(what_[2].first, what_[2].second));
00120 msg.setTimestamp(tv);
00121 msg.setApplication(std::string(what_[3].first, what_[3].second));
00122 # if MESSAGEFACILITY_HEX_VERSION >= 0x20002 // an indication of a switch from s48 to s50
00123 msg.setModule(std::string(what_[5].first, what_[5].second));
00124 # else
00125 msg.setProcess(std::string(what_[5].first, what_[5].second));
00126 # endif
00127
00128
00129 }
00130 std::string body;
00131 getline(log_, line);
00132
00133 while (!log_.eof() && line.find("%MSG") == std::string::npos)
00134 {
00135 body += line;
00136 getline(log_, line);
00137 }
00138
00139 msg.setMessage(filename_, std::to_string(counter_), body);
00140
00141 std::cout <<
00142 "Time: " << msg.timestr() <<
00143 ", Severity: " << msg.severity() <<
00144 ", Category: " << msg.category() <<
00145 ", Hostname: " << msg.hostname() <<
00146 ", Hostaddr: " << msg.hostaddr() <<
00147 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
00148 ", Process: " << msg.process() <<
00149 # endif
00150 ", Application: " << msg.application() <<
00151 ", Module: " << msg.module() <<
00152 ", Context: " << msg.context() <<
00153 ", File: " << msg.file() <<
00154 ", Message: " << msg.message() << std::endl;
00155
00156 return msg;
00157 }
00158
00159 #include "moc_LogReader_receiver.cpp"
00160
00161 DEFINE_MFVIEWER_RECEIVER(mfviewer::LogReader)