summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTorsten Hilbrich <torsten.hilbrich@secunet.com>2017-06-21 12:59:35 +0200
committerAleksander Morgado <aleksander@aleksander.es>2017-06-21 13:08:17 +0200
commitfd0bed1df9e306d8f0d2498afb8df316ca8b3f9e (patch)
tree228c09dfbaf4ab6a223215b2435001f5d95ec343 /src
parent13592c5d2d6fdf74a5525f1f15d4e7477c73c986 (diff)
downloadModemManager-fd0bed1df9e306d8f0d2498afb8df316ca8b3f9e.tar.gz
log: Add support for journal logging
This logging is available if the software was build with the configure option --with-systemd-journal. It will be enabled by default if libsystemd is found. The runtime parameter --log-journal enables to output of log messages to the systemd journal. Please note that the journal priority field has the same value as the syslog level so no conversion is required here.
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am5
-rw-r--r--src/main.c1
-rw-r--r--src/mm-context.c14
-rw-r--r--src/mm-context.h1
-rw-r--r--src/mm-log.c49
-rw-r--r--src/mm-log.h1
6 files changed, 71 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 6b29bfe92..402b2ae93 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -46,6 +46,11 @@ AM_CFLAGS += $(POLKIT_CFLAGS)
AM_LDFLAGS += $(POLKIT_LIBS)
endif
+if WITH_SYSTEMD_JOURNAL
+AM_CFLAGS += $(LIBSYSTEMD_CFLAGS)
+AM_LDFLAGS += $(LIBSYSTEMD_LIBS)
+endif
+
################################################################################
# generic udev rules
################################################################################
diff --git a/src/main.c b/src/main.c
index c133abc93..b61cabd97 100644
--- a/src/main.c
+++ b/src/main.c
@@ -141,6 +141,7 @@ main (int argc, char *argv[])
if (!mm_log_setup (mm_context_get_log_level (),
mm_context_get_log_file (),
+ mm_context_get_log_journal (),
mm_context_get_log_timestamps (),
mm_context_get_log_relative_timestamps (),
&err)) {
diff --git a/src/mm-context.c b/src/mm-context.c
index 0a896e6d0..6b0dab79f 100644
--- a/src/mm-context.c
+++ b/src/mm-context.c
@@ -89,6 +89,7 @@ mm_context_get_no_auto_scan (void)
static const gchar *log_level;
static const gchar *log_file;
+static gboolean log_journal;
static gboolean log_show_ts;
static gboolean log_rel_ts;
@@ -103,6 +104,13 @@ static const GOptionEntry log_entries[] = {
"Path to log file",
"[PATH]"
},
+#if WITH_SYSTEMD_JOURNAL
+ {
+ "log-journal", 0, 0, G_OPTION_ARG_NONE, &log_journal,
+ "Log to systemd journal",
+ NULL
+ },
+#endif
{
"log-timestamps", 0, 0, G_OPTION_ARG_NONE, &log_show_ts,
"Show timestamps in log output",
@@ -143,6 +151,12 @@ mm_context_get_log_file (void)
}
gboolean
+mm_context_get_log_journal (void)
+{
+ return log_journal;
+}
+
+gboolean
mm_context_get_log_timestamps (void)
{
return log_show_ts;
diff --git a/src/mm-context.h b/src/mm-context.h
index e355692f6..143ee15b7 100644
--- a/src/mm-context.h
+++ b/src/mm-context.h
@@ -33,6 +33,7 @@ gboolean mm_context_get_no_auto_scan (void);
/* Logging support */
const gchar *mm_context_get_log_level (void);
const gchar *mm_context_get_log_file (void);
+gboolean mm_context_get_log_journal (void);
gboolean mm_context_get_log_timestamps (void);
gboolean mm_context_get_log_relative_timestamps (void);
diff --git a/src/mm-log.c b/src/mm-log.c
index fcbef34de..5691b26f1 100644
--- a/src/mm-log.c
+++ b/src/mm-log.c
@@ -35,6 +35,11 @@
#include <libmbim-glib.h>
#endif
+#if defined WITH_SYSTEMD_JOURNAL
+#define SD_JOURNAL_SUPPRESS_LOCATION
+#include <systemd/sd-journal.h>
+#endif
+
#include "mm-log.h"
enum {
@@ -148,6 +153,43 @@ log_backend_syslog (const char *loc,
syslog (syslog_level, "%s", message);
}
+#if defined WITH_SYSTEMD_JOURNAL
+static void
+log_backend_systemd_journal (const char *loc,
+ const char *func,
+ int syslog_level,
+ const char *message,
+ size_t length)
+{
+ const char *line;
+ size_t file_length;
+
+ if (loc == NULL) {
+ sd_journal_send ("MESSAGE=%s", message,
+ "PRIORITY=%d", syslog_level,
+ NULL);
+ return;
+ }
+
+ line = strstr (loc, ":");
+ if (line) {
+ file_length = line - loc;
+ line++;
+ } else {
+ /* This is not supposed to happen but we must be prepared for this */
+ line = loc;
+ file_length = 0;
+ }
+
+ sd_journal_send ("MESSAGE=%s", message,
+ "PRIORITY=%d", syslog_level,
+ "CODE_FUNC=%s", func,
+ "CODE_FILE=%.*s", file_length, loc,
+ "CODE_LINE=%s", line,
+ NULL);
+}
+#endif
+
void
_mm_log (const char *loc,
const char *func,
@@ -242,6 +284,7 @@ mm_log_set_level (const char *level, GError **error)
gboolean
mm_log_setup (const char *level,
const char *log_file,
+ gboolean log_journal,
gboolean show_timestamps,
gboolean rel_timestamps,
GError **error)
@@ -258,6 +301,12 @@ mm_log_setup (const char *level,
/* Grab start time for relative timestamps */
g_get_current_time (&rel_start);
+#if defined WITH_SYSTEMD_JOURNAL
+ if (log_journal) {
+ log_backend = log_backend_systemd_journal;
+ append_log_level_text = FALSE;
+ } else
+#endif
if (log_file == NULL) {
openlog (G_LOG_DOMAIN, LOG_CONS | LOG_PID | LOG_PERROR, LOG_DAEMON);
log_backend = log_backend_syslog;
diff --git a/src/mm-log.h b/src/mm-log.h
index 6c34098ec..d9f11f27c 100644
--- a/src/mm-log.h
+++ b/src/mm-log.h
@@ -51,6 +51,7 @@ gboolean mm_log_set_level (const char *level, GError **error);
gboolean mm_log_setup (const char *level,
const char *log_file,
+ gboolean log_journal,
gboolean show_ts,
gboolean rel_ts,
GError **error);