From e91199a539264ee0883ad1e477f32e4d3983d892 Mon Sep 17 00:00:00 2001 From: Sylvain Chapeland Date: Tue, 24 Sep 2024 16:58:05 +0200 Subject: [PATCH] fix missing closedir for log rotate: was causing too many file open after large number of rotate --- src/SimpleLog.cxx | 7 ++----- test/testSimpleLog.cxx | 7 +++++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/SimpleLog.cxx b/src/SimpleLog.cxx index 95c8111..036d3fc 100644 --- a/src/SimpleLog.cxx +++ b/src/SimpleLog.cxx @@ -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) { @@ -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; } @@ -355,7 +352,7 @@ void SimpleLog::Impl::rotate() rotateIx.push_back(std::stoi(postfix)); } } - free(dp); + closedir(dp); } // sort indexes in order diff --git a/test/testSimpleLog.cxx b/test/testSimpleLog.cxx index b6a0044..32283d3 100644 --- a/test/testSimpleLog.cxx +++ b/test/testSimpleLog.cxx @@ -14,14 +14,17 @@ // helps to test e.g. command line parameters settings #include +#include 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; }