import: Only read last time from sql if --incremental lacks timestamp
[nagios-reports-module.git] / logging.c
blob316f8ebf870f05662223d7d122ad09614f8eb4af
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 & severity))
115 return;
117 /* don't try to write to a non-open FILE pointer */
118 if (!log_fp && log_init() < 0)
119 return;
122 * last ditch effort to avoid writing to a NULL file pointer,
123 * in case log_init() has bugs
125 if (!log_fp)
126 return;
128 va_copy(ap2, ap);
129 fprintf(log_fp, "[%lu] %d: ", time(NULL), severity);
130 vfprintf(log_fp, fmt, ap2);
131 fputc('\n', log_fp);
134 void loginfo(const char *fmt, ...)
136 va_list ap;
137 va_start(ap, fmt);
138 vlog(LOGINFO, fmt, ap);
139 va_end(ap);
142 void logwarn(const char *fmt, ...)
144 va_list ap;
145 va_start(ap, fmt);
146 vlog(LOGWARN, fmt, ap);
147 va_end(ap);
150 void logerr(const char *fmt, ...)
152 va_list ap;
153 va_start(ap, fmt);
154 vlog(LOGERR, fmt, ap);
155 va_end(ap);
158 void logdebug(const char *fmt, ...)
160 va_list ap;
161 va_start(ap, fmt);
162 vlog(LOGDEBUG, fmt, ap);
163 va_end(ap);
166 /* log a system call failure and return -1 to indicate failure */
167 int sflog(const char *syscall, const char *func, int line)
169 if (errno) {
170 lerr("%s() failed in %s @ %d: %d, %s",
171 syscall, func, line, errno, strerror(errno));
172 return -1;
175 ldebug("%s(): Success in %s @ %d", syscall, func, line);
176 return 0;