Deinitialize logging properly in nebmodule_deinit()
[nagios-reports-module.git] / logging.c
bloba84f424626219e01f831add298cd508f3f17f9d3
1 #include "ndbneb.h"
3 #include <stdarg.h>
4 #include <time.h>
5 #include <sys/time.h>
6 #include <string.h>
7 #include <syslog.h>
8 #include <unistd.h>
9 #include <errno.h>
11 #include "logging.h"
13 #define LOGERR 1
14 #define LOGWARN 2
15 #define LOGINFO 4
16 #define LOGDEBUG 8
18 static FILE *log_fp;
19 static char *log_file = "stdout";
20 static int log_opts = LOGERR | LOGWARN | LOGINFO | LOGDEBUG;
22 int log_grok_var(char *var, char *val)
24 if (!val)
25 return 0;
27 if (!strcmp(var, "log_levels")) {
28 struct opt_code {
29 char *name;
30 int val;
31 } opt_codes[] = {
32 { "all", -1 },
33 { "err", LOGERR },
34 { "warn", LOGWARN },
35 { "info", LOGINFO },
36 { "debug", LOGDEBUG },
37 { NULL, 0 },
39 char *p = val;
41 log_opts = 0;
43 for (p = val; p && *p; p = next_word(p)) {
44 int i, mod = 0;
46 if (*p == '+' || *p == '-')
47 mod = *p++;
49 for (i = 0; i < ARRAY_SIZE(opt_codes); i++) {
50 char *opt = opt_codes[i].name;
52 if (!opt) /* failed to find a valid word */
53 return 0;
55 if (!strncmp(p, opt, strlen(opt))) {
56 log_opts |= opt_codes[i].val;
57 if (!mod) /* not '+' or '-', so add all levels below it */
58 log_opts |= opt_codes[i].val - 1;
60 if (mod == '-') /* remove one level */
61 log_opts = log_opts & ~opt_codes[i].val;
66 return 1;
69 if (!strcmp(var, "log_file")) {
70 log_file = strdup(val);
71 return 1;
74 return 0;
77 void log_deinit(void)
79 if (!log_fp)
80 return;
82 fflush(log_fp);
83 if (log_fp == stdout || log_fp == stderr)
84 return;
86 fsync(fileno(log_fp));
87 fclose(log_fp);
88 log_fp = NULL;
91 int log_init(void)
93 if (!log_file || !strcmp(log_file, "stdout"))
94 log_fp = stdout;
96 if (!strcmp(log_file, "stderr"))
97 log_fp = stderr;
99 if (log_fp)
100 return 0;
102 log_fp = fopen(log_file, "w+");
104 if (!log_fp)
105 return -1;
107 return 0;
110 static void vlog(int severity, const char *fmt, va_list ap)
112 va_list ap2;
114 if (!(log_opts & (1 << severity)))
115 return;
117 if (!log_fp)
118 log_init();
120 va_copy(ap2, ap);
121 fprintf(log_fp, "[%lu] %d: ", time(NULL), severity);
122 vfprintf(log_fp, fmt, ap2);
123 putchar('\n');
126 void loginfo(const char *fmt, ...)
128 va_list ap;
129 va_start(ap, fmt);
130 vlog(LOG_INFO, fmt, ap);
131 va_end(ap);
134 void logwarn(const char *fmt, ...)
136 va_list ap;
137 va_start(ap, fmt);
138 vlog(LOG_WARNING, fmt, ap);
139 va_end(ap);
142 void logerr(const char *fmt, ...)
144 va_list ap;
145 va_start(ap, fmt);
146 vlog(LOG_ERR, fmt, ap);
147 va_end(ap);
150 void logdebug(const char *fmt, ...)
152 va_list ap;
153 va_start(ap, fmt);
154 vlog(LOG_DEBUG, fmt, ap);
155 va_end(ap);
158 /* log a system call failure and return -1 to indicate failure */
159 int sflog(const char *syscall, const char *func, int line)
161 if (errno) {
162 lerr("%s() failed in %s @ %d: %d, %s",
163 syscall, func, line, errno, strerror(errno));
164 return -1;
167 ldebug("%s(): Success in %s @ %d", syscall, func, line);
168 return 0;