From: Samuel Mendoza-Jonas Date: Mon, 30 Jul 2018 01:48:24 +0000 (+1000) Subject: lib/log: Include timestamp prefix X-Git-Tag: v1.7.2~2 X-Git-Url: https://git.ozlabs.org/?a=commitdiff_plain;h=69efae0cb03a414124e7080b93b8a911821ad89f;p=petitboot lib/log: Include timestamp prefix 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 (cherry picked from commit b0c603d007174e75452c1c14402d62d305da76ab) --- diff --git a/lib/log/log.c b/lib/log/log.c index 44543d0..adb2078 100644 --- a/lib/log/log.c +++ b/lib/log/log.c @@ -1,6 +1,8 @@ #include #include +#include +#include #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); }