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.
This commit is contained in:
Thomas Haller 2020-11-04 16:53:25 +01:00
parent 336270edd5
commit 0d083f5dab
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
3 changed files with 45 additions and 0 deletions

View file

@ -1766,4 +1766,5 @@ global:
nm_keyfile_read;
nm_keyfile_warn_severity_get_type;
nm_keyfile_write;
nm_utils_print;
} libnm_1_28_0;

View file

@ -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__ */

View file

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