diff options
author | Thomas Haller <thaller@redhat.com> | 2021-08-03 14:35:24 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2021-08-04 09:41:11 +0200 |
commit | d25a33f6041168ac0cac728a75854679b8e92b4a (patch) | |
tree | 550d0a95d5868d16f58a93b7dad348c5a84bcdc3 | |
parent | 4fe20e4cbeeead68ff9d7480242b2309aff6662b (diff) | |
download | NetworkManager-d25a33f6041168ac0cac728a75854679b8e92b4a.tar.gz |
dispatcher: support enabling debug logging via environment variable
The advantage of environment variables is that the user can use
`systemctl edit NetworkManager-dispatcher.service` for setting them,
without need to change the ExecStart= line.
Also, enabling debugging from the start is useful, despite that debug
logging can be enabled per-request.
Also, there is a difference whether we want verbose logging or whether
we want to log to stdout. There should be a flag, that only increases the
logging verbosity, but does not change the logging backend.
-rw-r--r-- | data/NetworkManager-dispatcher.service.in | 6 | ||||
-rw-r--r-- | po/POTFILES.skip | 1 | ||||
-rw-r--r-- | src/nm-dispatcher/nm-dispatcher.c | 53 |
3 files changed, 50 insertions, 10 deletions
diff --git a/data/NetworkManager-dispatcher.service.in b/data/NetworkManager-dispatcher.service.in index b192d0b0c4..1a45f35367 100644 --- a/data/NetworkManager-dispatcher.service.in +++ b/data/NetworkManager-dispatcher.service.in @@ -7,6 +7,12 @@ BusName=org.freedesktop.nm_dispatcher ExecStart=@libexecdir@/nm-dispatcher NotifyAccess=main +# Enable debug logging in dispatcher service. Note that dispatcher +# also honors debug logging requests from NetworkManager, so you +# can also control logging requests with +# `nmcli general logging domain DISPATCHER level TRACE`. +#Environment=NM_DISPATCHER_DEBUG_LOG=1 + # We want to allow scripts to spawn long-running daemons, so tell # systemd to not clean up when nm-dispatcher exits KillMode=process diff --git a/po/POTFILES.skip b/po/POTFILES.skip index 3f70738f8b..399b1e6b5c 100644 --- a/po/POTFILES.skip +++ b/po/POTFILES.skip @@ -1,4 +1,5 @@ contrib/fedora/rpm/ +data/NetworkManager-dispatcher.service.in data/NetworkManager-wait-online.service.in data/NetworkManager.service.in data/nm-sudo.service.in diff --git a/src/nm-dispatcher/nm-dispatcher.c b/src/nm-dispatcher/nm-dispatcher.c index c76b9c368f..8303c6df86 100644 --- a/src/nm-dispatcher/nm-dispatcher.c +++ b/src/nm-dispatcher/nm-dispatcher.c @@ -24,12 +24,19 @@ /*****************************************************************************/ +/* Serves only the purpose to mark environment variables that are honored by + * the application. You can search for this macro, and find what options are supported. */ +#define _ENV(var) ("" var "") + +/*****************************************************************************/ + typedef struct Request Request; static struct { GDBusConnection *dbus_connection; GCancellable * quit_cancellable; - gboolean debug; + bool log_verbose; + bool log_stdout; gboolean persist; GSource * quit_source; guint request_id_counter; @@ -145,7 +152,7 @@ struct Request { } \ G_STMT_END -#define _LOG_X_D_enabled() (gl.debug) +#define _LOG_X_D_enabled() (gl.log_verbose) #define _LOG_X_T_enabled() _LOG_X_D_enabled() #define _LOG_R_D_enabled(request) (_NM_ENSURE_TYPE_CONST(Request *, request)->debug) @@ -743,7 +750,7 @@ _method_call_action(GDBusMethodInvocation *invocation, GVariant *parameters) request = g_slice_new0(Request); request->request_id = ++gl.request_id_counter; - request->debug = debug || gl.debug; + request->debug = debug || gl.log_verbose; request->context = invocation; request->action = g_strdup(action); @@ -1034,11 +1041,32 @@ static gboolean parse_command_line(int *p_argc, char ***p_argv, GError **error) { GOptionContext *opt_ctx; - GOptionEntry entries[] = { - {"debug", 0, 0, G_OPTION_ARG_NONE, &gl.debug, "Output to console rather than syslog", NULL}, - {"persist", 0, 0, G_OPTION_ARG_NONE, &gl.persist, "Don't quit after a short timeout", NULL}, - {NULL}}; - gboolean success; + gboolean arg_debug = FALSE; + GOptionEntry entries[] = {{ + "debug", + 0, + 0, + G_OPTION_ARG_NONE, + &arg_debug, + "Output to console rather than syslog", + NULL, + }, + { + "persist", + 0, + 0, + G_OPTION_ARG_NONE, + &gl.persist, + "Don't quit after a short timeout", + NULL, + }, + { + NULL, + }}; + gboolean success; + + gl.log_stdout = FALSE; + gl.log_verbose = _nm_utils_ascii_str_to_bool(g_getenv(_ENV("NM_DISPATCHER_DEBUG_LOG")), FALSE); opt_ctx = g_option_context_new(NULL); g_option_context_set_summary(opt_ctx, "Executes scripts upon actions by NetworkManager."); @@ -1048,6 +1076,11 @@ parse_command_line(int *p_argc, char ***p_argv, GError **error) g_option_context_free(opt_ctx); + if (success && arg_debug) { + gl.log_stdout = TRUE; + gl.log_verbose = TRUE; + } + return success; } @@ -1070,7 +1103,7 @@ main(int argc, char **argv) goto done; } - if (gl.debug) { + if (gl.log_stdout) { if (!g_getenv("G_MESSAGES_DEBUG")) { /* we log our regular messages using g_debug() and g_info(). * When we redirect glib logging to syslog, there is no problem. @@ -1179,7 +1212,7 @@ done: _LOG_X_T("shutdown: exiting with %s", gl.exit_with_failure ? "failure" : "success"); - if (!gl.debug) + if (gl.log_stdout) logging_shutdown(); nm_clear_g_source_inst(&source_term); |