Skip to content

Commit

Permalink
Merge pull request #50 from sy-c/master
Browse files Browse the repository at this point in the history
fix simpleLog logrotate()  - dir entry not closed correctly, could cause too many file opened
  • Loading branch information
sy-c authored Sep 24, 2024
2 parents e7afeea + 4fad858 commit 53bae16
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
7 changes: 2 additions & 5 deletions src/SimpleLog.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,6 @@ 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 @@ -281,7 +278,7 @@ int SimpleLog::Impl::openLogFile()
if (rotateMaxFiles == 1) {
mode = "w";
}
fp = fopen(logFilePath.c_str(), "a");
fp = fopen(logFilePath.c_str(), mode);
if (fp == NULL) {
return -1;
}
Expand Down Expand Up @@ -355,7 +352,7 @@ void SimpleLog::Impl::rotate()
rotateIx.push_back(std::stoi(postfix));
}
}
free(dp);
closedir(dp);
}

// sort indexes in order
Expand Down
7 changes: 5 additions & 2 deletions test/testSimpleLog.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
// helps to test e.g. command line parameters settings

#include <Common/SimpleLog.h>
#include <unistd.h>

int main()
{

SimpleLog theLog;
theLog.setLogFile("/tmp/test.log", 100, 4, 0);
theLog.info("test message");

for (int i=0; i<10; i++) {
theLog.info("test message %d",i);
}
//sleep(10);
return 0;
}

0 comments on commit 53bae16

Please sign in to comment.