libnm: refactor levels for LIBNM_CLIENT_DEBUG

Previously, it was odd. The enum values like NML_DBUS_LOG_LEVEL_DEBUG were
actually the bit mask of all the levels "debug", "warn" and "error".

On the other hand, when parsing _nml_dbus_log_level, that variable only contained
the flags that were exactly requested. E.g. when setting LIBNM_CLIENT_DEBUG=trace,
then _nml_dbus_log_level only contained the trace flag 0x02. That was useful,
because with "LIBNM_CLIENT_DEBUG=warn,trace" the "warn" flag was not redundant,
it was used to enable printing via g_warning(). That was confusing.

Now, "LIBNM_CLIENT_DEBUG=warn,trace" is the same as "LIBNM_CLIENT_DEBUG=trace".
To enable printing via g_warning(), use "LIBNM_CLIENT_DEBUG=WARN,trace".

With this, we don't need this backward representation of the flags. Invert
it. The level enums are now just single bits.
This commit is contained in:
Thomas Haller 2023-11-17 14:38:36 +01:00
parent e5aed28b8e
commit 5c08fa2776
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 18 additions and 10 deletions

View file

@ -151,6 +151,12 @@ _nml_dbus_log(NMLDBusLogLevel level, gboolean use_stdout, const char *fmt, ...)
gint64 ts;
pid_t pid;
nm_assert(NM_IN_SET(level,
NML_DBUS_LOG_LEVEL_TRACE,
NML_DBUS_LOG_LEVEL_DEBUG,
NML_DBUS_LOG_LEVEL_WARN,
NML_DBUS_LOG_LEVEL_ERROR));
/* we only call _nml_dbus_log() after nml_dbus_log_enabled(), which already does
* an atomic access to the variable. Since the value is only initialized once and
* never changes, we can just access it without additional locking. */

View file

@ -26,9 +26,9 @@ typedef enum {
_NML_DBUS_LOG_LEVEL_INITIALIZED = 0x01,
_NML_DBUS_LOG_LEVEL_TRACE = 0x02,
NML_DBUS_LOG_LEVEL_TRACE = 0x02,
_NML_DBUS_LOG_LEVEL_DEBUG = 0x04,
NML_DBUS_LOG_LEVEL_DEBUG = 0x04,
/* the difference between a warning and a critical is that it results in
* g_warning() vs. g_critical() messages (with NML_DBUS_LOG_ASSERT). Note
@ -39,21 +39,22 @@ typedef enum {
* when NetworkManager exposes something on D-Bus that breaks the current
* expectations. Usually NetworkManager should not break API, hence such
* issues are more severe. */
_NML_DBUS_LOG_LEVEL_WARN = 0x08,
_NML_DBUS_LOG_LEVEL_ERROR = 0x10,
NML_DBUS_LOG_LEVEL_WARN = 0x08,
NML_DBUS_LOG_LEVEL_ERROR = 0x10,
/* ANY is only relevant for nml_dbus_log_enabled() to check whether any of the
* options is on. */
NML_DBUS_LOG_LEVEL_ANY = _NML_DBUS_LOG_LEVEL_INITIALIZED,
NML_DBUS_LOG_LEVEL_TRACE = _NML_DBUS_LOG_LEVEL_TRACE,
NML_DBUS_LOG_LEVEL_DEBUG = _NML_DBUS_LOG_LEVEL_DEBUG | NML_DBUS_LOG_LEVEL_TRACE,
NML_DBUS_LOG_LEVEL_WARN = _NML_DBUS_LOG_LEVEL_WARN | NML_DBUS_LOG_LEVEL_DEBUG,
NML_DBUS_LOG_LEVEL_ERROR = _NML_DBUS_LOG_LEVEL_ERROR | NML_DBUS_LOG_LEVEL_WARN,
NML_DBUS_LOG_STDOUT = 0x20,
NML_DBUS_LOG_ASSERT = 0x40,
_NML_DBUS_LOG_LEVEL_ERROR = NML_DBUS_LOG_LEVEL_ERROR,
_NML_DBUS_LOG_LEVEL_WARN = NML_DBUS_LOG_LEVEL_WARN | _NML_DBUS_LOG_LEVEL_ERROR,
_NML_DBUS_LOG_LEVEL_DEBUG = NML_DBUS_LOG_LEVEL_DEBUG | _NML_DBUS_LOG_LEVEL_WARN,
_NML_DBUS_LOG_LEVEL_TRACE = NML_DBUS_LOG_LEVEL_TRACE | _NML_DBUS_LOG_LEVEL_DEBUG,
} NMLDBusLogLevel;
#undef _LOGL_TRACE
@ -64,7 +65,6 @@ typedef enum {
#define _LOGL_TRACE NML_DBUS_LOG_LEVEL_TRACE
#define _LOGL_DEBUG NML_DBUS_LOG_LEVEL_DEBUG
#define _LOGL_INFO NML_DBUS_LOG_LEVEL_INFO
#define _LOGL_WARN NML_DBUS_LOG_LEVEL_WARN
#define _LOGL_ERR NML_DBUS_LOG_LEVEL_ERR
@ -90,7 +90,9 @@ nml_dbus_log_enabled_full(NMLDBusLogLevel level, gboolean *out_use_stdout)
l = _nml_dbus_log_level_init();
nm_assert(l & _NML_DBUS_LOG_LEVEL_INITIALIZED);
NM_SET_OUT(out_use_stdout, NM_FLAGS_HAS(l, NML_DBUS_LOG_STDOUT));
if (level == NML_DBUS_LOG_LEVEL_ANY) {
return NM_FLAGS_ANY(l,
NML_DBUS_LOG_LEVEL_TRACE | NML_DBUS_LOG_LEVEL_DEBUG