diff --git a/common/log/DummyLogger.cpp b/common/log/DummyLogger.cpp index 5f7e1b0046c8984f3d20b25875510879ae9c9442..1103e23f7f12193a8c1ecbd81a5d8802c029901a 100644 --- a/common/log/DummyLogger.cpp +++ b/common/log/DummyLogger.cpp @@ -40,9 +40,9 @@ DummyLogger::~DummyLogger() { void DummyLogger::prepareForFork() {} //------------------------------------------------------------------------------ -// reducedSyslog +// writeMsgToUnderlyingLoggingSystem //------------------------------------------------------------------------------ -void DummyLogger::reducedSyslog(const std::string & msg) {} +void DummyLogger::writeMsgToUnderlyingLoggingSystem(const std::string &msg) {} } // namespace log } // namespace cta diff --git a/common/log/DummyLogger.hpp b/common/log/DummyLogger.hpp index 18d528bdc880f1f4ea3e810853b7708faa54355e..c808f805d745990400f4cb4351389497c19b7467 100644 --- a/common/log/DummyLogger.hpp +++ b/common/log/DummyLogger.hpp @@ -57,7 +57,16 @@ public: void prepareForFork() ; protected: - virtual void reducedSyslog(const std::string & msg); + + /** + * Writes the specified log message to the underlying logging system. + * + * This method is to be implemented by concrete sub-classes of the Logger + * class. + * + * @param msg The message to be logged. + */ + void writeMsgToUnderlyingLoggingSystem(const std::string &msg) override; }; // class DummyLogger diff --git a/common/log/FileLogger.cpp b/common/log/FileLogger.cpp index 6cc7e92807dd483cb76b8066e5e83809f885bde4..6b777a5c2ccb0f7131c0fc48f0e4a30644149b5c 100644 --- a/common/log/FileLogger.cpp +++ b/common/log/FileLogger.cpp @@ -54,11 +54,11 @@ void FileLogger::prepareForFork() { } //----------------------------------------------------------------------------- -// reducedSyslog +// writeMsgToUnderlyingLoggingSystem //----------------------------------------------------------------------------- -void FileLogger::reducedSyslog(const std::string & msg) { +void FileLogger::writeMsgToUnderlyingLoggingSystem(const std::string &msg) { if (-1 == m_fd) - throw cta::exception::Exception("In FileLogger::reducedSyslog(): file is not properly initialized"); + throw cta::exception::Exception("In FileLogger::writeMsgToUnderlyingLoggingSystem(): file is not properly initialized"); // Prepare the string to print (for size) std::string m = msg.substr(0, m_maxMsgLen) + "\n"; @@ -67,7 +67,7 @@ void FileLogger::reducedSyslog(const std::string & msg) { // Append the message to the file cta::exception::Errnum::throwOnMinusOne(::write(m_fd, m.c_str(), m.size()), - "In FileLogger::reducedSyslog(): failed to write to file"); + "In FileLogger::writeMsgToUnderlyingLoggingSystem(): failed to write to file"); } } // namespace log diff --git a/common/log/FileLogger.hpp b/common/log/FileLogger.hpp index 14e36a8ee8b2f154b21be04113c828cede4565e6..f6b8f47b5a98a854e98d5d7e203bf60d9bb6e1aa 100644 --- a/common/log/FileLogger.hpp +++ b/common/log/FileLogger.hpp @@ -68,15 +68,14 @@ protected: int m_fd=-1; /** - * A reduced version of syslog. This method is able to set the message - * timestamp. This is necessary when logging messages asynchronously of there - * creation, such as when retrieving logs from the DB. This function truncates - * the message if necessary. It also adds a new line at the end of the message - * if needed. + * Writes the specified log message to the underlying logging system. + * + * This method is to be implemented by concrete sub-classes of the Logger + * class. * * @param msg The message to be logged. */ - void reducedSyslog(const std::string & msg); + void writeMsgToUnderlyingLoggingSystem(const std::string &msg) override; }; // class StringLogger diff --git a/common/log/Logger.cpp b/common/log/Logger.cpp index 4021a901bad92d5f31724d2af1bd623bfb0e6b86..d867fe846dc89639db77225567798bfab8210265 100644 --- a/common/log/Logger.cpp +++ b/common/log/Logger.cpp @@ -57,14 +57,12 @@ void Logger::operator() ( const std::list<Param> ¶ms) { const std::string rawParams; - struct timeval timeStamp; - gettimeofday(&timeStamp, NULL); const int pid = getpid(); //------------------------------------------------------------------------- // Note that we do here part of the work of the real syslog call, by // building the message ourselves. We then only call a reduced version of - // syslog (namely reducedSyslog). The reason behind it is to be able to set + // syslog (namely writeMsgToUnderlyingLoggingSystem). The reason behind it is to be able to set // the message timestamp ourselves, in case we log messages asynchronously, // as we do when retrieving logs from the DB //------------------------------------------------------------------------- @@ -95,11 +93,10 @@ void Logger::operator() ( msg, params, rawParams, - timeStamp, m_programName, pid); - reducedSyslog(os.str()); + writeMsgToUnderlyingLoggingSystem(os.str()); } @@ -113,26 +110,13 @@ void Logger::writeLogMsg( const std::string &msg, const std::list<Param> ¶ms, const std::string &rawParams, - const struct timeval &timeStamp, const std::string &programName, const int pid) { - //------------------------------------------------------------------------- - // Note that we do here part of the work of the real syslog call, by - // building the message ourselves. We then only call a reduced version of - // syslog (namely reducedSyslog). The reason behind it is to be able to set - // the message timestamp ourselves, in case we log messages asynchronously, - // as we do when retrieving logs from the DB - //------------------------------------------------------------------------- - - // Start message with priority, time, program and PID (syslog standard - // format) - writeHeader(os, priority | LOG_LOCAL3, timeStamp, programName, pid); - const int tid = syscall(__NR_gettid); // Append the log level, the thread id and the message text - os << "LVL=\"" << priorityText << "\" TID=\"" << tid << "\" MSG=\"" << + os << "LVL=\"" << priorityText << "\" PID=\"" << pid << "\" TID=\"" << tid << "\" MSG=\"" << msg << "\" "; // Process parameters @@ -155,35 +139,6 @@ void Logger::writeLogMsg( os << rawParams; } -//----------------------------------------------------------------------------- -// writeHeader -//----------------------------------------------------------------------------- -void Logger::writeHeader( - std::ostringstream &os, - const int priority, - const struct timeval &timeStamp, - const std::string &programName, - const int pid) { - char buf[80]; - int bufLen = sizeof(buf); - int len = 0; - - os << "<" << priority << ">"; - - struct tm localTime; - localtime_r(&(timeStamp.tv_sec), &localTime); - len += strftime(buf, bufLen, "%Y-%m-%dT%T", &localTime); - len += snprintf(buf + len, bufLen - len, ".%06ld", - (unsigned long)timeStamp.tv_usec); - len += strftime(buf + len, bufLen - len, "%z: ", &localTime); - // dirty trick to have the proper timezone format (':' between hh and mm) - buf[len-2] = buf[len-3]; - buf[len-3] = buf[len-4]; - buf[len-4] = ':'; - buf[sizeof(buf) - 1] = '\0'; - os << buf << programName << "[" << pid << "]: "; -} - //----------------------------------------------------------------------------- // cleanString //----------------------------------------------------------------------------- diff --git a/common/log/Logger.hpp b/common/log/Logger.hpp index 4353f34ac818a494ddbf486903f1cc25497c88e6..69d2405c3f5637882acade8e540e4f0a76a5ff56 100644 --- a/common/log/Logger.hpp +++ b/common/log/Logger.hpp @@ -144,23 +144,6 @@ public: * @param logMask The log mask. */ void setLogMask(const int logMask); - - /** - * Writes the header of a syslog message to teh specifed output stream. - * - * @param os The output stream to which the header will be written. - * @param priority The priority of the message. - * @param timeStamp The time stamp of the message. - * @param programName the program name of the log message. - * @param pid The process ID of the process logging the message. - * @return The header of the syslog message. - */ - static void writeHeader( - std::ostringstream &os, - const int priority, - const struct timeval &timeStamp, - const std::string &programName, - const int pid); /** * Creates a clean version of the specified string ready for use with syslog. @@ -181,13 +164,14 @@ protected: const std::string m_programName; /** - * A reduced version of syslog. This method is able to set the message - * timestamp. This is necessary when logging messages asynchronously of there - * creation, such as when retrieving logs from the DB. + * Writes the specified msg to the underlying logging system. + * + * This method is to be implemented by concrete sub-classes of the Logger + * class. * * @param msg The message to be logged. */ - virtual void reducedSyslog(const std::string & msg) = 0; + virtual void writeMsgToUnderlyingLoggingSystem(const std::string &msg) = 0; /** * The log mask. @@ -218,7 +202,6 @@ protected: * @param msg the message. * @param params the parameters of the message. * @param rawParams preprocessed parameters of the message. - * @param timeStamp the time stamp of the log message. * @param programName the program name of the log message. * @param pid the pid of the log message. */ @@ -229,7 +212,6 @@ protected: const std::string &msg, const std::list<Param> ¶ms, const std::string &rawParams, - const struct timeval &timeStamp, const std::string &programName, const int pid); diff --git a/common/log/StdoutLogger.cpp b/common/log/StdoutLogger.cpp index 5cb46251ee94f43520002bbe0e7eccc607cf2d1c..afd3aca3d885709346efca6b9a050bc26e9823e5 100644 --- a/common/log/StdoutLogger.cpp +++ b/common/log/StdoutLogger.cpp @@ -40,9 +40,9 @@ StdoutLogger::~StdoutLogger() { void StdoutLogger::prepareForFork() {} //------------------------------------------------------------------------------ -// reducedSyslog +// writeMsgToUnderlyingLoggingSystem //------------------------------------------------------------------------------ -void StdoutLogger::reducedSyslog(const std::string & msg) { +void StdoutLogger::writeMsgToUnderlyingLoggingSystem(const std::string &msg) { printf("%s\n", msg.c_str()); } diff --git a/common/log/StdoutLogger.hpp b/common/log/StdoutLogger.hpp index 82540d831fce65834122b1ef09238aa49dabc145..62f62d9c0b4ac87a2862e989b9a313dfcd20a26b 100644 --- a/common/log/StdoutLogger.hpp +++ b/common/log/StdoutLogger.hpp @@ -53,13 +53,14 @@ public: protected: /** - * A reduced version of syslog. This method is able to set the message - * timestamp. This is necessary when logging messages asynchronously of there - * creation, such as when retrieving logs from the DB. + * Writes the specified log message to the underlying logging system. + * + * This method is to be implemented by concrete sub-classes of the Logger + * class. * * @param msg The message to be logged. */ - void reducedSyslog(const std::string & msg); + void writeMsgToUnderlyingLoggingSystem(const std::string &msg) override; }; // class StringLogger diff --git a/common/log/StringLogger.cpp b/common/log/StringLogger.cpp index 5c03c64e9adbc820be9e146995cb0260f3dc4e4f..5c3ba3e2293e7d13c29aefd193febbfe08a7dfec 100644 --- a/common/log/StringLogger.cpp +++ b/common/log/StringLogger.cpp @@ -43,9 +43,9 @@ void StringLogger::prepareForFork() { } //----------------------------------------------------------------------------- -// reducedSyslog +// writeMsgToUnderlyingLoggingSystem //----------------------------------------------------------------------------- -void StringLogger::reducedSyslog(const std::string & msg) { +void StringLogger::writeMsgToUnderlyingLoggingSystem(const std::string &msg) { // enter critical section threading::MutexLocker lock(m_mutex); diff --git a/common/log/StringLogger.hpp b/common/log/StringLogger.hpp index 82d24a6d5a267481e3eab78bdc38adec494d6363..60138760b754a2138c2e74b6317ce857e589afc8 100644 --- a/common/log/StringLogger.hpp +++ b/common/log/StringLogger.hpp @@ -71,13 +71,14 @@ protected: std::stringstream m_log; /** - * A reduced version of syslog. This method is able to set the message - * timestamp. This is necessary when logging messages asynchronously of there - * creation, such as when retrieving logs from the DB. + * Writes the specified log message to the underlying logging system. + * + * This method is to be implemented by concrete sub-classes of the Logger + * class. * * @param msg The message to be logged. */ - void reducedSyslog(const std::string & msg); + void writeMsgToUnderlyingLoggingSystem(const std::string &msg) override; }; // class StringLogger diff --git a/common/log/SyslogLogger.cpp b/common/log/SyslogLogger.cpp index 9fd1ec0a1b648552221a0de2ec40083239dbeb6e..51ff38bcab5499e73719e871da3de29064babef8 100644 --- a/common/log/SyslogLogger.cpp +++ b/common/log/SyslogLogger.cpp @@ -22,6 +22,7 @@ #include "common/utils/utils.hpp" #include <errno.h> +#include <fstream> #include <sstream> #include <stdlib.h> #include <string.h> @@ -62,9 +63,9 @@ void SyslogLogger::prepareForFork() { } //----------------------------------------------------------------------------- -// reducedSyslog +// writeMsgToLoggingSystem //----------------------------------------------------------------------------- -void SyslogLogger::reducedSyslog(const std::string& msg) { +void SyslogLogger::writeMsgToUnderlyingLoggingSystem(const std::string &msg) { // Truncate the log message if it exceeds the permitted maximum std::string truncatedMsg = msg.substr(0, m_maxMsgLen); @@ -72,4 +73,4 @@ void SyslogLogger::reducedSyslog(const std::string& msg) { } } // namespace log -} // namespace cta \ No newline at end of file +} // namespace cta diff --git a/common/log/SyslogLogger.hpp b/common/log/SyslogLogger.hpp index 03dd15a5e7ede0ebddb744092c279d05f35c6aa9..d05cf73311bfb2b25412684c8f661b1c1846d41d 100644 --- a/common/log/SyslogLogger.hpp +++ b/common/log/SyslogLogger.hpp @@ -62,13 +62,14 @@ public: protected: /** - * A reduced version of syslog. This method is able to set the message - * timestamp. This is necessary when logging messages asynchronously of there - * creation, such as when retrieving logs from the DB. + * Writes the specified log message to the underlying logging system. + * + * This method is to be implemented by concrete sub-classes of the Logger + * class. * * @param msg The message to be logged. */ - void reducedSyslog(const std::string & msg) override; + void writeMsgToUnderlyingLoggingSystem(const std::string &msg) override; }; // class SyslogLogger