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"
|
|
|
|
|
2021-03-31 12:44:24 +02:00
|
|
|
static enum liftoff_log_priority log_priority = LIFTOFF_ERROR;
|
2019-10-07 08:47:53 +02:00
|
|
|
|
2021-03-31 12:44:24 +02:00
|
|
|
static void log_stderr(enum liftoff_log_priority priority, const char *fmt,
|
2019-10-07 08:47:53 +02:00
|
|
|
va_list args)
|
|
|
|
{
|
|
|
|
vfprintf(stderr, fmt, args);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
2021-03-31 12:44:24 +02:00
|
|
|
static liftoff_log_handler log_handler = log_stderr;
|
2019-10-07 08:47:53 +02:00
|
|
|
|
2021-03-31 12:44:24 +02:00
|
|
|
void liftoff_log_set_priority(enum liftoff_log_priority priority)
|
|
|
|
{
|
|
|
|
log_priority = priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
void liftoff_log_set_handler(liftoff_log_handler handler) {
|
|
|
|
if (handler) {
|
|
|
|
log_handler = handler;
|
2019-10-30 20:52:30 +01:00
|
|
|
} else {
|
2021-03-31 12:44:24 +02:00
|
|
|
log_handler = log_stderr;
|
2019-10-07 08:47:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 12:44:24 +02:00
|
|
|
bool log_has(enum liftoff_log_priority priority)
|
2020-02-26 10:25:05 +01:00
|
|
|
{
|
2021-03-31 12:44:24 +02:00
|
|
|
return priority <= log_priority;
|
2020-02-26 10:25:05 +01:00
|
|
|
}
|
|
|
|
|
2021-03-31 12:44:24 +02:00
|
|
|
void liftoff_log(enum liftoff_log_priority priority, const char *fmt, ...)
|
2019-10-07 08:47:53 +02:00
|
|
|
{
|
2021-03-31 12:44:24 +02:00
|
|
|
if (!log_has(priority)) {
|
2019-10-07 08:47:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2021-03-31 12:44:24 +02:00
|
|
|
log_handler(priority, fmt, args);
|
2019-10-07 08:47:53 +02:00
|
|
|
va_end(args);
|
|
|
|
}
|
2019-10-19 12:35:14 +02:00
|
|
|
|
2021-03-31 12:44:24 +02:00
|
|
|
void liftoff_log_errno(enum liftoff_log_priority priority, const char *msg)
|
2019-10-19 12:35:14 +02:00
|
|
|
{
|
2021-07-30 16:19:29 +02:00
|
|
|
// Ensure errno is still set to its original value when we return
|
|
|
|
int prev_errno = errno;
|
2021-03-31 12:44:24 +02:00
|
|
|
liftoff_log(priority, "%s: %s", msg, strerror(errno));
|
2021-07-30 16:19:29 +02:00
|
|
|
errno = prev_errno;
|
2019-10-19 12:35:14 +02:00
|
|
|
}
|