cwLog.h/cpp: Debug logging is now level controlled rather than by #define cwLOG_DEBUG.

Added levelFromString() levelToString().
This commit is contained in:
kevin 2023-11-14 07:53:43 -05:00
parent d499dfac1c
commit 831b85af64
2 changed files with 32 additions and 20 deletions

View File

@ -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, "<invalid>" }
@ -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;
}

18
cwLog.h
View File

@ -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__ )