[1/3] lib/log: Include timestamp prefix
diff mbox series

Message ID 20180730014826.19097-1-sam@mendozajonas.com
State Accepted
Headers show
Series
  • [1/3] lib/log: Include timestamp prefix
Related show

Commit Message

Samuel Mendoza-Jonas July 30, 2018, 1:48 a.m. UTC
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 | 11 +++++++++++
 1 file changed, 11 insertions(+)

Comments

Samuel Mendoza-Jonas July 30, 2018, 5:12 a.m. UTC | #1
On Mon, 2018-07-30 at 11:48 +1000, Samuel Mendoza-Jonas wrote:
> 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>

These merged as 70bb498 so I can include them in a patch release shortly.

> ---
>  lib/log/log.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> 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 <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);
>  }

Patch
diff mbox series

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 <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);
 }