[libgsystem] gs_log_structured_print(): New API



commit ba6376041cbab2cc74bd54410e0734602768ecbf
Author: Colin Walters <walters verbum org>
Date:   Thu May 9 15:04:27 2013 -0400

    gs_log_structured_print(): New API
    
    This is useful if anyone runs your binary directly.

 gsystem-log.c |   41 +++++++++++++++++++++++++++++++++++++++++
 gsystem-log.h |    3 +++
 2 files changed, 44 insertions(+), 0 deletions(-)
---
diff --git a/gsystem-log.c b/gsystem-log.c
index bb3675c..032e6cb 100644
--- a/gsystem-log.c
+++ b/gsystem-log.c
@@ -80,3 +80,44 @@ gs_log_structured (const char *message,
     g_print ("%s\n", message);
 #endif
 }
+
+/**
+ * gs_log_structured_print:
+ * @message: A message to log
+ * @keys: (allow-none) (array zero-terminated=1) (element-type utf8): Optional structured data
+ *
+ * Like gs_log_structured(), but also print to standard output (if it
+ * is not already connected to the system log).
+ */
+void
+gs_log_structured_print (const char *message,
+                         const char *const *keys)
+{
+  static gsize initialized;
+  static gboolean stdout_is_socket;
+
+  gs_log_structured (message, keys);
+
+#ifdef ENABLE_SYSTEMD_JOURNAL
+  if (g_once_init_enter (&initialized))
+    {
+      guint64 pid = (guint64) getpid ();
+      char *fdpath = g_strdup_printf ("/proc/%" G_GUINT64_FORMAT "/fd/1", pid);
+      char buf[1024];
+      ssize_t bytes_read;
+
+      if ((bytes_read = readlink (fdpath, buf, sizeof(buf) - 1)) != -1)
+        {
+          buf[bytes_read] = '\0';
+          stdout_is_socket = g_str_has_prefix (buf, "socket:");
+        }
+      else
+        stdout_is_socket = FALSE;
+
+      g_free (fdpath);
+      g_once_init_leave (&initialized, TRUE);
+    }
+  if (!stdout_is_socket)
+    g_print ("%s\n", message);
+#endif
+}
diff --git a/gsystem-log.h b/gsystem-log.h
index 9f3f9fd..36f6f57 100644
--- a/gsystem-log.h
+++ b/gsystem-log.h
@@ -28,6 +28,9 @@ G_BEGIN_DECLS
 void gs_log_structured (const char *message,
                         const char *const *keys);
 
+void gs_log_structured_print (const char *message,
+                              const char *const *keys);
+
 G_END_DECLS
 
 #endif


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