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
));
92 if (!log_file
|| !strcmp(log_file
, "stdout"))
95 if (!strcmp(log_file
, "stderr"))
101 log_fp
= fopen(log_file
, "w+");
109 static void vlog(int severity
, const char *fmt
, va_list ap
)
113 if (!(log_opts
& (1 << severity
)))
120 fprintf(log_fp
, "[%lu] %d: ", time(NULL
), severity
);
121 vfprintf(log_fp
, fmt
, ap2
);
125 void loginfo(const char *fmt
, ...)
129 vlog(LOG_INFO
, fmt
, ap
);
133 void logwarn(const char *fmt
, ...)
137 vlog(LOG_WARNING
, fmt
, ap
);
141 void logerr(const char *fmt
, ...)
145 vlog(LOG_ERR
, fmt
, ap
);
149 void logdebug(const char *fmt
, ...)
153 vlog(LOG_DEBUG
, fmt
, ap
);
157 /* log a system call failure and return -1 to indicate failure */
158 int sflog(const char *syscall
, const char *func
, int line
)
161 lerr("%s() failed in %s @ %d: %d, %s",
162 syscall
, func
, line
, errno
, strerror(errno
));
166 ldebug("%s(): Success in %s @ %d", syscall
, func
, line
);