diff options
author | Chris Wilson <chris+github@qwirx.com> | 2008-05-28 12:37:49 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2008-05-28 12:37:49 +0000 |
commit | e43fdd9d7acacd6d711b7f120e5b477767ad11c1 (patch) | |
tree | 206e3ac0cf4c4dea38fce2a1de754cce94aecebe | |
parent | 0ed03aa198fedb15283018dff384dfad0dc97c79 (diff) |
Add static Logging::GetNamedLevel() method to return a log level
specified by name as a string.
-rw-r--r-- | lib/common/Logging.cpp | 18 | ||||
-rw-r--r-- | lib/common/Logging.h | 22 |
2 files changed, 39 insertions, 1 deletions
diff --git a/lib/common/Logging.cpp b/lib/common/Logging.cpp index f49ab765..5eec1e81 100644 --- a/lib/common/Logging.cpp +++ b/lib/common/Logging.cpp @@ -179,6 +179,23 @@ void Logging::SetContext(std::string context) sContextSet = true; } +Log::Level Logging::GetNamedLevel(const std::string& rName) +{ + if (rName == "nothing") { return Log::NOTHING; } + else if (rName == "fatal") { return Log::FATAL; } + else if (rName == "error") { return Log::ERROR; } + else if (rName == "warning") { return Log::WARNING; } + else if (rName == "notice") { return Log::NOTICE; } + else if (rName == "info") { return Log::INFO; } + else if (rName == "trace") { return Log::TRACE; } + else if (rName == "everything") { return Log::EVERYTHING; } + else + { + BOX_ERROR("Unknown verbosity level: " << rName); + return Log::INVALID; + } +} + void Logging::ClearContext() { sContextSet = false; @@ -350,6 +367,7 @@ bool Syslog::Log(Log::Level level, const std::string& rFile, switch(level) { case Log::NOTHING: /* fall through */ + case Log::INVALID: /* fall through */ case Log::FATAL: syslogLevel = LOG_CRIT; break; case Log::ERROR: syslogLevel = LOG_ERR; break; case Log::WARNING: syslogLevel = LOG_WARNING; break; diff --git a/lib/common/Logging.h b/lib/common/Logging.h index 2c967c89..973a8944 100644 --- a/lib/common/Logging.h +++ b/lib/common/Logging.h @@ -93,7 +93,8 @@ namespace Log NOTICE, INFO, TRACE, - EVERYTHING + EVERYTHING, + INVALID = -1, }; } @@ -225,11 +226,30 @@ class Logging static void SetContext(std::string context); static void ClearContext(); static void SetGlobalLevel(Log::Level level) { sGlobalLevel = level; } + static Log::Level GetGlobalLevel() { return sGlobalLevel; } + static Log::Level GetNamedLevel(const std::string& rName); static bool IsEnabled(Log::Level level) { return (int)sGlobalLevel >= (int)level; } static void SetProgramName(const std::string& rProgramName); + + class Guard + { + private: + Log::Level mOldLevel; + + public: + Guard(Log::Level newLevel) + { + mOldLevel = Logging::GetGlobalLevel(); + Logging::SetGlobalLevel(newLevel); + } + ~Guard() + { + Logging::SetGlobalLevel(mOldLevel); + } + }; }; #endif // LOGGING__H |