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
& severity
))
117 /* don't try to write to a non-open FILE pointer */
118 if (!log_fp
&& log_init() < 0)
122 * last ditch effort to avoid writing to a NULL file pointer,
123 * in case log_init() has bugs
129 fprintf(log_fp
, "[%lu] %d: ", time(NULL
), severity
);
130 vfprintf(log_fp
, fmt
, ap2
);
134 void loginfo(const char *fmt
, ...)
138 vlog(LOGINFO
, fmt
, ap
);
142 void logwarn(const char *fmt
, ...)
146 vlog(LOGWARN
, fmt
, ap
);
150 void logerr(const char *fmt
, ...)
154 vlog(LOGERR
, fmt
, ap
);
158 void logdebug(const char *fmt
, ...)
162 vlog(LOGDEBUG
, fmt
, ap
);
166 /* log a system call failure and return -1 to indicate failure */
167 int sflog(const char *syscall
, const char *func
, int line
)
170 lerr("%s() failed in %s @ %d: %d, %s",
171 syscall
, func
, line
, errno
, strerror(errno
));
175 ldebug("%s(): Success in %s @ %d", syscall
, func
, line
);