Merge branch 'master' of gitea.larke.org:kevin/libcw

This commit is contained in:
kevin 2023-11-25 17:38:34 -05:00
commit f3e5c8456b
4 changed files with 41 additions and 25 deletions

View File

@ -47,7 +47,8 @@ namespace cw
kInvalidStateRC, // 28 kInvalidStateRC, // 28
kTypeMismatchRC, // 29 kTypeMismatchRC, // 29
kNotImplementedRC, // 30 kNotImplementedRC, // 30
kBaseAppRC // 31 kDataCorruptRC, // 31
kBaseAppRC // 32
} cwRC_t; } cwRC_t;
typedef unsigned rc_t; typedef unsigned rc_t;

View File

@ -252,9 +252,12 @@ cw::rc_t cw::file::write( handle_t h, const void* buf, unsigned bufByteCnt )
if( p->lastRC != kOkRC ) if( p->lastRC != kOkRC )
return p->lastRC; return p->lastRC;
errno = 0; if( bufByteCnt )
if( fwrite(buf,bufByteCnt,1,p->fp) != 1 ) {
return p->lastRC = cwLogSysError(kWriteFailRC,errno,"File write failed on '%s'.", cwStringNullGuard(p->fnStr)); 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; return kOkRC;
} }

View File

@ -26,8 +26,8 @@ namespace cw
{ {
{ kPrint_LogLevel, "" }, { kPrint_LogLevel, "" },
{ kDebug_LogLevel, "debug" }, { kDebug_LogLevel, "debug" },
{ kInfo_LogLevel, "info " }, { kInfo_LogLevel, "info" },
{ kWarning_LogLevel, "warn " }, { kWarning_LogLevel, "warn" },
{ kError_LogLevel, "error" }, { kError_LogLevel, "error" },
{ kFatal_LogLevel, "fatal" }, { kFatal_LogLevel, "fatal" },
{ kInvalid_LogLevel, "<invalid>" } { kInvalid_LogLevel, "<invalid>" }
@ -57,6 +57,23 @@ cw::rc_t cw::log::create( handle_t& hRef, unsigned level, logOutputCbFunc_t out
return rc; 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 ) cw::rc_t cw::log::destroy( handle_t& hRef )
{ {
rc_t rc = kOkRC; 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, ... ) 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; rc_t rc = returnCode;
va_list vl; if( level >= _handleToPtr(h)->level )
va_start(vl,fmt); {
rc = msg( h, level, function, filename, line, systemErrorCode, returnCode, fmt, vl ); va_list vl;
va_end(vl); va_start(vl,fmt);
rc = msg( h, level, function, filename, line, systemErrorCode, returnCode, fmt, vl );
va_end(vl);
}
return rc; return rc;
} }

16
cwLog.h
View File

@ -26,6 +26,10 @@ namespace cw
typedef void (*logOutputCbFunc_t)( void* cbArg, unsigned level, const char* text ); 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 ); 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 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 ); 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 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__ ) #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 cwLogVDebugRC(rc,fmt, vl) cwLogVDebugH( cw::log::globalhandle(), (rc), (fmt), (vl) )
#define cwLogDebugRC( rc,fmt,...) cwLogDebugH( cw::log::globalHandle(), (rc), (fmt), ##__VA_ARGS__ ) #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 cwLogVDebug(fmt, vl) cwLogVDebugH( cw::log::globalHandle(), cw::kOkRC, (fmt), (vl) )
#define cwLogDebug( fmt,...) cwLogDebugH( cw::log::globalHandle(), cw::kOkRC, (fmt), ##__VA_ARGS__ ) #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 cwLogVPrint(fmt, vl) cwLogVPrintH( cw::log::globalHandle(), (fmt), (vl) )
#define cwLogPrint( fmt,...) cwLogPrintH( cw::log::globalHandle(), (fmt), ##__VA_ARGS__ ) #define cwLogPrint( fmt,...) cwLogPrintH( cw::log::globalHandle(), (fmt), ##__VA_ARGS__ )