From 60f0c2494f33a3b303c9fc8848f8540d020e879b Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sat, 7 Feb 2015 16:57:36 +0100 Subject: [PATCH] Add most of the stdout output also to stdlog --- cmdline/portable.h | 6 +++++- cmdline/support.c | 22 ++++++++++++++-------- cmdline/support.h | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/cmdline/portable.h b/cmdline/portable.h index 7752a85f..7b3cdc03 100644 --- a/cmdline/portable.h +++ b/cmdline/portable.h @@ -293,7 +293,11 @@ void os_init(int opt); void os_done(void); /** - * Global log file. + * Log file. + * + * This stream if fully buffered. + * + * If no log file is selected, it's 0. */ FILE* stdlog; diff --git a/cmdline/support.c b/cmdline/support.c index 27cba149..a29fdece 100644 --- a/cmdline/support.c +++ b/cmdline/support.c @@ -26,32 +26,38 @@ void fout(const char* format, ...) { va_list ap; - va_start(ap, format); - vfprintf(stdout, format, ap); - va_end(ap); - + /* first output the same message to stdlog and flush it */ + /* to have the same behaviour of stdout */ if (stdlog) { va_start(ap, format); fprintf(stdlog, "stdout: "); vfprintf(stdlog, format, ap); va_end(ap); + fflush(stdlog); } + + va_start(ap, format); + vfprintf(stdout, format, ap); + va_end(ap); } void ferr(const char* format, ...) { va_list ap; - va_start(ap, format); - vfprintf(stderr, format, ap); - va_end(ap); - + /* first output the same message to stdlog and flush it */ + /* to have the same behaviour of stderr */ if (stdlog) { va_start(ap, format); fprintf(stdlog, "stderr: "); vfprintf(stdlog, format, ap); + fflush(stdlog); va_end(ap); } + + va_start(ap, format); + vfprintf(stderr, format, ap); + va_end(ap); } void flog(const char* format, ...) diff --git a/cmdline/support.h b/cmdline/support.h index bc352fb3..6deb131d 100644 --- a/cmdline/support.h +++ b/cmdline/support.h @@ -27,7 +27,7 @@ * Messages are in human readable format. * They must include the full line output and terminate with '\n'. * - * These messages go in stdout. + * These messages go in the log file and in stdout. */ void fout(const char* format, ...) __attribute__((format(printf, 1, 2)));