Change in libosmocore[master]: logging: introduce experimental systemd journal target

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

fixeria gerrit-no-reply at lists.osmocom.org
Tue Sep 8 22:17:59 UTC 2020


fixeria has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/20045 )


Change subject: logging: introduce experimental systemd journal target
......................................................................

logging: introduce experimental systemd journal target

TODO

Change-Id: I609f5cf438e6ad9038d8fc95f00add6aac29fb23
---
M configure.ac
M include/osmocom/core/logging.h
M src/Makefile.am
A src/logging_systemd.c
M src/vty/logging_vty.c
5 files changed, 153 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/45/20045/1

diff --git a/configure.ac b/configure.ac
index b07a3bd..b028058 100644
--- a/configure.ac
+++ b/configure.ac
@@ -181,6 +181,19 @@
 	AC_DEFINE([USE_GNUTLS], [1], [Use GnuTLS as a fallback for missing getrandom()])
 fi
 
+AC_ARG_ENABLE([systemd_logging],
+	[AS_HELP_STRING(
+		[--enable-systemd-logging],
+		[Build with systemd journal logging support]
+	)],
+	[systemd_logging=$enableval], [systemd_logging="no"])
+AS_IF([test "x$systemd_logging" = "xyes"], [
+	PKG_CHECK_MODULES(SYSTEMD, libsystemd)
+	AC_DEFINE([ENABLE_SYSTEMD_LOGGING], [1], [Enable systemd journalctl logging target])
+])
+AM_CONDITIONAL(ENABLE_SYSTEMD_LOGGING, test "x$systemd_logging" = "xyes")
+AC_SUBST(ENABLE_SYSTEMD_LOGGING)
+
 AC_ARG_ENABLE([libsctp], [AS_HELP_STRING([--disable-libsctp], [Do not enable socket multiaddr APIs requiring libsctp])],
 	[ENABLE_LIBSCTP=$enableval], [ENABLE_LIBSCTP="yes"])
 AM_CONDITIONAL(ENABLE_LIBSCTP, test x"$ENABLE_LIBSCTP" = x"yes")
diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h
index 79eec10..7150b8b 100644
--- a/include/osmocom/core/logging.h
+++ b/include/osmocom/core/logging.h
@@ -243,6 +243,7 @@
 	LOG_TGT_TYPE_STDERR,	/*!< stderr logging */
 	LOG_TGT_TYPE_STRRB,	/*!< osmo_strrb-backed logging */
 	LOG_TGT_TYPE_GSMTAP,	/*!< GSMTAP network logging */
+	LOG_TGT_TYPE_SYSTEMD,	/*!< systemd journal logging */
 };
 
 /*! Whether/how to log the source filename (and line number). */
@@ -391,6 +392,7 @@
 					    const char *ident,
 					    bool ofd_wq_mode,
 					    bool add_sink);
+struct log_target *log_target_create_systemd(void);
 int log_target_file_reopen(struct log_target *tgt);
 int log_targets_reopen(void);
 
diff --git a/src/Makefile.am b/src/Makefile.am
index 891b4a6..b2c9204 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -71,5 +71,10 @@
 libosmocore_la_SOURCES += serial.c
 endif
 
+if ENABLE_SYSTEMD_LOGGING
+libosmocore_la_SOURCES += logging_systemd.c
+libosmocore_la_LIBADD += $(SYSTEMD_LIBS)
+endif
+
 crc%gen.c: crcXXgen.c.tpl
 	$(AM_V_GEN)sed -e's/XX/$*/g' $< > $@
diff --git a/src/logging_systemd.c b/src/logging_systemd.c
new file mode 100644
index 0000000..7633217
--- /dev/null
+++ b/src/logging_systemd.c
@@ -0,0 +1,74 @@
+/*
+ * (C) 2020 by Vadim Yanitskiy <axilirator at gmail.com>
+ * All Rights Reserved
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+/*! \addtogroup logging
+ *  @{
+ * \file logging_systemd.c */
+
+#include <syslog.h>
+
+#include <systemd/sd-journal.h>
+
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/logging.h>
+
+/* FIXME: copy-pasted from logging_syslog.c */
+static int logp2syslog_level(unsigned int level)
+{
+	if (level >= LOGL_FATAL)
+		return LOG_CRIT;
+	else if (level >= LOGL_ERROR)
+		return LOG_ERR;
+	else if (level >= LOGL_NOTICE)
+		return LOG_NOTICE;
+	else if (level >= LOGL_INFO)
+		return LOG_INFO;
+	else
+		return LOG_DEBUG;
+}
+
+static void _systemd_output(struct log_target *target,
+			    unsigned int level, const char *log)
+{
+	/* systemd accepts the same level constants as syslog */
+	sd_journal_print(logp2syslog_level(level), "%s", log);
+}
+
+/*! Create a new logging target for systemd journal logging.
+ *  \returns Log target in case of success, NULL in case of error.
+ */
+struct log_target *log_target_create_systemd(void)
+{
+	struct log_target *target;
+
+	target = log_target_create();
+	if (!target)
+		return NULL;
+
+	target->type = LOG_TGT_TYPE_SYSTEMD;
+	target->output = _systemd_output;
+
+	return target;
+}
+
+/* @} */
diff --git a/src/vty/logging_vty.c b/src/vty/logging_vty.c
index 0e1782a..0471ca6 100644
--- a/src/vty/logging_vty.c
+++ b/src/vty/logging_vty.c
@@ -731,6 +731,60 @@
 }
 #endif /* HAVE_SYSLOG_H */
 
