diff options
author | Přemysl Janouch <p.janouch@gmail.com> | 2016-10-15 16:03:26 +0200 |
---|---|---|
committer | Přemysl Janouch <p.janouch@gmail.com> | 2016-10-15 16:05:59 +0200 |
commit | 95ffaf3bd927f2dae71eb540dcb79e29020c6890 (patch) | |
tree | 64c1baec9bef8808846988605bd0933847a7da35 | |
parent | 7a32fb8e55d28bd43b546f64a1c20b9246d85e67 (diff) | |
download | desktop-tools-95ffaf3bd927f2dae71eb540dcb79e29020c6890.tar.gz desktop-tools-95ffaf3bd927f2dae71eb540dcb79e29020c6890.tar.xz desktop-tools-95ffaf3bd927f2dae71eb540dcb79e29020c6890.zip |
big-brother: print timestamps
-rw-r--r-- | big-brother.c | 23 |
1 files changed, 22 insertions, 1 deletions
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); |