Skip to content

Commit 0b9a934

Browse files
committed
Add support for timestamps in messages
Add support for printing a timestamp before each message. This can be configured using the 'timestamp_level' field of the iio_context_params structure. By default, only messages of the LEVEL_DEBUG log level have timestamps. Signed-off-by: Paul Cercueil <[email protected]>
1 parent 35527e7 commit 0b9a934

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

iio-private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ extern const struct iio_backend iio_xml_backend;
237237
extern const struct iio_backend * const iio_backends[];
238238
extern const unsigned int iio_backends_size;
239239

240+
extern uint64_t library_startup_time_us;
241+
240242
ssize_t iio_xml_print_and_sanitized_param(char *ptr, ssize_t len,
241243
const char *before,
242244
const char *param,

library.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
#include "iio-private.h"
1010
#include <iio-config.h>
1111

12+
uint64_t library_startup_time_us;
13+
1214
static void libiio_init(void)
1315
{
16+
library_startup_time_us = iio_read_counter_us();
1417
}
1518

1619
static void libiio_exit(void)

utilities.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ void iio_prm_printf(const struct iio_context_params *params,
385385
const char *fmt, ...)
386386
{
387387
FILE *out = NULL;
388+
uint64_t now;
388389
va_list ap;
389390

390391
va_start(ap, fmt);
@@ -398,8 +399,17 @@ void iio_prm_printf(const struct iio_context_params *params,
398399
out = msg_level <= LEVEL_WARNING ? stderr : stdout;
399400
}
400401

401-
if (out)
402+
if (out) {
403+
if (params
404+
&& params->timestamp_level > LEVEL_NOLOG
405+
&& params->timestamp_level <= msg_level) {
406+
now = iio_read_counter_us() - library_startup_time_us;
407+
fprintf(out, "(%u.%06us) ", (unsigned int)(now / 1000000),
408+
(unsigned int)now % 1000000);
409+
}
410+
402411
vfprintf(out, fmt, ap);
412+
}
403413

404414
va_end(ap);
405415
}

0 commit comments

Comments
 (0)