diff --git a/man/systemd-socket-activate.xml b/man/systemd-socket-activate.xml index f3dbb47a617..20a55721925 100644 --- a/man/systemd-socket-activate.xml +++ b/man/systemd-socket-activate.xml @@ -141,6 +141,7 @@ $SYSTEMD_LOG_TARGET $SYSTEMD_LOG_LEVEL + $SYSTEMD_LOG_TIME $SYSTEMD_LOG_COLOR $SYSTEMD_LOG_LOCATION diff --git a/man/systemd-system.conf.xml b/man/systemd-system.conf.xml index e22b335d30d..435c3882dbe 100644 --- a/man/systemd-system.conf.xml +++ b/man/systemd-system.conf.xml @@ -63,10 +63,11 @@ - LogLevel= - LogTarget= LogColor= + LogLevel= LogLocation= + LogTarget= + LogTime= DumpCore=yes CrashChangeVT=no CrashShell=no diff --git a/man/systemd.xml b/man/systemd.xml index 94b3845a0e8..f5e4a9526c5 100644 --- a/man/systemd.xml +++ b/man/systemd.xml @@ -606,6 +606,13 @@ Environment + + $SYSTEMD_LOG_COLOR + Controls whether systemd highlights important + log messages. This can be overridden with + . + + $SYSTEMD_LOG_LEVEL systemd reads the log level from this @@ -613,6 +620,13 @@ . + + $SYSTEMD_LOG_LOCATION + Controls whether systemd prints the code + location along with log messages. This can be overridden with + . + + $SYSTEMD_LOG_TARGET systemd reads the log target from this @@ -621,17 +635,10 @@ - $SYSTEMD_LOG_COLOR - Controls whether systemd highlights important - log messages. This can be overridden with - . - - - - $SYSTEMD_LOG_LOCATION - Controls whether systemd prints the code - location along with log messages. This can be overridden with - . + $SYSTEMD_LOG_TIME + Controls whether systemd prefixes log + messages with the current time. This can be overridden with + . @@ -829,18 +836,21 @@ - systemd.log_target= - systemd.log_level= - systemd.log_location= systemd.log_color + systemd.log_level= + systemd.log_location + systemd.log_target= + systemd.log_time Controls log output, with the same effect as the - $SYSTEMD_LOG_TARGET, + $SYSTEMD_LOG_COLOR, $SYSTEMD_LOG_LEVEL, $SYSTEMD_LOG_LOCATION, - $SYSTEMD_LOG_COLOR environment variables described above. - systemd.log_color can be specified without an argument, - with the same effect as a positive boolean. + $SYSTEMD_LOG_TARGET, + $SYSTEMD_LOG_TIME, environment variables described above. + systemd.log_color, systemd.log_location, and + systemd.log_time can be specified without an argument, with the + same effect as a positive boolean. @@ -1135,9 +1145,10 @@ - + - Set log target. See systemd.log_target above. + Highlight important log messages. See systemd.log_color above. + @@ -1146,13 +1157,6 @@ Set log level. See systemd.log_level above. - - - - Highlight important log messages. See systemd.log_color above. - - - @@ -1160,6 +1164,19 @@ above. + + + + Set log target. See systemd.log_target above. + + + + + + Prefix messages with timestamp. See systemd.log_time above. + + + diff --git a/src/basic/log.c b/src/basic/log.c index 17557e1844b..ca024d970e6 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)) diff --git a/src/basic/log.h b/src/basic/log.h index 740fdbf6171..2c1b00fb88f 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -59,9 +59,12 @@ void log_show_color(bool b); bool log_get_show_color(void) _pure_; void log_show_location(bool b); bool log_get_show_location(void) _pure_; +void log_show_time(bool b); +bool log_get_show_time(void) _pure_; int log_show_color_from_string(const char *e); int log_show_location_from_string(const char *e); +int log_show_time_from_string(const char *e); LogTarget log_get_target(void) _pure_; int log_get_max_level_realm(LogRealm realm) _pure_; diff --git a/src/core/main.c b/src/core/main.c index f7cda72fddc..d700b3afc52 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -542,8 +542,9 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat DEFINE_SETTER(config_parse_level2, log_set_max_level_from_string, "log level"); DEFINE_SETTER(config_parse_target, log_set_target_from_string, "target"); -DEFINE_SETTER(config_parse_color, log_show_color_from_string, "color" ); +DEFINE_SETTER(config_parse_color, log_show_color_from_string, "color"); DEFINE_SETTER(config_parse_location, log_show_location_from_string, "location"); +DEFINE_SETTER(config_parse_time, log_show_time_from_string, "time"); static int config_parse_default_timeout_abort( const char *unit, @@ -571,6 +572,7 @@ static int parse_config_file(void) { { "Manager", "LogTarget", config_parse_target, 0, NULL }, { "Manager", "LogColor", config_parse_color, 0, NULL }, { "Manager", "LogLocation", config_parse_location, 0, NULL }, + { "Manager", "LogTime", config_parse_time, 0, NULL }, { "Manager", "DumpCore", config_parse_bool, 0, &arg_dump_core }, { "Manager", "CrashChVT", /* legacy */ config_parse_crash_chvt, 0, &arg_crash_chvt }, { "Manager", "CrashChangeVT", config_parse_crash_chvt, 0, &arg_crash_chvt }, @@ -718,6 +720,7 @@ static int parse_argv(int argc, char *argv[]) { ARG_LOG_TARGET, ARG_LOG_COLOR, ARG_LOG_LOCATION, + ARG_LOG_TIME, ARG_UNIT, ARG_SYSTEM, ARG_USER, @@ -745,6 +748,7 @@ static int parse_argv(int argc, char *argv[]) { { "log-target", required_argument, NULL, ARG_LOG_TARGET }, { "log-color", optional_argument, NULL, ARG_LOG_COLOR }, { "log-location", optional_argument, NULL, ARG_LOG_LOCATION }, + { "log-time", optional_argument, NULL, ARG_LOG_TIME }, { "unit", required_argument, NULL, ARG_UNIT }, { "system", no_argument, NULL, ARG_SYSTEM }, { "user", no_argument, NULL, ARG_USER }, @@ -818,6 +822,18 @@ static int parse_argv(int argc, char *argv[]) { break; + case ARG_LOG_TIME: + + if (optarg) { + r = log_show_time_from_string(optarg); + if (r < 0) + return log_error_errno(r, "Failed to parse log time setting \"%s\": %m", + optarg); + } else + log_show_time(true); + + break; + case ARG_DEFAULT_STD_OUTPUT: r = exec_output_from_string(optarg); if (r < 0) @@ -1037,6 +1053,7 @@ static int help(void) { " --log-level=LEVEL Set log level (debug, info, notice, warning, err, crit, alert, emerg)\n" " --log-color[=BOOL] Highlight important log messages\n" " --log-location[=BOOL] Include code location in log messages\n" + " --log-time[=BOOL] Prefix log messages with current time\n" " --default-standard-output= Set default standard output for services\n" " --default-standard-error= Set default standard error output for services\n" "\nSee the %s for details.\n" @@ -1419,6 +1436,9 @@ static int become_shutdown( if (log_get_show_location()) command_line[pos++] = "--log-location"; + if (log_get_show_time()) + command_line[pos++] = "--log-time"; + if (streq(shutdown_verb, "exit")) { command_line[pos++] = "--exit-code"; command_line[pos++] = exit_code; diff --git a/src/core/system.conf.in b/src/core/system.conf.in index 8112125468a..40bb5488871 100644 --- a/src/core/system.conf.in +++ b/src/core/system.conf.in @@ -16,6 +16,7 @@ #LogTarget=journal-or-kmsg #LogColor=yes #LogLocation=no +#LogTime=no #DumpCore=yes #ShowStatus=yes #CrashChangeVT=no diff --git a/src/core/user.conf.in b/src/core/user.conf.in index 95a162e0f6c..bbe06319c9a 100644 --- a/src/core/user.conf.in +++ b/src/core/user.conf.in @@ -15,6 +15,7 @@ #LogTarget=console #LogColor=yes #LogLocation=no +#LogTime=no #SystemCallArchitectures= #TimerSlackNSec= #StatusUnitFormat=@STATUS_UNIT_FORMAT_DEFAULT@ diff --git a/src/shutdown/shutdown.c b/src/shutdown/shutdown.c index 918cf20bd0a..f2ad23d0557 100644 --- a/src/shutdown/shutdown.c +++ b/src/shutdown/shutdown.c @@ -51,6 +51,7 @@ static int parse_argv(int argc, char *argv[]) { ARG_LOG_TARGET, ARG_LOG_COLOR, ARG_LOG_LOCATION, + ARG_LOG_TIME, ARG_EXIT_CODE, ARG_TIMEOUT, }; @@ -60,6 +61,7 @@ static int parse_argv(int argc, char *argv[]) { { "log-target", required_argument, NULL, ARG_LOG_TARGET }, { "log-color", optional_argument, NULL, ARG_LOG_COLOR }, { "log-location", optional_argument, NULL, ARG_LOG_LOCATION }, + { "log-time", optional_argument, NULL, ARG_LOG_TIME }, { "exit-code", required_argument, NULL, ARG_EXIT_CODE }, { "timeout", required_argument, NULL, ARG_TIMEOUT }, {} @@ -110,6 +112,17 @@ static int parse_argv(int argc, char *argv[]) { break; + case ARG_LOG_TIME: + + if (optarg) { + r = log_show_time_from_string(optarg); + if (r < 0) + log_error_errno(r, "Failed to parse log time setting %s, ignoring: %m", optarg); + } else + log_show_time(true); + + break; + case ARG_EXIT_CODE: r = safe_atou8(optarg, &arg_exit_code); if (r < 0)