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(); +}