From e571fbb7e2f4fb84f93e7c335826ad09b424adcc Mon Sep 17 00:00:00 2001 From: "HARMEL, Bernard" Date: Fri, 12 Jul 2024 12:59:43 +0200 Subject: [PATCH] 5.7.1.4 --- CMakeLists.txt | 2 +- lib/include/bofstd/bofbasicloggerfactory.h | 57 ++++++++++++++++++++-- tests/src/ut_logger.cpp | 2 +- vcpkg.json | 2 +- 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4dece3f..e94a221 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,7 +47,7 @@ include(cmake/fetch_content.cmake) # in code coverage computation as they are test programs themselves. set(EXTRA_COVERAGE_EXCLUSION "\'${CMAKE_CURRENT_SOURCE_DIR}/integration/*\'") -project(bofstd VERSION 5.7.0.3) +project(bofstd VERSION 5.7.1.4) if (EMSCRIPTEN) message("Force pthread detection for BofStd compilation under EMSCRIPTEN") diff --git a/lib/include/bofstd/bofbasicloggerfactory.h b/lib/include/bofstd/bofbasicloggerfactory.h index 97c835f..38bce72 100644 --- a/lib/include/bofstd/bofbasicloggerfactory.h +++ b/lib/include/bofstd/bofbasicloggerfactory.h @@ -28,7 +28,7 @@ class BasicLogger : public BOF::IBofLogger BasicLogger(const std::string &_rLibNamePrefix_S, const std::string &_rLoggerChannelName_S) : BOF::IBofLogger() { mChannelName_S = _rLibNamePrefix_S + _rLoggerChannelName_S; - Configure(false, ""); + Open(false, false, false, ""); } virtual ~BasicLogger() { @@ -66,15 +66,20 @@ class BasicLogger : public BOF::IBofLogger { fwrite(pHeader_c, strlen(pHeader_c), 1, mpLogFile_X); fwrite(pLog_c, strlen(pLog_c), 1, mpLogFile_X); + if (mAutoFlush_B) + { + Flush(); + } } } } - bool Configure(bool _OutputOnScreen_B, const std::string &_rLogFileSubDir_S) + bool Open(bool _OutputOnScreen_B, bool _Append_B, bool _AutoFlush_B, const std::string &_rLogFileSubDir_S) { bool Rts_B = true; char pLogFile_c[512]; mOutputOnScreen_B = _OutputOnScreen_B; + mAutoFlush_B = _AutoFlush_B; if (_rLogFileSubDir_S.empty()) { mpLogFile_X = nullptr; @@ -82,26 +87,66 @@ class BasicLogger : public BOF::IBofLogger else { sprintf(pLogFile_c, "%s/%s.log", _rLogFileSubDir_S.c_str(), mChannelName_S.c_str()); - mpLogFile_X = fopen(pLogFile_c, "w+"); + mpLogFile_X = _Append_B ? fopen(pLogFile_c, "w+"): fopen(pLogFile_c, "a+"); if (mpLogFile_X == nullptr) { + V_Log(LOG_SEVERITY_FORCE, "New log session started...\n"); Rts_B = false; } } return Rts_B; } + bool Close() + { + bool Rts_B = true; + + if (mpLogFile_X) + { + V_Log(LOG_SEVERITY_FORCE, "Log session finished !\n"); + fclose(mpLogFile_X); + mpLogFile_X = nullptr; + } + mOutputOnScreen_B = false; + + return Rts_B; + } + uint64_t Size() + { + uint64_t Rts_U64=0; //CurrentPosition_U64 + + if (mpLogFile_X) + { + //CurrentPosition_U64 = ftell(mpLogFile_X); + //fseek(mpLogFile_X, 0, SEEK_END); + Rts_U64 = ftell(mpLogFile_X); + //fseek(mpLogFile_X, CurrentPosition_U64, SEEK_SET); + } + return Rts_U64; + } + bool Flush() + { + bool Rts_B = true; + + if (mpLogFile_X) + { + fflush(mpLogFile_X); + } + return Rts_B; + } private: std::string mChannelName_S; const std::chrono::time_point mLogEpoch = std::chrono::high_resolution_clock::now(); bool mOutputOnScreen_B = false; + bool mAutoFlush_B = false; FILE *mpLogFile_X = nullptr; }; class BofBasicLoggerFactory : public BOF::IBofLoggerFactory { public: - BofBasicLoggerFactory(bool _OutputOnScreen_B, const std::string &_rLogFileSubDir_S) : mOutputOnScreen_B(_OutputOnScreen_B), mLogFileSubDir_S(_rLogFileSubDir_S) + BofBasicLoggerFactory(bool _OutputOnScreen_B, bool _Append_B, bool _AutoFlush_B, const std::string &_rLogFileSubDir_S) : + mOutputOnScreen_B(_OutputOnScreen_B), mAppend_B(_Append_B), mAutoFlush_B(_AutoFlush_B), mLogFileSubDir_S(_rLogFileSubDir_S) { } virtual ~BofBasicLoggerFactory() = default; @@ -121,7 +166,7 @@ class BofBasicLoggerFactory : public BOF::IBofLoggerFactory psRts = std::make_shared(_rLibNamePrefix_S, _rLoggerChannelName_S); if (psRts) { - if (psRts->Configure(mOutputOnScreen_B, mLogFileSubDir_S)) + if (psRts->Open(mOutputOnScreen_B, mAppend_B, mAutoFlush_B, mLogFileSubDir_S)) { ChannelName_S = BuildChannelName(_rLibNamePrefix_S, _rLoggerChannelName_S); mLoggerCollection[ChannelName_S] = psRts; @@ -166,6 +211,8 @@ class BofBasicLoggerFactory : public BOF::IBofLoggerFactory std::mutex mLoggerCollectionMtx; std::map> mLoggerCollection; bool mOutputOnScreen_B; + bool mAppend_B; + bool mAutoFlush_B; const std::string mLogFileSubDir_S; }; END_BOF_NAMESPACE() diff --git a/tests/src/ut_logger.cpp b/tests/src/ut_logger.cpp index 67c6e9e..234b444 100644 --- a/tests/src/ut_logger.cpp +++ b/tests/src/ut_logger.cpp @@ -334,7 +334,7 @@ void MyLibCode(bool _NullTestCase_B) TEST(ut_logger_ibofloggerfactory, MultipleChannel) { - std::shared_ptr psLoggerFactory = std::make_shared(true, "."); + std::shared_ptr psLoggerFactory = std::make_shared(true,false,false, "."); MyLibInit(psLoggerFactory); EXPECT_TRUE(S_psLoggerCollection[UT_LOGGER_CHANNEL::UT_LOGGER_CHANNEL_INIT] != nullptr); EXPECT_TRUE(S_psLoggerCollection[UT_LOGGER_CHANNEL::UT_LOGGER_CHANNEL_CODEC] != nullptr); diff --git a/vcpkg.json b/vcpkg.json index bc3249f..ad5ac18 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,6 +1,6 @@ { "name": "bofstd", - "version": "5.7.0.3", + "version": "5.7.1.4", "description": "The onbings general purpose C++ Multiplatform library", "dependencies": [ {