#include #include #include #include #include #include #include "JLogger/JMessageLogger.hh" #include "JNet/JControlHost.hh" #include "JLang/JTimeval.hh" #include "JLang/JException.hh" #include "JLang/gzstream.h" #include "JSystem/JDate.hh" #include "JSystem/JStat.hh" #include "Jeep/JeepToolkit.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" namespace { using namespace JPP; const std::string save = "save"; //!< Close and re-open file; const std::string stop = "stop"; //!< Stop process. const int MINIMUM_NUMBER_OF_MESSAGES = 100; const double MINIMUM_ELAPSED_TIME_S = 60*60; /** * Logger file. */ struct JLoggerFile : public ogzstream, public JDateAndTime { static const int MAXIMUM_FILE_NUMBER = 100; /** * Constructor. * * \param path path */ JLoggerFile(const std::string& path) : path(path) {} /** * Get current file name. * * \return file name */ std::string getFilename() const { return file_name; } /** * Open file. */ void open() { using namespace std; JDateAndTime::set(); for (int i = 0; !this->is_open() && i != MAXIMUM_FILE_NUMBER; ++i) { ostringstream os; os << getFullPath(path) << "KM3NeT" << "_" << this->getYear() << '-' << FILL(2,'0') << this->getMonth() << '-' << FILL(2,'0') << this->getDay(); if (i != 0) { os << "_" << i; } os << ".txt.gz"; file_name = os.str(); if (!getFileStatus(file_name.c_str())) { ogzstream::open(file_name.c_str()); } } } /** * Close file. */ void close() { using namespace std; ogzstream::close(); file_name = ""; } protected: std::string path; std::string file_name; }; } /** * \file * * Auxiliary program to save logger messages from ControlHost server. * * The option -H \[:port] correponds to the hostname and the port of the server, respectively. * Each message tag will be written to file in the directory specified. * The program will terminate when it receives message \. * \author mdejong */ int main(int argc, const char *argv[]) { using namespace std; using namespace JPP; string hostname; set taglist; string path; JTimeval timeout; set happy_hour; int debug; taglist.insert(MESSAGE_TAG); try { JParser<> zap("Auxiliary program to save logger messages from ControlHost server."); zap['H'] = make_field(hostname) = "localhost"; zap['T'] = make_field(taglist) = JPARSER::initialised(); zap['D'] = make_field(path) = "/tmp/"; zap['t'] = make_field(timeout) = JTimeval(1, 0); zap['W'] = make_field(happy_hour) = JPARSER::initialised(); zap['d'] = make_field(debug) = 1; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } if (happy_hour.empty()) { WARNING("No happy hours (option -W); set to midnight." << endl); happy_hour.insert(0); } JControlHost::Throw(true); try { JControlHost in(hostname); { JSubscriptionList buffer; for (set::const_iterator i = taglist.begin(); i != taglist.end(); ++i) { buffer.add(JSubscriptionAll(*i)); } in.Subscribe(buffer); in.SendMeAlways(); } JLoggerFile out(path); out.open(); DEBUG("Open file " << out.getFilename() << endl); JPrefix prefix; string buffer; JDateAndTime clock; for (int number_of_messages; buffer.size() != stop.size() || string(buffer.data(), stop.size()) != stop; ) { const int check = in.CheckHead(prefix, timeout); DEBUG("Check head " << check << endl); if (check < 0) { FATAL("Error at JControlHost::CheckHead " << check << endl); } else if (check == 1) { in.GetFullString(buffer); DEBUG("Message <" << buffer << ">" << endl); if (buffer == stop) { break; } else if (buffer == save) { DEBUG("Close file " << out.getFilename() << endl); out.close(); out.open(); DEBUG("Open file " << out.getFilename() << endl); } else { ++number_of_messages; if ( ! (out << buffer << endl)) { FATAL("Error writing to file " << out.getFilename() << endl); } } } if (check == 0 || number_of_messages > MINIMUM_NUMBER_OF_MESSAGES) { number_of_messages = 0; clock.set(); DEBUG("Test " << setw(2) << clock.getHour() << ' ' << happy_hour.count(clock.getHour()) << ' ' << clock.getElapsedTime(out) << endl); if (happy_hour.count(clock.getHour()) != 0 && clock.getElapsedTime(out) > MINIMUM_ELAPSED_TIME_S) { DEBUG("Close file " << out.getFilename() << endl); out.close(); out.open(); DEBUG("Open file " << out.getFilename() << endl); } } } out.close(); } catch(const JControlHostException& error) { ERROR(error << endl); } }