From a05ebff37385d09e287951aabc0da38f5b37eeed Mon Sep 17 00:00:00 2001 From: Steven Murray <Steven.Murray@cern.ch> Date: Wed, 31 May 2017 17:02:11 +0200 Subject: [PATCH] cta/CTA#106 cta-taped logging a timestamp I have removed the generation and formating of timestamps from the CTA logging system. --- common/log/DummyLogger.cpp | 4 +-- common/log/DummyLogger.hpp | 11 +++++++- common/log/FileLogger.cpp | 8 +++--- common/log/FileLogger.hpp | 11 ++++---- common/log/Logger.cpp | 51 +++---------------------------------- common/log/Logger.hpp | 28 ++++---------------- common/log/StdoutLogger.cpp | 4 +-- common/log/StdoutLogger.hpp | 9 ++++--- common/log/StringLogger.cpp | 4 +-- common/log/StringLogger.hpp | 9 ++++--- common/log/SyslogLogger.cpp | 7 ++--- common/log/SyslogLogger.hpp | 9 ++++--- 12 files changed, 52 insertions(+), 103 deletions(-) diff --git a/common/log/DummyLogger.cpp b/common/log/DummyLogger.cpp index 5f7e1b0046..1103e23f7f 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 18d528bdc8..c808f805d7 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 6cc7e92807..6b777a5c2c 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 14e36a8ee8..f6b8f47b5a 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 4021a901ba..d867fe846d 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 4353f34ac8..69d2405c3f 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 5cb46251ee..afd3aca3d8 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 82540d831f..62f62d9c0b 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 5c03c64e9a..5c3ba3e229 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 82d24a6d5a..60138760b7 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 9fd1ec0a1b..51ff38bcab 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 03dd15a5e7..d05cf73311 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 -- GitLab