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 qt_mf_msg mfviewer::LogReader::read_next()
00075 {
00076 std::string line;
00077
00078
00079 getline(log_, line);
00080
00081 std::string category, application, eventID;
00082 timeval tv;
00083 sev_code_t sev = SERROR;
00084
00085 if (boost::regex_search(line, what_, metadata_1))
00086 {
00087 #if 0
00088 std::cout << ">> " << std::string(what_[1].first, what_[1].second) << "\n";
00089 std::cout << ">> " << std::string(what_[2].first, what_[2].second) << "\n";
00090 std::cout << ">> " << std::string(what_[3].first, what_[3].second) << "\n";
00091 std::cout << ">> " << std::string(what_[4].first, what_[4].second) << "\n";
00092 std::cout << ">> " << std::string(what_[5].first, what_[5].second) << "\n";
00093 #endif
00094
00095 std::string value = std::string(what_[1].first, what_[1].second);
00096
00097 switch (value[0])
00098 {
00099 case 'e': sev = SERROR;
00100 break;
00101 case 'w':sev = SWARNING;
00102 break;
00103 case 'i': sev = SINFO;
00104 break;
00105 case 'd': sev = SDEBUG;
00106 break;
00107 default: break;
00108 }
00109
00110 struct tm tm;
00111 time_t t;
00112
00113 value = std::string(what_[4].first, what_[4].second);
00114 strptime(value.c_str(), "%d-%b-%Y %H:%M:%S", &tm);
00115
00116 tm.tm_isdst = -1;
00117 t = mktime(&tm);
00118 tv.tv_sec = t;
00119 tv.tv_usec = 0;
00120
00121 category = std::string(what_[2].first, what_[2].second);
00122 application = std::string(what_[3].first, what_[3].second);
00123
00124 eventID = std::string(what_[5].first, what_[5].second);
00125
00126
00127 }
00128 std::string body;
00129 getline(log_, line);
00130
00131 qt_mf_msg msg("", category, application, 0, tv);
00132 msg.setSeverityLevel(sev);
00133 msg.setEventID(eventID);
00134
00135
00136 while (!log_.eof() && line.find("%MSG") == std::string::npos)
00137 {
00138 body += line;
00139 getline(log_, line);
00140 }
00141
00142 msg.setMessage(filename_, counter_, body);
00143 msg.updateText();
00144
00145 return msg;
00146 }
00147
00148 #include "moc_LogReader_receiver.cpp"
00149
00150 DEFINE_MFVIEWER_RECEIVER(mfviewer::LogReader)