Split liftoff_log_init into set_handler and set_priority

This allows callers to change only one of those, without the other.
Also rename importance to priority and func to handler.
This commit is contained in:
Simon Ser 2021-03-31 12:44:24 +02:00
parent 12eb851a40
commit b08bbaa5e6
8 changed files with 41 additions and 35 deletions

View file

@ -98,23 +98,28 @@ void liftoff_layer_set_fb_composited(struct liftoff_layer *layer);
*/
uint32_t liftoff_layer_get_plane_id(struct liftoff_layer *layer);
enum liftoff_log_importance {
enum liftoff_log_priority {
LIFTOFF_SILENT,
LIFTOFF_ERROR,
LIFTOFF_DEBUG,
};
typedef void (*liftoff_log_func)(enum liftoff_log_importance importance,
const char *fmt, va_list args);
typedef void (*liftoff_log_handler)(enum liftoff_log_priority priority,
const char *fmt, va_list args);
/**
* Initialize libliftoff's log infrastructure.
* Set libliftoff's log priority.
*
* Only messages with a priority higher than the provided `verbosity` will be
* logged. If `callback` is non-NULL, libliftoff will call the function instead
* of printing the messages to stderr.
* Only messages with a priority higher than the provided priority will be
* logged. The default priority is LIFTOFF_ERROR.
*/
void liftoff_log_init(enum liftoff_log_importance verbosity,
liftoff_log_func callback);
void liftoff_log_set_priority(enum liftoff_log_priority priority);
/**
* Set libliftoff's log handler.
*
* The default handler prints messages to stderr. NULL restores the default
* handler.
*/
void liftoff_log_set_handler(liftoff_log_handler handler);
#endif

View file

@ -9,10 +9,10 @@
#define _LIFTOFF_ATTRIB_PRINTF(start, end)
#endif
bool log_has(enum liftoff_log_importance verbosity);
bool log_has(enum liftoff_log_priority priority);
void liftoff_log(enum liftoff_log_importance verbosity,
void liftoff_log(enum liftoff_log_priority priority,
const char *format, ...) _LIFTOFF_ATTRIB_PRINTF(2, 3);
void liftoff_log_errno(enum liftoff_log_importance verbosity, const char *msg);
void liftoff_log_errno(enum liftoff_log_priority priority, const char *msg);
#endif

35
log.c
View file

@ -3,45 +3,48 @@
#include <string.h>
#include "log.h"
static enum liftoff_log_importance log_importance = LIFTOFF_ERROR;
static enum liftoff_log_priority log_priority = LIFTOFF_ERROR;
static void log_stderr(enum liftoff_log_importance verbosity, const char *fmt,
static void log_stderr(enum liftoff_log_priority priority, const char *fmt,
va_list args)
{
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
}
static liftoff_log_func log_callback = log_stderr;
static liftoff_log_handler log_handler = log_stderr;
void liftoff_log_init(enum liftoff_log_importance verbosity,
liftoff_log_func callback) {
log_importance = verbosity;
if (callback) {
log_callback = callback;
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;
} else {
log_callback = log_stderr;
log_handler = log_stderr;
}
}
bool log_has(enum liftoff_log_importance verbosity)
bool log_has(enum liftoff_log_priority priority)
{
return verbosity <= log_importance;
return priority <= log_priority;
}
void liftoff_log(enum liftoff_log_importance verbosity, const char *fmt, ...)
void liftoff_log(enum liftoff_log_priority priority, const char *fmt, ...)
{
if (!log_has(verbosity)) {
if (!log_has(priority)) {
return;
}
va_list args;
va_start(args, fmt);
log_callback(verbosity, fmt, args);
log_handler(priority, fmt, args);
va_end(args);
}
void liftoff_log_errno(enum liftoff_log_importance verbosity, const char *msg)
void liftoff_log_errno(enum liftoff_log_priority priority, const char *msg)
{
liftoff_log(verbosity, "%s: %s", msg, strerror(errno));
liftoff_log(priority, "%s: %s", msg, strerror(errno));
}

View file

@ -64,7 +64,7 @@ int main(int argc, char *argv[])
}
}
liftoff_log_init(LIFTOFF_SILENT, NULL);
liftoff_log_set_priority(LIFTOFF_SILENT);
for (i = 0; i < planes_len; i++) {
plane_type = i == 0 ? DRM_PLANE_TYPE_PRIMARY :

View file

@ -805,7 +805,7 @@ int main(int argc, char *argv[]) {
const char *test_name;
size_t i;
liftoff_log_init(LIFTOFF_DEBUG, NULL);
liftoff_log_set_priority(LIFTOFF_DEBUG);
if (argc != 2) {
fprintf(stderr, "usage: %s <test-name>\n", argv[0]);

View file

@ -308,7 +308,7 @@ static void run(const struct test_case *test) {
int main(int argc, char *argv[]) {
const char *test_name;
liftoff_log_init(LIFTOFF_DEBUG, NULL);
liftoff_log_set_priority(LIFTOFF_DEBUG);
if (argc != 2) {
fprintf(stderr, "usage: %s <test-name>\n", argv[0]);

View file

@ -40,7 +40,7 @@ int main(int argc, char *argv[]) {
bool ok;
int ret;
liftoff_log_init(LIFTOFF_SILENT, NULL);
liftoff_log_set_priority(LIFTOFF_SILENT);
mock_plane = liftoff_mock_drm_create_plane(DRM_PLANE_TYPE_PRIMARY);
/* Plane incompatible with all layers */

View file

@ -38,8 +38,6 @@ static int test_prop_default(const char *prop_name)
bool ok;
int ret;
liftoff_log_init(LIFTOFF_DEBUG, NULL);
mock_plane_without_prop = liftoff_mock_drm_create_plane(DRM_PLANE_TYPE_OVERLAY);
mock_plane_with_prop = liftoff_mock_drm_create_plane(DRM_PLANE_TYPE_OVERLAY);
@ -240,7 +238,7 @@ static int test_immutable_zpos(void) {
int main(int argc, char *argv[]) {
const char *test_name;
liftoff_log_init(LIFTOFF_DEBUG, NULL);
liftoff_log_set_priority(LIFTOFF_DEBUG);
if (argc != 2) {
fprintf(stderr, "usage: %s <test-name>\n", argv[0]);