From 3d650eb94f55bb4a2c364c525255a5686eb36a8d Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Sun, 28 Oct 2012 18:26:36 -0700 Subject: [PATCH] To make logs more greppable, include the partition name in the log message. --- src/Log.cpp | 16 ++++++++++------ src/Log.h | 18 ++++++++++-------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/Log.cpp b/src/Log.cpp index c4cf54ea2..5f4e3425d 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -41,14 +41,18 @@ std::vector< std::pair > LogPartition::getSeverities() Log::~Log() { std::string logMsg = boost::posix_time::to_simple_string(boost::posix_time::second_clock::universal_time()); + if (!mPartitionName.empty()) + logMsg += " " + mPartitionName + ":"; + else + logMsg += " "; switch (mSeverity) { - case lsTRACE: logMsg += " TRAC "; break; - case lsDEBUG: logMsg += " DEBG "; break; - case lsINFO: logMsg += " INFO "; break; - case lsWARNING: logMsg += " WARN "; break; - case lsERROR: logMsg += " EROR "; break; - case lsFATAL: logMsg += " FATL "; break; + case lsTRACE: logMsg += "TRC "; break; + case lsDEBUG: logMsg += "DBG "; break; + case lsINFO: logMsg += "NFO "; break; + case lsWARNING: logMsg += "WRN "; break; + case lsERROR: logMsg += "ERR "; break; + case lsFATAL: logMsg += "FTL "; break; case lsINVALID: assert(false); return; } logMsg += oss.str(); diff --git a/src/Log.h b/src/Log.h index 7dbc2c833..6da9e0b5c 100644 --- a/src/Log.h +++ b/src/Log.h @@ -17,10 +17,10 @@ #define SETUP_LOG() static LogPartition logPartition(__FILE__) // Standard conditional log -#define cLog(x) if (!logPartition.doLog(x)) do {} while (0); else Log(x) +#define cLog(x) if (!logPartition.doLog(x)) do {} while (0); else Log(x, logPartition) // Log only if an additional condition 'c' is true. Condition is not computed if not needed -#define tLog(c,x) if (!logPartition.doLog(x) || !(c)) do {} while(0); else Log(x) +#define tLog(c,x) if (!logPartition.doLog(x) || !(c)) do {} while(0); else Log(x, logPartition) // Check if should log #define sLog(x) (logPartition.doLog(x)) @@ -49,10 +49,8 @@ protected: public: LogPartition(const char *name); - bool doLog(LogSeverity s) - { - return s >= mMinSeverity; - } + bool doLog(LogSeverity s) { return s >= mMinSeverity; } + const std::string& getName() const { return mName; } static bool setSeverity(const std::string& partition, LogSeverity severity); static void setSeverity(LogSeverity severity); @@ -70,8 +68,9 @@ protected: static LogSeverity sMinSeverity; static std::ofstream* outStream; - mutable std::ostringstream oss; - LogSeverity mSeverity; + mutable std::ostringstream oss; + LogSeverity mSeverity; + std::string mPartitionName; static boost::filesystem::path *pathToLog; static uint32 logRotateCounter; @@ -80,6 +79,9 @@ public: Log(LogSeverity s) : mSeverity(s) { ; } + Log(LogSeverity s, const LogPartition& p) : mSeverity(s), mPartitionName(p.getName()) + { ; } + ~Log(); template std::ostream& operator<<(const T& t) const