Skip to content

Commit

Permalink
5.7.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bha-evs committed Jul 26, 2024
1 parent ca5d6e2 commit 2e3c462
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 176 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.1.5)
project(bofstd VERSION 5.7.2.1)

if (EMSCRIPTEN)
message("Force pthread detection for BofStd compilation under EMSCRIPTEN")
Expand Down
85 changes: 63 additions & 22 deletions lib/include/bofstd/bofbasicloggerfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@
* V 1.00 Feb 19 2024 BHA : Initial release
*/
#pragma once
#include <bofstd/ibofloggerfactory.h>
#include <chrono>
#include <ctime>
#include <map>
#include <mutex>
#include <string.h>
#include <bofstd/ibofloggerfactory.h>

BEGIN_BOF_NAMESPACE()
class BasicLogger : public BOF::IBofLogger
{
public:
BasicLogger(const std::string &_rLibNamePrefix_S, const std::string &_rLoggerChannelName_S) : BOF::IBofLogger()
BasicLogger(const std::string &_rLibNamePrefix_S, const std::string &_rLoggerChannelName_S)
: BOF::IBofLogger()
{
mChannelName_S = _rLibNamePrefix_S + _rLoggerChannelName_S;
Open(false, false, false, "");
Open(false, false, false, 0, "");
}
virtual ~BasicLogger()
{
Expand All @@ -38,11 +39,12 @@ class BasicLogger : public BOF::IBofLogger
mpLogFile_X = nullptr;
}
}
void V_Log(LogSeverity /*_SeverityLevel_E*/, const char *_pLogMessage_c, ...) override
void V_Log(LogSeverity /*_SeverityLevel_E*/, const std::string &_rFile_S, uint32_t _Line_U32, const std::string &_rFunc_S, const char *_pLogMessage_c, ...) override
{
if ((mOutputOnScreen_B) || (mpLogFile_X))
{
char pHeader_c[0x100], pLog_c[0x1000];
const char *pFile_c;
va_list VaList_X;
const auto Now = std::chrono::high_resolution_clock::now();
const std::chrono::duration<double> Delta = Now - mLogEpoch;
Expand All @@ -57,40 +59,57 @@ class BasicLogger : public BOF::IBofLogger

t = std::time(nullptr);
std::strftime(pDateTime_c, sizeof(pDateTime_c), "%Y-%m-%d %H:%M:%S", std::localtime(&t));
sprintf(pHeader_c, "%s: %zd [%s]->", pDateTime_c, DeltaInuS, mChannelName_S.c_str());
BOF_GET_FILE_FROM_PATH(_rFile_S.c_str(), pFile_c)
sprintf(pHeader_c, "%s: %zd [%s] %s:%d (%s)->", pDateTime_c, DeltaInuS, mChannelName_S.c_str(), pFile_c, _Line_U32, _rFunc_S.c_str());
mNbLogLine_U32++;
if (mOutputOnScreen_B)
{
printf("%s%s", pHeader_c, pLog_c);
}
if (mpLogFile_X)
{
fwrite(pHeader_c, strlen(pHeader_c), 1, mpLogFile_X);
fwrite(pLog_c, strlen(pLog_c), 1, mpLogFile_X);
if (mAutoFlush_B)
if ((mNbLogLine_U32 & 0x0000000F)==0)
{
if (Size() > mMaxSizeInByte_U32)
{
SwapLogFile();
}
}
if (mpLogFile_X)
{
Flush();
fwrite(pHeader_c, strlen(pHeader_c), 1, mpLogFile_X);
fwrite(pLog_c, strlen(pLog_c), 1, mpLogFile_X);
if ((mNbLogLine_U32 & 0x0000000F) == 0)
{
if (mAutoFlush_B)
{
Flush();
}
}
}
}
}
}
bool Open(bool _OutputOnScreen_B, bool _Append_B, bool _AutoFlush_B, const std::string &_rLogFileSubDir_S)
bool Open(bool _OutputOnScreen_B, bool _Append_B, bool _AutoFlush_B, uint32_t _MaxSizeInByte_U32, const std::string &_rLogFileSubDir_S)
{
bool Rts_B = true;
char pLogFile_c[512];

mOutputOnScreen_B = _OutputOnScreen_B;
mAutoFlush_B = _AutoFlush_B;
mMaxSizeInByte_U32 = _MaxSizeInByte_U32;
mLogFileSubDir_S = _rLogFileSubDir_S;
mNbLogLine_U32 = 0; //Not really true if append mode but not serious as it is only used to lower call frequency to SwapLogFile check and Flush
if (_rLogFileSubDir_S.empty())
{
mpLogFile_X = nullptr;
}
else
{
sprintf(pLogFile_c, "%s/%s.log", _rLogFileSubDir_S.c_str(), mChannelName_S.c_str());
mpLogFile_X = _Append_B ? fopen(pLogFile_c, "w+"): fopen(pLogFile_c, "a+");
sprintf(mpLogFilePath_c, "%s/%s.log", _rLogFileSubDir_S.c_str(), mChannelName_S.c_str());
mpLogFile_X = _Append_B ? fopen(mpLogFilePath_c, "w+") : fopen(mpLogFilePath_c, "a+");
if (mpLogFile_X)
{
V_Log(LOG_SEVERITY_FORCE, "New log session started...\n");
V_Log(LOG_SEVERITY_FORCE, __FILE__, __LINE__, __func__, "New log session started...\n");
}
else
{
Expand All @@ -105,25 +124,42 @@ class BasicLogger : public BOF::IBofLogger

if (mpLogFile_X)
{
V_Log(LOG_SEVERITY_FORCE, "Log session finished !\n");
V_Log(LOG_SEVERITY_FORCE, __FILE__, __LINE__, __func__, "Log session finished !\n");
fclose(mpLogFile_X);
mpLogFile_X = nullptr;
}
mOutputOnScreen_B = false;

return Rts_B;
}
bool SwapLogFile()
{
bool Rts_B = false;
char pBackLogFilePath_c[1024];

//if (Close()) //No because never ending loop (Close call V_Log) and modify var mOutputOnScreen_B
if (mpLogFile_X)
{
fclose(mpLogFile_X);
sprintf(pBackLogFilePath_c, "%s.back", mpLogFilePath_c);
Bof_DeleteFile(pBackLogFilePath_c);
if (Bof_RenameFile(mpLogFilePath_c, pBackLogFilePath_c) == BOF_ERR_NO_ERROR)
{
Rts_B = Open(mOutputOnScreen_B, false, mAutoFlush_B, mMaxSizeInByte_U32, mLogFileSubDir_S);
}
}
return Rts_B;
}
uint64_t Size()
{
uint64_t Rts_U64=0; //CurrentPosition_U64
uint64_t Rts_U64 = 0, CurrentPosition_U64;

if (mpLogFile_X)
{
//CurrentPosition_U64 = ftell(mpLogFile_X);
//fseek(mpLogFile_X, 0, SEEK_END);
CurrentPosition_U64 = ftell(mpLogFile_X);
fseek(mpLogFile_X, 0, SEEK_END);
Rts_U64 = ftell(mpLogFile_X);
//fseek(mpLogFile_X, CurrentPosition_U64, SEEK_SET);
fseek(mpLogFile_X, CurrentPosition_U64, SEEK_SET);
}
return Rts_U64;
}
Expand All @@ -137,19 +173,24 @@ class BasicLogger : public BOF::IBofLogger
}
return Rts_B;
}

