From 04896a8b64e1676ee3c1430af63d6dc8b312ff48 Mon Sep 17 00:00:00 2001 From: Ludovico de Nittis <ludovico.denittis@collabora.com> Date: Mon, 14 Dec 2020 10:49:08 +0100 Subject: [PATCH] Factor out cli_log_func Moving this log function to "utils" allows us to avoid a lot of duplication. Signed-off-by: Ludovico de Nittis <ludovico.denittis@collabora.com> --- pressure-vessel/adverb.c | 17 ++--------------- pressure-vessel/launch.c | 17 ++--------------- pressure-vessel/launcher.c | 17 ++--------------- pressure-vessel/utils.c | 16 ++++++++++++++++ pressure-vessel/utils.h | 5 +++++ pressure-vessel/wrap.c | 24 ++++++------------------ 6 files changed, 33 insertions(+), 63 deletions(-) diff --git a/pressure-vessel/adverb.c b/pressure-vessel/adverb.c index 29188fc47..ae826df34 100644 --- a/pressure-vessel/adverb.c +++ b/pressure-vessel/adverb.c @@ -620,17 +620,6 @@ static GOptionEntry options[] = { NULL } }; -static int my_pid = -1; - -static void -cli_log_func (const gchar *log_domain, - GLogLevelFlags log_level, - const gchar *message, - gpointer user_data) -{ - g_printerr ("%s[%d]: %s\n", (const char *) user_data, my_pid, message); -} - int main (int argc, char *argv[]) @@ -649,8 +638,6 @@ main (int argc, struct sigaction terminate_child_action = {}; g_autoptr(FlatpakBwrap) wrapped_command = NULL; - my_pid = getpid (); - sigemptyset (&mask); sigaddset (&mask, SIGCHLD); @@ -674,7 +661,7 @@ main (int argc, g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE, - cli_log_func, (void *) g_get_prgname ()); + pv_log_to_stderr, NULL); context = g_option_context_new ( "COMMAND [ARG...]\n" @@ -710,7 +697,7 @@ main (int argc, if (opt_verbose) g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO, - cli_log_func, (void *) g_get_prgname ()); + pv_log_to_stderr, NULL); original_stdout = _srt_divert_stdout_to_stderr (error); diff --git a/pressure-vessel/launch.c b/pressure-vessel/launch.c index cc9023917..3740dcc3f 100644 --- a/pressure-vessel/launch.c +++ b/pressure-vessel/launch.c @@ -516,17 +516,6 @@ static const GOptionEntry options[] = { NULL } }; -static int my_pid = -1; - -static void -cli_log_func (const gchar *log_domain, - GLogLevelFlags log_level, - const gchar *message, - gpointer user_data) -{ - g_printerr ("%s[%d]: %s\n", (const char *) user_data, my_pid, message); -} - int main (int argc, char *argv[]) @@ -555,8 +544,6 @@ main (int argc, GHashTableIter iter; gpointer key, value; - my_pid = getpid (); - setlocale (LC_ALL, ""); original_environ = g_get_environ (); @@ -566,7 +553,7 @@ main (int argc, g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE, - cli_log_func, (void *) g_get_prgname ()); + pv_log_to_stderr, NULL); context = g_option_context_new ("COMMAND [ARG...]"); g_option_context_set_summary (context, @@ -594,7 +581,7 @@ main (int argc, if (opt_verbose) g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO, - cli_log_func, (void *) g_get_prgname ()); + pv_log_to_stderr, NULL); original_stdout = _srt_divert_stdout_to_stderr (error); diff --git a/pressure-vessel/launcher.c b/pressure-vessel/launcher.c index 37e3b31fa..6315cfe7b 100644 --- a/pressure-vessel/launcher.c +++ b/pressure-vessel/launcher.c @@ -873,17 +873,6 @@ static GOptionEntry options[] = { NULL } }; -static int my_pid = -1; - -static void -cli_log_func (const gchar *log_domain, - GLogLevelFlags log_level, - const gchar *message, - gpointer user_data) -{ - g_printerr ("%s[%d]: %s\n", (const char *) user_data, my_pid, message); -} - int main (int argc, char *argv[]) @@ -896,8 +885,6 @@ main (int argc, int ret = EX_USAGE; GBusNameOwnerFlags flags; - my_pid = getpid (); - global_listener = pv_portal_listener_new (); setlocale (LC_ALL, ""); @@ -906,7 +893,7 @@ main (int argc, g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE, - cli_log_func, (void *) g_get_prgname ()); + pv_log_to_stderr, NULL); context = g_option_context_new (""); g_option_context_set_summary (context, @@ -935,7 +922,7 @@ main (int argc, if (opt_verbose) g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO, - cli_log_func, (void *) g_get_prgname ()); + pv_log_to_stderr, NULL); if (!pv_portal_listener_set_up_info_fd (global_listener, opt_info_fd, diff --git a/pressure-vessel/utils.c b/pressure-vessel/utils.c index 8de743af3..35b526d2a 100644 --- a/pressure-vessel/utils.c +++ b/pressure-vessel/utils.c @@ -907,3 +907,19 @@ pv_current_namespace_path_to_host_path (const gchar *current_env_path) return path_on_host; } + +/** + * pv_log_to_stderr: + * @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 + */ +void +pv_log_to_stderr (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data) +{ + g_printerr ("%s[%d]: %s\n", g_get_prgname (), getpid (), message); +} diff --git a/pressure-vessel/utils.h b/pressure-vessel/utils.h index 266deebf3..91cce4bb4 100644 --- a/pressure-vessel/utils.h +++ b/pressure-vessel/utils.h @@ -73,3 +73,8 @@ gchar *pv_current_namespace_path_to_host_path (const gchar *current_env_path); const char *pv_get_path_after (const char *str, const char *prefix); + +void pv_log_to_stderr (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data); diff --git a/pressure-vessel/wrap.c b/pressure-vessel/wrap.c index e91328060..474264092 100644 --- a/pressure-vessel/wrap.c +++ b/pressure-vessel/wrap.c @@ -1187,17 +1187,6 @@ tristate_environment (const gchar *name) return TRISTATE_MAYBE; } -static int my_pid = -1; - -static void -cli_log_func (const gchar *log_domain, - GLogLevelFlags log_level, - const gchar *message, - gpointer user_data) -{ - g_printerr ("%s[%d]: %s\n", (const char *) user_data, my_pid, message); -} - int main (int argc, char *argv[]) @@ -1243,15 +1232,13 @@ main (int argc, g_autofree char *lock_env_fd = NULL; g_autoptr(GArray) pass_fds_through_adverb = g_array_new (FALSE, FALSE, sizeof (int)); - my_pid = getpid (); - setlocale (LC_ALL, ""); g_set_prgname ("pressure-vessel-wrap"); g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE, - cli_log_func, (void *) g_get_prgname ()); + pv_log_to_stderr, NULL); original_argv = g_new0 (char *, argc + 1); @@ -1311,6 +1298,11 @@ main (int argc, if (!g_option_context_parse (context, &argc, &argv, error)) goto out; + if (opt_verbose) + g_log_set_handler (G_LOG_DOMAIN, + G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO, + pv_log_to_stderr, NULL); + if (opt_runtime == NULL) opt_runtime = g_strdup (g_getenv ("PRESSURE_VESSEL_RUNTIME")); @@ -1546,10 +1538,6 @@ main (int argc, { g_auto(GStrv) env = g_strdupv (original_environ); - g_log_set_handler (G_LOG_DOMAIN, - G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_INFO, - cli_log_func, (void *) g_get_prgname ()); - g_message ("Original argv:"); for (i = 0; i < original_argc; i++) -- GitLab