#ifndef __JDB_JDATALOGSTRING__ #define __JDB_JDATALOGSTRING__ #include #include #include #include #include #include #include #include #include #include #include "JLang/JException.hh" #include "JLang/gzstream.h" #include "JSystem/JDateAndTime.hh" #include "Jeep/JeepToolkit.hh" namespace JDB {} namespace JPP { using namespace JDB; } namespace JDB { using JLANG::JFileOpenException; /** * Auxiliary data structure for datalog strings. */ struct JDatalogString { static const char* const getName() { return "datalogstrings"; } //!< Table name /** * Get match. * * \param datalog datalog * \return true if datalog matches; else false */ bool match(const JDatalogString& datalog) const { using namespace std; if (!datalog.source .empty() && datalog.source != source) { return false; } if (!datalog.parameter.empty() && datalog.parameter != parameter) { return false; } if (!datalog.data .empty() && !regex_match(data, regex(datalog.data))) { return false; } return true; } /** * Read datalog from input. * * \param in input stream * \param object datalog * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JDatalogString& object) { object = JDatalogString(); in >> object.run >> object.utc >> object.source >> object.parameter; while (in.peek() != EOF && isspace((char) in.peek())) { in.ignore(1); } return getline(in, object.data); } /** * Write datalog to output. * * \param out output stream * \param object datalog * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JDatalogString& object) { using namespace JPP; JDateAndTime utc(object.utc / 1000, 1.0e-3 * (float) (object.utc%1000) , true); //out << object.run << ' '; out << object.utc << ' '; out << utc << ' '; out << object.source << ' '; out << object.parameter << ' '; out << object.data; return out; } int run; long int utc; std::string source; std::string parameter; std::string data; }; /** * Auxiliary data structure for selecting data. */ struct JKeywords : public std::vector { /** * Constructor. * * \param buffer list of valid source names */ JKeywords(const std::initializer_list& buffer) : std::vector(buffer) {} /** * Test validity of target. * A target is valid if it contains one of the keywords. * * \param target target * \return true if valid; else false */ bool operator()(const std::string& target) const { for (const_iterator i = this->begin(); i != this->end(); ++i) { if (target.find(*i) != std::string::npos) { return true; } } return false; } }; /** * Valid source names. */ const JKeywords SOURCE_NAMES = { "3.4.3.2", "Born", "Died", "LocalAuthenticationProvider", "MasterControlProgram", "MSG", "RC_LOG", "RC_REPLY", "TriDASManager" }; /** * Ignored parameter names. */ const JKeywords IGNORED_PARAMETER_NAMES = { //"DispatcherPutMessage" "DispatchMessage" }; /** * Container for datalog strings. */ struct JDatalogs_t : public std::vector { /** * Constructor. * * \param buffer list of valid source names */ JDatalogs_t(const std::initializer_list& buffer = {}) : sources(buffer) {} /** * Constructor. * * \param buffer list of valid source names */ JDatalogs_t(const std::set& buffer) : sources(buffer) {} /** * Load message from input file. * * \param file_name file name */ void load(const std::string& file_name) { using namespace std; using namespace JPP; if (getFilenameExtension(file_name) == "log" || getFilenameExtension(file_name) == "txt") { ifstream in(file_name.c_str()); in.ignore(numeric_limits::max(), '\n'); in >> *this; in.close(); } else if (getFilenameExtension(file_name) == "gz") { igzstream in(file_name.c_str()); in.ignore(numeric_limits::max(), '\n'); in >> *this; in.close(); } else { THROW(JFileOpenException, "Invalid file name extension " << file_name); } } /** * Read message from input. * * \param in input stream * \param object messages * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JDatalogs_t& object) { using namespace std; JDatalogString datalog; bool append = false; for (string buffer; getline(in, buffer); ) { istringstream is(buffer); if (is >> datalog.run >> datalog.utc >> datalog.source >> datalog.parameter) { append = false; if (SOURCE_NAMES(datalog.source) && !IGNORED_PARAMETER_NAMES(datalog.parameter)) { while (is.peek() != EOF && isspace((char) is.peek())) { is.ignore(1); } getline(is, datalog.data); // new datalog if (object.sources.empty() || object.sources.count(datalog.source) != 0) { object.push_back(datalog); append = true; } } } else if (append) { // append traling data to latest datalog if (!object.empty()) { object.rbegin()->data.append(" " + buffer); } } } return in; } protected: std::set sources; }; } #endif