From 29bf98cfe263ddb828f3e22b44e642c64c7870c5 Mon Sep 17 00:00:00 2001 From: Vijay Chadachan Date: Sun, 16 Oct 2022 14:06:27 +0200 Subject: [PATCH] Add wall_clock option to the log subsystem to get absolute time stamps --- common/utils/LOG/DOC/rtusage.md | 2 +- common/utils/LOG/log.c | 11 ++++++++--- common/utils/LOG/log.h | 1 + 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/common/utils/LOG/DOC/rtusage.md b/common/utils/LOG/DOC/rtusage.md index 2f6fa5e6ea..5c08a429bd 100644 --- a/common/utils/LOG/DOC/rtusage.md +++ b/common/utils/LOG/DOC/rtusage.md @@ -9,7 +9,7 @@ All logging facility parameters are defined in the log_config section. Some para |:---:|:---:|:---:|:----| | `global_log_level` | `pre-defined string of char` | `info` | Allows printing of messages up to the specified level. Available levels, from lower to higher are `error`,`warn`,`analysis`,`info`,`debug`,`trace` | | `global_log_online` | `boolean` | 1 (=true) | If false, all console messages are discarded, whatever their level | -| `global_log_options` | `list of pre-defined string of char` | | 5 options can be specified to trigger the information added in the header of the message: `nocolor`, disable color usage in log messages, usefull when redirecting logs to a file, where escape sequences used for color selection can be annoying, `level`, add a one letter level id in the message header (T,D,I,A,W,E for trace, debug, info, analysis, warning, error),`thread`, add the thread name in the message header, `thread_id`, adds the thread ID in the message header, `function`, adds the function name, `line_num`, adds the line number, `time` adds the time since process starts| +| `global_log_options` | `list of pre-defined string of char` | | 5 options can be specified to trigger the information added in the header of the message: `nocolor`, disable color usage in log messages, usefull when redirecting logs to a file, where escape sequences used for color selection can be annoying, `level`, add a one letter level id in the message header (T,D,I,A,W,E for trace, debug, info, analysis, warning, error),`thread`, add the thread name in the message header, `thread_id`, adds the thread ID in the message header, `function`, adds the function name, `line_num`, adds the line number, `time` adds the time since process starts, `wall_clock` adds the system-wide clock time that measures real (i.e., wall-clock) time | ### Component specific parameters | name | type | default | description | diff --git a/common/utils/LOG/log.c b/common/utils/LOG/log.c index 043631d706..76489fa0b5 100644 --- a/common/utils/LOG/log.c +++ b/common/utils/LOG/log.c @@ -84,10 +84,10 @@ mapping log_options[] = { {"function", FLAG_FUNCT}, {"time", FLAG_TIME}, {"thread_id", FLAG_THREAD_ID}, + {"wall_clock", FLAG_REAL_TIME}, {NULL,-1} }; - mapping log_maskmap[] = LOG_MASKMAP_INIT; char *log_level_highlight_start[] = {LOG_RED, LOG_ORANGE, LOG_GREEN, "", LOG_BLUE, LOG_CYBL}; /*!< \brief Optional start-format strings for highlighting */ @@ -512,6 +512,9 @@ int logInit (void) memset(&(g_log->log_component[i]),0,sizeof(log_component_t)); } + AssertFatal(!((g_log->flag & FLAG_TIME) && (g_log->flag & FLAG_REAL_TIME)), + "Invalid log options: time and wall_clock both set but are mutually exclusive\n"); + g_log->flag = g_log->flag | FLAG_INITIALIZED; printf("log init done\n"); return 0; @@ -554,10 +557,12 @@ static inline int log_header(log_component_t *c, else l[0] = 0; + // output time information char timeString[32]; - if ( flag & FLAG_TIME ) { + if ((flag & FLAG_TIME) || (flag & FLAG_REAL_TIME)) { struct timespec t; - if (clock_gettime(CLOCK_MONOTONIC, &t) == -1) + const clockid_t clock = flag & FLAG_TIME ? CLOCK_MONOTONIC : CLOCK_REALTIME; + if (clock_gettime(clock, &t) == -1) abort(); snprintf(timeString, sizeof(timeString), "%lu.%06lu ", t.tv_sec, diff --git a/common/utils/LOG/log.h b/common/utils/LOG/log.h index be5d6ac7fe..06cc9a83ba 100644 --- a/common/utils/LOG/log.h +++ b/common/utils/LOG/log.h @@ -129,6 +129,7 @@ extern "C" { #define FLAG_FILE_LINE 0x0040 #define FLAG_TIME 0x0100 #define FLAG_THREAD_ID 0x0200 +#define FLAG_REAL_TIME 0x0400 #define FLAG_INITIALIZED 0x8000 #define SET_LOG_OPTION(O) g_log->flag = (g_log->flag | O)