+DEFUN(cfg_log_systemd, cfg_log_systemd_cmd,
+      "log systemd",
+      LOG_STR "Logging to systemd journal\n")
+{
+#ifdef ENABLE_SYSTEMD_LOGGING
+	struct log_target *tgt;
+
+	log_tgt_mutex_lock();
+	tgt = log_target_find(LOG_TGT_TYPE_SYSTEMD, NULL);
+	if (tgt == NULL) {
+		tgt = log_target_create_systemd();
+		if (tgt == NULL) {
+			vty_out(vty, "%% Unable to create systemd journal "
+				"log target%s", VTY_NEWLINE);
+			RET_WITH_UNLOCK(CMD_WARNING);
+		}
+		log_add_target(tgt);
+	}
+
+	vty->index = tgt;
+	vty->node = CFG_LOG_NODE;
+
+	RET_WITH_UNLOCK(CMD_SUCCESS);
+#else
+	vty_out(vty, "%% systemd journal logging is not available "
+		"in this build of libosmocore%s", VTY_NEWLINE);
+	return CMD_WARNING;
+#endif /* ENABLE_SYSTEMD_LOGGING */
+}
+
+DEFUN(cfg_no_log_systemd, cfg_no_log_systemd_cmd,
+	"no log systemd",
+	NO_STR LOG_STR "Logging to systemd journal\n")
+{
+#ifdef ENABLE_SYSTEMD_LOGGING
+	struct log_target *tgt;
+
+	log_tgt_mutex_lock();
+	tgt = log_target_find(LOG_TGT_TYPE_SYSTEMD, NULL);
+	if (!tgt) {
+		vty_out(vty, "%% No systemd logging active%s", VTY_NEWLINE);
+		RET_WITH_UNLOCK(CMD_WARNING);
+	}
+
+	log_target_destroy(tgt);
+
+	RET_WITH_UNLOCK(CMD_SUCCESS);
+#else
+	vty_out(vty, "%% systemd journal logging is not available "
+		"in this build of libosmocore%s", VTY_NEWLINE);
+	return CMD_WARNING;
+#endif /* ENABLE_SYSTEMD_LOGGING */
+}
+
 DEFUN(cfg_log_gsmtap, cfg_log_gsmtap_cmd,
 	"log gsmtap [HOSTNAME]",
 	LOG_STR "Logging via GSMTAP\n"
@@ -924,6 +978,9 @@
 		vty_out(vty, "log gsmtap %s%s",
 			tgt->tgt_gsmtap.hostname, VTY_NEWLINE);
 		break;
+	case LOG_TGT_TYPE_SYSTEMD:
+		vty_out(vty, "log systemd%s", VTY_NEWLINE);
+		break;
 	}
 
 	vty_out(vty, " logging filter all %u%s",
@@ -1125,5 +1182,7 @@
 	install_element(CONFIG_NODE, &cfg_log_syslog_local_cmd);
 	install_element(CONFIG_NODE, &cfg_no_log_syslog_cmd);
 #endif
+	install_element(CONFIG_NODE, &cfg_log_systemd_cmd);
+	install_element(CONFIG_NODE, &cfg_no_log_systemd_cmd);
 	install_element(CONFIG_NODE, &cfg_log_gsmtap_cmd);
 }

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/20045
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I609f5cf438e6ad9038d8fc95f00add6aac29fb23
Gerrit-Change-Number: 20045
Gerrit-PatchSet: 1
Gerrit-Owner: fixeria <vyanitskiy at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200908/b6916a74/attachment.htm>


More information about the gerrit-log mailing list