X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=src%2Frmr%2Fcommon%2Fsrc%2Flogging.c;h=b38721aef0a7b0c9659bd48319787dc59bb2344e;hb=c8e651e15839411c85e105d16fd2ffc14c1cc1dd;hp=e38d7b0e1cf7c46ec34b5dfafc843eed80ec9b36;hpb=43b7981cee870dcf523b910a3af92ccc53556b2a;p=ric-plt%2Flib%2Frmr.git diff --git a/src/rmr/common/src/logging.c b/src/rmr/common/src/logging.c index e38d7b0..b38721a 100644 --- a/src/rmr/common/src/logging.c +++ b/src/rmr/common/src/logging.c @@ -89,6 +89,23 @@ static int log_hrlogging = 1; static int log_pid = 0; static char* log_situations[RMR_VL_DEBUG+1]; +/* + Return the current unix timestamp as milliseconds since the epoch. + If time() returns 515300400, this function will add three didgets which + represent the milliseconds: 515300400123 (515300400.123). +*/ +extern long long mstime( ) { + struct timespec now; + long long rv = 0; + + if( clock_gettime( CLOCK_REALTIME, &now ) ) { + return rv; + } + + rv = ((long long) now.tv_sec * 1000) + ( (long long) now.tv_nsec/1000000 ); + return rv; +} + /* Initialise logging. Returns the current log level. */ @@ -101,8 +118,8 @@ extern int rmr_vlog_init( ) { if( (data = getenv( ENV_LOG_VLEVEL )) != NULL ) { log_vlevel = atoi( data ); - if( log_vlevel < 0 ) { - log_vlevel = 0; + if( log_vlevel < -1 ) { // allow housekeeping stats to be squelched with -1 + log_vlevel = -1; } else { if( log_vlevel > RMR_VL_DEBUG ) { log_vlevel = RMR_VL_DEBUG; @@ -146,13 +163,18 @@ extern void rmr_vlog( int write_level, char* fmt, ... ) { } memset( msg, 0, sizeof( msg ) ); // logging is slow; this ensures 0 term if msg is too large - hlen = snprintf( msg, sizeof( msg ), "%ld %d/RMR [%s] ", (long) time( NULL ), log_pid, log_situations[write_level] ); + hlen = snprintf( msg, sizeof( msg ), "%lld %d/RMR [%s] ", mstime( ), log_pid, log_situations[write_level] ); + if( hlen > sizeof( msg ) - 1024 ) { // should never happen, but be parinoid + return; + } body = msg + hlen; va_start( argp, fmt ); // suss out parm past fmt vsnprintf( body, sizeof( msg ) - (hlen+2), fmt, argp ); // add in user message formatting it along the way fprintf( stderr, "%s", msg ); // we grew from printfs so all existing msg have \n; assume there + + va_end( argp ); } /* @@ -180,13 +202,15 @@ extern void rmr_vlog_force( int write_level, char* fmt, ... ) { write_level = RMR_VL_DEBUG; } - hlen = snprintf( msg, sizeof( msg ), "%ld %d/RMR [%s] ", (long) time( NULL ), log_pid, log_situations[write_level] ); + hlen = snprintf( msg, sizeof( msg ), "%lld %d/RMR [%s] ", mstime( ), log_pid, log_situations[write_level] ); body = msg + hlen; va_start( argp, fmt ); // suss out parm past fmt vsnprintf( body, sizeof( msg ) - (hlen+2), fmt, argp ); // add in user message formatting it along the way fprintf( stderr, "%s", msg ); // we grew from printfs so all existing msg have \n; assume there + + va_end( argp ); } // -------------------- public functions that are needed ----------------- @@ -203,4 +227,16 @@ extern void rmr_set_vlevel( int new_level ) { } } +#ifdef QUICK_TEST +/* + Quick tests are some simple "pre" unit tests. + Compile with: + gcc -DQUICK_TEST=1 logging.c +*/ +int main( ) { + printf( "mstime=%lld unix=%ld\n", mstime(), (long) time( NULL ) ); +} +#endif + + #endif