#ifndef __JDB_JLOGGERMESSAGE__ #define __JDB_JLOGGERMESSAGE__ #include #include #include #include #include #include "JSystem/JDateAndTime.hh" #include "JNet/JTag.hh" #include "JLang/JException.hh" #include "JLang/JManip.hh" #include "JLogger/JMessageLogger.hh" #include "JDAQ/JDAQTags.hh" #include "JDatalogString.hh" namespace JDB {} namespace JPP { using namespace JDB; } namespace JDB { using JLANG::JFileOpenException; using JLANG::JParseError; using JSYSTEM::JDateAndTime; /** * Auxiliary data structure for logger message. */ struct JLoggerMessage { /** * Default constructor. */ JLoggerMessage() {} /** * Copy constructor. * * \param datalog data log */ JLoggerMessage(const JDatalogString& datalog) { using namespace std; using namespace JPP; using namespace KM3NETDAQ; if (datalog.source == MESSAGE_TAG) { this->tag = datalog.source; istringstream is(datalog.data); if (is >> this->datim >> this->ip >> this->nickname >> this->level) { this->datim.set(datalog.utc / 1000, (float) (datalog.utc%1000) * 1.0e-3, true); while (is.peek() != EOF && isspace((char) is.peek())) { is.ignore(1); } getline(is, this->data); } else { THROW(JParseError, "Invalid string " << datalog.source << " " << datalog.data); } } else { THROW(JParseError, "Invalid source " << datalog.source); } } /** * Get match. * * \param message message * \return true if message matches; else false */ bool match(const JLoggerMessage& message) const { using namespace std; if (!message.tag .empty() && message.tag != tag) { return false; } if (!message.process .empty() && message.process != process) { return false; } if (!message.nickname.empty() && message.nickname != nickname) { return false; } if (!message.ip .empty() && message.ip != ip) { return false; } if (!message.fullname.empty() && message.fullname != fullname) { return false; } if (!message.level .empty() && message.level != level) { return false; } if (!message.data .empty() && !regex_match(data, regex(message.data))) { return false; } return true; } /** * Check if message has date and time. * * \return true if messages has date and time; else false */ bool hasTime() const { using namespace JPP; return (tag != DISPTAG_Born.toString() && tag != DISPTAG_Died.toString()); } /** * Get UTC time. * * \return time [ms] */ long int getTime() const { return (long int) 1000 * (long int) datim.getTime() + (long int) 500; } /** * Read message from input. * * \param in input stream * \param object message * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JLoggerMessage& object) { using namespace JPP; object = JLoggerMessage(); in >> object.file_name >> object.tag >> object.process; if (object.tag != DISPTAG_Born.toString() && object.tag != DISPTAG_Died.toString()) { in >> object.datim >> object.ip >> object.nickname >> object.level; while (in.peek() != EOF && isspace((char) in.peek())) { in.ignore(1); } return getline(in, object.data); } else { return in >> object.fullname; } } /** * Write message to output. * * \param out output stream * \param object message * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JLoggerMessage& object) { using namespace std; using namespace JPP; if (object.tag != DISPTAG_Born.toString() && object.tag != DISPTAG_Died.toString()) { out << LEFT(8) << object.tag << ' ' << right; out << object.datim << ' '; //out << object.getTime() << ' '; //out << object.ip << ' '; out << object.nickname << ' '; //out << object.level << ' '; out << object.data; } else { out << object.tag << ' '; //out << object.process << ' '; out << object.fullname; } return out; } std::string file_name; std::string tag; std::string process; JDateAndTime datim; std::string ip; std::string nickname; std::string fullname; std::string level; std::string data; }; } #endif