[Box Backup-commit] COMMIT r1261 - box/chris/merge/lib/common

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Tue Jan 16 23:05:20 GMT 2007


Author: chris
Date: 2007-01-16 23:05:20 +0000 (Tue, 16 Jan 2007)
New Revision: 1261

Modified:
   box/chris/merge/lib/common/Logging.cpp
   box/chris/merge/lib/common/Logging.h
Log:
Automatically initialise logging at startup, with a static object, to log
to console and syslog.

All logging objects automatically register themselves with the global
logging system. (refs #3)


Modified: box/chris/merge/lib/common/Logging.cpp
===================================================================
--- box/chris/merge/lib/common/Logging.cpp	2007-01-16 23:04:03 UTC (rev 1260)
+++ box/chris/merge/lib/common/Logging.cpp	2007-01-16 23:05:20 UTC (rev 1261)
@@ -21,20 +21,41 @@
 
 std::vector<Logger*> Logging::sLoggers;
 std::string Logging::sContext;
-Console     Logging::sConsole;
-Syslog      Logging::sSyslog;
+Console*    Logging::spConsole = NULL;
+Syslog*     Logging::spSyslog  = NULL;
 Log::Level  Logging::sGlobalLevel = Log::EVERYTHING;
+Logging     Logging::sGlobalLogging; //automatic initialisation
 
+Logging::Logging()
+{
+	ASSERT(!spConsole);
+	ASSERT(!spSyslog);
+	spConsole = new Console();
+	spSyslog  = new Syslog();
+	sLogToConsole = true;
+	sLogToSyslog  = true;
+}
+
+Logging::~Logging()
+{
+	sLogToConsole = false;
+	sLogToSyslog  = false;
+	delete spConsole;
+	delete spSyslog;
+	spConsole = NULL;
+	spSyslog  = NULL;
+}
+
 void Logging::ToSyslog(bool enabled)
 {
 	if (!sLogToSyslog && enabled)
 	{
-		Add(&sSyslog);
+		Add(spSyslog);
 	}
 	
 	if (sLogToSyslog && !enabled)
 	{
-		Remove(&sSyslog);
+		Remove(spSyslog);
 	}
 	
 	sLogToSyslog = enabled;
@@ -44,12 +65,12 @@
 {
 	if (!sLogToConsole && enabled)
 	{
-		Add(&sConsole);
+		Add(spConsole);
 	}
 	
 	if (sLogToConsole && !enabled)
 	{
-		Remove(&sConsole);
+		Remove(spConsole);
 	}
 	
 	sLogToConsole = enabled;
@@ -57,12 +78,12 @@
 
 void Logging::FilterConsole(Log::Level level)
 {
-	sConsole.Filter(level);
+	spConsole->Filter(level);
 }
 
 void Logging::FilterSyslog(Log::Level level)
 {
-	sSyslog.Filter(level);
+	spSyslog->Filter(level);
 }
 
 void Logging::Add(Logger* pNewLogger)
@@ -140,6 +161,17 @@
 	}
 }
 
+Logger::Logger() 
+: mCurrentLevel(Log::EVERYTHING) 
+{
+	Logging::Add(this);
+}
+
+Logger::~Logger() 
+{
+	Logging::Remove(this);
+}
+
 bool Console::Log(Log::Level level, const std::string& rFile, 
 	int line, std::string& rMessage)
 {
@@ -176,6 +208,7 @@
 		case Log::FATAL:      syslogLevel = LOG_CRIT;    break;
 		case Log::ERROR:      syslogLevel = LOG_ERR;     break;
 		case Log::WARNING:    syslogLevel = LOG_WARNING; break;
+		case Log::NOTICE:     syslogLevel = LOG_NOTICE;  break;
 		case Log::INFO:       syslogLevel = LOG_INFO;    break;
 		case Log::TRACE:      /* fall through */
 		case Log::EVERYTHING: syslogLevel = LOG_DEBUG;   break;

Modified: box/chris/merge/lib/common/Logging.h
===================================================================
--- box/chris/merge/lib/common/Logging.h	2007-01-16 23:04:03 UTC (rev 1260)
+++ box/chris/merge/lib/common/Logging.h	2007-01-16 23:05:20 UTC (rev 1261)
@@ -63,8 +63,8 @@
 	Log::Level mCurrentLevel;
 	
 	public:
-	Logger() : mCurrentLevel(Log::EVERYTHING) { }
-	virtual ~Logger() { }
+	Logger();
+	virtual ~Logger();
 	
 	virtual bool Log(Log::Level level, const std::string& rFile, 
 		int line, std::string& rMessage) = 0;
@@ -136,12 +136,14 @@
 	static bool sLogToSyslog, sLogToConsole;
 	static std::string sContext;
 	static bool sContextSet;
-	static Console sConsole;
-	static Syslog  sSyslog;
+	static Console* spConsole;
+	static Syslog*  spSyslog;
 	static Log::Level sGlobalLevel;
+	static Logging    sGlobalLogging;
 	
 	public:
-	static void Init(const std::string& rProgramName);
+	Logging ();
+	~Logging();
 	static void ToSyslog  (bool enabled);
 	static void ToConsole (bool enabled);
 	static void FilterSyslog  (Log::Level level);




More information about the Boxbackup-commit mailing list