summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGergely Nagy <algernon@balabit.hu>2012-03-21 14:23:35 +0100
committerGergely Nagy <algernon@balabit.hu>2012-03-21 14:23:35 +0100
commitb49beb4e96a317e9d279274dc13607dda978792c (patch)
tree9486077d17f0bd39fd480a5c9e3815c2582e0481
parent765ee19aec8a9a88c52f71336aadbc8991e8cb95 (diff)
downloadlibumberlog-b49beb4e96a317e9d279274dc13607dda978792c.tar.gz
libumberlog-b49beb4e96a317e9d279274dc13607dda978792c.tar.xz
libumberlog-b49beb4e96a317e9d279274dc13607dda978792c.zip
Add the timestamp to the JSON payload too.
Add high-precision timestamp to the JSON payload (unless disabled). Signed-off-by: Gergely Nagy <algernon@balabit.hu>
-rw-r--r--TODO.org5
-rw-r--r--configure.ac2
-rw-r--r--lib/Makefile.am2
-rw-r--r--lib/cee-syslog.c25
-rw-r--r--lib/cee-syslog.h1
-rw-r--r--t/test_cee_format.c30
6 files changed, 62 insertions, 3 deletions
diff --git a/TODO.org b/TODO.org
index 8af859a..c0f4632 100644
--- a/TODO.org
+++ b/TODO.org
@@ -13,7 +13,10 @@ LOG_CEE_NOCACHE flag, and also with LOG_CEE_NOCACHE_UID flag.
Not cached by default, but can be turned on with
LOG_CEE_CACHE_TID. This is linux-specific, and glibc does not provide
a wrapper, so this also needs some configure-time discovery.
-*** TODO Timestamp in the payload
+*** DONE Timestamp in the payload
+CLOSED: [2012-03-21 Wed 14:20]
+- CLOSING NOTE [2012-03-21 Wed 14:20] \\
+ Use clock_gettime(), and format the string with full precision.
Not enabled by default, can be toggled on with LOG_CEE_TIMESTAMP.
**** What precision do we want?
*** DONE Hostname in the payload
diff --git a/configure.ac b/configure.ac
index f5bf402..1596cd3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -39,7 +39,7 @@ AC_CHECK_HEADERS([syslog.h dlfcn.h limits.h])
dnl ***************************************************************************
dnl Checks for libraries
-AC_CHECK_FUNCS([gethostname strdup])
+AC_CHECK_FUNCS([gethostname strdup clock_gettime])
dnl ***************************************************************************
dnl JSON-C headers/libraries
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 <string.h>
#include <syslog.h>
#include <limits.h>
+#include <time.h>
#include "cee-syslog.h"
@@ -179,6 +180,25 @@ _cee_json_append (struct json_object *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)
{
if (cee_sys_settings.flags & LOG_CEE_NODISCOVER)
@@ -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);
diff --git a/t/test_cee_format.c b/t/test_cee_format.c
index 145c810..ec48603 100644
--- a/t/test_cee_format.c
+++ b/t/test_cee_format.c
@@ -78,6 +78,7 @@ test_simple (void)
verify_value_exists (jo, "pid");
verify_value_exists (jo, "uid");
verify_value_exists (jo, "gid");
+ verify_value_exists (jo, "timestamp");
verify_value (jo, "host", host);
json_object_put (jo);
@@ -105,6 +106,7 @@ test_no_discover (void)
verify_value_missing (jo, "uid");
verify_value_missing (jo, "gid");
verify_value_missing (jo, "host");
+ verify_value_missing (jo, "timestamp");
json_object_put (jo);
@@ -160,6 +162,33 @@ test_discover_priority (void)
closelog ();
}
+static void
+test_no_timestamp (void)
+{
+ char *msg;
+ struct json_object *jo;
+
+ openlog ("cee-syslog/test_no_timestamp", LOG_CEE_NOTIME, LOG_LOCAL0);
+
+ msg = cee_format (LOG_DEBUG, "hello, I'm %s!", __FUNCTION__, NULL);
+ jo = parse_msg (msg);
+ free (msg);
+
+ verify_value (jo, "msg", "hello, I'm test_no_timestamp!");
+ verify_value (jo, "facility", "local0");
+ verify_value (jo, "priority", "debug");
+ verify_value (jo, "program", "cee-syslog/test_no_timestamp");
+ verify_value_exists (jo, "pid");
+ verify_value_exists (jo, "uid");
+ verify_value_exists (jo, "gid");
+ verify_value_missing (jo, "timestamp");
+ verify_value_exists (jo, "host");
+
+ json_object_put (jo);
+
+ closelog ();
+}
+
int
main (void)
{
@@ -167,6 +196,7 @@ main (void)
test_no_discover ();
test_additional_fields ();
test_discover_priority ();
+ test_no_timestamp ();
return 0;
}