[Box Backup-commit] COMMIT r2175 - box/trunk/lib/common

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Wed May 28 13:37:49 BST 2008


Author: chris
Date: 2008-05-28 13:37:49 +0100 (Wed, 28 May 2008)
New Revision: 2175

Modified:
   box/trunk/lib/common/Logging.cpp
   box/trunk/lib/common/Logging.h
Log:
Add static Logging::GetNamedLevel() method to return a log level 
specified by name as a string.


Modified: box/trunk/lib/common/Logging.cpp
===================================================================
--- box/trunk/lib/common/Logging.cpp	2008-05-28 12:36:53 UTC (rev 2174)
+++ box/trunk/lib/common/Logging.cpp	2008-05-28 12:37:49 UTC (rev 2175)
@@ -179,6 +179,23 @@
 	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 @@
 	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;

Modified: box/trunk/lib/common/Logging.h
===================================================================
--- box/trunk/lib/common/Logging.h	2008-05-28 12:36:53 UTC (rev 2174)
+++ box/trunk/lib/common/Logging.h	2008-05-28 12:37:49 UTC (rev 2175)
@@ -93,7 +93,8 @@
 		NOTICE,
 		INFO,
 		TRACE, 
-		EVERYTHING
+		EVERYTHING,
+		INVALID = -1,
 	};
 }
 
@@ -225,11 +226,30 @@
 	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




More information about the Boxbackup-commit mailing list