]> git.ozlabs.org Git - petitboot/blob - lib/log/log.c
lib/log: Always flush after writing logs
[petitboot] / lib / log / log.c
1
2 #include <assert.h>
3 #include <stdarg.h>
4
5 #include "log.h"
6
7 static FILE *logf;
8 static bool debug;
9
10 static void __log(const char *fmt, va_list ap)
11 {
12         if (!logf)
13                 return;
14         vfprintf(logf, fmt, ap);
15         fflush(logf);
16 }
17
18 void pb_log(const char *fmt, ...)
19 {
20         va_list ap;
21         va_start(ap, fmt);
22         __log(fmt, ap);
23         va_end(ap);
24 }
25
26 void pb_debug(const char *fmt, ...)
27 {
28         va_list ap;
29         if (!debug)
30                 return;
31         va_start(ap, fmt);
32         __log(fmt, ap);
33         va_end(ap);
34 }
35
36 void __pb_log_init(FILE *fp, bool _debug)
37 {
38         if (logf)
39                 fflush(logf);
40         logf = fp;
41         debug = _debug;
42 }
43
44 FILE *pb_log_get_stream(void)
45 {
46         static FILE *null_stream;
47         if (!logf) {
48                 if (!null_stream)
49                         null_stream = fopen("/dev/null", "a");
50                 return null_stream;
51         }
52         return logf;
53 }