]> git.ozlabs.org Git - ccan/blob - ccan/pr_log/pr_log.c
tal: allow notifiers on NULL.
[ccan] / ccan / pr_log / pr_log.c
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #include "pr_log.h"
3
4 #include <ctype.h>
5 #include <stdarg.h>
6 #include <stdbool.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <limits.h>
10
11 #include <ccan/str/str.h>
12
13 #define DEBUG_NEED_INIT INT_MIN
14 static int debug = DEBUG_NEED_INIT;
15
16 bool debug_is(int lvl)
17 {
18         return lvl <= debug_level();
19 }
20
21 int debug_level(void)
22 {
23         if (debug != DEBUG_NEED_INIT)
24                 return debug;
25         char *c = getenv("DEBUG");
26         if (!c) {
27                 debug = CCAN_PR_LOG_DEFAULT_LEVEL;
28                 return debug;
29         }
30
31         debug = atoi(c);
32         return debug;
33 }
34
35 void pr_log_(char const *fmt, ...)
36 {
37         int level = INT_MIN;
38         if (fmt[0] == '<' && cisdigit(fmt[1]) && fmt[2] == '>')
39                 level = fmt[1] - '0';
40
41         if (!debug_is(level))
42                 return;
43
44         va_list va;
45         va_start(va, fmt);
46         vfprintf(stderr, fmt, va);
47         va_end(va);
48 }