From b120b2932d7d72738a73448b669d978c869f6e1e Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Mon, 8 Apr 2024 21:14:54 +0200 Subject: [PATCH] tests: add timestamp to kernel line Parse timestamps included in kernel kmsg line and add current system time to the messsage as well - this makes it easier to look over, when chasing some messages in journal after some time... --- test/lib/brick-shelltest.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/test/lib/brick-shelltest.h b/test/lib/brick-shelltest.h index 400612564..e50d59cf7 100644 --- a/test/lib/brick-shelltest.h +++ b/test/lib/brick-shelltest.h @@ -710,14 +710,35 @@ struct KMsg : Source { return fd >= 0; } + void transform( char *buf, ssize_t *sz ) { + char newbuf[ buffer_size ]; + struct tm time_info; + unsigned level, num, pos; + unsigned long t; + time_t tt; + size_t len; + + if (sscanf( buf, "%u,%u,%lu,-;%n", &level, &num, &t, &pos ) == 3) { + memcpy( newbuf, buf, *sz ); + tt = time( 0 ); + len = snprintf( buf, 64, "[%lu.%06lu] <%u> ", t / 1000000, t % 1000000, level ); + if ( localtime_r( &tt, &time_info ) ) + len += strftime( buf + len, 64, "%F %T ", &time_info ); + memcpy( buf + len, newbuf + pos, *sz - pos ); + *sz = *sz - pos + len; + } + } + void sync( Sink *s ) { #ifdef __unix ssize_t sz; char buf[ buffer_size ]; if ( dev_kmsg() ) { - while ( (sz = ::read(fd, buf, buffer_size)) > 0 ) + while ( (sz = ::read( fd, buf, buffer_size ) ) > 0 ) { + transform( buf, &sz ); s->push( std::string( buf, sz ) ); + } } else if ( can_clear ) { while ( ( sz = klogctl( BRICK_SYSLOG_ACTION_READ_CLEAR, buf, ( int) buffer_size ) ) > 0 )