private:
std::string mChannelName_S;
const std::chrono::time_point<std::chrono::high_resolution_clock> mLogEpoch = std::chrono::high_resolution_clock::now();
bool mOutputOnScreen_B = false;
bool mAutoFlush_B = false;
FILE *mpLogFile_X = nullptr;
std::string mLogFileSubDir_S;
char mpLogFilePath_c[1024];
uint32_t mMaxSizeInByte_U32 = 0;
uint32_t mNbLogLine_U32 = 0;
};

class BofBasicLoggerFactory : public BOF::IBofLoggerFactory
{
public:
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)
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;
Expand All @@ -169,7 +210,7 @@ class BofBasicLoggerFactory : public BOF::IBofLoggerFactory
psRts = std::make_shared<BasicLogger>(_rLibNamePrefix_S, _rLoggerChannelName_S);
if (psRts)
{
if (psRts->Open(mOutputOnScreen_B, mAppend_B, mAutoFlush_B, mLogFileSubDir_S))
if (psRts->Open(mOutputOnScreen_B, mAppend_B, mAutoFlush_B, (1024*1024), mLogFileSubDir_S))
{
ChannelName_S = BuildChannelName(_rLibNamePrefix_S, _rLoggerChannelName_S);
mLoggerCollection[ChannelName_S] = psRts;
Expand Down
55 changes: 54 additions & 1 deletion lib/include/bofstd/bofthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,60 @@ struct BOF_THREAD_POOL_ENTRY
FctToExec = nullptr;
}
};
#if 1
struct BOF_THREAD_POOL_PARAM
{
uint32_t PoolSize_U32;
uint32_t MaxQueuedRequests_U32;
std::string BaseName_S;
bool PriorityInversionAware_B;
BOF::BOF_THREAD_SCHEDULER_POLICY ThreadSchedulerPolicy_E;
BOF::BOF_THREAD_PRIORITY ThreadPriority_E;
uint64_t ThreadCpuCoreAffinityMask_U64;
uint32_t StackSize_U32;

BOF_THREAD_POOL_PARAM()
{
Reset();
}

void Reset()
{
PoolSize_U32 = 0;
MaxQueuedRequests_U32 = 0;
BaseName_S = "";
PriorityInversionAware_B = false;
ThreadSchedulerPolicy_E = BOF::BOF_THREAD_SCHEDULER_POLICY::BOF_THREAD_SCHEDULER_POLICY_MAX;
ThreadPriority_E = BOF::BOF_THREAD_PRIORITY::BOF_THREAD_PRIORITY_000;
ThreadCpuCoreAffinityMask_U64 = 0;
StackSize_U32 = 0;
}
};
class BofThreadPool
{
public:
BofThreadPool(const BOF_THREAD_POOL_PARAM &_rThreadPoolParam_X);
BofThreadPool(const BofThreadPool &) = delete;
~BofThreadPool();

bool Enqueue(std::function<void(void *)> _Fn, void *_pArg);

private:
BOFERR OnProcessing();

struct THREAD_PARAM
{
std::function<void(void *)> Fn;
void *pArg;
};
BOF_THREAD_POOL_PARAM mThreadPoolParam_X;
std::vector<std::unique_ptr<BOF::BofThread>> mThreadCollection;
std::list<THREAD_PARAM> mJobCollection;
bool mDoShutdown_B = false;
std::condition_variable mDoShedulCv;
std::mutex mMtx;
};
#else
class ThreadPoolExecutor;
class BOFSTD_EXPORT BofThreadPool
{
Expand All @@ -234,5 +287,5 @@ class BOFSTD_EXPORT BofThreadPool
BOFERR ReleaseDispatch(BOFERR _Sts_E, BOF_THREAD_POOL_ENTRY *_pThreadPoolEntry_X, ThreadPoolExecutor *_pThreadPoolExecutor); //Called internally, do not call this method (ThreadPoolExecutor is internal)
};

