2019-10-19 12:35:14 +02:00
|
|
|
#include <errno.h>
|
2019-10-07 08:47:53 +02:00
|
|
|
#include <stdio.h>
|
2019-10-19 12:35:14 +02:00
|
|
|
#include <string.h>
|
2019-10-07 08:47:53 +02:00
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
static enum liftoff_log_importance log_importance = LIFTOFF_ERROR;
|
|
|
|
|
2020-02-26 14:16:10 +01:00
|
|
|
static void log_stderr(enum liftoff_log_importance verbosity, bool newline, const char *fmt,
|
2019-10-07 08:47:53 +02:00
|
|
|
va_list args)
|
|
|
|
{
|
|
|
|
vfprintf(stderr, fmt, args);
|
2020-02-26 14:16:10 +01:00
|
|
|
if (newline) {
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2019-10-07 08:47:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static liftoff_log_func log_callback = log_stderr;
|
|
|
|
|
|
|
|
void liftoff_log_init(enum liftoff_log_importance verbosity,
|
|
|
|
liftoff_log_func callback) {
|
|
|
|
log_importance = verbosity;
|
|
|
|
if (callback) {
|
|
|
|
log_callback = callback;
|
2019-10-30 20:52:30 +01:00
|
|
|
} else {
|
|
|
|
log_callback = log_stderr;
|
2019-10-07 08:47:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:25:05 +01:00
|
|
|
bool log_has(enum liftoff_log_importance verbosity)
|
|
|
|
{
|
|
|
|
return verbosity <= log_importance;
|
|
|
|
}
|
|
|
|
|
2019-10-07 08:47:53 +02:00
|
|
|
void liftoff_log(enum liftoff_log_importance verbosity, const char *fmt, ...)
|
|
|
|
{
|
2020-02-26 10:25:05 +01:00
|
|
|
if (!log_has(verbosity)) {
|
2019-10-07 08:47:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2020-02-26 14:16:10 +01:00
|
|
|
log_callback(verbosity, true, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void liftoff_log_cnt(enum liftoff_log_importance verbosity, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
if (!log_has(verbosity)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
log_callback(verbosity, false, fmt, args);
|
2019-10-07 08:47:53 +02:00
|
|
|
va_end(args);
|
|
|
|
}
|
2019-10-19 12:35:14 +02:00
|
|
|
|
|
|
|
void liftoff_log_errno(enum liftoff_log_importance verbosity, const char *msg)
|
|
|
|
{
|
|
|
|
liftoff_log(verbosity, "%s: %s", msg, strerror(errno));
|
|
|
|
}
|