diff --git a/include/Common/SimpleLog.h b/include/Common/SimpleLog.h index 97b14b5..7b7572d 100644 --- a/include/Common/SimpleLog.h +++ b/include/Common/SimpleLog.h @@ -23,7 +23,7 @@ class SimpleLog // Set (or change) the log file name. Previous file used is closed. New file is appended when already existing. // Optionnaly, an automatic log rotation can be defined. Older files are renamed, appending .1, .2, .3, etc. - // \param logFilePath Path to log file. If NULL, using stdout/stderr. + // \param logFilePath Path to log file. If NULL, using stdout/stderr. If /dev/null, messages are completely dropped. // \param rotateMaxBytes Maximum file size, after which a new file is created. If zero, no limit. // \param rotateMaxFiles Maximum number of files to keep (including the "current" file). If zero, no limit. // \param rotateNow If non-zero, the file is immediately rotated (independently of its size), otherwise it is appended. diff --git a/src/Daemon.cxx b/src/Daemon.cxx index 39779ce..252e043 100644 --- a/src/Daemon.cxx +++ b/src/Daemon.cxx @@ -61,6 +61,17 @@ int createDaemon(int closeFiles) return 0; } +void print_usage() +{ + printf("Daemon startup command line options:\n"); + printf(" -z [pathToConfigurationFile] Define the path to the configuration file to be loaded.\n"); + printf(" -o [key]=[value] Set an optional parameter, defined as a key/value pair.\n"); + printf(" Possibly overwritten by corresponding content in configuration file [daemon] section\n"); + printf(" Valid keys: isInteractive, idleSleepTime, userName, redirectOutput,\n"); + printf(" logFile, logRotateMaxBytes, logRotateMaxFiles, logRotateNow.\n"); + printf(" -h This help.\n"); +} + Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams) { isInitialized = 0; @@ -72,7 +83,7 @@ Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams) try { // parse command line parameters int option; - while ((option = getopt(argc, argv, "z:o:")) != -1) { + while ((option = getopt(argc, argv, "z:o:h")) != -1) { switch (option) { case 'z': @@ -120,6 +131,10 @@ Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams) } } break; + case 'h': { + print_usage(); + } break; + default: throw __LINE__; } diff --git a/src/SimpleLog.cxx b/src/SimpleLog.cxx index f2bc2ef..422f791 100644 --- a/src/SimpleLog.cxx +++ b/src/SimpleLog.cxx @@ -36,6 +36,7 @@ class SimpleLog::Impl int formatOptions; int fdStdout; int fdStderr; + bool disableOutput = 0; // when set, messages completely dropped (logfile=/dev/null) // log rotation settings unsigned long rotateMaxBytes = 0; @@ -65,6 +66,11 @@ SimpleLog::Impl::~Impl() int SimpleLog::Impl::logV(SimpleLog::Impl::Severity s, const char* message, va_list ap) { + // immediate return if output disabled + if (disableOutput) { + return 0; + } + char buffer[1024] = ""; size_t len = sizeof(buffer) - 2; size_t ix = 0; @@ -167,21 +173,25 @@ SimpleLog::~SimpleLog() int SimpleLog::setLogFile(const char* logFilePath, unsigned long rotateMaxBytes, unsigned int rotateMaxFiles, unsigned int rotateNow) { pImpl->closeLogFile(); + pImpl->logFilePath = ""; + pImpl->rotateMaxFiles = 0; + pImpl->rotateMaxBytes = 0; + pImpl->logFileSize = 0; + pImpl->disableOutput = 0; if (logFilePath != NULL) { + pImpl->logFilePath = logFilePath; + if (!strcmp(logFilePath, "/dev/null")) { + pImpl->disableOutput = 1; + return 0; + } pImpl->rotateMaxBytes = rotateMaxBytes; pImpl->rotateMaxFiles = rotateMaxFiles; - pImpl->logFilePath = logFilePath; if (rotateNow) { pImpl->rotate(); } if (pImpl->openLogFile()) { return -1; } - } else { - pImpl->logFilePath = ""; - pImpl->rotateMaxFiles = 0; - pImpl->rotateMaxBytes = 0; - pImpl->logFileSize = 0; } return 0; }