Add log option for UTC time stamp

If "utc_time" is selected, log lines are prepended with the UTC time,
formatted as YYYY-MM-DD hh:mm:ss.ssssss UTC.

The documentation has been updated. For other time methods, additional
description of what they show has been added.
This commit is contained in:
Jaroslava Fiedlerova
2025-02-28 12:15:57 +01:00
committed by Robert Schmidt
parent ff58b5e169
commit 95e661de6f
2 changed files with 24 additions and 10 deletions
+19 -8
View File
@@ -103,7 +103,8 @@ static const unsigned int FLAG_FILE_LINE = 1 << 4;
static const unsigned int FLAG_TIME = 1 << 5;
static const unsigned int FLAG_THREAD_ID = 1 << 6;
static const unsigned int FLAG_REAL_TIME = 1 << 7;
static const unsigned int FLAG_INITIALIZED = 1 << 8;
static const unsigned int FLAG_UTC_TIME = 1 << 8;
static const unsigned int FLAG_INITIALIZED = 1 << 9;
/** @}*/
static mapping log_options[] = {{"nocolor", FLAG_NOCOLOR},
@@ -114,6 +115,7 @@ static mapping log_options[] = {{"nocolor", FLAG_NOCOLOR},
{"time", FLAG_TIME},
{"thread_id", FLAG_THREAD_ID},
{"wall_clock", FLAG_REAL_TIME},
{"utc_time", FLAG_UTC_TIME},
{NULL, -1}};
mapping * log_option_names_ptr(void)
{
@@ -500,8 +502,8 @@ 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");
AssertFatal(__builtin_popcount(g_log->flag & (FLAG_TIME | FLAG_REAL_TIME | FLAG_UTC_TIME)) <= 1,
"Invalid log options: time, wall_clock and utc_time are mutually exclusive\n");
g_log->flag = g_log->flag | FLAG_INITIALIZED;
return 0;
@@ -545,15 +547,24 @@ static inline int log_header(log_component_t *c,
l[0] = 0;
// output time information
char timeString[32];
if ((flag & FLAG_TIME) || (flag & FLAG_REAL_TIME)) {
char timeString[64];
if ((flag & FLAG_TIME) || (flag & FLAG_REAL_TIME) || (flag & FLAG_UTC_TIME)) {
struct timespec t;
const clockid_t clock = flag & FLAG_TIME ? CLOCK_MONOTONIC : CLOCK_REALTIME;
if (clock_gettime(clock, &t) == -1)
abort();
if (flag & FLAG_UTC_TIME) {
struct tm utc_time;
if (gmtime_r(&t.tv_sec, &utc_time) == NULL)
abort();
snprintf(timeString, sizeof(timeString), "%lu.%06lu ",
t.tv_sec,
t.tv_nsec / 1000);
snprintf(timeString, sizeof(timeString), "%04d-%02d-%02d %02d:%02d:%02d.%06lu UTC ",
utc_time.tm_year + 1900, utc_time.tm_mon + 1, utc_time.tm_mday,
utc_time.tm_hour, utc_time.tm_min, utc_time.tm_sec, t.tv_nsec / 1000);
} else {
snprintf(timeString, sizeof(timeString), "%lu.%06lu ",
t.tv_sec,
t.tv_nsec / 1000);
}
} else {
timeString[0] = 0;
}