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

subversion at boxbackup.org subversion at boxbackup.org
Wed Apr 9 23:15:49 BST 2014


Author: chris
Date: 2014-04-09 23:15:49 +0100 (Wed, 09 Apr 2014)
New Revision: 3323

Modified:
   box/trunk/lib/common/Logging.cpp
   box/trunk/lib/common/Logging.h
   box/trunk/lib/server/Daemon.cpp
   box/trunk/lib/server/Daemon.h
Log:
Refactor logging option processing out of Daemon class.

Allows command-line tools to reuse the same option processing.

Modified: box/trunk/lib/common/Logging.cpp
===================================================================
--- box/trunk/lib/common/Logging.cpp	2014-04-09 22:15:45 UTC (rev 3322)
+++ box/trunk/lib/common/Logging.cpp	2014-04-09 22:15:49 UTC (rev 3323)
@@ -554,3 +554,151 @@
 	return false;
 }
 
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    Logging::OptionParser::GetOptionString()
+//		Purpose: Returns the valid Getopt command-line options
+//			 that Logging::OptionParser::ProcessOption will handle.
+//		Created: 2014/04/09
+//
+// --------------------------------------------------------------------------
+std::string Logging::OptionParser::GetOptionString()
+{
+	return "PqQt:TUvVW:";
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    Logging::OptionParser::ProcessOption(signed int option)
+//		Purpose: Processes the supplied option (equivalent to the
+//			 return code from getopt()). Return zero if the
+//			 option was handled successfully, or nonzero to
+//			 abort the program with that return value.
+//		Created: 2007/09/18
+//
+// --------------------------------------------------------------------------
+int Logging::OptionParser::ProcessOption(signed int option)
+{
+	switch(option)
+	{
+		case 'P':
+		{
+			Console::SetShowPID(true);
+		}
+		break;
+
+		case 'q':
+		{
+			if(mCurrentLevel == Log::NOTHING)
+			{
+				BOX_FATAL("Too many '-q': "
+					"Cannot reduce logging "
+					"level any more");
+				return 2;
+			}
+			mCurrentLevel--;
+		}
+		break;
+
+		case 'Q':
+		{
+			mCurrentLevel = Log::NOTHING;
+		}
+		break;
+
+		case 't':
+		{
+			Logging::SetProgramName(optarg);
+			Console::SetShowTag(true);
+		}
+		break;
+
+		case 'T':
+		{
+			Console::SetShowTime(true);
+		}
+		break;
+
+		case 'U':
+		{
+			Console::SetShowTime(true);
+			Console::SetShowTimeMicros(true);
+		}
+		break;
+
+		case 'v':
+		{
+			if(mCurrentLevel == Log::EVERYTHING)
+			{
+				BOX_FATAL("Too many '-v': "
+					"Cannot increase logging "
+					"level any more");
+				return 2;
+			}
+			mCurrentLevel++;
+		}
+		break;
+
+		case 'V':
+		{
+			mCurrentLevel = Log::EVERYTHING;
+		}
+		break;
+
+		case 'W':
+		{
+			mCurrentLevel = Logging::GetNamedLevel(optarg);
+			if (mCurrentLevel == Log::INVALID)
+			{
+				BOX_FATAL("Invalid logging level: " << optarg);
+				return 2;
+			}
+		}
+		break;
+
+		case '?':
+		{
+			BOX_FATAL("Unknown option on command line: " 
+				<< "'" << (char)optopt << "'");
+			return 2;
+		}
+		break;
+
+		default:
+		{
+			BOX_FATAL("Unknown error in getopt: returned "
+				<< "'" << option << "'");
+			return 1;
+		}
+	}
+
+	// If we didn't explicitly return an error code above, then the option
+	// was fine, so return 0 to continue processing.
+	return 0;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    Logging::OptionParser::GetUsageString()
+//		Purpose: Returns a string suitable for displaying as part
+//			 of a program's command-line usage help message,
+//			 describing the logging options.
+//		Created: 2014/04/09
+//
+// --------------------------------------------------------------------------
+std::string Logging::OptionParser::GetUsageString()
+{
+	return 
+	"  -P         Show process ID (PID) in console output\n"
+	"  -q         Run more quietly, reduce verbosity level by one, can repeat\n"
+	"  -Q         Run at minimum verbosity, log nothing to console and system\n"
+	"  -t <tag>   Tag console output with specified marker\n"
+	"  -T         Timestamp console output\n"
+	"  -U         Timestamp console output with microseconds\n"
+	"  -v         Run more verbosely, increase verbosity level by one, can repeat\n"
+	"  -V         Run at maximum verbosity, log everything to console and system\n"
+	"  -W <level> Set verbosity to error/warning/notice/info/trace/everything\n";
+}

Modified: box/trunk/lib/common/Logging.h
===================================================================
--- box/trunk/lib/common/Logging.h	2014-04-09 22:15:45 UTC (rev 3322)
+++ box/trunk/lib/common/Logging.h	2014-04-09 22:15:49 UTC (rev 3323)
@@ -446,6 +446,37 @@
 			Logging::Remove(mpLogger);
 		}
 	};
+
+	// --------------------------------------------------------------------------
+	//
+	// Class
+	//		Name:    Logging::OptionParser
+	//		Purpose: Process command-line options
+	//		Created: 2014/04/09
+	//
+	// --------------------------------------------------------------------------
+	class OptionParser
+	{
+	public:
+		OptionParser(Log::Level InitialLevel =
+			#ifdef BOX_RELEASE_BUILD
+			Log::NOTICE
+			#else
+			Log::INFO
+			#endif
+			)
+		: mCurrentLevel(InitialLevel)
+		{ }
+		
+		static std::string GetOptionString();
+		int ProcessOption(signed int option);
+		static std::string GetUsageString();
+		int mCurrentLevel; // need an int to do math with
+		Log::Level GetCurrentLevel()
+		{
+			return (Log::Level) mCurrentLevel;
+		}
+	};
 };
 
 class FileLogger : public Logger

