[libgsystem] gsystemlog.la: New library which wraps sd_journal
- From: Colin Walters <walters src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgsystem] gsystemlog.la: New library which wraps sd_journal
- Date: Sun, 13 Jan 2013 23:29:20 +0000 (UTC)
commit 7229abafe3f8fac571cc1032dbfa16fce8954ade
Author: Colin Walters <walters verbum org>
Date: Sun Jan 13 18:26:44 2013 -0500
gsystemlog.la: New library which wraps sd_journal
An experimental API which allows logging to systemd. It's is simpler
for the case where you just want a MESSAGE_ID+MESSAGE+PRIORITY (very
common), and also can fallback to syslog if systemd isn't available.
Makefile-libgsystem.am | 19 +++++++++
gsystem-log-syslog.c | 47 +++++++++++++++++++++
gsystem-log-systemd.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++
gsystem-log.h | 36 ++++++++++++++++
libgsystem-testlog.c | 35 ++++++++++++++++
5 files changed, 241 insertions(+), 0 deletions(-)
---
diff --git a/Makefile-libgsystem.am b/Makefile-libgsystem.am
index adc997f..6110a33 100644
--- a/Makefile-libgsystem.am
+++ b/Makefile-libgsystem.am
@@ -31,6 +31,25 @@ libgsystem_la_SOURCES = \
$(libgsystem_srcpath)/gsystem-subprocess.c \
$(libgsystem_srcpath)/libgsystem.h \
$(NULL)
+
libgsystem_la_CFLAGS = $(AM_CFLAGS) $(libgsystem_cflags)
libgsystem_la_LDFLAGS = -avoid-version -Bsymbolic-functions -export-symbols-regex "^gs_" -no-undefined -export-dynamic
libgsystem_la_LIBADD = $(libgsystem_libs)
+
+libgsystemlog_la_CFLAGS = $(libgsystem_la_CFLAGS)
+libgsystemlog_la_LDFLAGS = $(libgsystem_la_LDFLAGS)
+libgsystemlog_la_LIBADD = $(libgsystem_la_LIBADD)
+libgsystemlog_la_SOURCES = $(libgsystem_srcpath)/gsystem-log.h
+if HAVE_SYSTEMD_JOURNAL
+libgsystemlog_la_SOURCES += $(libgsystem_srcpath)/gsystem-log-systemd.c
+libgsystemlog_la_CFLAGS += $(SYSTEMD_JOURNAL_CFLAGS)
+libgsystemlog_la_LIBADD += $(SYSTEMD_JOURNAL_LIBS)
+else
+libgsystemlog_la_SOURCES += $(libgsystem_srcpath)/gsystem-log-syslog.c
+endif
+
+noinst_LTLIBRARIES += libgsystemlog.la
+noinst_PROGRAMS += libgsystem-testlog
+libgsystem_testlog_SOURCES = $(libgsystem_srcpath)/libgsystem-testlog.c
+libgsystem_testlog_CFLAGS = $(libgsystem_la_CFLAGS)
+libgsystem_testlog_LDADD = libgsystemlog.la $(libgsystem_libs)
diff --git a/gsystem-log-syslog.c b/gsystem-log-syslog.c
new file mode 100644
index 0000000..62a746e
--- /dev/null
+++ b/gsystem-log-syslog.c
@@ -0,0 +1,47 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2012 Colin Walters <walters verbum org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include <string.h>
+
+#include "gsystem-log.h"
+
+void
+gs_slog_id (const gchar *message_id,
+ int priority,
+ const char *format, ...)
+{
+ va_list args;
+ char *msg;
+
+ va_start (args, format);
+ msg = g_strdup_vprintf (format, args);
+ va_end (args);
+
+ syslog (priority, "%s", msg);
+}
+
+void
+gs_log_error (GError *error)
+{
+ gs_slog_id (NULL, 4, "%s", error->message);
+}
+
diff --git a/gsystem-log-systemd.c b/gsystem-log-systemd.c
new file mode 100644
index 0000000..0e16dfe
--- /dev/null
+++ b/gsystem-log-systemd.c
@@ -0,0 +1,104 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2012 Colin Walters <walters verbum org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include <string.h>
+
+#include "gsystem-log.h"
+/* No point including location since we wrap */
+#define SD_JOURNAL_SUPPRESS_LOCATION
+#include <systemd/sd-journal.h>
+
+/**
+ * gs_slog_id:
+ * @message_id: An 128bit ID, formatted as a lowercase hexadecimal (32 bytes)
+ * @format: A format string
+ *
+ * Log a message with a unique ID. On systemd-based operating
+ * systems, this writes to the journal. The @message_id
+ * must follow the restrictions listed here:
+ * <ulink url="http://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html">.
+ */
+void
+gs_slog_id (const char *message_id,
+ int priority,
+ const char *format, ...)
+{
+ va_list args;
+ const char *msgid_key = "MESSAGE_ID=";
+ const int msgid_keylen = strlen (msgid_key);
+ char msgid_buf[msgid_keylen + 32]; /* NOTE: Not NUL terminated */
+ const char *priority_key = "PRIORITY=";
+ const int priority_keylen = strlen (priority_key);
+ char priority_buf[priority_keylen + 1]; /* NOTE: Not NUL terminated */
+ struct iovec iov[3];
+ char *raw_msg;
+ char *msg;
+ int n_iov = 0;
+ int res;
+
+ g_return_if_fail (priority >= 0 && priority <= 7);
+
+ va_start (args, format);
+ raw_msg = g_strdup_vprintf (format, args);
+ va_end (args);
+
+ msg = g_strconcat ("MESSAGE=", raw_msg, NULL);
+ g_free (raw_msg);
+
+ iov[n_iov].iov_base = msg;
+ iov[n_iov].iov_len = strlen (msg);
+ n_iov++;
+ memcpy (priority_buf, priority_key, priority_keylen);
+ *(priority_buf + priority_keylen) = 48 + priority;
+ iov[n_iov].iov_base = priority_buf;
+ iov[n_iov].iov_len = priority_keylen + 1;
+ n_iov++;
+
+ if (message_id)
+ {
+ memcpy (msgid_buf, msgid_key, msgid_keylen);
+ g_assert (strlen (message_id) == 32);
+ memcpy (msgid_buf + msgid_keylen, message_id, 32);
+
+ iov[n_iov].iov_base = msgid_buf;
+ iov[n_iov].iov_len = sizeof (msgid_buf);
+ n_iov++;
+ }
+
+ res = sd_journal_sendv (iov, n_iov);
+ if (res != 0)
+ {
+ g_printerr ("sd_journal_send(): %s\n", g_strerror (-res));
+ }
+
+ g_free (msg);
+}
+
+void
+gs_log_error (GError *error)
+{
+ (void) sd_journal_send ("MESSAGE=%s", error->message,
+ "PRIORITY=%d", 4, /* LOG_ERR */
+ "GERROR_DOMAIN=%d", error->domain,
+ "GERROR_CODE=%d", error->code,
+ NULL);
+}
diff --git a/gsystem-log.h b/gsystem-log.h
new file mode 100644
index 0000000..302a4b7
--- /dev/null
+++ b/gsystem-log.h
@@ -0,0 +1,36 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2012 Colin Walters <walters verbum org>.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GSYSTEM_LOG_H__
+#define __GSYSTEM_LOG_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+void gs_slog_id (const gchar *message_id,
+ int priority,
+ const char *format, ...) G_GNUC_PRINTF (3, 4);
+
+void gs_log_error (GError *error);
+
+G_END_DECLS
+
+#endif
diff --git a/libgsystem-testlog.c b/libgsystem-testlog.c
new file mode 100644
index 0000000..48f1704
--- /dev/null
+++ b/libgsystem-testlog.c
@@ -0,0 +1,35 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2012 Colin Walters <walters verbum org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+
+#include "config.h"
+
+#include "gsystem-log.h"
+
+int
+main (int argc,
+ char **argv)
+{
+ gs_slog_id ("bc93e4eddc624cd889f21846a672d6e8", 3,
+ "hello %d %s ", 42, "world");
+ gs_slog_id ("65bad39b7ef4426d9f4cd93c60ebfb9c", 5,
+ "moo %f cow", 3.14159);
+ return 0;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]