X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=src%2Frmr%2Fcommon%2Fsrc%2Flogging.c;h=b38721aef0a7b0c9659bd48319787dc59bb2344e;hb=26864559bd7ae1b0fd2054ae07c3080fa9121e08;hp=56457f7b530be291d5bf7419bcdcc3cedcb45d2c;hpb=ce1c741c01e8387cb095dac5e36a4d8ad91d006d;p=ric-plt%2Flib%2Frmr.git diff --git a/src/rmr/common/src/logging.c b/src/rmr/common/src/logging.c index 56457f7..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. */ @@ -146,7 +163,7 @@ 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; } @@ -185,7 +202,7 @@ 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 @@ -210,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