diff options
author | Thomas Haller <thaller@redhat.com> | 2016-10-05 15:25:08 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2016-10-05 15:34:45 +0200 |
commit | 06b3b06a9535e7d9c22e419d40da565ba20a5426 (patch) | |
tree | 6151078dd435feb3e13a3e88a28857f8bf44dde8 /src | |
parent | bedb19840f5df12905a7d5a905a87afa4c80494f (diff) |
logging: support system-wide prefix
Will be used by nm-iface-helper.
Diffstat (limited to 'src')
-rw-r--r-- | src/nm-logging.c | 36 | ||||
-rw-r--r-- | src/nm-logging.h | 3 |
2 files changed, 35 insertions, 4 deletions
diff --git a/src/nm-logging.c b/src/nm-logging.c index 51dd2aad2..df1566625 100644 --- a/src/nm-logging.c +++ b/src/nm-logging.c @@ -82,6 +82,7 @@ NMLogDomain _nm_logging_enabled_state[_LOGL_N_REAL] = { static struct { NMLogLevel log_level; bool uses_syslog:1; + const char *prefix; enum { LOG_BACKEND_GLIB, LOG_BACKEND_SYSLOG, @@ -98,6 +99,7 @@ static struct { /* nm_logging_setup ("INFO", LOGD_DEFAULT_STRING, NULL, NULL); */ .log_level = LOGL_INFO, .log_backend = LOG_BACKEND_GLIB, + .prefix = "", .level_desc = { [LOGL_TRACE] = { "TRACE", "<trace>", LOG_DEBUG, G_LOG_LEVEL_DEBUG, }, [LOGL_DEBUG] = { "DEBUG", "<debug>", LOG_DEBUG, G_LOG_LEVEL_DEBUG, }, @@ -531,7 +533,8 @@ _nm_log_impl (const char *file, _iovec_set_format (iov, iov_free, i_field++, "PRIORITY=%d", global.level_desc[level].syslog_level); _iovec_set_format (iov, iov_free, i_field++, "MESSAGE=" - "%-7s%s %s", + "%s%-7s%s %s", + global.prefix, global.level_desc[level].level_str, s_buf_timestamp, msg); @@ -602,7 +605,8 @@ _nm_log_impl (const char *file, break; #endif default: - fullmsg = g_strdup_printf ("%-7s%s %s", + fullmsg = g_strdup_printf ("%s%-7s%s %s", + global.prefix, global.level_desc[level].level_str, s_buf_timestamp, msg); @@ -660,7 +664,7 @@ nm_log_handler (const gchar *log_domain, boottime = nm_utils_monotonic_timestamp_as_boottime (now, 1); sd_journal_send ("PRIORITY=%d", syslog_priority, - "MESSAGE=%s", message ?: "", + "MESSAGE=%s%s", global.prefix, message ?: "", "SYSLOG_IDENTIFIER=%s", G_LOG_DOMAIN, "SYSLOG_PID=%ld", (long) getpid (), "SYSLOG_FACILITY=GLIB", @@ -673,7 +677,7 @@ nm_log_handler (const gchar *log_domain, break; #endif default: - syslog (syslog_priority, "%s", message ?: ""); + syslog (syslog_priority, "%s%s", global.prefix, message ?: ""); break; } } @@ -685,6 +689,30 @@ nm_logging_syslog_enabled (void) } void +nm_logging_set_prefix (const char *format, ...) +{ + char *prefix; + va_list ap; + + /* prefix can only be set once, to a non-empty string. Also, after + * nm_logging_syslog_openlog() the prefix cannot be set either. */ + if (global.log_backend != LOG_BACKEND_GLIB) + g_return_if_reached (); + if (global.prefix[0]) + g_return_if_reached (); + + va_start (ap, format); + prefix = g_strdup_vprintf (format, ap); + va_end (ap); + + if (!prefix || !prefix[0]) + g_return_if_reached (); + + /* we pass the allocated string on and never free it. */ + global.prefix = prefix; +} + +void nm_logging_syslog_openlog (const char *logging_backend) { if (global.log_backend != LOG_BACKEND_GLIB) diff --git a/src/nm-logging.h b/src/nm-logging.h index 9f85c1699..d680bf1af 100644 --- a/src/nm-logging.h +++ b/src/nm-logging.h @@ -188,6 +188,9 @@ gboolean nm_logging_setup (const char *level, const char *domains, char **bad_domains, GError **error); + +void nm_logging_set_prefix (const char *format, ...) _nm_printf (1, 2); + void nm_logging_syslog_openlog (const char *logging_backend); gboolean nm_logging_syslog_enabled (void); |