Skip to content

Commit 6c2920b

Browse files
committed
optimized message drop when log file set to /dev/null
1 parent 124b9cd commit 6c2920b

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

include/Common/SimpleLog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SimpleLog
2323

2424
// Set (or change) the log file name. Previous file used is closed. New file is appended when already existing.
2525
// Optionnaly, an automatic log rotation can be defined. Older files are renamed, appending .1, .2, .3, etc.
26-
// \param logFilePath Path to log file. If NULL, using stdout/stderr.
26+
// \param logFilePath Path to log file. If NULL, using stdout/stderr. If /dev/null, messages are completely dropped.
2727
// \param rotateMaxBytes Maximum file size, after which a new file is created. If zero, no limit.
2828
// \param rotateMaxFiles Maximum number of files to keep (including the "current" file). If zero, no limit.
2929
// \param rotateNow If non-zero, the file is immediately rotated (independently of its size), otherwise it is appended.

src/SimpleLog.cxx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class SimpleLog::Impl
3636
int formatOptions;
3737
int fdStdout;
3838
int fdStderr;
39+
bool disableOutput = 0; // when set, messages completely dropped (logfile=/dev/null)
3940

4041
// log rotation settings
4142
unsigned long rotateMaxBytes = 0;
@@ -65,6 +66,11 @@ SimpleLog::Impl::~Impl()
6566

6667
int SimpleLog::Impl::logV(SimpleLog::Impl::Severity s, const char* message, va_list ap)
6768
{
69+
// immediate return if output disabled
70+
if (disableOutput) {
71+
return 0;
72+
}
73+
6874
char buffer[1024] = "";
6975
size_t len = sizeof(buffer) - 2;
7076
size_t ix = 0;
@@ -167,21 +173,25 @@ SimpleLog::~SimpleLog()
167173
int SimpleLog::setLogFile(const char* logFilePath, unsigned long rotateMaxBytes, unsigned int rotateMaxFiles, unsigned int rotateNow)
168174
{
169175
pImpl->closeLogFile();
176+
pImpl->logFilePath = "";
177+
pImpl->rotateMaxFiles = 0;
178+
pImpl->rotateMaxBytes = 0;
179+
pImpl->logFileSize = 0;
180+
pImpl->disableOutput = 0;
170181
if (logFilePath != NULL) {
171-
pImpl->rotateMaxBytes = rotateMaxBytes;
172-
pImpl->rotateMaxFiles = rotateMaxFiles;
173182
pImpl->logFilePath = logFilePath;
183+
if (!strcmp(logFilePath,"/dev/null")) {
184+
pImpl->disableOutput = 1;
185+
return 0;
186+
}
187+
pImpl->rotateMaxBytes = rotateMaxBytes;
188+
pImpl->rotateMaxFiles = rotateMaxFiles;
174189
if (rotateNow) {
175190
pImpl->rotate();
176191
}
177192
if (pImpl->openLogFile()) {
178193
return -1;
179194
}
180-
} else {
181-
pImpl->logFilePath = "";
182-
pImpl->rotateMaxFiles = 0;
183-
pImpl->rotateMaxBytes = 0;
184-
pImpl->logFileSize = 0;
185195
}
186196
return 0;
187197
}

0 commit comments

Comments
 (0)