diff options
author | Colin Walters <walters@verbum.org> | 2014-04-04 16:10:23 -0400 |
---|---|---|
committer | Colin Walters <walters@verbum.org> | 2014-04-04 16:10:25 -0400 |
commit | f418bb82d289fd613c22754fa559e7deefb1f524 (patch) | |
tree | 6b7187e8fd8ee478ad4e6a03ed4eda66f3d8f04d | |
parent | 1afbc2e476331f2cef3e46dee7a7fbcd9485535c (diff) | |
download | libgsystem-f418bb82d289fd613c22754fa559e7deefb1f524.tar.gz |
log: Add gs_log_with_id()
This is useful when you want to log with a MESSAGE_ID, but not print
to stdout.
-rw-r--r-- | src/gsystem-log.c | 30 | ||||
-rw-r--r-- | src/gsystem-log.h | 4 |
2 files changed, 34 insertions, 0 deletions
diff --git a/src/gsystem-log.c b/src/gsystem-log.c index 7b03145..2b23970 100644 --- a/src/gsystem-log.c +++ b/src/gsystem-log.c @@ -138,6 +138,36 @@ gs_log_structured_print (const char *message, } /** + * gs_log_with_id: + * @message_id: A unique MESSAGE_ID + * @format: A format string + * + * The provided @message_id is a unique MESSAGE_ID (see <ulink url="http://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html"> for more information). + * + * This function logs the given message to the systemd journal, + * attaching @message_id. If systemd is not available, the message is + * printed to stdout. + */ +void +gs_log_with_id (const char *message_id, + const char *format, + ...) +{ + char *keys[] = { NULL, NULL }; + char *msg; + va_list args; + + va_start (args, format); + msg = g_strdup_vprintf (format, args); + va_end (args); + + keys[0] = g_strconcat ("MESSAGE_ID=", message_id, NULL); + gs_log_structured (msg, (const char *const *)keys); + g_free (keys[0]); + g_free (msg); +} + +/** * gs_log_structured_print_id_v: * @message_id: A unique MESSAGE_ID * @format: A format string diff --git a/src/gsystem-log.h b/src/gsystem-log.h index 80cfc34..43dc9e5 100644 --- a/src/gsystem-log.h +++ b/src/gsystem-log.h @@ -27,6 +27,10 @@ G_BEGIN_DECLS gboolean gs_stdout_is_journal (void); +void gs_log_with_id (const char *message_id, + const char *format, + ...) G_GNUC_PRINTF (2, 3); + void gs_log_structured (const char *message, const char *const *keys); |