19 static char *log_file
= "stdout";
20 static int log_opts
= LOGERR
| LOGWARN
| LOGINFO
| LOGDEBUG
;
22 int log_grok_var(char *var
, char *val
)
27 if (!strcmp(var
, "log_levels")) {
36 { "debug", LOGDEBUG
},
43 for (p
= val
; p
&& *p
; p
= next_word(p
)) {
46 if (*p
== '+' || *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 */
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
;
69 if (!strcmp(var
, "log_file")) {
70 log_file
= strdup(val
);
83 if (log_fp
== stdout
|| log_fp
== stderr
)
86 fsync(fileno(log_fp
));
93 if (!log_file
|| !strcmp(log_file
, "stdout"))
96 if (!strcmp(log_file
, "stderr"))
102 log_fp
= fopen(log_file
, "w+");
110 static void vlog(int severity
, const char *fmt
, va_list ap
)
114 if (!(log_opts
& (1 << severity
)))
121 fprintf(log_fp
, "[%lu] %d: ", time(NULL
), severity
);
122 vfprintf(log_fp
, fmt
, ap2
);
126 void loginfo(const char *fmt
, ...)
130 vlog(LOG_INFO
, fmt
, ap
);
134 void logwarn(const char *fmt
, ...)
138 vlog(LOG_WARNING
, fmt
, ap
);
142 void logerr(const char *fmt
, ...)
146 vlog(LOG_ERR
, fmt
, ap
);
150 void logdebug(const char *fmt
, ...)
154 vlog(LOG_DEBUG
, fmt
, ap
);
158 /* log a system call failure and return -1 to indicate failure */
159 int sflog(const char *syscall
, const char *func
, int line
)
162 lerr("%s() failed in %s @ %d: %d, %s",
163 syscall
, func
, line
, errno
, strerror(errno
));
167 ldebug("%s(): Success in %s @ %d", syscall
, func
, line
);