]> git.ozlabs.org Git - petitboot/blob - lib/log/log.c
discover: Implement device priorities
[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         if (debug)
16                 fflush(logf);
17 }
18
19 void pb_log(const char *fmt, ...)
20 {
21         va_list ap;
22         va_start(ap, fmt);
23         __log(fmt, ap);
24         va_end(ap);
25 }
26
27 void pb_debug(const char *fmt, ...)
28 {
29         va_list ap;
30         if (!debug)
31                 return;
32         va_start(ap, fmt);
33         __log(fmt, ap);
34         va_end(ap);
35 }
36
37 void __pb_log_init(FILE *fp, bool _debug)
38 {
39         if (logf)
40                 fflush(logf);
41         logf = fp;
42         debug = _debug;
43 }
44
45 FILE *pb_log_get_stream(void)
46 {
47         static FILE *null_stream;
48         if (!logf) {
49                 if (!null_stream)
50                         null_stream = fopen("/dev/null", "a");
51                 return null_stream;
52         }
53         return logf;
54 }