#endif
END_BOF_NAMESPACE()
52 changes: 26 additions & 26 deletions lib/include/bofstd/ibofloggerfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* V 1.00 Feb 19 2024 BHA : Initial release
*/
#pragma once
#include <cstdarg>
#include <memory>
#include <string>
#include <vector>
#include <cstdarg>

#include "bofstd/bofstd.h"

Expand All @@ -38,7 +38,7 @@ class IBofLogger

IBofLogger() = default;
virtual ~IBofLogger() = default;
virtual void V_Log(LogSeverity _SeverityLevel_E, const char *_pLogMessage_c, ...) = 0;
virtual void V_Log(LogSeverity _SeverityLevel_E, const std::string &_rFile_S, uint32_t _Line_U32, const std::string &_rFunc_S, const char *_pLogMessage_c, ...) = 0;
inline bool SetLogSeverityLevel(LogSeverity _SeverityLevel_E)
{
bool Rts_B = false;
Expand Down Expand Up @@ -71,33 +71,33 @@ class IBofLoggerFactory
virtual bool V_Destroy(const std::string &_rLibNamePrefix_S, const std::string &_rLoggerChannelName_S) = 0;
};

#define LOGGER_LOG(psLogger, level, format, ...) \
if (psLogger) \
{ \
if (level <= psLogger->GetLogSeverityLevel()) \
{ \
psLogger->V_Log(level, format, ##__VA_ARGS__); \
psLogger->mNbLogOut_U32++; \
} \
else \
{ \
psLogger->mNbLogRejected_U32++; \
} \
#define LOGGER_LOG(psLogger, Level, File, Line, Func, Format, ...) \
if (psLogger) \
{ \
if (Level <= psLogger->GetLogSeverityLevel()) \
{ \
psLogger->V_Log(Level, File, Line, Func, Format, ##__VA_ARGS__); \
psLogger->mNbLogOut_U32++; \
} \
else \
{ \
psLogger->mNbLogRejected_U32++; \
} \
}

#ifdef LOGGER_FACTORY_DISABLE_LOGGING
#define LOG_FORCE(psLogger, format, ...)
#define LOG_ERROR(psLogger, format, ...)
#define LOG_WARNING(psLogger, format, ...)
#define LOG_INFO(psLogger, format, ...)
#define LOG_VERBOSE(psLogger, format, ...)
#define LOG_DEBUG(psLogger, format, ...)
#define LOG_FORCE(psLogger, Format, ...)
#define LOG_ERROR(psLogger, Format, ...)
#define LOG_WARNING(psLogger, Format, ...)
#define LOG_INFO(psLogger, Format, ...)
#define LOG_VERBOSE(psLogger, Format, ...)
#define LOG_DEBUG(psLogger, Format, ...)
#else
#define LOG_FORCE(psLogger, format, ...) LOGGER_LOG(psLogger, BOF::IBofLogger::LOG_SEVERITY_FORCE, format, ##__VA_ARGS__)
#define LOG_ERROR(psLogger, format, ...) LOGGER_LOG(psLogger, BOF::IBofLogger::LOG_SEVERITY_ERROR, format, ##__VA_ARGS__)
#define LOG_WARNING(psLogger, format, ...) LOGGER_LOG(psLogger, BOF::IBofLogger::LOG_SEVERITY_WARNING, format, ##__VA_ARGS__)
#define LOG_INFO(psLogger, format, ...) LOGGER_LOG(psLogger, BOF::IBofLogger::LOG_SEVERITY_INFO, format, ##__VA_ARGS__)
#define LOG_VERBOSE(psLogger, format, ...) LOGGER_LOG(psLogger, BOF::IBofLogger::LOG_SEVERITY_VERBOSE, format, ##__VA_ARGS__)
#define LOG_DEBUG(psLogger, format, ...) LOGGER_LOG(psLogger, BOF::IBofLogger::LOG_SEVERITY_DEBUG, format, ##__VA_ARGS__)
#define LOG_FORCE(psLogger, Format, ...) LOGGER_LOG(psLogger, BOF::IBofLogger::LOG_SEVERITY_FORCE, __FILE__, __LINE__, __func__, Format, ##__VA_ARGS__)
#define LOG_ERROR(psLogger, Format, ...) LOGGER_LOG(psLogger, BOF::IBofLogger::LOG_SEVERITY_ERROR, __FILE__, __LINE__, __func__, Format, ##__VA_ARGS__)
#define LOG_WARNING(psLogger, Format, ...) LOGGER_LOG(psLogger, BOF::IBofLogger::LOG_SEVERITY_WARNING, __FILE__, __LINE__, __func__, Format, ##__VA_ARGS__)
#define LOG_INFO(psLogger, Format, ...) LOGGER_LOG(psLogger, BOF::IBofLogger::LOG_SEVERITY_INFO, __FILE__, __LINE__, __func__, Format, ##__VA_ARGS__)
#define LOG_VERBOSE(psLogger, Format, ...) LOGGER_LOG(psLogger, BOF::IBofLogger::LOG_SEVERITY_VERBOSE, __FILE__, __LINE__, __func__, Format, ##__VA_ARGS__)
#define LOG_DEBUG(psLogger, Format, ...) LOGGER_LOG(psLogger, BOF::IBofLogger::LOG_SEVERITY_DEBUG, __FILE__, __LINE__, __func__, Format, ##__VA_ARGS__)
#endif
END_BOF_NAMESPACE()
Loading

0 comments on commit 2e3c462

Please sign in to comment.