2019-10-19 12:35:14 +02:00
|
|
|
#include <errno.h>
|
2019-10-07 08:47:53 +02:00
|
|
|
#include <stdio.h>
|
2020-02-29 02:04:26 +01:00
|
|
|
#include <stdlib.h>
|
2019-10-19 12:35:14 +02:00
|
|
|
#include <string.h>
|
2019-10-07 08:47:53 +02:00
|
|
|
#include "log.h"
|
2020-02-29 02:04:26 +01:00
|
|
|
#include "private.h"
|
2019-10-07 08:47:53 +02:00
|
|
|
|
|
|
|
static enum liftoff_log_importance log_importance = LIFTOFF_ERROR;
|
|
|
|
|
2020-02-29 02:51:17 +01:00
|
|
|
static void log_stderr(enum liftoff_log_importance verbosity,
|
|
|
|
enum liftoff_log_flags flags,
|
|
|
|
const char *fmt, va_list args)
|
2019-10-07 08:47:53 +02:00
|
|
|
{
|
2020-02-29 02:51:17 +01:00
|
|
|
if (flags & LIFTOFF_LOG_SECTION_START) {
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
2019-10-07 08:47:53 +02:00
|
|
|
vfprintf(stderr, fmt, args);
|
2020-02-29 02:04:26 +01:00
|
|
|
fprintf(stderr, "\n");
|
2020-02-29 02:51:17 +01:00
|
|
|
if (flags & LIFTOFF_LOG_SECTION_END) {
|
|
|
|
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-29 02:51:17 +01:00
|
|
|
log_callback(verbosity, 0, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void liftoff_log_formatted(enum liftoff_log_importance verbosity,
|
|
|
|
enum liftoff_log_flags flags,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
if (!log_has(verbosity)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
log_callback(verbosity, flags, fmt, args);
|
2020-02-26 14:16:10 +01:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2020-02-29 02:04:26 +01:00
|
|
|
void liftoff_log_errno(enum liftoff_log_importance verbosity, const char *msg)
|
2020-02-26 14:16:10 +01:00
|
|
|
{
|
2020-02-29 02:04:26 +01:00
|
|
|
liftoff_log(verbosity, "%s: %s", msg, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void debug_cnt(struct liftoff_device *device, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
if (!log_has(LIFTOFF_DEBUG)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-26 14:16:10 +01:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2019-10-19 12:35:14 +02:00
|
|
|
|
2020-02-29 02:04:26 +01:00
|
|
|
if (device->log_buf == NULL) {
|
|
|
|
device->log_buf = malloc(4096);
|
|
|
|
if (device->log_buf == NULL) {
|
|
|
|
liftoff_log_errno(LIFTOFF_ERROR, "malloc");
|
|
|
|
goto cleanup_out;
|
|
|
|
}
|
|
|
|
device->log_buf_len = 4096 / sizeof(char);
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
size_t max_len = device->log_buf_len - device->log_buf_index;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = vsnprintf(device->log_buf + device->log_buf_index,
|
|
|
|
max_len, fmt, args);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto cleanup_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret - (int)max_len > 0) {
|
|
|
|
device->log_buf_index = device->log_buf_len;
|
|
|
|
device->log_buf_len *= 2;
|
|
|
|
|
|
|
|
if (realloc(device->log_buf, device->log_buf_len) == NULL) {
|
|
|
|
liftoff_log_errno(LIFTOFF_ERROR, "realloc");
|
|
|
|
goto cleanup_out;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
device->log_buf_index += ret;
|
|
|
|
goto final_out;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
cleanup_out:
|
|
|
|
free(device->log_buf);
|
|
|
|
device->log_buf = NULL;
|
|
|
|
device->log_buf_index = 0;
|
|
|
|
device->log_buf_len = 0;
|
|
|
|
|
|
|
|
final_out:
|
|
|
|
va_end(args);
|
2019-10-19 12:35:14 +02:00
|
|
|
}
|
2020-02-29 02:51:17 +01:00
|
|
|
|
|
|
|
void debug_end(struct liftoff_device *device, enum liftoff_log_flags flags)
|
|
|
|
{
|
|
|
|
if (!log_has(LIFTOFF_DEBUG)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (device->log_buf == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
liftoff_log_formatted(LIFTOFF_DEBUG, flags, "%s", device->log_buf);
|
|
|
|
free(device->log_buf);
|
|
|
|
device->log_buf = NULL;
|
|
|
|
device->log_buf_index = 0;
|
|
|
|
device->log_buf_len = 0;
|
|
|
|
}
|