[v1,3/6] pb_log: Break out timestamp

Message ID bed565beeed66f0ce8aba275be3be522c26f373f.1533686335.git.geoff@infradead.org
State Accepted
Headers show
Series
  • [v1,1/6] docker: Add strace for interactive debugging
Related show

Commit Message

Geoff Levand Aug. 8, 2018, 12:01 a.m.
Fixes double timestamp on pb_log_fn, pb_debug_fn.

Signed-off-by: Geoff Levand <geoff@infradead.org>
---
 lib/log/log.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

Comments

Samuel Mendoza-Jonas Aug. 13, 2018, 3:36 a.m. | #1
On Wed, 2018-08-08 at 00:01 +0000, Geoff Levand wrote:
> Fixes double timestamp on pb_log_fn, pb_debug_fn.
> 
> Signed-off-by: Geoff Levand <geoff@infradead.org>
> ---
>  lib/log/log.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/log/log.c b/lib/log/log.c
> index 5466d81..98b38dd 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);

Do we want this in pb_debug() as well?

Cheers,
Sam

>  }

Patch

diff --git a/lib/log/log.c b/lib/log/log.c
index 5466d81..98b38dd 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);
 }