Skip to content

Commit

Permalink
Merge pull request #41 from sy-c/master
Browse files Browse the repository at this point in the history
optimized log /dev/null
  • Loading branch information
sy-c authored Jan 12, 2021
2 parents 124b9cd + d56f96b commit fa7ecee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion include/Common/SimpleLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 16 additions & 1 deletion src/Daemon.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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':
Expand Down Expand Up @@ -120,6 +131,10 @@ Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams)
}
} break;

case 'h': {
print_usage();
} break;

default:
throw __LINE__;
}
Expand Down
22 changes: 16 additions & 6 deletions src/SimpleLog.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit fa7ecee

Please sign in to comment.