From b2a85e250ae595266fba001c9889fe8a3ee660e3 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sat, 13 Sep 2008 15:11:35 +0000 Subject: Unify program name and console logging tags. Add a new class, Logging::Tagger, which can be used to temporarily add information to the program name, such as the client ID in bbstored. --- lib/common/Logging.cpp | 13 ++++++++++--- lib/common/Logging.h | 27 +++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) (limited to 'lib/common') diff --git a/lib/common/Logging.cpp b/lib/common/Logging.cpp index 3edb5ce6..df7c609e 100644 --- a/lib/common/Logging.cpp +++ b/lib/common/Logging.cpp @@ -35,6 +35,7 @@ Console* Logging::spConsole = NULL; Syslog* Logging::spSyslog = NULL; Log::Level Logging::sGlobalLevel = Log::EVERYTHING; Logging Logging::sGlobalLogging; //automatic initialisation +std::string Logging::sProgramName; Logging::Logging() { @@ -206,6 +207,8 @@ void Logging::ClearContext() void Logging::SetProgramName(const std::string& rProgramName) { + sProgramName = rProgramName; + for (std::vector::iterator i = sLoggers.begin(); i != sLoggers.end(); i++) { @@ -230,10 +233,14 @@ bool Console::sShowTag = false; bool Console::sShowPID = false; std::string Console::sTag; -void Console::SetTag(const std::string& rTag) +void Console::SetProgramName(const std::string& rProgramName) +{ + sTag = rProgramName; +} + +void Console::SetShowTag(bool enabled) { - sTag = rTag; - sShowTag = true; + sShowTag = enabled; } void Console::SetShowTime(bool enabled) diff --git a/lib/common/Logging.h b/lib/common/Logging.h index 6224a73f..9a850c44 100644 --- a/lib/common/Logging.h +++ b/lib/common/Logging.h @@ -143,19 +143,19 @@ class Logger class Console : public Logger { private: + static bool sShowTag; static bool sShowTime; static bool sShowTimeMicros; - static bool sShowTag; - static std::string sTag; static bool sShowPID; + static std::string sTag; public: virtual bool Log(Log::Level level, const std::string& rFile, int line, std::string& rMessage); virtual const char* GetType() { return "Console"; } - virtual void SetProgramName(const std::string& rProgramName) { } + virtual void SetProgramName(const std::string& rProgramName); - static void SetTag(const std::string& rTag); + static void SetShowTag(bool enabled); static void SetShowTime(bool enabled); static void SetShowTimeMicros(bool enabled); static void SetShowPID(bool enabled); @@ -206,6 +206,7 @@ class Logging static Syslog* spSyslog; static Log::Level sGlobalLevel; static Logging sGlobalLogging; + static std::string sProgramName; public: Logging (); @@ -230,6 +231,7 @@ class Logging return (int)sGlobalLevel >= (int)level; } static void SetProgramName(const std::string& rProgramName); + static std::string GetProgramName() { return sProgramName; } class Guard { @@ -247,6 +249,23 @@ class Logging Logging::SetGlobalLevel(mOldLevel); } }; + + class Tagger + { + private: + std::string mOldTag; + + public: + Tagger(const std::string& rTempTag) + { + mOldTag = Logging::GetProgramName(); + Logging::SetProgramName(mOldTag + " " + rTempTag); + } + ~Tagger() + { + Logging::SetProgramName(mOldTag); + } + }; }; #endif // LOGGING__H -- cgit v1.2.3