import.c: Disable warnings for ltime < last_ltime
[nagios-reports-module.git] / logging.c
blobe33d5ff4cc7a02b55501f60fc2524f4434326fbd
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);
90 int log_init(void)
92 if (!log_file || !strcmp(log_file, "stdout"))
93 log_fp = stdout;
95 if (!strcmp(log_file, "stderr"))
96 log_fp = stderr;
98 if (log_fp)
99 return 0;
101 log_fp = fopen(log_file, "w+");
103 if (!log_fp)
104 return -1;
106 return 0;
109 static void vlog(int severity, const char *fmt, va_list ap)
111 va_list ap2;
113 if (!(log_opts & (1 << severity)))
114 return;
116 if (!log_fp)
117 log_init();
119 va_copy(ap2, ap);
120 fprintf(log_fp, "[%lu] %d: ", time(NULL), severity);
121 vfprintf(log_fp, fmt, ap2);
122 putchar('\n');
125 void loginfo(const char *fmt, ...)
127 va_list ap;
128 va_start(ap, fmt);
129 vlog(LOG_INFO, fmt, ap);
130 va_end(ap);
133 void logwarn(const char *fmt, ...)
135 va_list ap;
136 va_start(ap, fmt);
137 vlog(LOG_WARNING, fmt, ap);
138 va_end(ap);
141 void logerr(const char *fmt, ...)
143 va_list ap;
144 va_start(ap, fmt);
145 vlog(LOG_ERR, fmt, ap);
146 va_end(ap);
149 void logdebug(const char *fmt, ...)
151 va_list ap;
152 va_start(ap, fmt);
153 vlog(LOG_DEBUG, fmt, ap);
154 va_end(ap);
157 /* log a system call failure and return -1 to indicate failure */
158 int sflog(const char *syscall, const char *func, int line)
160 if (errno) {
161 lerr("%s() failed in %s @ %d: %d, %s",
162 syscall, func, line, errno, strerror(errno));
163 return -1;
166 ldebug("%s(): Success in %s @ %d", syscall, func, line);
167 return 0;