[Box Backup-commit] COMMIT r3324 - in box/trunk/bin: bbackupctl bbackupquery

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


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

Modified:
   box/trunk/bin/bbackupctl/bbackupctl.cpp
   box/trunk/bin/bbackupquery/bbackupquery.cpp
Log:
Use shared code for logging options in bbackupctl and bbackupquery.

Enables all standard logging options and reduces code duplication.

Modified: box/trunk/bin/bbackupctl/bbackupctl.cpp
===================================================================
--- box/trunk/bin/bbackupctl/bbackupctl.cpp	2014-04-09 22:15:49 UTC (rev 3323)
+++ box/trunk/bin/bbackupctl/bbackupctl.cpp	2014-04-09 22:15:52 UTC (rev 3324)
@@ -11,13 +11,12 @@
 
 #include <cstdio>
 #include <cstdlib>
+#include <iostream>
 
 #ifdef HAVE_UNISTD_H
 	#include <unistd.h>
 #endif
 
-#include <cstdlib>
-
 #include "box_getopt.h"
 #include "MainHelper.h"
 #include "BoxPortsAndFiles.h"
@@ -41,9 +40,14 @@
 	NoCommand,
 };
 
-void PrintUsageAndExit()
+void PrintUsageAndExit(int ret)
 {
-	printf("Usage: bbackupctl [-q] [-c config_file] <command>\n"
+	std::cout << 
+	"Usage: bbackupctl [options] <command>\n"
+	"\n"
+	"Options:\n" <<
+	Logging::OptionParser::GetUsageString() <<
+	"\n"
 	"Commands are:\n"
 	"  status -- report daemon status without changing anything\n"
 	"  sync -- start a synchronisation (backup) run now\n"
@@ -54,8 +58,8 @@
 	"  wait-for-sync -- wait until the next sync starts, then exit\n"
 	"  wait-for-end  -- wait until the next sync finishes, then exit\n"
 	"  sync-and-wait -- start sync, wait until it finishes, then exit\n"
-	);
-	exit(1);
+	;
+	exit(ret);
 }
 
 int main(int argc, const char *argv[])
@@ -72,28 +76,27 @@
 	// Filename for configuration file?
 	std::string configFilename = BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE;
 	
-	// Quiet?
-	bool quiet = false;
-	
 	// See if there's another entry on the command line
 	int c;
-	while((c = getopt(argc, (char * const *)argv, "qc:l:")) != -1)
+	std::string options("c:");
+	options += Logging::OptionParser::GetOptionString();
+	Logging::OptionParser LogLevel;
+
+	while((c = getopt(argc, (char * const *)argv, options.c_str())) != -1)
 	{
 		switch(c)
 		{
-		case 'q':
-			// Quiet mode
-			quiet = true;
-			break;
-		
 		case 'c':
 			// store argument
 			configFilename = optarg;
 			break;
 		
-		case '?':
 		default:
-			PrintUsageAndExit();
+			int ret = LogLevel.ProcessOption(c);
+			if(ret != 0)
+			{
+				PrintUsageAndExit(ret);
+			}
 		}
 	}
 	// Adjust arguments
@@ -103,11 +106,13 @@
 	// Check there's a command
 	if(argc != 1)
 	{
-		PrintUsageAndExit();
+		PrintUsageAndExit(2);
 	}
 
+	Logging::FilterConsole(LogLevel.GetCurrentLevel());
+
 	// Read in the configuration file
-	if(!quiet) BOX_NOTICE("Using configuration file " << configFilename);
+	BOX_INFO("Using configuration file " << configFilename);
 
 	std::string errs;
 	std::auto_ptr<Configuration> config(
@@ -191,16 +196,13 @@
 		return 1;
 	}
 	// Print summary?
-	if(!quiet)
-	{
-		BOX_INFO("Daemon configuration summary:\n"
-			"  AutomaticBackup = " << 
-			(autoBackup?"true":"false") << "\n"
-			"  UpdateStoreInterval = " << updateStoreInterval << 
-			" seconds\n"
-			"  MinimumFileAge = " << minimumFileAge << " seconds\n"
-			"  MaxUploadWait = " << maxUploadWait << " seconds");
-	}
+	BOX_TRACE("Daemon configuration summary:\n"
+		"  AutomaticBackup = " << 
+		(autoBackup?"true":"false") << "\n"
+		"  UpdateStoreInterval = " << updateStoreInterval << 
+		" seconds\n"
+		"  MinimumFileAge = " << minimumFileAge << " seconds\n"
+		"  MaxUploadWait = " << maxUploadWait << " seconds");
 
 	std::string stateLine;
 	if(!getLine.GetLine(stateLine) || getLine.IsEOF())
