Skip to content

Commit

Permalink
fix missing closedir for log rotate: was causing too many file open a…
Browse files Browse the repository at this point in the history
…fter large number of rotate
  • Loading branch information
sy-c committed Sep 24, 2024
1 parent 1d2bb2a commit e91199a
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 e91199a

Please sign in to comment.