summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2013-05-09 15:04:27 -0400
committerColin Walters <walters@verbum.org>2013-05-09 15:06:15 -0400
commitba6376041cbab2cc74bd54410e0734602768ecbf (patch)
tree1b45fd3c4af77be1f961de917f7a87af58b303f0
parent18a925895d54fa539ff52182695945b819eddd36 (diff)
downloadlibgsystem-ba6376041cbab2cc74bd54410e0734602768ecbf.tar.gz
gs_log_structured_print(): New API
This is useful if anyone runs your binary directly.
-rw-r--r--gsystem-log.c41
-rw-r--r--gsystem-log.h3
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