[libgsystem] gsystem-log.h: New structured logging API



commit 51736db5f49b4b798fd2f9d0ccb8eff453105162
Author: Colin Walters <walters verbum org>
Date:   Tue Apr 23 16:50:02 2013 -0400

    gsystem-log.h: New structured logging API
    
    This is a wrapper for systemd-journal that's introspectable.

 Makefile-libgsystem.am |    2 +
 gsystem-log.c          |   78 ++++++++++++++++++++++++++++++++++++++++++++++++
 gsystem-log.h          |   33 ++++++++++++++++++++
 libgsystem.h           |    1 +
 4 files changed, 114 insertions(+), 0 deletions(-)
---
diff --git a/Makefile-libgsystem.am b/Makefile-libgsystem.am
index 83e8928..9e027ae 100644
--- a/Makefile-libgsystem.am
+++ b/Makefile-libgsystem.am
@@ -26,6 +26,8 @@ libgsystem_la_SOURCES = \
        $(libgsystem_srcpath)/gsystem-file-utils.c \
        $(libgsystem_srcpath)/gsystem-shutil.h \
        $(libgsystem_srcpath)/gsystem-shutil.c \
+       $(libgsystem_srcpath)/gsystem-log.h \
+       $(libgsystem_srcpath)/gsystem-log.c \
        $(libgsystem_srcpath)/gsystem-subprocess-context.h \
        $(libgsystem_srcpath)/gsystem-subprocess-context-private.h \
        $(libgsystem_srcpath)/gsystem-subprocess-context.c \
diff --git a/gsystem-log.c b/gsystem-log.c
new file mode 100644
index 0000000..587f86a
--- /dev/null
+++ b/gsystem-log.c
@@ -0,0 +1,78 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2012 William Jon McCann <mccann redhat com>
+ * 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"
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#ifdef ENABLE_SYSTEMD_JOURNAL
+#include <systemd/sd-journal.h>
+#endif
+#include <glib-unix.h>
+
+#include "libgsystem.h"
+
+/**
+ * gs_log_structured:
+ * @message: Text message to send 
+ * @keys: (allow-none) (array zero-terminated=1) (element-type utf8): Optional structured data
+ * 
+ * Log structured data in an operating-system specific fashion.  The
+ * parameter @opts should be an array of UTF-8 KEY=VALUE strings.
+ * This function does not support binary data.  See
+ * http://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html
+ * for more information about fields that can be used on a systemd
+ * system.
+ */
+void
+gs_log_structured (const char *message,
+                   const char *const *keys)
+{
+#ifdef ENABLE_SYSTEMD_JOURNAL
+    const char *const*iter;
+    char *msgkey;
+    guint i, n_opts;
+    struct iovec *iovs;
+
+    for (n_opts = 0, iter = keys; *iter; iter++, n_opts++)
+        ;
+
+    n_opts++; /* Add one for MESSAGE= */
+    iovs = g_alloca (sizeof (struct iovec) * n_opts);
+    
+    for (i = 0, iter = keys; *iter; iter++, i++) {
+        iovs[i].iov_base = (char*)keys[i];
+        iovs[i].iov_len = strlen (keys[i]);
+    }
+    g_assert(i == n_opts-1);
+    msgkey = g_strconcat ("MESSAGE=", message, NULL);
+    iovs[i].iov_base = msgkey;
+    iovs[i].iov_len = strlen (msgkey);
+
+    sd_journal_sendv (iovs, n_opts);
+    
+    g_free (msgkey);
+#else
+    g_print ("%s\n", message);
+#endif
+}
diff --git a/gsystem-log.h b/gsystem-log.h
new file mode 100644
index 0000000..9f3f9fd
--- /dev/null
+++ b/gsystem-log.h
@@ -0,0 +1,33 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2013 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 <gio/gio.h>
+
+G_BEGIN_DECLS
+
+void gs_log_structured (const char *message,
+                        const char *const *keys);
+
+G_END_DECLS
+
+#endif
diff --git a/libgsystem.h b/libgsystem.h
index 5871a4b..d2fa608 100644
--- a/libgsystem.h
+++ b/libgsystem.h
@@ -29,6 +29,7 @@ G_BEGIN_DECLS
 #include <gsystem-file-utils.h>
 #include <gsystem-shutil.h>
 #include <gsystem-subprocess.h>
+#include <gsystem-log.h>
 
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]