From cea07592bcaadb7e02cfe54111bb586bd263389c Mon Sep 17 00:00:00 2001 From: kevin Date: Tue, 12 Sep 2023 17:38:44 -0400 Subject: [PATCH] cwLog.h/cpp : Added flags(),set_flags(). --- cwLog.cpp | 22 +++++++++++++++++++--- cwLog.h | 11 +++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cwLog.cpp b/cwLog.cpp index 7d24a0f..26be1bb 100644 --- a/cwLog.cpp +++ b/cwLog.cpp @@ -16,6 +16,7 @@ namespace cw logFormatCbFunc_t fmtCbFunc; void* fmtCbArg; unsigned level; + unsigned flags; } 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); 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); @@ -113,6 +114,19 @@ unsigned cw::log::level( handle_t h ) 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 ) { log_t* p = _handleToPtr(h); @@ -147,7 +161,7 @@ void cw::log::defaultOutput( void* arg, unsigned level, const char* text ) 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. // @@ -179,7 +193,9 @@ void cw::log::defaultFormatter( void* cbArg, logOutputCbFunc_t outFunc, void* ou int tdn = 256; char td[tdn]; - time::formatDateTime( td, (unsigned)tdn ); + td[0] = 0; + if( cwIsFlag(flags,kDateTimeFl) ) + time::formatDateTime( td, (unsigned)tdn ); tdn = strlen(td); diff --git a/cwLog.h b/cwLog.h index 623d0a2..59b4495 100644 --- a/cwLog.h +++ b/cwLog.h @@ -17,10 +17,14 @@ namespace cw kFatal_LogLevel, } logLevelId_t; + enum { + kDateTimeFl = 0x01, + }; + typedef handle handle_t; 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 destroy( handle_t& hRef ); @@ -35,9 +39,12 @@ namespace cw void setFormatCb( handle_t h, logFormatCbFunc_t fmtFunc, void* fmtCbArg ); 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 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 destroyGlobal( );