]> git.ozlabs.org Git - petitboot/commitdiff
lib/log: Include timestamp prefix
authorSamuel Mendoza-Jonas <sam@mendozajonas.com>
Mon, 30 Jul 2018 01:48:24 +0000 (11:48 +1000)
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>
Mon, 30 Jul 2018 04:17:05 +0000 (14:17 +1000)
The relative time between logged events is very useful during debugging,
particularly when debugging autoboot failures. Prepend a short HH:MM:SS
timestamp to each line logged.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
lib/log/log.c

index 44543d03b4ce900368cbeb593e2f08feaf855059..adb207862b1c4a65286f6bb63aaf2b70ed76f99c 100644 (file)
@@ -1,6 +1,8 @@
 
 #include <assert.h>
 #include <stdarg.h>
+#include <stdlib.h>
+#include <time.h>
 
 #include "log.h"
 
@@ -9,8 +11,17 @@ static bool debug;
 
 static void __log(const char *fmt, va_list ap)
 {
+       char hms[20] = {'\0'};
+       time_t t;
+
        if (!logf)
                return;
+
+       /* Add timestamp */
+       t = time(NULL);
+       strftime(hms, sizeof(hms), "%T", localtime(&t));
+       fprintf(logf, "[%s] ", hms);
+
        vfprintf(logf, fmt, ap);
        fflush(logf);
 }