[Box Backup-commit] COMMIT r2081 - in box/trunk/lib: common server

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Thu, 31 Jan 2008 23:44:54 +0000 (UTC)


Author: chris
Date: 2008-01-31 23:44:54 +0000 (Thu, 31 Jan 2008)
New Revision: 2081

Modified:
   box/trunk/lib/common/Logging.cpp
   box/trunk/lib/common/Logging.h
   box/trunk/lib/server/Daemon.cpp
Log:
Add support for microsecond timestamps and PID logging on console log
for daemons.


Modified: box/trunk/lib/common/Logging.cpp
===================================================================
--- box/trunk/lib/common/Logging.cpp	2008-01-31 23:43:24 UTC (rev 2080)
+++ box/trunk/lib/common/Logging.cpp	2008-01-31 23:44:54 UTC (rev 2081)
@@ -182,6 +182,7 @@
 bool Console::sShowTime = false;
 bool Console::sShowTimeMicros = false;
 bool Console::sShowTag = false;
+bool Console::sShowPID = false;
 std::string Console::sTag;
 
 void Console::SetTag(const std::string& rTag)
@@ -200,6 +201,11 @@
 	sShowTimeMicros = enabled;
 }
 
+void Console::SetShowPID(bool enabled)
+{
+	sShowPID = enabled;
+}
+
 bool Console::Log(Log::Level level, const std::string& rFile, 
 	int line, std::string& rMessage)
 {
@@ -215,7 +221,7 @@
 		target = stderr;
 	}
 
-	std::string msg;
+	std::ostringstream buf;
 
 	if (sShowTime)
 	{
@@ -231,8 +237,6 @@
 			if (localtime_r(&seconds, &tm_now) != NULL)
 		#endif
 		{
-			std::ostringstream buf;
-
 			buf << std::setfill('0') <<
 				std::setw(2) << tm_ptr->tm_hour << ":" << 
 				std::setw(2) << tm_ptr->tm_min  << ":" <<
@@ -244,40 +248,58 @@
 			}
 
 			buf << " ";
-			msg += buf.str();
 		}
 		else
 		{
-			msg += strerror(errno);
-			msg += " ";
+			buf << strerror(errno);
+			buf << " ";
 		}
 	}
 
 	if (sShowTag)
 	{
-		msg += "[" + sTag + "] ";
+		if (sShowPID)
+		{
+			buf << "[" << sTag << " " << getpid() << "] ";
+		}
+		else
+		{
+			buf << "[" << sTag << "] ";
+		}
 	}
+	else if (sShowPID)
+	{
+		buf << "[" << getpid() << "] ";
+	}
 
 	if (level <= Log::FATAL)
 	{
-		msg += "FATAL: ";
+		buf << "FATAL:   ";
 	}
 	else if (level <= Log::ERROR)
 	{
-		msg += "ERROR: ";
+		buf << "ERROR:   ";
 	}
 	else if (level <= Log::WARNING)
 	{
-		msg += "WARNING: ";
+		buf << "WARNING: ";
 	}
 	else if (level <= Log::NOTICE)
 	{
-		msg += "NOTICE: ";
+		buf << "NOTICE:  ";
 	}
-	
-	msg += rMessage;
+	else if (level <= Log::INFO)
+	{
+		buf << "INFO:    ";
+	}
+	else if (level <= Log::TRACE)
+	{
+		buf << "TRACE:   ";
+	}
 
-	fprintf(target, "%s\n", msg.c_str());
+	buf << rMessage;
+
+	fprintf(target, "%s\n", buf.str().c_str());
 	
 	return true;
 }

Modified: box/trunk/lib/common/Logging.h
===================================================================
--- box/trunk/lib/common/Logging.h	2008-01-31 23:43:24 UTC (rev 2080)
+++ box/trunk/lib/common/Logging.h	2008-01-31 23:44:54 UTC (rev 2081)
@@ -121,6 +121,7 @@
 	static bool sShowTimeMicros;
 	static bool sShowTag;
 	static std::string sTag;
+	static bool sShowPID;
 
 	public:
 	virtual bool Log(Log::Level level, const std::string& rFile, 
@@ -131,6 +132,7 @@
 	static void SetTag(const std::string& rTag);
 	static void SetShowTime(bool enabled);
 	static void SetShowTimeMicros(bool enabled);
+	static void SetShowPID(bool enabled);
 };
 
 // --------------------------------------------------------------------------

Modified: box/trunk/lib/server/Daemon.cpp
===================================================================
--- box/trunk/lib/server/Daemon.cpp	2008-01-31 23:43:24 UTC (rev 2080)
+++ box/trunk/lib/server/Daemon.cpp	2008-01-31 23:44:54 UTC (rev 2081)
@@ -106,7 +106,7 @@
 	#ifndef WIN32
 		"DFk"
 	#endif
-		"hqvVt:T";
+		"hPqvVt:TU";
 }
 
 void Daemon::Usage()
@@ -125,11 +125,13 @@
 	"  -F         Do not fork into background, but fork to serve multiple clients\n"
 	"  -k         Keep console open after fork, keep writing log messages to it\n"
 #endif
+	"  -P         Show process ID (PID) in console output\n"
 	"  -q         Run more quietly, reduce verbosity level by one, can repeat\n"
 	"  -v         Run more verbosely, increase verbosity level by one, can repeat\n"
 	"  -V         Run at maximum verbosity\n"
 	"  -t <tag>   Tag console output with specified marker\n"
-	"  -T         Timestamp console output\n";
+	"  -T         Timestamp console output\n"
+	"  -U         Timestamp console output with microseconds\n";
 }
 
 // --------------------------------------------------------------------------
@@ -181,6 +183,12 @@
 		}
 		break;
 
+		case 'P':
+		{
+			Console::SetShowPID(true);
+		}
+		break;
+
 		case 'q':
 		{
 			if(mLogLevel == Log::NOTHING)
@@ -225,6 +233,13 @@
 		}
 		break;
 
+		case 'U':
+		{
+			Console::SetShowTime(true);
+			Console::SetShowTimeMicros(true);
+		}
+		break;
+
 		case '?':
 		{
 			BOX_FATAL("Unknown option on command line: "