Modified: box/trunk/lib/server/Daemon.cpp
===================================================================
--- box/trunk/lib/server/Daemon.cpp	2014-04-09 22:15:45 UTC (rev 3322)
+++ box/trunk/lib/server/Daemon.cpp	2014-04-09 22:15:49 UTC (rev 3323)
@@ -106,11 +106,11 @@
 // --------------------------------------------------------------------------
 std::string Daemon::GetOptionString()
 {
-	return "c:"
+	return std::string("c:"
 	#ifndef WIN32
 		"DF"
 	#endif
-		"hkKo:O:PqQt:TUvVW:";
+		"hkKo:O:") + Logging::OptionParser::GetOptionString();
 }
 
 void Daemon::Usage()
@@ -132,17 +132,8 @@
 	"  -k         Keep console open after fork, keep writing log messages to it\n"
 	"  -K         Stop writing log messages to console while daemon is running\n"
 	"  -o <file>  Log to a file, defaults to maximum verbosity\n"
-	"  -O <level> Set file log verbosity to error/warning/notice/info/trace/everything\n"
-	"  -P         Show process ID (PID) in console output\n"
-	"  -q         Run more quietly, reduce verbosity level by one, can repeat\n"
-	"  -Q         Run at minimum verbosity, log nothing to console and system\n"
-	"  -t <tag>   Tag console output with specified marker\n"
-	"  -T         Timestamp console output\n"
-	"  -U         Timestamp console output with microseconds\n"
-	"  -v         Run more verbosely, increase verbosity level by one, can repeat\n"
-	"  -V         Run at maximum verbosity, log everything to console and system\n"
-	"  -W <level> Set verbosity to error/warning/notice/info/trace/everything\n"
-	;
+	"  -O <level> Set file log verbosity to error/warning/notice/info/trace/everything\n" <<
+	Logging::OptionParser::GetUsageString();
 }
 
 // --------------------------------------------------------------------------
@@ -218,94 +209,9 @@
 		}
 		break;
 
-		case 'P':
-		{
-			Console::SetShowPID(true);
-		}
-		break;
-
-		case 'q':
-		{
-			if(mLogLevel == Log::NOTHING)
-			{
-				BOX_FATAL("Too many '-q': "
-					"Cannot reduce logging "
-					"level any more");
-				return 2;
-			}
-			mLogLevel--;
-		}
-		break;
-
-		case 'Q':
-		{
-			mLogLevel = Log::NOTHING;
-		}
-		break;
-
-		case 't':
-		{
-			Logging::SetProgramName(optarg);
-			Console::SetShowTag(true);
-		}
-		break;
-
-		case 'T':
-		{
-			Console::SetShowTime(true);
-		}
-		break;
-
-		case 'U':
-		{
-			Console::SetShowTime(true);
-			Console::SetShowTimeMicros(true);
-		}
-		break;
-
-		case 'v':
-		{
-			if(mLogLevel == Log::EVERYTHING)
-			{
-				BOX_FATAL("Too many '-v': "
-					"Cannot increase logging "
-					"level any more");
-				return 2;
-			}
-			mLogLevel++;
-		}
-		break;
-
-		case 'V':
-		{
-			mLogLevel = Log::EVERYTHING;
-		}
-		break;
-
-		case 'W':
-		{
-			mLogLevel = Logging::GetNamedLevel(optarg);
-			if (mLogLevel == Log::INVALID)
-			{
-				BOX_FATAL("Invalid logging level: " << optarg);
-				return 2;
-			}
-		}
-		break;
-
-		case '?':
-		{
-			BOX_FATAL("Unknown option on command line: " 
-				<< "'" << (char)optopt << "'");
-			return 2;
-		}
-		break;
-
 		default:
 		{
-			BOX_FATAL("Unknown error in getopt: returned "
-				<< "'" << option << "'");
-			return 1;
+			return mLogLevel.ProcessOption(option);
 		}
 	}
 
@@ -351,12 +257,6 @@
 
 int Daemon::ProcessOptions(int argc, const char *argv[])
 {
-	#ifdef BOX_RELEASE_BUILD
-	mLogLevel = Log::NOTICE;
-	#else
-	mLogLevel = Log::INFO;
-	#endif
-
 	if (argc == 2 && strcmp(argv[1], "/?") == 0)
 	{
 		Usage();
@@ -406,8 +306,8 @@
 		return 2;
 	}
 
-	Logging::FilterConsole((Log::Level)mLogLevel);
-	Logging::FilterSyslog ((Log::Level)mLogLevel);
+	Logging::FilterConsole(mLogLevel.GetCurrentLevel());
+	Logging::FilterSyslog (mLogLevel.GetCurrentLevel());
 
 	if (mLogFileLevel != Log::INVALID)
 	{

Modified: box/trunk/lib/server/Daemon.h
===================================================================
--- box/trunk/lib/server/Daemon.h	2014-04-09 22:15:45 UTC (rev 3322)
+++ box/trunk/lib/server/Daemon.h	2014-04-09 22:15:49 UTC (rev 3323)
@@ -99,7 +99,7 @@
 	bool mRunInForeground;
 	bool mKeepConsoleOpenAfterFork;
 	bool mHaveConfigFile;
-	int mLogLevel; // need an int to do math with
+	Logging::OptionParser mLogLevel;
 	std::string mLogFile;
 	Log::Level mLogFileLevel;
 	std::auto_ptr<FileLogger> mapLogFileLogger;




More information about the Boxbackup-commit mailing list