From 1001048dcb035ced409d2b11067fe816f924ed10 Mon Sep 17 00:00:00 2001 From: Ludovico de Nittis <ludovico.denittis@collabora.com> Date: Tue, 5 Jan 2021 11:13:09 +0100 Subject: [PATCH] Add option to enable timestamp for the logs This is especially useful when we are logging to a file to keep track of when a specific operation happened. Signed-off-by: Ludovico de Nittis <ludovico.denittis@collabora.com> --- pressure-vessel/adverb.1.md | 4 ++++ pressure-vessel/launch.1.md | 4 ++++ pressure-vessel/launcher.1.md | 4 ++++ pressure-vessel/utils.c | 28 +++++++++++++++++++++++++++- pressure-vessel/wrap.1.md | 4 ++++ 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/pressure-vessel/adverb.1.md b/pressure-vessel/adverb.1.md index 6488004e6..d198ab628 100644 --- a/pressure-vessel/adverb.1.md +++ b/pressure-vessel/adverb.1.md @@ -159,6 +159,10 @@ exit status. : If set to `1`, increase the log verbosity up to the info level. If set to `0`, no effect. +`PRESSURE_VESSEL_LOG_WITH_TIMESTAMP` (boolean) +: If set to `1`, prepend the log entries with a timestamp. + If set to `0`, no effect. + # OUTPUT The standard output from *COMMAND* is printed on standard output. diff --git a/pressure-vessel/launch.1.md b/pressure-vessel/launch.1.md index 785aa65b0..a5e07bdf8 100644 --- a/pressure-vessel/launch.1.md +++ b/pressure-vessel/launch.1.md @@ -143,6 +143,10 @@ it set, and pass through **FONTS** from the caller. : If set to `1`, increase the log verbosity up to the info level. If set to `0`, no effect. +`PRESSURE_VESSEL_LOG_WITH_TIMESTAMP` (boolean) +: If set to `1`, prepend the log entries with a timestamp. + If set to `0`, no effect. + # OUTPUT The standard output from *COMMAND* is printed on standard output. diff --git a/pressure-vessel/launcher.1.md b/pressure-vessel/launcher.1.md index dd97cb2a3..546bc84ac 100644 --- a/pressure-vessel/launcher.1.md +++ b/pressure-vessel/launcher.1.md @@ -84,6 +84,10 @@ D-Bus session bus, and executes arbitrary commands as subprocesses. : If set to `1`, increase the log verbosity up to the info level. If set to `0`, no effect. +`PRESSURE_VESSEL_LOG_WITH_TIMESTAMP` (boolean) +: If set to `1`, prepend the log entries with a timestamp. + If set to `0`, no effect. + # OUTPUT **pressure-vessel-launcher** prints zero or more lines of diff --git a/pressure-vessel/utils.c b/pressure-vessel/utils.c index 1c46bda18..ba28e4c13 100644 --- a/pressure-vessel/utils.c +++ b/pressure-vessel/utils.c @@ -911,6 +911,30 @@ pv_current_namespace_path_to_host_path (const gchar *current_env_path) return path_on_host; } +/** + * log_handler_with_timestamp: + * @log_domain: the log domain of the message + * @log_level: the log level of the message + * @message: the message to process + * @user_data: not used + */ +static void +pv_log_to_stderr_with_timestamp (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data) +{ + g_autoptr(GDateTime) date_time = g_date_time_new_now_local (); + + /* We can't use the format specifier "%f" for microseconds because it + * was introduced in GLib 2.66 and we are targeting an older version */ + g_autofree gchar *timestamp = g_date_time_format (date_time, "%T"); + + g_printerr ("%s.%06i: %s[%d]: %s\n", timestamp, + g_date_time_get_microsecond (date_time), + my_prgname, my_pid, message); +} + /** * pv_log_to_stderr: * @log_domain: the log domain of the message @@ -935,6 +959,8 @@ void pv_set_up_logging (gboolean opt_verbose) { GLogLevelFlags log_levels = G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE; + gboolean opt_timestamp = pv_boolean_environment ("PRESSURE_VESSEL_LOG_WITH_TIMESTAMP", + FALSE); gboolean opt_info = pv_boolean_environment ("PRESSURE_VESSEL_LOG_INFO", FALSE); my_pid = getpid (); @@ -947,6 +973,6 @@ pv_set_up_logging (gboolean opt_verbose) log_levels |= G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO; g_log_set_handler (G_LOG_DOMAIN, log_levels, - pv_log_to_stderr, + opt_timestamp ? pv_log_to_stderr_with_timestamp : pv_log_to_stderr, NULL); } diff --git a/pressure-vessel/wrap.1.md b/pressure-vessel/wrap.1.md index b31c8a603..028852c42 100644 --- a/pressure-vessel/wrap.1.md +++ b/pressure-vessel/wrap.1.md @@ -249,6 +249,10 @@ The following environment variables (among others) are read by : If set to `1`, increase the log verbosity up to the info level. If set to `0`, no effect. +`PRESSURE_VESSEL_LOG_WITH_TIMESTAMP` (boolean) +: If set to `1`, prepend the log entries with a timestamp. + If set to `0`, no effect. + `PRESSURE_VESSEL_REMOVE_GAME_OVERLAY` (boolean) : If set to `1`, equivalent to `--remove-game-overlay`. If set to `0`, equivalent to `--keep-game-overlay`. -- GitLab