diff mbox series

[v2] pb_log: Break out timestamp

Message ID b1d428d4-1f7e-b2f2-1e66-b4f867c82663@infradead.org
State Accepted
Headers show
Series [v2] pb_log: Break out timestamp | expand

Commit Message

Geoff Levand Aug. 13, 2018, 4:23 p.m. UTC
Fixes double timestamp on pb_log_fn, pb_debug_fn.

Signed-off-by: Geoff Levand <geoff@infradead.org>
---
v2: Add timestamp to pb_debug.

 lib/log/log.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/lib/log/log.c b/lib/log/log.c
index 5466d81..7f14232 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -9,7 +9,7 @@ 
 static FILE *logf;
 static bool debug;
 
-static void __log(const char *fmt, va_list ap)
+static void __log_timestamp(void)
 {
 	char hms[20] = {'\0'};
 	time_t t;
@@ -17,10 +17,15 @@  static void __log(const char *fmt, va_list ap)
 	if (!logf)
 		return;
 
-	/* Add timestamp */
 	t = time(NULL);
 	strftime(hms, sizeof(hms), "%T", localtime(&t));
 	fprintf(logf, "[%s] ", hms);
+}
+
+static void __log(const char *fmt, va_list ap)
+{
+	if (!logf)
+		return;
 
 	vfprintf(logf, fmt, ap);
 	fflush(logf);
@@ -30,6 +35,7 @@  void pb_log(const char *fmt, ...)
 {
 	va_list ap;
 	va_start(ap, fmt);
+	__log_timestamp();
 	__log(fmt, ap);
 	va_end(ap);
 }
@@ -49,6 +55,7 @@  void pb_debug(const char *fmt, ...)
 	if (!debug)
 		return;
 	va_start(ap, fmt);
+	__log_timestamp();
 	__log(fmt, ap);
 	va_end(ap);
 }