From ce55c64a3bef8e7574d8657bb7cdfc6ca9cf714f Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 14 Nov 2023 07:51:50 -0500 Subject: [PATCH 1/3] cwCommon.h : Added kDataCorruptRC. --- cwCommon.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cwCommon.h b/cwCommon.h index 2d57f0e..67974db 100644 --- a/cwCommon.h +++ b/cwCommon.h @@ -47,7 +47,8 @@ namespace cw kInvalidStateRC, // 28 kTypeMismatchRC, // 29 kNotImplementedRC, // 30 - kBaseAppRC // 31 + kDataCorruptRC, // 31 + kBaseAppRC // 32 } cwRC_t; typedef unsigned rc_t; From d499dfac1c4dacf97d4bfae9285595c76ab2f01c Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 14 Nov 2023 07:52:15 -0500 Subject: [PATCH 2/3] cwFile.cpp : Added guard to not write if bufByteCnt is 0. --- cwFile.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cwFile.cpp b/cwFile.cpp index 51a5642..e8a35bd 100644 --- a/cwFile.cpp +++ b/cwFile.cpp @@ -251,11 +251,14 @@ cw::rc_t cw::file::write( handle_t h, const void* buf, unsigned bufByteCnt ) if( p->lastRC != kOkRC ) return p->lastRC; - - errno = 0; - if( fwrite(buf,bufByteCnt,1,p->fp) != 1 ) - return p->lastRC = cwLogSysError(kWriteFailRC,errno,"File write failed on '%s'.", cwStringNullGuard(p->fnStr)); + if( bufByteCnt ) + { + errno = 0; + if( fwrite(buf,bufByteCnt,1,p->fp) != 1 ) + return p->lastRC = cwLogSysError(kWriteFailRC,errno,"File write failed on '%s'.", cwStringNullGuard(p->fnStr)); + } + return kOkRC; } From 831b85af649b0cf919fddcddaeddfc1b5ea407e3 Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 14 Nov 2023 07:53:43 -0500 Subject: [PATCH 3/3] cwLog.h/cpp: Debug logging is now level controlled rather than by #define cwLOG_DEBUG. Added levelFromString() levelToString(). --- cwLog.cpp | 34 +++++++++++++++++++++++++++------- cwLog.h | 18 +++++------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/cwLog.cpp b/cwLog.cpp index 26be1bb..bf494c0 100644 --- a/cwLog.cpp +++ b/cwLog.cpp @@ -26,8 +26,8 @@ namespace cw { { kPrint_LogLevel, "" }, { kDebug_LogLevel, "debug" }, - { kInfo_LogLevel, "info " }, - { kWarning_LogLevel, "warn " }, + { kInfo_LogLevel, "info" }, + { kWarning_LogLevel, "warn" }, { kError_LogLevel, "error" }, { kFatal_LogLevel, "fatal" }, { kInvalid_LogLevel, "" } @@ -57,6 +57,23 @@ cw::rc_t cw::log::create( handle_t& hRef, unsigned level, logOutputCbFunc_t out return rc; } +cw::log::logLevelId_t cw::log::levelFromString( const char* label ) +{ + for(unsigned i=0; logLevelLabelArray[i].id != kInvalid_LogLevel; ++i) + if( strcasecmp(label,logLevelLabelArray[i].label) == 0 ) + return (logLevelId_t)logLevelLabelArray[i].id; + return kInvalid_LogLevel; +} + +const char* cw::log::levelToString( logLevelId_t level ) +{ + for(unsigned i=0; logLevelLabelArray[i].id != kInvalid_LogLevel; ++i) + if( logLevelLabelArray[i].id == level ) + return logLevelLabelArray[i].label; + return nullptr; +} + + cw::rc_t cw::log::destroy( handle_t& hRef ) { rc_t rc = kOkRC; @@ -94,11 +111,14 @@ cw::rc_t cw::log::msg( handle_t h, unsigned level, const char* function, const c cw::rc_t cw::log::msg( handle_t h, unsigned level, const char* function, const char* filename, unsigned line, int systemErrorCode, rc_t returnCode, const char* fmt, ... ) { - rc_t rc; - va_list vl; - va_start(vl,fmt); - rc = msg( h, level, function, filename, line, systemErrorCode, returnCode, fmt, vl ); - va_end(vl); + rc_t rc = returnCode; + if( level >= _handleToPtr(h)->level ) + { + va_list vl; + va_start(vl,fmt); + rc = msg( h, level, function, filename, line, systemErrorCode, returnCode, fmt, vl ); + va_end(vl); + } return rc; } diff --git a/cwLog.h b/cwLog.h index 59b4495..708a174 100644 --- a/cwLog.h +++ b/cwLog.h @@ -25,7 +25,11 @@ namespace cw typedef void (*logOutputCbFunc_t)( void* cbArg, unsigned level, const char* text ); typedef void (*logFormatCbFunc_t)( void* cbArg, logOutputCbFunc_t outFunc, void* outCbArg, unsigned flags, unsigned level, const char* function, const char* filename, unsigned line, int systemErrorCode, rc_t rc, const char* msg ); - + + logLevelId_t levelFromString( const char* label ); + const char* levelToString( logLevelId_t level ); + + rc_t create( handle_t& hRef, unsigned level=kDebug_LogLevel, logOutputCbFunc_t outCb=nullptr, void* outCbArg=nullptr, logFormatCbFunc_t fmtCb=nullptr, void* fmtCbArg=nullptr ); rc_t destroy( handle_t& hRef ); @@ -79,24 +83,12 @@ namespace cw #define cwLogVFatalH(h,rc,fmt, vl) cw::log::msg( h, cw::log::kFatal_LogLevel, __FUNCTION__, __FILE__, __LINE__, 0, rc, fmt, vl ) #define cwLogFatalH( h,rc,fmt,...) cw::log::msg( h, cw::log::kFatal_LogLevel, __FUNCTION__, __FILE__, __LINE__, 0, rc, fmt, ##__VA_ARGS__ ) -#ifdef cwLOG_DEBUG - #define cwLogVDebugRC(rc,fmt, vl) cwLogVDebugH( cw::log::globalhandle(), (rc), (fmt), (vl) ) #define cwLogDebugRC( rc,fmt,...) cwLogDebugH( cw::log::globalHandle(), (rc), (fmt), ##__VA_ARGS__ ) #define cwLogVDebug(fmt, vl) cwLogVDebugH( cw::log::globalHandle(), cw::kOkRC, (fmt), (vl) ) #define cwLogDebug( fmt,...) cwLogDebugH( cw::log::globalHandle(), cw::kOkRC, (fmt), ##__VA_ARGS__ ) -#else - -#define cwLogVDebugRC(rc,fmt, vl) -#define cwLogDebugRC( rc,fmt,...) - -#define cwLogVDebug(fmt, vl) -#define cwLogDebug( fmt,...) - -#endif - #define cwLogVPrint(fmt, vl) cwLogVPrintH( cw::log::globalHandle(), (fmt), (vl) ) #define cwLogPrint( fmt,...) cwLogPrintH( cw::log::globalHandle(), (fmt), ##__VA_ARGS__ )