diff options
author | Dan Streetman <ddstreet@canonical.com> | 2019-07-15 09:56:24 -0400 |
---|---|---|
committer | Dan Streetman <ddstreet@canonical.com> | 2020-02-10 07:01:30 -0500 |
commit | c5673ed0de3bec38f68d8113d253842b47766e27 (patch) | |
tree | ab275a4760e03c667da4702f99fc096944b90cf6 /src/basic/log.c | |
parent | 2526af6ddde359cc2ac9e475f3dc6effde2e25af (diff) | |
download | systemd-c5673ed0de3bec38f68d8113d253842b47766e27.tar.gz |
log: add support for prefixing console log messages with current timestamp
Diffstat (limited to 'src/basic/log.c')
-rw-r--r-- | src/basic/log.c | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/src/basic/log.c b/src/basic/log.c index 17557e1844..ca024d970e 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -51,6 +51,7 @@ static bool syslog_is_stream = false; static bool show_color = false; static bool show_location = false; +static bool show_time = false; static bool upgrade_syslog_to_journal = false; static bool always_reopen_console = false; @@ -332,8 +333,10 @@ static int write_to_console( const char *func, const char *buffer) { - char location[256], prefix[1 + DECIMAL_STR_MAX(int) + 2]; - struct iovec iovec[6] = {}; + char location[256], + header_time[FORMAT_TIMESTAMP_MAX], + prefix[1 + DECIMAL_STR_MAX(int) + 2]; + struct iovec iovec[8] = {}; const char *on = NULL, *off = NULL; size_t n = 0; @@ -345,6 +348,13 @@ static int write_to_console( iovec[n++] = IOVEC_MAKE_STRING(prefix); } + if (show_time) { + if (format_timestamp(header_time, sizeof(header_time), now(CLOCK_REALTIME))) { + iovec[n++] = IOVEC_MAKE_STRING(header_time); + iovec[n++] = IOVEC_MAKE_STRING(" "); + } + } + if (show_color) get_log_colors(LOG_PRI(level), &on, &off, NULL); @@ -1099,6 +1109,12 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat if (log_show_location_from_string(value ?: "1") < 0) log_warning("Failed to parse log location setting '%s'. Ignoring.", value); + + } else if (proc_cmdline_key_streq(key, "systemd.log_time")) { + + if (log_show_time_from_string(value ?: "1") < 0) + log_warning("Failed to parse log time setting '%s'. Ignoring.", value); + } return 0; @@ -1130,6 +1146,10 @@ void log_parse_environment_realm(LogRealm realm) { e = getenv("SYSTEMD_LOG_LOCATION"); if (e && log_show_location_from_string(e) < 0) log_warning("Failed to parse log location '%s'. Ignoring.", e); + + e = getenv("SYSTEMD_LOG_TIME"); + if (e && log_show_time_from_string(e) < 0) + log_warning("Failed to parse log time '%s'. Ignoring.", e); } LogTarget log_get_target(void) { @@ -1156,6 +1176,14 @@ bool log_get_show_location(void) { return show_location; } +void log_show_time(bool b) { + show_time = b; +} + +bool log_get_show_time(void) { + return show_time; +} + int log_show_color_from_string(const char *e) { int t; @@ -1178,6 +1206,17 @@ int log_show_location_from_string(const char *e) { return 0; } +int log_show_time_from_string(const char *e) { + int t; + + t = parse_boolean(e); + if (t < 0) + return t; + + log_show_time(t); + return 0; +} + bool log_on_console(void) { if (IN_SET(log_target, LOG_TARGET_CONSOLE, LOG_TARGET_CONSOLE_PREFIXED)) |