summaryrefslogtreecommitdiff
path: root/bin/bbackupctl
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2014-04-09 22:15:52 +0000
committerChris Wilson <chris+github@qwirx.com>2014-04-09 22:15:52 +0000
commit06af9253bc5df053dbe2208cbc724853294020a3 (patch)
tree124ceca9b71589f87ab9ac904cd5cf4f255347e6 /bin/bbackupctl
parente6fe203cf40a3542cb6949187387f2ab018bc762 (diff)
Use shared code for logging options in bbackupctl and bbackupquery.
Enables all standard logging options and reduces code duplication.
Diffstat (limited to 'bin/bbackupctl')
-rw-r--r--bin/bbackupctl/bbackupctl.cpp75
1 files changed, 37 insertions, 38 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")