shared: move log level info from core to "nm-logging-base.h"

We have our NM specific logging and log levels. Maybe we should
not have that, and instead only rely on syslog (like systemd)
or glog(). Anyway, currently we have one way and it makes sense
that this is also used outside from "src".

Move the helper function to parse log levels from string to
"nm-logging-base.h" so that we can use the same logging levels
outside of core.

This moves code that is currently GPL2+ licensed to
LGPL2.1+. However as far as I see, this code was entirely written
by Red Hat employees who would not object with this change. Also,
it's as obvious and trivial as it gets.
This commit is contained in:
Thomas Haller 2019-11-22 17:27:00 +01:00
parent 32d3a3f7ef
commit 40012e2aa8
3 changed files with 57 additions and 34 deletions

View file

@ -3,3 +3,36 @@
#include "nm-default.h"
#include "nm-logging-base.h"
#include <syslog.h>
/*****************************************************************************/
const LogLevelDesc level_desc[_LOGL_N] = {
[LOGL_TRACE] = { "TRACE", "<trace>", LOG_DEBUG, G_LOG_LEVEL_DEBUG, },
[LOGL_DEBUG] = { "DEBUG", "<debug>", LOG_DEBUG, G_LOG_LEVEL_DEBUG, },
[LOGL_INFO] = { "INFO", "<info>", LOG_INFO, G_LOG_LEVEL_INFO, },
[LOGL_WARN] = { "WARN", "<warn>", LOG_WARNING, G_LOG_LEVEL_MESSAGE, },
[LOGL_ERR] = { "ERR", "<error>", LOG_ERR, G_LOG_LEVEL_MESSAGE, },
[_LOGL_OFF] = { "OFF", NULL, 0, 0, },
[_LOGL_KEEP] = { "KEEP", NULL, 0, 0, },
};
gboolean
_nm_log_parse_level (const char *level,
NMLogLevel *out_level)
{
int i;
if (!level)
return FALSE;
for (i = 0; i < (int) G_N_ELEMENTS (level_desc); i++) {
if (!g_ascii_strcasecmp (level_desc[i].name, level)) {
NM_SET_OUT (out_level, i);
return TRUE;
}
}
return FALSE;
}

View file

@ -5,4 +5,25 @@
#include "nm-logging-fwd.h"
typedef struct {
const char *name;
const char *level_str;
/* nm-logging uses syslog internally. Note that the three most-verbose syslog levels
* are LOG_DEBUG, LOG_INFO and LOG_NOTICE. Journal already highlights LOG_NOTICE
* as special.
*
* On the other hand, we have three levels LOGL_TRACE, LOGL_DEBUG and LOGL_INFO,
* which are regular messages not to be highlighted. For that reason, we must map
* LOGL_TRACE and LOGL_DEBUG both to syslog level LOG_DEBUG. */
int syslog_level;
GLogLevelFlags g_log_level;
} LogLevelDesc;
extern const LogLevelDesc level_desc[_LOGL_N];
gboolean _nm_log_parse_level (const char *level,
NMLogLevel *out_level);
#endif /* __NM_LOGGING_BASE_H__ */

View file

@ -22,6 +22,7 @@
#include <systemd/sd-journal.h>
#endif
#include "nm-glib-aux/nm-logging-base.h"
#include "nm-glib-aux/nm-time-utils.h"
#include "nm-errors.h"
@ -84,22 +85,6 @@ typedef struct {
const char *name;
} LogDesc;
typedef struct {
const char *name;
const char *level_str;
/* nm-logging uses syslog internally. Note that the three most-verbose syslog levels
* are LOG_DEBUG, LOG_INFO and LOG_NOTICE. Journal already highlights LOG_NOTICE
* as special.
*
* On the other hand, we have three levels LOGL_TRACE, LOGL_DEBUG and LOGL_INFO,
* which are regular messages not to be highlighted. For that reason, we must map
* LOGL_TRACE and LOGL_DEBUG both to syslog level LOG_DEBUG. */
int syslog_level;
GLogLevelFlags g_log_level;
} LogLevelDesc;
typedef struct {
char *logging_domains_to_string;
} GlobalMain;
@ -158,16 +143,6 @@ NMLogDomain _nm_logging_enabled_state[_LOGL_N_REAL] = {
/*****************************************************************************/
static const LogLevelDesc level_desc[_LOGL_N] = {
[LOGL_TRACE] = { "TRACE", "<trace>", LOG_DEBUG, G_LOG_LEVEL_DEBUG, },
[LOGL_DEBUG] = { "DEBUG", "<debug>", LOG_DEBUG, G_LOG_LEVEL_DEBUG, },
[LOGL_INFO] = { "INFO", "<info>", LOG_INFO, G_LOG_LEVEL_INFO, },
[LOGL_WARN] = { "WARN", "<warn>", LOG_WARNING, G_LOG_LEVEL_MESSAGE, },
[LOGL_ERR] = { "ERR", "<error>", LOG_ERR, G_LOG_LEVEL_MESSAGE, },
[_LOGL_OFF] = { "OFF", NULL, 0, 0, },
[_LOGL_KEEP] = { "KEEP", NULL, 0, 0, },
};
static const LogDesc domain_desc[] = {
{ LOGD_PLATFORM, "PLATFORM" },
{ LOGD_RFKILL, "RFKILL" },
@ -271,14 +246,8 @@ match_log_level (const char *level,
NMLogLevel *out_level,
GError **error)
{
int i;
for (i = 0; i < G_N_ELEMENTS (level_desc); i++) {
if (!g_ascii_strcasecmp (level_desc[i].name, level)) {
*out_level = i;
return TRUE;
}
}
if (_nm_log_parse_level (level, out_level))
return TRUE;
g_set_error (error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_UNKNOWN_LOG_LEVEL,
_("Unknown log level '%s'"), level);