cwLog.cpp : Add date/time stamp to default output.

This commit is contained in:
kevin 2023-05-19 21:21:35 -04:00
parent c93a9670f9
commit 4459a0b130

View File

@ -3,6 +3,7 @@
#include "cwCommonImpl.h" #include "cwCommonImpl.h"
#include "cwMem.h" #include "cwMem.h"
#include "cwTime.h"
namespace cw namespace cw
{ {
@ -25,7 +26,7 @@ namespace cw
{ kPrint_LogLevel, "" }, { kPrint_LogLevel, "" },
{ kDebug_LogLevel, "debug" }, { kDebug_LogLevel, "debug" },
{ kInfo_LogLevel, "info " }, { kInfo_LogLevel, "info " },
{ kWarning_LogLevel, "warning" }, { kWarning_LogLevel, "warn " },
{ kError_LogLevel, "error" }, { kError_LogLevel, "error" },
{ kFatal_LogLevel, "fatal" }, { kFatal_LogLevel, "fatal" },
{ kInvalid_LogLevel, "<invalid>" } { kInvalid_LogLevel, "<invalid>" }
@ -176,6 +177,12 @@ void cw::log::defaultFormatter( void* cbArg, logOutputCbFunc_t outFunc, void* ou
cwAssert(lon==lom); cwAssert(lon==lom);
const char* loStr = los; const char* loStr = los;
int tdn = 256;
char td[tdn];
time::formatDateTime( td, (unsigned)tdn );
tdn = strlen(td);
// don't print the function,file,line when this is an 'info' msg. // don't print the function,file,line when this is an 'info' msg.
if( level == kInfo_LogLevel ) if( level == kInfo_LogLevel )
{ {
@ -189,23 +196,23 @@ void cw::log::defaultFormatter( void* cbArg, logOutputCbFunc_t outFunc, void* ou
if( level == kPrint_LogLevel ) if( level == kPrint_LogLevel )
{ {
const char* fmt = "%s"; const char* fmt = "%s: %s";
int n = snprintf(nullptr,0,fmt,msg); int n = snprintf(nullptr,0,fmt,td,msg);
cwAssert(n != -1); cwAssert(n != -1);
char s[n+1]; char s[n+1];
int m = snprintf(s,n+1,fmt,msg); int m = snprintf(s,n+1,fmt,td,msg);
cwAssert(m==n); cwAssert(m==n);
outFunc(outCbArg,level,s); outFunc(outCbArg,level,s);
} }
else else
{ {
// levelStr, msg,sys_msg, rc, function, lineno, filename // levelStr, msg,sys_msg, rc, function, lineno, filename
const char* fmt = "%s: %s %s %s %s\n"; const char* fmt = "%s: %s: %s %s %s %s\n";
int n = snprintf(nullptr,0,fmt,levelStr,msg,syStr,rcStr,loStr); int n = snprintf(nullptr,0,fmt,levelStr,td,msg,syStr,rcStr,loStr);
cwAssert(n != -1); cwAssert(n != -1);
char s[n+1]; char s[n+1];
int m = snprintf(s,n+1,fmt,levelStr,msg,syStr,rcStr,loStr); int m = snprintf(s,n+1,fmt,levelStr,td,msg,syStr,rcStr,loStr);
cwAssert(m==n); cwAssert(m==n);
outFunc(outCbArg,level,s); outFunc(outCbArg,level,s);
} }