cwLog.h/cpp : Added flags(),set_flags().

This commit is contained in:
kevin 2023-09-12 17:38:44 -04:00
parent 75c6959529
commit cea07592bc
2 changed files with 28 additions and 5 deletions

View File

@ -16,6 +16,7 @@ namespace cw
logFormatCbFunc_t fmtCbFunc; logFormatCbFunc_t fmtCbFunc;
void* fmtCbArg; void* fmtCbArg;
unsigned level; unsigned level;
unsigned flags;
} log_t; } log_t;
@ -84,7 +85,7 @@ cw::rc_t cw::log::msg( handle_t h, unsigned level, const char* function, const c
int m = vsnprintf(msg,n+1,fmt,vl1); int m = vsnprintf(msg,n+1,fmt,vl1);
cwAssert(m==n); cwAssert(m==n);
p->fmtCbFunc( p->fmtCbArg, p->outCbFunc, p->outCbArg, level, function, filename, line, systemErrorCode, rc, msg ); p->fmtCbFunc( p->fmtCbArg, p->outCbFunc, p->outCbArg, p->flags, level, function, filename, line, systemErrorCode, rc, msg );
} }
va_end(vl1); va_end(vl1);
@ -113,6 +114,19 @@ unsigned cw::log::level( handle_t h )
return p->level; return p->level;
} }
void cw::log::set_flags( handle_t h, unsigned flags )
{
log_t* p = _handleToPtr(h);
p->flags = flags;
}
unsigned cw::log::flags( handle_t h )
{
log_t* p = _handleToPtr(h);
return p->flags;
}
void cw::log::setOutputCb( handle_t h, logOutputCbFunc_t outFunc, void* outCbArg ) void cw::log::setOutputCb( handle_t h, logOutputCbFunc_t outFunc, void* outCbArg )
{ {
log_t* p = _handleToPtr(h); log_t* p = _handleToPtr(h);
@ -147,7 +161,7 @@ void cw::log::defaultOutput( void* arg, unsigned level, const char* text )
fflush(f); fflush(f);
} }
void cw::log::defaultFormatter( void* cbArg, logOutputCbFunc_t outFunc, void* outCbArg, unsigned level, const char* function, const char* filename, unsigned lineno, int sys_errno, rc_t rc, const char* msg ) void cw::log::defaultFormatter( void* cbArg, logOutputCbFunc_t outFunc, void* outCbArg, unsigned flags, unsigned level, const char* function, const char* filename, unsigned lineno, int sys_errno, rc_t rc, const char* msg )
{ {
// TODO: This code is avoids the use of dynamic memory allocation but relies on stack allocation. It's a security vulnerability. // TODO: This code is avoids the use of dynamic memory allocation but relies on stack allocation. It's a security vulnerability.
// //
@ -179,6 +193,8 @@ void cw::log::defaultFormatter( void* cbArg, logOutputCbFunc_t outFunc, void* ou
int tdn = 256; int tdn = 256;
char td[tdn]; char td[tdn];
td[0] = 0;
if( cwIsFlag(flags,kDateTimeFl) )
time::formatDateTime( td, (unsigned)tdn ); time::formatDateTime( td, (unsigned)tdn );
tdn = strlen(td); tdn = strlen(td);

11
cwLog.h
View File

@ -17,10 +17,14 @@ namespace cw
kFatal_LogLevel, kFatal_LogLevel,
} logLevelId_t; } logLevelId_t;
enum {
kDateTimeFl = 0x01,
};
typedef handle<struct log_str> handle_t; typedef handle<struct log_str> handle_t;
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 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 );
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 );
@ -36,8 +40,11 @@ namespace cw
const char* levelToLabel( unsigned level ); const char* levelToLabel( unsigned level );
unsigned flags( handle_t h );
void set_flags( handle_t h, unsigned flags );
void defaultOutput( void* arg, unsigned level, const char* text ); void defaultOutput( void* arg, unsigned level, const char* text );
void defaultFormatter( void* cbArg, logOutputCbFunc_t outFunc, void* outCbArg, unsigned level, const char* function, const char* filename, unsigned line, int systemErrorCode, rc_t rc, const char* msg ); void defaultFormatter( 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 );
rc_t createGlobal( unsigned level=kDebug_LogLevel, logOutputCbFunc_t outCb=nullptr, void* outCbArg=nullptr, logFormatCbFunc_t fmtCb=nullptr, void* fmtCbArg=nullptr ); rc_t createGlobal( unsigned level=kDebug_LogLevel, logOutputCbFunc_t outCb=nullptr, void* outCbArg=nullptr, logFormatCbFunc_t fmtCb=nullptr, void* fmtCbArg=nullptr );
rc_t destroyGlobal( ); rc_t destroyGlobal( );