From 6c2920b1da63bb3d63381e2bc140d346a8495d54 Mon Sep 17 00:00:00 2001 From: Sylvain Chapeland Date: Tue, 12 Jan 2021 10:36:16 +0100 Subject: [PATCH 1/4] optimized message drop when log file set to /dev/null --- include/Common/SimpleLog.h | 2 +- src/SimpleLog.cxx | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) 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/SimpleLog.cxx b/src/SimpleLog.cxx index f2bc2ef..c8926c5 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->rotateMaxBytes = rotateMaxBytes; - pImpl->rotateMaxFiles = rotateMaxFiles; pImpl->logFilePath = logFilePath; + if (!strcmp(logFilePath,"/dev/null")) { + pImpl->disableOutput = 1; + return 0; + } + pImpl->rotateMaxBytes = rotateMaxBytes; + pImpl->rotateMaxFiles = rotateMaxFiles; if (rotateNow) { pImpl->rotate(); } if (pImpl->openLogFile()) { return -1; } - } else { - pImpl->logFilePath = ""; - pImpl->rotateMaxFiles = 0; - pImpl->rotateMaxBytes = 0; - pImpl->logFileSize = 0; } return 0; } From 9d096f393cc38ad636160b96af2d8f642010c3cc Mon Sep 17 00:00:00 2001 From: Sylvain Chapeland Date: Tue, 12 Jan 2021 10:42:02 +0100 Subject: [PATCH 2/4] clang-format --- src/SimpleLog.cxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/SimpleLog.cxx b/src/SimpleLog.cxx index c8926c5..422f791 100644 --- a/src/SimpleLog.cxx +++ b/src/SimpleLog.cxx @@ -70,7 +70,7 @@ int SimpleLog::Impl::logV(SimpleLog::Impl::Severity s, const char* message, va_l if (disableOutput) { return 0; } - + char buffer[1024] = ""; size_t len = sizeof(buffer) - 2; size_t ix = 0; @@ -180,12 +180,12 @@ int SimpleLog::setLogFile(const char* logFilePath, unsigned long rotateMaxBytes, pImpl->disableOutput = 0; if (logFilePath != NULL) { pImpl->logFilePath = logFilePath; - if (!strcmp(logFilePath,"/dev/null")) { + if (!strcmp(logFilePath, "/dev/null")) { pImpl->disableOutput = 1; return 0; } pImpl->rotateMaxBytes = rotateMaxBytes; - pImpl->rotateMaxFiles = rotateMaxFiles; + pImpl->rotateMaxFiles = rotateMaxFiles; if (rotateNow) { pImpl->rotate(); } From 726204d16b8c9c5e0898d9054ada87fc18369695 Mon Sep 17 00:00:00 2001 From: Sylvain Chapeland Date: Tue, 12 Jan 2021 11:43:21 +0100 Subject: [PATCH 3/4] added daemon command line help --- src/Daemon.cxx | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Daemon.cxx b/src/Daemon.cxx index 39779ce..9a3c402 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': @@ -119,6 +130,10 @@ Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams) throw __LINE__; } } break; + + case 'h': { + print_usage(); + } break; default: throw __LINE__; From d56f96b5d17bea79107215e5c50bcd95f1b3ba34 Mon Sep 17 00:00:00 2001 From: Sylvain Chapeland Date: Tue, 12 Jan 2021 11:45:27 +0100 Subject: [PATCH 4/4] clang-format --- src/Daemon.cxx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Daemon.cxx b/src/Daemon.cxx index 9a3c402..252e043 100644 --- a/src/Daemon.cxx +++ b/src/Daemon.cxx @@ -130,10 +130,10 @@ Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams) throw __LINE__; } } break; - - case 'h': { - print_usage(); - } break; + + case 'h': { + print_usage(); + } break; default: throw __LINE__;