2019-06-12 20:08:54 +02:00
|
|
|
#include "log.h"
|
|
|
|
|
|
2021-01-15 20:39:45 +00:00
|
|
|
#include <errno.h>
|
2019-06-12 20:08:54 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <stdbool.h>
|
2021-01-15 20:39:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <syslog.h>
|
2019-06-12 20:08:54 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2021-01-15 20:39:45 +00:00
|
|
|
#include "debug.h"
|
2019-07-30 21:48:14 +02:00
|
|
|
|
2019-06-12 20:08:54 +02:00
|
|
|
static bool colorize = false;
|
2020-02-05 19:54:16 +01:00
|
|
|
static bool do_syslog = true;
|
2019-12-17 19:07:28 +01:00
|
|
|
|
2019-11-20 19:43:31 +01:00
|
|
|
void
|
2020-02-05 19:54:16 +01:00
|
|
|
log_init(enum log_colorize _colorize, bool _do_syslog,
|
|
|
|
|
enum log_facility syslog_facility, enum log_class syslog_level)
|
2019-06-12 20:08:54 +02:00
|
|
|
{
|
2019-11-20 19:43:31 +01:00
|
|
|
static const int facility_map[] = {
|
|
|
|
|
[LOG_FACILITY_USER] = LOG_USER,
|
|
|
|
|
[LOG_FACILITY_DAEMON] = LOG_DAEMON,
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-17 19:07:28 +01:00
|
|
|
static const int level_map[] = {
|
|
|
|
|
[LOG_CLASS_ERROR] = LOG_ERR,
|
|
|
|
|
[LOG_CLASS_WARNING] = LOG_WARNING,
|
|
|
|
|
[LOG_CLASS_INFO] = LOG_INFO,
|
|
|
|
|
[LOG_CLASS_DEBUG] = LOG_DEBUG,
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-05 19:54:16 +01:00
|
|
|
colorize = _colorize == LOG_COLORIZE_NEVER ? false : _colorize == LOG_COLORIZE_ALWAYS ? true : isatty(STDERR_FILENO);
|
|
|
|
|
do_syslog = _do_syslog;
|
|
|
|
|
|
|
|
|
|
if (do_syslog) {
|
|
|
|
|
openlog(NULL, /*LOG_PID*/0, facility_map[syslog_facility]);
|
|
|
|
|
setlogmask(LOG_UPTO(level_map[syslog_level]));
|
|
|
|
|
}
|
2019-07-30 21:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
2019-11-20 19:43:31 +01:00
|
|
|
void
|
|
|
|
|
log_deinit(void)
|
2019-07-30 21:48:14 +02:00
|
|
|
{
|
2020-02-05 19:54:16 +01:00
|
|
|
if (do_syslog)
|
|
|
|
|
closelog();
|
2019-06-12 20:08:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_log(enum log_class log_class, const char *module, const char *file, int lineno,
|
|
|
|
|
const char *fmt, int sys_errno, va_list va)
|
|
|
|
|
{
|
|
|
|
|
const char *class = "abcd";
|
|
|
|
|
int class_clr = 0;
|
|
|
|
|
switch (log_class) {
|
|
|
|
|
case LOG_CLASS_ERROR: class = " err"; class_clr = 31; break;
|
|
|
|
|
case LOG_CLASS_WARNING: class = "warn"; class_clr = 33; break;
|
|
|
|
|
case LOG_CLASS_INFO: class = "info"; class_clr = 97; break;
|
|
|
|
|
case LOG_CLASS_DEBUG: class = " dbg"; class_clr = 36; break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char clr[16];
|
2020-08-02 23:54:04 +01:00
|
|
|
snprintf(clr, sizeof(clr), "\033[%dm", class_clr);
|
|
|
|
|
fprintf(stderr, "%s%s%s: ", colorize ? clr : "", class, colorize ? "\033[0m" : "");
|
2019-06-12 20:08:54 +02:00
|
|
|
|
|
|
|
|
if (colorize)
|
2020-08-02 23:54:04 +01:00
|
|
|
fprintf(stderr, "\033[2m");
|
2020-02-04 18:29:04 +01:00
|
|
|
fprintf(stderr, "%s:%d: ", file, lineno);
|
2019-06-12 20:08:54 +02:00
|
|
|
if (colorize)
|
2020-08-02 23:54:04 +01:00
|
|
|
fprintf(stderr, "\033[0m");
|
2019-06-12 20:08:54 +02:00
|
|
|
|
2020-02-04 18:29:04 +01:00
|
|
|
vfprintf(stderr, fmt, va);
|
2019-06-12 20:08:54 +02:00
|
|
|
|
|
|
|
|
if (sys_errno != 0)
|
2020-02-04 18:29:04 +01:00
|
|
|
fprintf(stderr, ": %s", strerror(sys_errno));
|
2019-06-12 20:08:54 +02:00
|
|
|
|
2020-02-04 18:29:04 +01:00
|
|
|
fprintf(stderr, "\n");
|
2019-06-12 20:08:54 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-30 21:48:14 +02:00
|
|
|
static void
|
2019-07-30 21:50:08 +02:00
|
|
|
_sys_log(enum log_class log_class, const char *module,
|
2020-08-07 20:42:34 +01:00
|
|
|
const char UNUSED *file, int UNUSED lineno,
|
2019-07-30 21:50:08 +02:00
|
|
|
const char *fmt, int sys_errno, va_list va)
|
2019-07-30 21:48:14 +02:00
|
|
|
{
|
2020-02-05 19:54:16 +01:00
|
|
|
if (!do_syslog)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-07-30 21:48:14 +02:00
|
|
|
/* Map our log level to syslog's level */
|
|
|
|
|
int level = -1;
|
|
|
|
|
switch (log_class) {
|
|
|
|
|
case LOG_CLASS_ERROR: level = LOG_ERR; break;
|
|
|
|
|
case LOG_CLASS_WARNING: level = LOG_WARNING; break;
|
|
|
|
|
case LOG_CLASS_INFO: level = LOG_INFO; break;
|
|
|
|
|
case LOG_CLASS_DEBUG: level = LOG_DEBUG; break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(level != -1);
|
|
|
|
|
|
2020-08-06 23:13:06 +01:00
|
|
|
char msg[4096];
|
|
|
|
|
int n = vsnprintf(msg, sizeof(msg), fmt, va);
|
|
|
|
|
assert(n >= 0);
|
2019-07-30 21:48:14 +02:00
|
|
|
|
2020-08-06 23:13:06 +01:00
|
|
|
if (sys_errno != 0 && (size_t)n < sizeof(msg))
|
|
|
|
|
snprintf(msg + n, sizeof(msg) - n, ": %s", strerror(sys_errno));
|
2019-07-30 21:48:14 +02:00
|
|
|
|
2020-08-06 23:13:06 +01:00
|
|
|
syslog(level, "%s: %s", module, msg);
|
2019-07-30 21:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-12 20:08:54 +02:00
|
|
|
void
|
|
|
|
|
log_msg(enum log_class log_class, const char *module,
|
|
|
|
|
const char *file, int lineno, const char *fmt, ...)
|
|
|
|
|
{
|
2019-07-30 21:48:14 +02:00
|
|
|
va_list ap1, ap2;
|
2019-06-12 20:08:54 +02:00
|
|
|
va_start(ap1, fmt);
|
2019-07-30 21:48:14 +02:00
|
|
|
va_copy(ap2, ap1);
|
2019-06-12 20:08:54 +02:00
|
|
|
_log(log_class, module, file, lineno, fmt, 0, ap1);
|
2019-07-30 21:48:14 +02:00
|
|
|
_sys_log(log_class, module, file, lineno, fmt, 0, ap2);
|
2019-06-12 20:08:54 +02:00
|
|
|
va_end(ap1);
|
2019-07-30 21:48:14 +02:00
|
|
|
va_end(ap2);
|
2019-06-12 20:08:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void log_errno(enum log_class log_class, const char *module,
|
|
|
|
|
const char *file, int lineno,
|
|
|
|
|
const char *fmt, ...)
|
|
|
|
|
{
|
2019-07-30 21:48:14 +02:00
|
|
|
va_list ap1, ap2;
|
2019-06-12 20:08:54 +02:00
|
|
|
va_start(ap1, fmt);
|
2019-07-30 21:48:14 +02:00
|
|
|
va_copy(ap2, ap1);
|
2019-06-12 20:08:54 +02:00
|
|
|
_log(log_class, module, file, lineno, fmt, errno, ap1);
|
2019-07-30 21:48:14 +02:00
|
|
|
_sys_log(log_class, module, file, lineno, fmt, errno, ap2);
|
2019-06-12 20:08:54 +02:00
|
|
|
va_end(ap1);
|
2019-07-30 21:48:14 +02:00
|
|
|
va_end(ap2);
|
2019-06-12 20:08:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void log_errno_provided(enum log_class log_class, const char *module,
|
|
|
|
|
const char *file, int lineno, int _errno,
|
|
|
|
|
const char *fmt, ...)
|
|
|
|
|
{
|
2019-07-30 21:48:14 +02:00
|
|
|
va_list ap1, ap2;
|
2019-06-12 20:08:54 +02:00
|
|
|
va_start(ap1, fmt);
|
2019-07-30 21:48:14 +02:00
|
|
|
va_copy(ap2, ap1);
|
2019-06-12 20:08:54 +02:00
|
|
|
_log(log_class, module, file, lineno, fmt, _errno, ap1);
|
2019-07-30 21:48:14 +02:00
|
|
|
_sys_log(log_class, module, file, lineno, fmt, _errno, ap2);
|
2019-06-12 20:08:54 +02:00
|
|
|
va_end(ap1);
|
2019-07-30 21:48:14 +02:00
|
|
|
va_end(ap2);
|
2019-06-12 20:08:54 +02:00
|
|
|
}
|