diff options
author | Thomas Haller <thaller@redhat.com> | 2020-11-04 16:53:25 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2020-11-09 17:25:25 +0100 |
commit | 0d083f5dabe10a8bbd32fdfce032394b877c5bee (patch) | |
tree | a0e64d5680bc29242dfba68fbb77f890ef46f50c | |
parent | 336270edd5a6237f2569a1260d11afdde6bec629 (diff) | |
download | NetworkManager-0d083f5dabe10a8bbd32fdfce032394b877c5bee.tar.gz |
libnm: add nm_utils_print() function
libnm supports verbose debug logging by setting "LIBNM_CLIENT_DEBUG"
environment variable. That mechanism uses g_printerr() (or g_print()).
When testing an application it's useful to combine printf debugging
with this debug logging. However, python's print() statement is
additionally buffered and not in sync with the logging functions that
libnm uses.
As far as I see, g_print() and g_printerr() is not accessible via
introspections/pygobject, probably because these are variadic functions.
Add nm_utils_print() to libnm. This ensures to use the same logging
mechanism as libnm.
-rw-r--r-- | libnm/libnm.ver | 1 | ||||
-rw-r--r-- | libnm/nm-client.h | 5 | ||||
-rw-r--r-- | libnm/nm-libnm-utils.c | 39 |
3 files changed, 45 insertions, 0 deletions
diff --git a/libnm/libnm.ver b/libnm/libnm.ver index 552e550646..baf1e06f86 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -1766,4 +1766,5 @@ global: nm_keyfile_read; nm_keyfile_warn_severity_get_type; nm_keyfile_write; + nm_utils_print; } libnm_1_28_0; diff --git a/libnm/nm-client.h b/libnm/nm-client.h index 86e9486e29..befd454f96 100644 --- a/libnm/nm-client.h +++ b/libnm/nm-client.h @@ -480,6 +480,11 @@ void nm_client_dbus_set_property(NMClient * client, NM_AVAILABLE_IN_1_24 gboolean nm_client_dbus_set_property_finish(NMClient *client, GAsyncResult *result, GError **error); +/*****************************************************************************/ + +NM_AVAILABLE_IN_1_30 +void nm_utils_print(int output_mode, const char *msg); + G_END_DECLS #endif /* __NM_CLIENT_H__ */ diff --git a/libnm/nm-libnm-utils.c b/libnm/nm-libnm-utils.c index 9c27f64de8..775aebf803 100644 --- a/libnm/nm-libnm-utils.c +++ b/libnm/nm-libnm-utils.c @@ -869,3 +869,42 @@ nm_utils_g_param_spec_is_default(const GParamSpec *pspec) * strictly asserts and only support argument types that we expect. */ g_return_val_if_reached(FALSE); } + +/*****************************************************************************/ + +/** + * nm_utils_print: + * @output_mode: if 1 it uses g_print(). If 2, it uses g_printerr(). + * If 0, it uses either g_print() or g_printerr(), depending + * on LIBNM_CLIENT_DEBUG (and the "stdout" flag). + * @msg: the message to print. The function does not append + * a trailing newline. + * + * The only purpose of this function is to give access to g_print() + * or g_printerr() from pygobject. libnm can do debug logging by + * setting LIBNM_CLIENT_DEBUG and uses thereby g_printerr() or + * g_print(). A plain "print()" function in python is not in sync + * with these functions (it implements additional buffering). By + * using nm_utils_print(), the same logging mechanisms can be used. + * + * Since: 1.30 + */ +void +nm_utils_print(int output_mode, const char *msg) +{ + gboolean use_stdout; + + g_return_if_fail(msg); + + if (output_mode == 0) { + nml_dbus_log_enabled_full(NML_DBUS_LOG_LEVEL_ANY, &use_stdout); + output_mode = use_stdout ? 1 : 2; + } + + if (output_mode == 1) + g_print("%s", msg); + else if (output_mode == 2) + g_printerr("%s", msg); + else + g_return_if_reached(); +} |