Skip to content

Commit

Permalink
Merge pull request #48 from sy-c/master
Browse files Browse the repository at this point in the history
SimpleLog
  • Loading branch information
sy-c authored Oct 24, 2022
2 parents 604a4c3 + ca352b6 commit 51f9c7e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/Common/SimpleLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SimpleLog
// 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. 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 rotateMaxFiles Maximum number of files to keep (including the "current" file). If zero, no limit. If one, a single file is created and cleared immediately, and messages are discarded after reaching rotateMaxBytes.
// \param rotateNow If non-zero, the file is immediately rotated (independently of its size), otherwise it is appended.
int setLogFile(const char* logFilePath = NULL,
unsigned long rotateMaxBytes = 0, unsigned int rotateMaxFiles = 0, unsigned int rotateNow = 0);
Expand Down
14 changes: 13 additions & 1 deletion src/SimpleLog.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ int SimpleLog::Impl::logV(SimpleLog::Impl::Severity s, const char* message, va_l
if (fp != NULL) {
if ((ix + logFileSize > rotateMaxBytes) && (rotateMaxBytes > 0)) {
closeLogFile();
if (rotateMaxFiles == 1) {
// stop after first file
disableOutput = 1;
return -1;
}
rotate();
if (openLogFile())
return -1;
Expand Down Expand Up @@ -195,6 +200,9 @@ int SimpleLog::setLogFile(const char* logFilePath, unsigned long rotateMaxBytes,
pImpl->disableOutput = 1;
return 0;
}
if (rotateMaxFiles < 0) {
rotateMaxFiles = 1;
}
pImpl->rotateMaxBytes = rotateMaxBytes;
pImpl->rotateMaxFiles = rotateMaxFiles;
if (rotateNow) {
Expand Down Expand Up @@ -269,6 +277,10 @@ int SimpleLog::Impl::openLogFile()
if (logFilePath.length() == 0) {
return 0;
}
const char* mode = "a";
if (rotateMaxFiles == 1) {
mode = "w";
}
fp = fopen(logFilePath.c_str(), "a");
if (fp == NULL) {
return -1;
Expand Down Expand Up @@ -371,7 +383,7 @@ void SimpleLog::Impl::rotate()
} else {
inFile = dirName + fileName + "." + std::to_string(oldIndex);
}
if ((newIndex > rotateMaxFiles) && (rotateMaxFiles != 0)) {
if ((newIndex >= rotateMaxFiles) && (rotateMaxFiles != 0)) {
// this file should be removed
unlink(inFile.c_str());
} else {
Expand Down

0 comments on commit 51f9c7e

Please sign in to comment.