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

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Sun Apr 22 15:23:24 BST 2007


Author: chris
Date: 2007-04-22 15:23:24 +0100 (Sun, 22 Apr 2007)
New Revision: 1551

Modified:
   box/chris/merge/lib/common/Logging.cpp
   box/chris/merge/lib/common/Logging.h
Log:
Add options to log timestamps, and a custom tag, with each message to
the console, e.g.:

	14:53:17 [bbackupd] Finished scan of local files

(refs #3)


Modified: box/chris/merge/lib/common/Logging.cpp
===================================================================
--- box/chris/merge/lib/common/Logging.cpp	2007-04-22 14:08:13 UTC (rev 1550)
+++ box/chris/merge/lib/common/Logging.cpp	2007-04-22 14:23:24 UTC (rev 1551)
@@ -9,12 +9,16 @@
 
 #include "Box.h"
 
+#include <time.h>
+
 #ifdef HAVE_SYSLOG_H
 	#include <syslog.h>
 #endif
 
 #include "Logging.h"
 
+#include <iomanip>
+
 bool Logging::sLogToSyslog  = false;
 bool Logging::sLogToConsole = false;
 bool Logging::sContextSet   = false;
@@ -172,6 +176,21 @@
 	Logging::Remove(this);
 }
 
+bool Console::sShowTime = false;
+bool Console::sShowTag  = false;
+std::string Console::sTag;
+
+void Console::SetTag(const std::string& rTag)
+{
+	sTag = rTag;
+	sShowTag = true;
+}
+
+void Console::SetShowTime(bool enabled)
+{
+	sShowTime = enabled;
+}
+
 bool Console::Log(Log::Level level, const std::string& rFile, 
 	int line, std::string& rMessage)
 {
@@ -186,8 +205,43 @@
 	{
 		target = stderr;
 	}
+
+	std::string msg;
+
+	if (sShowTime)
+	{
+		struct tm time_now;
+		time_t time_t_now = time(NULL);
+
+		if (time_t_now == ((time_t)-1))
+		{
+			msg += strerror(errno);
+			msg += " ";
+		}
+		else if (localtime_r(&time_t_now, &time_now) != NULL)
+		{
+			std::ostringstream buf;
+			buf << std::setfill('0') <<
+				std::setw(2) << time_now.tm_hour << ":" << 
+				std::setw(2) << time_now.tm_min  << ":" <<
+				std::setw(2) << time_now.tm_sec  << " ";
+			msg += buf.str();
+		}
+		else
+		{
+			msg += strerror(errno);
+			msg += " ";
+		}
+	}
+
+	if (sShowTag)
+	{
+		msg += "[" + sTag + "] ";
+	}
 	
-	fprintf(target, "%s\n", rMessage.c_str());
+	msg += rMessage;
+
+	fprintf(target, "%s\n", msg.c_str());
 	
 	return true;
 }

Modified: box/chris/merge/lib/common/Logging.h
===================================================================
--- box/chris/merge/lib/common/Logging.h	2007-04-22 14:08:13 UTC (rev 1550)
+++ box/chris/merge/lib/common/Logging.h	2007-04-22 14:23:24 UTC (rev 1551)
@@ -102,11 +102,19 @@
 
 class Console : public Logger
 {
+	private:
+	static bool sShowTime;
+	static bool sShowTag;
+	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) { }
+
+	static void SetTag(const std::string& rTag);
+	static void SetShowTime(bool enabled);
 };
 
 // --------------------------------------------------------------------------




More information about the Boxbackup-commit mailing list