@@ -318,7 +320,7 @@
 			{
 				if(line == "start-sync")
 				{
-					if (!quiet) BOX_INFO("Sync started...");
+					BOX_TRACE("Sync started...");
 					syncIsRunning = true;
 				}
 				else if(line == "finish-sync")
@@ -325,7 +327,7 @@
 				{
 					if (syncIsRunning)
 					{
-						if (!quiet) BOX_INFO("Sync finished.");
+						BOX_TRACE("Sync finished.");
 						// Send a quit command to finish nicely
 						connection.Write("quit\n", 5);
 					
@@ -334,7 +336,7 @@
 					}
 					else
 					{
-						if (!quiet) BOX_INFO("Previous sync finished.");
+						BOX_TRACE("Previous sync finished.");
 					}
 					// daemon must still be busy
 				}
@@ -346,12 +348,9 @@
 				// Is this an OK or error line?
 				if(line == "ok")
 				{
-					if(!quiet)
-					{
-						BOX_INFO("Control command "
-							"sent: " <<
-							commandName);
-					}
+					BOX_TRACE("Control command "
+						"sent: " <<
+						commandName);
 					finished = true;
 				}
 				else if(line == "error")

Modified: box/trunk/bin/bbackupquery/bbackupquery.cpp
===================================================================
--- box/trunk/bin/bbackupquery/bbackupquery.cpp	2014-04-09 22:15:49 UTC (rev 3323)
+++ box/trunk/bin/bbackupquery/bbackupquery.cpp	2014-04-09 22:15:52 UTC (rev 3324)
@@ -66,11 +66,6 @@
 		"Usage: bbackupquery [options] [command]...\n"
 		"\n"
 		"Options:\n"
-		"  -q         Run more quietly, reduce verbosity level by one, can repeat\n"
-		"  -Q         Run at minimum verbosity, log nothing\n"
-		"  -v         Run more verbosely, increase verbosity level by one, can repeat\n"
-		"  -V         Run at maximum verbosity, log everything\n"
-		"  -W <level> Set verbosity to error/warning/notice/info/trace/everything\n"
 		"  -w         Read/write mode, allow changes to store\n"
 #ifdef WIN32
 		"  -u         Enable Unicode console, requires font change to Lucida Console\n"
@@ -80,11 +75,13 @@
 #endif
 		"  -c <file>  Use the specified configuration file. If -c is omitted, the last\n"
 		"             argument is the configuration file, or else the default \n"
-		"             [" << BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE << 
-		"]\n"
+		"             [" << BOX_GET_DEFAULT_BBACKUPD_CONFIG_FILE << "]\n"
 		"  -o <file>  Write logging output to specified file as well as console\n"
 		"  -O <level> Set file verbosity to error/warning/notice/info/trace/everything\n"
 		"  -l <file>  Write protocol debugging logs to specified file\n"
+		<<
+		Logging::OptionParser::GetUsageString()
+		<<
 		"\n"
 		"Parameters: as many commands as you like. If commands are multiple words,\n"
 		"remember to enclose the command in quotes. Remember to use the quit command\n"
@@ -220,12 +217,6 @@
 
 	Logging::SetProgramName("bbackupquery");
 
-	#ifdef BOX_RELEASE_BUILD
-	int consoleLogLevel = Log::NOTICE; // need an int to do math with
-	#else
-	int consoleLogLevel = Log::INFO; // need an int to do math with
-	#endif
-
 #ifdef WIN32
 	#define WIN32_OPTIONS "u"
 	bool unicodeConsole = false;
@@ -240,7 +231,9 @@
 	#define READLINE_OPTIONS
 #endif
 
-	const char* validOpts = "qvVwc:l:o:O:W:" WIN32_OPTIONS READLINE_OPTIONS;
+	std::string options("wc:l:o:O:" WIN32_OPTIONS READLINE_OPTIONS);
+	options += Logging::OptionParser::GetOptionString();
+	Logging::OptionParser LogLevel;
 
 	std::string fileLogFile;
 	Log::Level fileLogLevel = Log::INVALID;
@@ -247,53 +240,10 @@
 
 	// See if there's another entry on the command line
 	int c;
-	while((c = getopt(argc, (char * const *)argv, validOpts)) != -1)
+	while((c = getopt(argc, (char * const *)argv, options.c_str())) != -1)
 	{
 		switch(c)
 		{
-		case 'q':
-			{
-				if(consoleLogLevel == Log::NOTHING)
-				{
-					BOX_FATAL("Too many '-q': "
-						"Cannot reduce logging "
-						"level any more");
-					return 2;
-				}
-				consoleLogLevel--;
-			}
-			break;
-
-		case 'v':
-			{
-				if(consoleLogLevel == Log::EVERYTHING)
-				{
-					BOX_FATAL("Too many '-v': "
-						"Cannot increase logging "
-						"level any more");
-					return 2;
-				}
-				consoleLogLevel++;
-			}
-			break;
-
-		case 'V':
-			{
-				consoleLogLevel = Log::EVERYTHING;
-			}
-			break;
-
-		case 'W':
-			{
-				consoleLogLevel = Logging::GetNamedLevel(optarg);
-				if (consoleLogLevel == Log::INVALID)
-				{
-					BOX_FATAL("Invalid logging level");
-					return 2;
-				}
-			}
-			break;
-
 		case 'w':
 			// Read/write mode
 			readWrite = true;
@@ -342,9 +292,12 @@
 			break;
 #endif
 		
-		case '?':
 		default:
-			PrintUsageAndExit();
+			int ret = LogLevel.ProcessOption(c);
+			if (ret != 0)
+			{
+				PrintUsageAndExit();
+			}
 		}
 	}
 	// Adjust arguments
@@ -351,7 +304,7 @@
 	argc -= optind;
 	argv += optind;
 	
-	Logging::GetConsole().Filter((Log::Level)consoleLogLevel);
+	Logging::GetConsole().Filter(LogLevel.GetCurrentLevel());
 
 	std::auto_ptr<FileLogger> fileLogger;
 	if (fileLogLevel != Log::INVALID)




More information about the Boxbackup-commit mailing list