[Box Backup-commit] COMMIT r1849 - box/chris/general/lib/server

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Fri Sep 21 20:48:23 BST 2007


Author: chris
Date: 2007-09-21 20:48:23 +0100 (Fri, 21 Sep 2007)
New Revision: 1849

Modified:
   box/chris/general/lib/server/Daemon.cpp
   box/chris/general/lib/server/Daemon.h
Log:
Refactor out command-line option processing, so that it can be extended
by subclasses.


Modified: box/chris/general/lib/server/Daemon.cpp
===================================================================
--- box/chris/general/lib/server/Daemon.cpp	2007-09-21 19:47:07 UTC (rev 1848)
+++ box/chris/general/lib/server/Daemon.cpp	2007-09-21 19:48:23 UTC (rev 1849)
@@ -50,7 +50,8 @@
 	  mTerminateWanted(false),
 	  mSingleProcess(false),
 	  mRunInForeground(false),
-	  mKeepConsoleOpenAfterFork(false)
+	  mKeepConsoleOpenAfterFork(false),
+	  mHaveConfigFile(false);
 {
 	if(spDaemon != NULL)
 	{
@@ -87,6 +88,126 @@
 // --------------------------------------------------------------------------
 //
 // Function
+//		Name:    Daemon::GetOptionString()
+//		Purpose: Returns the valid Getopt command-line options.
+//			 This should be overridden by subclasses to add
+//			 their own options, which should override 
+//			 ProcessOption, handle their own, and delegate to
+//			 ProcessOption for the standard options.
+//		Created: 2007/09/18
+//
+// --------------------------------------------------------------------------
+std::string Daemon::GetOptionString()
+{
+	return "c:DFqvVt:Tk";
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+//		Name:    Daemon::ProcessOption(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 Daemon::ProcessOption(signed int option)
+{
+	switch(option)
+	{
+		case 'c':
+		{
+			mConfigFileName = optarg;
+			mHaveConfigFile = true;
+		}
+		break;
+
+		case 'D':
+		{
+			mSingleProcess = true;
+		}
+		break;
+
+		case 'F':
+		{
+			mRunInForeground = true;
+		}
+		break;
+
+		case 'q':
+		{
+			if(mLogLevel == Log::NOTHING)
+			{
+				BOX_FATAL("Too many '-q': "
+					"Cannot reduce logging "
+					"level any more");
+				return 2;
+			}
+			mLogLevel--;
+		}
+		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 't':
+		{
+			Console::SetTag(optarg);
+		}
+		break;
+
+		case 'T':
+		{
+			Console::SetShowTime(true);
+		}
+		break;
+
+		case 'k':
+		{
+			mKeepConsoleOpenAfterFork = true;
+		}
+		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 0;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
 //		Name:    Daemon::Main(const char *, int, const char *[])
 //		Purpose: Parses command-line options, and then calls
 //			Main(std::string& configFile, bool singleProcess)
@@ -98,12 +219,11 @@
 {
 	// Find filename of config file
 	mConfigFileName = DefaultConfigFile;
-	bool haveConfigFile = false;
 
 	#ifdef NDEBUG
-	int masterLevel = Log::NOTICE; // need an int to do math with
+	mLogLevel = Log::NOTICE; // need an int to do math with
 	#else
-	int masterLevel = Log::INFO; // need an int to do math with
+	mLogLevel = Log::INFO; // need an int to do math with
 	#endif
 
 	signed int c;
@@ -118,97 +238,18 @@
 		optreset = 1;
 	#endif
 
-	while((c = getopt(argc, (char * const *)argv, "c:DFqvVt:Tk")) != -1)
+	while((c = getopt(argc, (char * const *)argv, 
+		GetOptionString().c_str())) != -1)
 	{
-		switch(c)
+		int returnCode = ProcessOption(c);
+
+		if (returnCode != 0)
 		{
-			case 'c':
-			{
-				mConfigFileName = optarg;
-				haveConfigFile = true;
-			}
-			break;
-
-			case 'D':
-			{
-				mSingleProcess = true;
-			}
-			break;
-
-			case 'F':
-			{
-				mRunInForeground = true;
-			}
-			break;
-
-			case 'q':
-			{
-				if(masterLevel == Log::NOTHING)
-				{
-					BOX_FATAL("Too many '-q': "
-						"Cannot reduce logging "
-						"level any more");
-					return 2;
-				}
-				masterLevel--;
-			}
-			break;
-
-			case 'v':
-			{
-				if(masterLevel == Log::EVERYTHING)
-				{
-					BOX_FATAL("Too many '-v': "
-						"Cannot increase logging "
-						"level any more");
-					return 2;
-				}
-				masterLevel++;
-			}
-			break;
-
-			case 'V':
-			{
-				masterLevel = Log::EVERYTHING;
-			}
-			break;
-
-			case 't':
-			{
-				Console::SetTag(optarg);
-			}
-			break;
-
-			case 'T':
-			{
-				Console::SetShowTime(true);
-			}
-			break;
-
-			case 'k':
-			{
-				mKeepConsoleOpenAfterFork = true;
-			}
-			break;
-
-			case '?':
-			{
-				BOX_FATAL("Unknown option on command line: " 
-					<< "'" << (char)optopt << "'");
-				return 2;
-			}
-			break;
-
-			default:
-			{
-				BOX_FATAL("Unknown error in getopt: returned "
-					<< "'" << c << "'");
-				return 1;
-			}
+			return returnCode;
 		}
 	}
 
-	if (argc > optind && !haveConfigFile)
+	if (argc > optind && !mHaveConfigFile)
 	{
 		mConfigFileName = argv[optind]; optind++;
 	}
@@ -225,7 +266,7 @@
 		return 2;
 	}
 
-	Logging::SetGlobalLevel((Log::Level)masterLevel);
+	Logging::SetGlobalLevel((Log::Level)mLogLevel);
 
 	return Main(mConfigFileName);
 }

Modified: box/chris/general/lib/server/Daemon.h
===================================================================
--- box/chris/general/lib/server/Daemon.h	2007-09-21 19:47:07 UTC (rev 1848)
+++ box/chris/general/lib/server/Daemon.h	2007-09-21 19:48:23 UTC (rev 1849)
@@ -67,12 +67,13 @@
 protected:
 	box_time_t GetLoadedConfigModifiedTime() const;
 	bool IsSingleProcess() { return mSingleProcess; }
+	std::string GetOptionString();
+	int ProcessOption(signed int option);
 	
 private:
 	static void SignalHandler(int sigraised);
 	box_time_t GetConfigFileModifiedTime() const;
 	
-private:
 	std::string mConfigFileName;
 	Configuration *mpConfiguration;
 	box_time_t mLoadedConfigModifiedTime;
@@ -81,6 +82,8 @@
 	bool mSingleProcess;
 	bool mRunInForeground;
 	bool mKeepConsoleOpenAfterFork;
+	bool mHaveConfigFile;
+	int mLogLevel; // need an int to do math with
 	static Daemon *spDaemon;
 };
 




More information about the Boxbackup-commit mailing list