summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2014-04-09 22:15:25 +0000
committerChris Wilson <chris+github@qwirx.com>2014-04-09 22:15:25 +0000
commite0d11ae476981fd56229b4ccffdd30166361548b (patch)
tree1454c0ef13ea79acc495f10ca79de31ee613e9ab
parentcd45df502bf91e550692d9930c6aaffc137eaadf (diff)
Remove the global logging level.
It's incompatible with having a logger that logs everything, regardless of the global log level.
-rw-r--r--bin/bbackupquery/BackupQueries.cpp4
-rw-r--r--bin/bbackupquery/bbackupquery.cpp49
-rw-r--r--infrastructure/buildenv-testmain-template.cpp2
-rw-r--r--lib/backupstore/BackupCommands.cpp2
-rw-r--r--lib/backupstore/StoreTestUtils.cpp7
-rw-r--r--lib/common/Box.h20
-rw-r--r--lib/common/Logging.cpp17
-rw-r--r--lib/common/Logging.h54
-rw-r--r--lib/common/Test.cpp67
-rw-r--r--test/backupstore/testbackupstore.cpp5
-rw-r--r--test/bbackupd/testbbackupd.cpp23
11 files changed, 55 insertions, 195 deletions
diff --git a/bin/bbackupquery/BackupQueries.cpp b/bin/bbackupquery/BackupQueries.cpp
index 6204cf90..413b5c1c 100644
--- a/bin/bbackupquery/BackupQueries.cpp
+++ b/bin/bbackupquery/BackupQueries.cpp
@@ -1967,11 +1967,9 @@ void BackupQueries::CommandRestore(const std::vector<std::string> &args, const b
// At TRACE level, we print a line for each file and
// directory, so we don't need dots.
- bool printDots = ! Logging::IsEnabled(Log::TRACE);
-
result = BackupClientRestore(mrConnection, dirID,
storeDirEncoded.c_str(), localName.c_str(),
- printDots /* print progress dots */, restoreDeleted,
+ true /* print progress dots */, restoreDeleted,
false /* don't undelete after restore! */,
opts['r'] /* resume? */,
opts['f'] /* force continue after errors */);
diff --git a/bin/bbackupquery/bbackupquery.cpp b/bin/bbackupquery/bbackupquery.cpp
index 1ff3b28a..07d14871 100644
--- a/bin/bbackupquery/bbackupquery.cpp
+++ b/bin/bbackupquery/bbackupquery.cpp
@@ -221,9 +221,9 @@ int main(int argc, const char *argv[])
Logging::SetProgramName("bbackupquery");
#ifdef BOX_RELEASE_BUILD
- int masterLevel = Log::NOTICE; // need an int to do math with
+ int consoleLogLevel = Log::NOTICE; // need an int to do math with
#else
- int masterLevel = Log::INFO; // need an int to do math with
+ int consoleLogLevel = Log::INFO; // need an int to do math with
#endif
#ifdef WIN32
@@ -253,40 +253,40 @@ int main(int argc, const char *argv[])
{
case 'q':
{
- if(masterLevel == Log::NOTHING)
+ if(consoleLogLevel == Log::NOTHING)
{
BOX_FATAL("Too many '-q': "
"Cannot reduce logging "
"level any more");
return 2;
}
- masterLevel--;
+ consoleLogLevel--;
}
break;
case 'v':
{
- if(masterLevel == Log::EVERYTHING)
+ if(consoleLogLevel == Log::EVERYTHING)
{
BOX_FATAL("Too many '-v': "
"Cannot increase logging "
"level any more");
return 2;
}
- masterLevel++;
+ consoleLogLevel++;
}
break;
case 'V':
{
- masterLevel = Log::EVERYTHING;
+ consoleLogLevel = Log::EVERYTHING;
}
break;
case 'W':
{
- masterLevel = Logging::GetNamedLevel(optarg);
- if (masterLevel == Log::INVALID)
+ consoleLogLevel = Logging::GetNamedLevel(optarg);
+ if (consoleLogLevel == Log::INVALID)
{
BOX_FATAL("Invalid logging level");
return 2;
@@ -351,7 +351,7 @@ int main(int argc, const char *argv[])
argc -= optind;
argv += optind;
- Logging::SetGlobalLevel((Log::Level)masterLevel);
+ Logging::GetConsole().Filter((Log::Level)consoleLogLevel);
std::auto_ptr<FileLogger> fileLogger;
if (fileLogLevel != Log::INVALID)
@@ -359,19 +359,7 @@ int main(int argc, const char *argv[])
fileLogger.reset(new FileLogger(fileLogFile, fileLogLevel));
}
- bool quiet = false;
- if (masterLevel < Log::NOTICE)
- {
- // Quiet mode
- quiet = true;
- }
-
- // Print banner?
- if(!quiet)
- {
- const char *banner = BANNER_TEXT("Backup Query Tool");
- BOX_NOTICE(banner);
- }
+ BOX_NOTICE(BANNER_TEXT("Backup Query Tool"));
#ifdef WIN32
if (unicodeConsole)
@@ -399,7 +387,7 @@ int main(int argc, const char *argv[])
#endif // WIN32
// Read in the configuration file
- if(!quiet) BOX_INFO("Using configuration file " << configFilename);
+ BOX_INFO("Using configuration file " << configFilename);
std::string errs;
std::auto_ptr<Configuration> config(
@@ -428,7 +416,7 @@ int main(int argc, const char *argv[])
BackupClientCryptoKeys_Setup(conf.GetKeyValue("KeysFile").c_str());
// 2. Connect to server
- if(!quiet) BOX_INFO("Connecting to store...");
+ BOX_INFO("Connecting to store...");
SocketStreamTLS *socket = new SocketStreamTLS;
std::auto_ptr<SocketStream> apSocket(socket);
socket->Open(tlsContext, Socket::TypeINET,
@@ -436,7 +424,7 @@ int main(int argc, const char *argv[])
conf.GetKeyValueInt("StorePort"));
// 3. Make a protocol, and handshake
- if(!quiet) BOX_INFO("Handshake with store...");
+ BOX_INFO("Handshake with store...");
std::auto_ptr<BackupProtocolClient>
apConnection(new BackupProtocolClient(apSocket));
BackupProtocolClient& connection(*(apConnection.get()));
@@ -449,7 +437,7 @@ int main(int argc, const char *argv[])
}
// 4. Log in to server
- if(!quiet) BOX_INFO("Login to store...");
+ BOX_INFO("Login to store...");
// Check the version of the server
{
std::auto_ptr<BackupProtocolVersion> serverVersion(connection.QueryVersion(BACKUP_STORE_SERVER_VERSION));
@@ -463,7 +451,8 @@ int main(int argc, const char *argv[])
(readWrite)?0:(BackupProtocolLogin::Flags_ReadOnly));
// 5. Tell user.
- if(!quiet) printf("Login complete.\n\nType \"help\" for a list of commands.\n\n");
+ BOX_INFO("Login complete.");
+ BOX_INFO("Type \"help\" for a list of commands.");
// Set up a context for our work
BackupQueries context(connection, conf, readWrite);
@@ -572,9 +561,9 @@ int main(int argc, const char *argv[])
}
// Done... stop nicely
- if(!quiet) BOX_INFO("Logging off...");
+ BOX_INFO("Logging off...");
connection.QueryFinished();
- if(!quiet) BOX_INFO("Session finished.");
+ BOX_INFO("Session finished.");
// Return code
returnCode = context.GetReturnCode();
diff --git a/infrastructure/buildenv-testmain-template.cpp b/infrastructure/buildenv-testmain-template.cpp
index e0fb71f0..71b6a60d 100644
--- a/infrastructure/buildenv-testmain-template.cpp
+++ b/infrastructure/buildenv-testmain-template.cpp
@@ -372,7 +372,7 @@ int main(int argc, char * const * argv)
}
}
- Logging::SetGlobalLevel((Log::Level)logLevel);
+ Logging::FilterSyslog(Log::NOTHING);
Logging::FilterConsole((Log::Level)logLevel);
argc -= optind - 1;
diff --git a/lib/backupstore/BackupCommands.cpp b/lib/backupstore/BackupCommands.cpp
index 2d927358..8b485d0c 100644
--- a/lib/backupstore/BackupCommands.cpp
+++ b/lib/backupstore/BackupCommands.cpp
@@ -298,7 +298,7 @@ std::auto_ptr<BackupProtocolMessage> BackupProtocolGetObject::DoCommand(BackupPr
// Check the object exists
if(!rContext.ObjectExists(mObjectID))
{
- return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(NoObject));
+ return PROTOCOL_ERROR(Err_DoesNotExist);
}
// Open the object
diff --git a/lib/backupstore/StoreTestUtils.cpp b/lib/backupstore/StoreTestUtils.cpp
index a5dbc28f..b1c8e424 100644
--- a/lib/backupstore/StoreTestUtils.cpp
+++ b/lib/backupstore/StoreTestUtils.cpp
@@ -34,7 +34,8 @@ bool create_account(int soft, int hard)
Configuration::LoadAndVerify
("testfiles/bbstored.conf", &BackupConfigFileVerify, errs));
BackupStoreAccountsControl control(*config);
- Logging::Guard guard(Log::WARNING);
+
+ Logger::LevelGuard guard(Logging::GetConsole(), Log::WARNING);
int result = control.CreateAccount(0x01234567, 0, soft, hard);
TEST_EQUAL(0, result);
return (result == 0);
@@ -47,7 +48,7 @@ bool delete_account()
Configuration::LoadAndVerify
("testfiles/bbstored.conf", &BackupConfigFileVerify, errs));
BackupStoreAccountsControl control(*config);
- Logging::Guard guard(Log::WARNING);
+ Logger::LevelGuard guard(Logging::GetConsole(), Log::WARNING);
TEST_THAT_THROWONFAIL(control.DeleteAccount(0x01234567, false) == 0);
return true;
}
@@ -204,7 +205,7 @@ bool check_num_blocks(BackupProtocolCallable& Client, int Current, int Old,
int check_account_for_errors(Log::Level log_level)
{
- Logging::Guard guard(log_level);
+ Logger::LevelGuard guard(Logging::GetConsole(), log_level);
Logging::Tagger tag("check fix", true);
Logging::ShowTagOnConsole show;
std::string errs;
diff --git a/lib/common/Box.h b/lib/common/Box.h
index 316f4364..8ce2a625 100644
--- a/lib/common/Box.h
+++ b/lib/common/Box.h
@@ -116,16 +116,8 @@
{ \
if((!HideExceptionMessageGuard::ExceptionsHidden() \
&& !HideSpecificExceptionGuard::IsHidden( \
- type::ExceptionType, type::subtype)) \
- || Logging::Guard::IsGuardingFrom(Log::EVERYTHING)) \
+ type::ExceptionType, type::subtype))) \
{ \
- std::auto_ptr<Logging::Guard> guard; \
- \
- if(Logging::Guard::IsGuardingFrom(Log::EVERYTHING)) \
- { \
- guard.reset(new Logging::Guard(Log::EVERYTHING)); \
- } \
- \
OPTIONAL_DO_BACKTRACE \
BOX_WARNING("Exception thrown: " \
#type "(" #subtype ") " \
@@ -140,16 +132,8 @@
_box_throw_line << message; \
if((!HideExceptionMessageGuard::ExceptionsHidden() \
&& !HideSpecificExceptionGuard::IsHidden( \
- type::ExceptionType, type::subtype)) \
- || Logging::Guard::IsGuardingFrom(Log::EVERYTHING)) \
+ type::ExceptionType, type::subtype))) \
{ \
- std::auto_ptr<Logging::Guard> guard; \
- \
- if(Logging::Guard::IsGuardingFrom(Log::EVERYTHING)) \
- { \
- guard.reset(new Logging::Guard(Log::EVERYTHING)); \
- } \
- \
OPTIONAL_DO_BACKTRACE \
BOX_WARNING("Exception thrown: " \
#type "(" #subtype ") (" << \
diff --git a/lib/common/Logging.cpp b/lib/common/Logging.cpp
index 0a52fa5b..af132fa3 100644
--- a/lib/common/Logging.cpp
+++ b/lib/common/Logging.cpp
@@ -42,16 +42,12 @@ std::vector<Logger*> Logging::sLoggers;
std::string Logging::sContext;
Console* Logging::spConsole = NULL;
Syslog* Logging::spSyslog = NULL;
-Log::Level Logging::sGlobalLevel = Log::EVERYTHING;
Logging Logging::sGlobalLogging; //automatic initialisation
std::string Logging::sProgramName;
HideSpecificExceptionGuard::SuppressedExceptions_t
HideSpecificExceptionGuard::sSuppressedExceptions;
-int Logging::Guard::sGuardCount = 0;
-Log::Level Logging::Guard::sOriginalLevel = Log::INVALID;
-
Logging::Logging()
{
ASSERT(!spConsole);
@@ -142,11 +138,6 @@ void Logging::Remove(Logger* pOldLogger)
void Logging::Log(Log::Level level, const std::string& rFile,
int line, const std::string& rMessage)
{
- if (level > sGlobalLevel)
- {
- return;
- }
-
std::string newMessage;
if (sContextSet)
@@ -175,11 +166,6 @@ void Logging::LogToSyslog(Log::Level level, const std::string& rFile,
return;
}
- if (level > sGlobalLevel)
- {
- return;
- }
-
std::string newMessage;
if (sContextSet)
@@ -255,8 +241,7 @@ Logger::~Logger()
bool Logger::IsEnabled(Log::Level level)
{
- return Logging::IsEnabled(level) &&
- (int)mCurrentLevel >= (int)level;
+ return (int)mCurrentLevel >= (int)level;
}
bool Console::sShowTime = false;
diff --git a/lib/common/Logging.h b/lib/common/Logging.h
index 55d09c91..3e38b6af 100644
--- a/lib/common/Logging.h
+++ b/lib/common/Logging.h
@@ -39,9 +39,7 @@
#define BOX_WARNING(stuff) BOX_LOG(Log::WARNING, stuff)
#define BOX_NOTICE(stuff) BOX_LOG(Log::NOTICE, stuff)
#define BOX_INFO(stuff) BOX_LOG(Log::INFO, stuff)
-#define BOX_TRACE(stuff) \
- if (Logging::IsEnabled(Log::TRACE)) \
- { BOX_LOG(Log::TRACE, stuff) }
+#define BOX_TRACE(stuff) BOX_LOG(Log::TRACE, stuff)
#define BOX_SYS_ERRNO_MESSAGE(error_number, stuff) \
stuff << ": " << std::strerror(error_number) << \
@@ -201,19 +199,23 @@ class Logger
virtual void SetProgramName(const std::string& rProgramName) = 0;
- class Guard
+ class LevelGuard
{
private:
Logger& mLogger;
Log::Level mOldLevel;
public:
- Guard(Logger& Logger)
+ LevelGuard(Logger& Logger, Log::Level newLevel = Log::INVALID)
: mLogger(Logger)
{
mOldLevel = Logger.GetLevel();
+ if (newLevel != Log::INVALID)
+ {
+ Logger.Filter(newLevel);
+ }
}
- ~Guard()
+ ~LevelGuard()
{
mLogger.Filter(mOldLevel);
}
@@ -298,7 +300,6 @@ class Logging
static bool sContextSet;
static Console* spConsole;
static Syslog* spSyslog;
- static Log::Level sGlobalLevel;
static Logging sGlobalLogging;
static std::string sProgramName;
@@ -317,52 +318,13 @@ class Logging
int line, const std::string& rMessage);
static void SetContext(std::string context);
static void ClearContext();
- static void SetGlobalLevel(Log::Level level) { sGlobalLevel = level; }
- static Log::Level GetGlobalLevel() { return sGlobalLevel; }
static Log::Level GetNamedLevel(const std::string& rName);
- static bool IsEnabled(Log::Level level)
- {
- return (int)sGlobalLevel >= (int)level;
- }
static void SetProgramName(const std::string& rProgramName);
static std::string GetProgramName() { return sProgramName; }
static void SetFacility(int facility);
static Console& GetConsole() { return *spConsole; }
static Syslog& GetSyslog() { return *spSyslog; }
- class Guard
- {
- private:
- Log::Level mOldLevel;
- static int sGuardCount;
- static Log::Level sOriginalLevel;
-
- public:
- Guard(Log::Level newLevel)
- {
- mOldLevel = Logging::GetGlobalLevel();
- if(sGuardCount == 0)
- {
- sOriginalLevel = mOldLevel;
- }
- sGuardCount++;
- Logging::SetGlobalLevel(newLevel);
- }
- ~Guard()
- {
- sGuardCount--;
- Logging::SetGlobalLevel(mOldLevel);
- }
-
- static bool IsActive() { return (sGuardCount > 0); }
- static Log::Level GetOriginalLevel() { return sOriginalLevel; }
- static bool IsGuardingFrom(Log::Level originalLevel)
- {
- return IsActive() &&
- (int)sOriginalLevel >= (int)originalLevel;
- }
- };
-
class ShowTagOnConsole
{
private:
diff --git a/lib/common/Test.cpp b/lib/common/Test.cpp
index de87c465..bfa7bcb0 100644
--- a/lib/common/Test.cpp
+++ b/lib/common/Test.cpp
@@ -230,14 +230,7 @@ int WaitForServerStartup(const char *pidFile, int pidIfKnown)
#endif
// time for it to start up
- if (Logging::GetGlobalLevel() >= Log::TRACE)
- {
- BOX_TRACE("Waiting for server to start");
- }
- else
- {
- ::fprintf(stdout, "Waiting for server to start: ");
- }
+ BOX_TRACE("Waiting for server to start");
for (int i = 0; i < 15; i++)
{
@@ -251,12 +244,6 @@ int WaitForServerStartup(const char *pidFile, int pidIfKnown)
break;
}
- if (Logging::GetGlobalLevel() < Log::TRACE)
- {
- ::fprintf(stdout, ".");
- ::fflush(stdout);
- }
-
::sleep(1);
}
@@ -265,42 +252,17 @@ int WaitForServerStartup(const char *pidFile, int pidIfKnown)
if (pidIfKnown && !ServerIsAlive(pidIfKnown))
{
- if (Logging::GetGlobalLevel() >= Log::TRACE)
- {
- BOX_ERROR("server died!");
- }
- else
- {
- ::fprintf(stdout, " server died!\n");
- }
-
TEST_FAIL_WITH_MESSAGE("Server died!");
return -1;
}
if (!TestFileNotEmpty(pidFile))
{
- if (Logging::GetGlobalLevel() >= Log::TRACE)
- {
- BOX_ERROR("timed out!");
- }
- else
- {
- ::fprintf(stdout, " timed out!\n");
- }
-
TEST_FAIL_WITH_MESSAGE("Server didn't save PID file");
return -1;
}
- if (Logging::GetGlobalLevel() >= Log::TRACE)
- {
- BOX_TRACE("Server started");
- }
- else
- {
- ::fprintf(stdout, " done.\n");
- }
+ BOX_TRACE("Server started");
// wait a second for the pid to be written to the file
::sleep(1);
@@ -419,35 +381,14 @@ void terminate_bbackupd(int pid)
// Wait a given number of seconds for something to complete
void wait_for_operation(int seconds, const char* message)
{
- if (Logging::GetGlobalLevel() >= Log::TRACE)
- {
- BOX_TRACE("Waiting " << seconds << " seconds for " << message);
- }
- else
- {
- printf("Waiting for %s: ", message);
- fflush(stdout);
- }
+ BOX_TRACE("Waiting " << seconds << " seconds for " << message);
for(int l = 0; l < seconds; ++l)
{
sleep(1);
- if (Logging::GetGlobalLevel() < Log::TRACE)
- {
- printf(".");
- fflush(stdout);
- }
}
- if (Logging::GetGlobalLevel() >= Log::TRACE)
- {
- BOX_TRACE("Finished waiting for " << message);
- }
- else
- {
- printf(" done.\n");
- fflush(stdout);
- }
+ BOX_TRACE("Finished waiting for " << message);
}
void safe_sleep(int seconds)
diff --git a/test/backupstore/testbackupstore.cpp b/test/backupstore/testbackupstore.cpp
index 04472a5d..f38d634d 100644
--- a/test/backupstore/testbackupstore.cpp
+++ b/test/backupstore/testbackupstore.cpp
@@ -1104,6 +1104,11 @@ bool test_server_housekeeping()
ConnectionException, Conn_Protocol_UnexpectedReply);
}
+ // Try retrieving an object that doesn't exist. That used to return
+ // BackupProtocolSuccess(NoObject) for no apparent reason.
+ TEST_COMMAND_RETURNS_ERROR(protocol, QueryGetObject(store1objid + 1),
+ Err_DoesNotExist);
+
// Close the protocol, so we can housekeep the account
protocol.QueryFinished();
TEST_THAT(run_housekeeping_and_check_account());
diff --git a/test/bbackupd/testbbackupd.cpp b/test/bbackupd/testbbackupd.cpp
index 28651233..827e3ea0 100644
--- a/test/bbackupd/testbbackupd.cpp
+++ b/test/bbackupd/testbbackupd.cpp
@@ -324,7 +324,7 @@ int test_basics()
BackupClientFileAttributes t3;
{
- Logging::Guard guard(Log::ERROR);
+ Logger::LevelGuard(Logging::GetConsole(), Log::ERROR);
TEST_CHECK_THROWS(t3.ReadAttributes("doesn't exist"),
CommonException, OSFileError);
}
@@ -345,7 +345,7 @@ int test_basics()
#ifndef WIN32
{
- Logging::Guard guard(Log::ERROR);
+ Logger::LevelGuard(Logging::GetConsole(), Log::ERROR);
TEST_CHECK_THROWS(t1.WriteAttributes("testfiles/test1_nXX"),
CommonException, OSFileError);
TEST_CHECK_THROWS(t3.WriteAttributes("doesn't exist"),
@@ -879,7 +879,7 @@ int test_bbackupd()
context, 0 /* read-write */);
{
- Logging::Guard guard(Log::ERROR);
+ Logger::LevelGuard(Logging::GetConsole(), Log::ERROR);
TEST_CHECK_THROWS(ReadDirectory(*client, 0x12345678),
ConnectionException,
Conn_Protocol_UnexpectedReply);
@@ -1713,11 +1713,6 @@ int test_bbackupd()
// in fork child
TEST_THAT(setsid() != -1);
- if (!Logging::IsEnabled(Log::TRACE))
- {
- Logging::SetGlobalLevel(Log::NOTHING);
- }
-
// BackupStoreDaemon must be destroyed before exit(),
// to avoid memory leaks being reported.
{
@@ -1742,15 +1737,14 @@ int test_bbackupd()
::signal(SIGPIPE, SIG_IGN);
{
- Log::Level newLevel = Logging::GetGlobalLevel();
+ Console& console(Logging::GetConsole());
+ Logger::LevelGuard guard(console);
- if (!Logging::IsEnabled(Log::TRACE))
+ if (console.GetLevel() < Log::TRACE)
{
- newLevel = Log::NOTHING;
+ console.Filter(Log::NOTHING);
}
- Logging::Guard guard(newLevel);
-
BackupDaemon bbackupd;
bbackupd.Configure("testfiles/bbackupd.conf");
bbackupd.InitCrypto();
@@ -3424,7 +3418,8 @@ int test_bbackupd()
fflush(stdout);
{
- Logging::Guard guard(Log::FATAL);
+ Logger::LevelGuard(Logging::GetConsole(),
+ Log::FATAL);
TEST_THAT(BackupClientRestore(*client,
restoredirid, "Test1",
"testfiles/no-such-path/subdir",