Skip to content

Commit 53bae16

Browse files
authored
Merge pull request #50 from sy-c/master
fix simpleLog logrotate() - dir entry not closed correctly, could cause too many file opened
2 parents e7afeea + 4fad858 commit 53bae16

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/SimpleLog.cxx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,6 @@ int SimpleLog::setLogFile(const char* logFilePath, unsigned long rotateMaxBytes,
200200
pImpl->disableOutput = 1;
201201
return 0;
202202
}
203-
if (rotateMaxFiles < 0) {
204-
rotateMaxFiles = 1;
205-
}
206203
pImpl->rotateMaxBytes = rotateMaxBytes;
207204
pImpl->rotateMaxFiles = rotateMaxFiles;
208205
if (rotateNow) {
@@ -281,7 +278,7 @@ int SimpleLog::Impl::openLogFile()
281278
if (rotateMaxFiles == 1) {
282279
mode = "w";
283280
}
284-
fp = fopen(logFilePath.c_str(), "a");
281+
fp = fopen(logFilePath.c_str(), mode);
285282
if (fp == NULL) {
286283
return -1;
287284
}
@@ -355,7 +352,7 @@ void SimpleLog::Impl::rotate()
355352
rotateIx.push_back(std::stoi(postfix));
356353
}
357354
}
358-
free(dp);
355+
closedir(dp);
359356
}
360357

361358
// sort indexes in order

test/testSimpleLog.cxx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@
1414
// helps to test e.g. command line parameters settings
1515

1616
#include <Common/SimpleLog.h>
17+
#include <unistd.h>
1718

1819
int main()
1920
{
2021

2122
SimpleLog theLog;
2223
theLog.setLogFile("/tmp/test.log", 100, 4, 0);
23-
theLog.info("test message");
24-
24+
for (int i=0; i<10; i++) {
25+
theLog.info("test message %d",i);
26+
}
27+
//sleep(10);
2528
return 0;
2629
}
2730

0 commit comments

Comments
 (0)