tlog.c - ploot - simple plotting tools
(HTM) git clone git://bitreich.org/ploot git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ploot
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Tags
(DIR) README
---
tlog.c (1502B)
---
1 #include "log.h"
2
3 #include <string.h>
4
5 /*
6 * log.c - log to standard error according to the log level
7 *
8 * Instead of logging to syslog, delegate logging to a separate
9 * tool, such as FreeBSD's daemon(8), POSIX's logger(1).
10 *
11 * log_init() sets the log level to the "LOG" environment variable
12 * if set, or to 4 (log down to info included) otherwise.
13 */
14
15 #include <errno.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18
19 #define LOG_DEFAULT 2 /* info */
20
21 int log_level = -1;
22
23 void
24 vlogf(int level, char const *flag, char const *fmt, va_list va)
25 {
26 char *env;
27
28 if (log_level == -1) {
29 env = getenv("LOG");
30 log_level = env ? atoi(env) : 0;
31 log_level = log_level > 0 ? log_level : LOG_DEFAULT;
32 }
33
34 if (log_level < level)
35 return;
36
37 fprintf(stderr, "%s: ", flag);
38 vfprintf(stderr, fmt, va);
39
40 if (errno)
41 fprintf(stderr, ": %s", strerror(errno));
42 errno = 0;
43
44 fprintf(stderr, "\n");
45 fflush(stderr);
46 }
47
48 void
49 die(int exitcode, char const *fmt, ...)
50 {
51 va_list va;
52
53 va_start(va, fmt);
54 vlogf(0, "error", fmt, va);
55 va_end(va);
56 exit(exitcode);
57 }
58
59 void
60 error(char const *fmt, ...)
61 {
62 va_list va;
63
64 va_start(va, fmt);
65 vlogf(0, "error", fmt, va);
66 va_end(va);
67 }
68
69 void
70 warn(char const *fmt, ...)
71 {
72 va_list va;
73
74 va_start(va, fmt);
75 vlogf(1, "warn", fmt, va);
76 va_end(va);
77 }
78
79 void
80 info(char const *fmt, ...)
81 {
82 va_list va;
83
84 va_start(va, fmt);
85 vlogf(2, "info", fmt, va);
86 va_end(va);
87 }
88
89 void
90 debug(char const *fmt, ...)
91 {
92 va_list va;
93
94 va_start(va, fmt);
95 vlogf(3, "debug", fmt, va);
96 va_end(va);
97 }