00001 00007 #ifndef _QMSG_LOGGER_HH_ 00008 #define _QMSG_LOGGER_HH_ 00009 00010 // QFramework includes 00011 #include "QCuore.hh" 00012 #include "QLogInfo.hh" 00013 #include "QDaqCommonDefs.hh" 00014 #include "QMessageDefs.hh" 00015 #include "QError.hh" 00016 00017 // standard C/C++ includes 00018 #include <fstream> 00019 #include <string> 00020 #include <list> 00021 #include <map> 00022 00023 class QMsgLogger { 00024 public: 00025 00026 struct QProcessInfo_t 00027 { 00028 pid_t pid; 00029 QDaqCommonDefs::ProcessStatus_t status; 00030 std::string hostname; 00031 }; 00032 00034 static QMsgLogger& GetInstance(); 00035 00037 virtual ~QMsgLogger(); 00038 00039 bool HasError() const { return fError != QERR_SUCCESS; } 00040 void ResetError() { fError.Set(QERR_SUCCESS); } 00041 const QError& GetError() const { return fError; } 00042 00051 bool RegisterProcess(const std::string& process, 00052 const std::string& host, 00053 pid_t pid); 00054 00061 bool ReleaseProcess(const std::string& process); 00062 00070 bool UpdateStatus(const std::string& process, 00071 QDaqCommonDefs::ProcessStatus_t status); 00072 00081 void WriteLog(MsgLevel severity, 00082 const std::string& sender, 00083 const std::string& message); 00084 00090 void Write(const std::string& message, MsgLevel severity); 00091 00096 std::list<std::string> GetRegisteredProcesses() const; 00097 00113 bool GetProcessInfo(const std::string& process, 00114 pid_t* pid, 00115 QDaqCommonDefs::ProcessStatus_t* status, 00116 std::string* host); 00117 00118 00120 unsigned long GetLastLogId() const { return fMessageCtr; } 00121 00123 unsigned long GetFirstValidLogId() const { return fFirstValidMsgId; } 00124 00126 unsigned long GetTimeLogId(const time_t& msgTime) const; 00127 00134 bool GetFormattedLogMessage(const unsigned long& id,std::string& message); 00135 00142 bool GetLogMessage(const unsigned long& id,QLogInfo& logInfo); 00143 00144 private: 00146 QMsgLogger(); 00147 00149 std::map<std::string,QProcessInfo_t*> fProcesses; 00150 00151 std::map<unsigned long,QLogInfo> fMessages; 00152 00153 unsigned long fFirstValidMsgId; 00154 unsigned long fMessageCtr; 00155 00156 const unsigned int kMaxMsgSize; 00157 const unsigned int kMsgDelBunchSize; 00158 00159 std::ofstream fOs; 00160 00161 void AddLogEntry(MsgLevel severity, 00162 const std::string& sender, 00163 const std::string& message, 00164 const time_t& msgTime); 00165 00166 QError fError; 00167 00172 const std::string& GetTime() const; 00173 00179 const std::string& FormatForLog(MsgLevel severity) const; 00180 00181 }; 00182 00183 #endif // _QMSG_LOGGER_HH_