Skip to content

Commit fa7ecee

Browse files
authored
Merge pull request #41 from sy-c/master
optimized log /dev/null
2 parents 124b9cd + d56f96b commit fa7ecee

3 files changed

Lines changed: 33 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/Daemon.cxx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ int createDaemon(int closeFiles)
6161
return 0;
6262
}
6363

64+
void print_usage()
65+
{
66+
printf("Daemon startup command line options:\n");
67+
printf(" -z [pathToConfigurationFile] Define the path to the configuration file to be loaded.\n");
68+
printf(" -o [key]=[value] Set an optional parameter, defined as a key/value pair.\n");
69+
printf(" Possibly overwritten by corresponding content in configuration file [daemon] section\n");
70+
printf(" Valid keys: isInteractive, idleSleepTime, userName, redirectOutput,\n");
71+
printf(" logFile, logRotateMaxBytes, logRotateMaxFiles, logRotateNow.\n");
72+
printf(" -h This help.\n");
73+
}
74+
6475
Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams)
6576
{
6677
isInitialized = 0;
@@ -72,7 +83,7 @@ Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams)
7283
try {
7384
// parse command line parameters
7485
int option;
75-
while ((option = getopt(argc, argv, "z:o:")) != -1) {
86+
while ((option = getopt(argc, argv, "z:o:h")) != -1) {
7687
switch (option) {
7788

7889
case 'z':
@@ -120,6 +131,10 @@ Daemon::Daemon(int argc, char* argv[], DaemonConfigParameters* dConfigParams)
120131
}
121132
} break;
122133

134+
case 'h': {
135+
print_usage();
136+
} break;
137+
123138
default:
124139
throw __LINE__;
125140
}

src/SimpleLog.cxx

Lines changed: 16 additions & 6 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) {
182+
pImpl->logFilePath = logFilePath;
183+
if (!strcmp(logFilePath, "/dev/null")) {
184+
pImpl->disableOutput = 1;
185+
return 0;
186+
}
171187
pImpl->rotateMaxBytes = rotateMaxBytes;
172188
pImpl->rotateMaxFiles = rotateMaxFiles;
173-
pImpl->logFilePath = logFilePath;
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)