diff options
author | Chris Wilson <chris+github@qwirx.com> | 2014-04-09 22:15:52 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2014-04-09 22:15:52 +0000 |
commit | 06af9253bc5df053dbe2208cbc724853294020a3 (patch) | |
tree | 124ceca9b71589f87ab9ac904cd5cf4f255347e6 /bin | |
parent | e6fe203cf40a3542cb6949187387f2ab018bc762 (diff) |
Use shared code for logging options in bbackupctl and bbackupquery.
Enables all standard logging options and reduces code duplication.
Diffstat (limited to 'bin')
-rw-r--r-- | bin/bbackupctl/bbackupctl.cpp | 75 | ||||
-rw-r--r-- | bin/bbackupquery/bbackupquery.cpp | 75 |
2 files changed, 51 insertions, 99 deletions
diff --git a/bin/bbackupctl/bbackupctl.cpp b/bin/bbackupctl/bbackupctl.cpp index 69dd7215..becb1fcf 100644 --- a/bin/bbackupctl/bbackupctl.cpp +++ b/bin/bbackupctl/bbackupctl.cpp @@ -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 @@ enum Command 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 @@ void PrintUsageAndExit() " 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 @@ int main(int argc, const char *argv[]) // 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 @@ int main(int argc, const char *argv[]) // 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 @@ int main(int argc, const char *argv[]) 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,14 +320,14 @@ int main(int argc, const char *argv[]) { if(line == "start-sync") { - if (!quiet) BOX_INFO("Sync started..."); + BOX_TRACE("Sync started..."); syncIsRunning = true; } else if(line == "finish-sync") { 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 @@ int main(int argc, const char *argv[]) } else { - if (!quiet) BOX_INFO("Previous sync finished."); + BOX_TRACE("Previous sync finished."); } // daemon must still be busy } @@ -346,12 +348,9 @@ int main(int argc, const char *argv[]) // 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") diff --git a/bin/bbackupquery/bbackupquery.cpp b/bin/bbackupquery/bbackupquery.cpp index 07d14871..62315b12 100644 --- a/bin/bbackupquery/bbackupquery.cpp +++ b/bin/bbackupquery/bbackupquery.cpp @@ -66,11 +66,6 @@ void PrintUsageAndExit() "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 @@ void PrintUsageAndExit() #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 @@ int main(int argc, const char *argv[]) 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,60 +231,19 @@ int main(int argc, const char *argv[]) #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; // 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,16 +292,19 @@ int main(int argc, const char *argv[]) break; #endif - case '?': default: - PrintUsageAndExit(); + int ret = LogLevel.ProcessOption(c); + if (ret != 0) + { + PrintUsageAndExit(); + } } } // Adjust arguments argc -= optind; argv += optind; - Logging::GetConsole().Filter((Log::Level)consoleLogLevel); + Logging::GetConsole().Filter(LogLevel.GetCurrentLevel()); std::auto_ptr<FileLogger> fileLogger; if (fileLogLevel != Log::INVALID) |