From 95ffaf3bd927f2dae71eb540dcb79e29020c6890 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Sat, 15 Oct 2016 16:03:26 +0200 Subject: big-brother: print timestamps --- big-brother.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/big-brother.c b/big-brother.c index bb489b9..c5c9c1a 100644 --- a/big-brother.c +++ b/big-brother.c @@ -34,6 +34,24 @@ // --- Utilities --------------------------------------------------------------- +static int64_t +clock_msec (clockid_t clock) +{ + struct timespec tp; + hard_assert (clock_gettime (clock, &tp) != -1); + return (int64_t) tp.tv_sec * 1000 + (int64_t) tp.tv_nsec / 1000000; +} + +static char * +timestamp (int64_t ts) +{ + char buf[24]; + struct tm tm; + time_t when = ts / 1000; + strftime (buf, sizeof buf, "%F %T", gmtime_r (&when, &tm)); + return xstrdup_printf ("%s.%03d", buf, (int) (ts % 1000)); +} + static void log_message_custom (void *user_data, const char *quote, const char *fmt, va_list ap) @@ -41,7 +59,10 @@ log_message_custom (void *user_data, const char *quote, const char *fmt, (void) user_data; FILE *stream = stdout; - fprintf (stream, PROGRAM_NAME ": "); + char *ts = timestamp (clock_msec (CLOCK_REALTIME)); + fprintf (stream, "%s ", ts); + free (ts); + fputs (quote, stream); vfprintf (stream, fmt, ap); fputs ("\n", stream); -- cgit v1.2.3-70-g09d2