diff options
author | Jeremy Kerr <jk@ozlabs.org> | 2013-09-19 12:18:56 +0800 |
---|---|---|
committer | Jeremy Kerr <jk@ozlabs.org> | 2013-09-19 21:36:32 +0800 |
commit | 307b4b091e0bc298964e90888bf179431e32c134 (patch) | |
tree | 47cc758b0929eb29a216efd945b9ad46f4ffb19a /lib/log/log.c | |
parent | e52b37c27c267c882d82cd1e34412817b5a4dbce (diff) | |
download | petitboot-307b4b091e0bc298964e90888bf179431e32c134.zip petitboot-307b4b091e0bc298964e90888bf179431e32c134.tar.gz |
lib/log: Cleanup log API
Rather than exposing log internals (through always_flush and
set_stream), do all logging init through pb_log_init(). If pb_log_init()
hasn't been called, pb_log will drop messages.
Also, add a pb_debug() function, specifically for debugging information.
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Diffstat (limited to 'lib/log/log.c')
-rw-r--r-- | lib/log/log.c | 48 |
1 files changed, 32 insertions, 16 deletions
diff --git a/lib/log/log.c b/lib/log/log.c index ecbd714..b250a9e 100644 --- a/lib/log/log.c +++ b/lib/log/log.c @@ -1,38 +1,54 @@ +#include <assert.h> #include <stdarg.h> #include "log.h" static FILE *logf; -static int always_flush; +static bool debug; + +static void __log(const char *fmt, va_list ap) +{ + if (!logf) + return; + vfprintf(logf, fmt, ap); + if (debug) + fflush(logf); +} void pb_log(const char *fmt, ...) { va_list ap; - FILE *stream; - - stream = logf ? logf : stderr; - va_start(ap, fmt); - vfprintf(stream, fmt, ap); + __log(fmt, ap); va_end(ap); - - if (always_flush) - fflush(stream); } -void pb_log_set_stream(FILE *stream) +void pb_debug(const char *fmt, ...) { - fflush(logf ? logf : stderr); - logf = stream; + va_list ap; + if (!debug) + return; + va_start(ap, fmt); + __log(fmt, ap); + va_end(ap); } -FILE * pb_log_get_stream(void) +void __pb_log_init(FILE *fp, bool _debug) { - return logf ? logf : stderr; + if (logf) + fflush(logf); + logf = fp; + debug = _debug; } -void pb_log_always_flush(int state) +FILE *pb_log_get_stream(void) { - always_flush = state; + static FILE *null_stream; + if (!logf) { + if (!null_stream) + null_stream = fopen("/dev/null", "a"); + return null_stream; + } + return logf; } |