From b49beb4e96a317e9d279274dc13607dda978792c Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Wed, 21 Mar 2012 14:23:35 +0100 Subject: Add the timestamp to the JSON payload too. Add high-precision timestamp to the JSON payload (unless disabled). Signed-off-by: Gergely Nagy --- lib/Makefile.am | 2 +- lib/cee-syslog.c | 25 +++++++++++++++++++++++++ lib/cee-syslog.h | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/Makefile.am b/lib/Makefile.am index 99ccf47..d5c99a3 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -3,7 +3,7 @@ LCS_REVISION = 0 LCS_AGE = 0 lib_LTLIBRARIES = libcee-syslog.la -libcee_syslog_la_LIBADD = @JSON_LIBS@ -ldl +libcee_syslog_la_LIBADD = @JSON_LIBS@ -ldl -lrt libcee_syslog_la_CFLAGS = @JSON_CFLAGS@ libcee_syslog_la_SOURCES = \ diff --git a/lib/cee-syslog.c b/lib/cee-syslog.c index 6ad7abb..d6ae7fc 100644 --- a/lib/cee-syslog.c +++ b/lib/cee-syslog.c @@ -38,6 +38,7 @@ #include #include #include +#include #include "cee-syslog.h" @@ -178,6 +179,25 @@ _cee_json_append (struct json_object *json, ...) return json; } +static inline void +_cee_json_append_timestamp (struct json_object *jo) +{ + struct timespec ts; + struct tm *tm; + char stamp[64], zone[16]; + + clock_gettime (CLOCK_REALTIME, &ts); + + tm = localtime (&ts.tv_sec); + + strftime (stamp, sizeof (stamp), "%FT%T", tm); + strftime (zone, sizeof (zone), "%z", tm); + + _cee_json_append (jo, "timestamp", "%s.%lu%s", + stamp, ts.tv_nsec, zone, + NULL); +} + static inline void _cee_discover (struct json_object *jo, int priority) { @@ -193,6 +213,11 @@ _cee_discover (struct json_object *jo, int priority) "gid", "%d", _get_gid (), "host", "%s", _get_hostname (), NULL); + + if (cee_sys_settings.flags & LOG_CEE_NOTIME) + return; + + _cee_json_append_timestamp (jo); } static struct json_object * diff --git a/lib/cee-syslog.h b/lib/cee-syslog.h index 70ac0cf..0725230 100644 --- a/lib/cee-syslog.h +++ b/lib/cee-syslog.h @@ -34,6 +34,7 @@ #define LOG_CEE_NODISCOVER 0x0040 #define LOG_CEE_NOCACHE 0x0080 #define LOG_CEE_NOCACHE_UID 0x0100 +#define LOG_CEE_NOTIME 0x0200 char *cee_format (int priority, const char *msg_format, ...); char *cee_vformat (int priority, const char *msg_format, va_list ap); -- cgit