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:
committed by
Robert Schmidt
parent
ff58b5e169
commit
95e661de6f
@@ -19,8 +19,11 @@ The following options can be specified to trigger the information added in the h
|
||||
- `thread_id`: add the thread ID
|
||||
- `function`: add the function name
|
||||
- `line_num`: adds the (source code) line number
|
||||
- `time`: add the time since process started
|
||||
- `wall_clock`: add the system-wide clock time that measures real (i.e., wall-clock) time (`time` and `wall_clock` are mutually exclusive)
|
||||
- `time`: add the time since the system started in format `ss.ssssss` (seconds and microseconds sourced from `CLOCK_MONOTONIC`)
|
||||
- `wall_clock`: add the system-wide clock time that measures real (i.e., wall-clock) time in format `ss.ssssss` (seconds and microseconds since 1970-01-01 00:00:00 Coordinated Universal Time (UTC))
|
||||
- `utc_time`: add the UTC (Coordinated Universal Time) time in format `YYYY-MM-DD hh:mm:ss.ssssss UTC`. Note that this time is independent of the current time zone (it shows GMT). Also, printing this time has additional overhead compared to other time methods (due to time conversion and formatting).
|
||||
|
||||
Note: `time`, `utc_time` and `wall_clock` are mutually exclusive and cannot be used together.
|
||||
|
||||
### Component specific parameters
|
||||
| name | type | default | description |
|
||||
|
||||
+19
-8
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user