Add logging functions

This allows turning on and off debug logs, as well as defining a logging
callback.
This commit is contained in:
Simon Ser 2019-10-07 02:47:53 -04:00
parent 3671a64ade
commit 1ee86c6290
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
8 changed files with 110 additions and 31 deletions

View file

@ -6,6 +6,7 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "log.h"
#include "private.h"
static int guess_plane_zpos_from_type(struct liftoff_display *display,
@ -96,9 +97,9 @@ static struct liftoff_plane *plane_create(struct liftoff_display *display,
drmModeFreeObjectProperties(drm_props);
if (!has_type) {
fprintf(stderr,
"plane %"PRIu32" is missing the 'type' property\n",
plane->id);
liftoff_log(LIFTOFF_ERROR,
"plane %"PRIu32" is missing the 'type' property",
plane->id);
free(plane);
return NULL;
} else if (!has_zpos) {
@ -223,7 +224,8 @@ static bool plane_set_prop(struct liftoff_plane *plane, drmModeAtomicReq *req,
{
int ret;
fprintf(stderr, " Setting %s = %"PRIu64"\n", prop->name, value);
liftoff_log(LIFTOFF_DEBUG, " Setting %s = %"PRIu64,
prop->name, value);
ret = drmModeAtomicAddProperty(req, plane->id, prop->id, value);
if (ret < 0) {
perror("drmModeAtomicAddProperty");
@ -241,8 +243,9 @@ static bool set_plane_prop_str(struct liftoff_plane *plane,
prop = plane_get_property(plane, name);
if (prop == NULL) {
fprintf(stderr, "plane %"PRIu32" is missing the %s property\n",
plane->id, name);
liftoff_log(LIFTOFF_DEBUG,
"plane %"PRIu32" is missing the %s property",
plane->id, name);
return false;
}
@ -564,8 +567,9 @@ bool output_choose_layers(struct liftoff_output *output,
goto skip;
}
fprintf(stderr, "Performing allocation for plane %"PRIu32" (%zu/%zu)\n",
plane->id, step->plane_idx + 1, result->planes_len);
liftoff_log(LIFTOFF_DEBUG,
"Performing allocation for plane %"PRIu32" (%zu/%zu)",
plane->id, step->plane_idx + 1, result->planes_len);
liftoff_list_for_each(layer, &output->layers, link) {
if (layer->plane != NULL) {
@ -583,9 +587,10 @@ bool output_choose_layers(struct liftoff_output *output,
has_allocated_layer_over(output, step, layer)) {
/* This layer needs to be on top of the last
* allocated one */
fprintf(stderr, "Layer %p -> plane %"PRIu32": "
"layer zpos invalid\n",
(void *)layer, plane->id);
liftoff_log(LIFTOFF_DEBUG,
"Layer %p -> plane %"PRIu32": "
"layer zpos invalid",
(void *)layer, plane->id);
continue;
}
if ((int)zpos_prop->value < step->last_layer_zpos &&
@ -595,39 +600,44 @@ bool output_choose_layers(struct liftoff_output *output,
* last one (in practice, since planes are
* sorted by zpos it means it has the same zpos,
* ie. undefined ordering). */
fprintf(stderr, "Layer %p -> plane %"PRIu32": "
"plane zpos invalid\n",
(void *)layer, plane->id);
liftoff_log(LIFTOFF_DEBUG,
"Layer %p -> plane %"PRIu32": "
"plane zpos invalid",
(void *)layer, plane->id);
continue;
}
}
if (plane->type != DRM_PLANE_TYPE_PRIMARY &&
has_composited_layer_over(output, step, layer)) {
fprintf(stderr, "Layer %p -> plane %"PRIu32": "
"has composited layer on top\n",
(void *)layer, plane->id);
liftoff_log(LIFTOFF_DEBUG,
"Layer %p -> plane %"PRIu32": "
"has composited layer on top",
(void *)layer, plane->id);
continue;
}
/* Try to use this layer for the current plane */
fprintf(stderr, "Layer %p -> plane %"PRIu32": "
"applying properties...\n", (void *)layer, plane->id);
liftoff_log(LIFTOFF_DEBUG, "Layer %p -> plane %"PRIu32": "
"applying properties...",
(void *)layer, plane->id);
if (!plane_apply(plane, layer, result->req, &compatible)) {
return false;
}
if (!compatible) {
fprintf(stderr, "Layer %p -> plane %"PRIu32": "
"incompatible properties\n",
(void *)layer, plane->id);
liftoff_log(LIFTOFF_DEBUG,
"Layer %p -> plane %"PRIu32": "
"incompatible properties",
(void *)layer, plane->id);
continue;
}
ret = drmModeAtomicCommit(display->drm_fd, result->req,
DRM_MODE_ATOMIC_TEST_ONLY, NULL);
if (ret == 0) {
fprintf(stderr, "Layer %p -> plane %"PRIu32": success\n",
(void *)layer, plane->id);
liftoff_log(LIFTOFF_DEBUG,
"Layer %p -> plane %"PRIu32": success",
(void *)layer, plane->id);
/* Continue with the next plane */
plane_step_init_next(&next_step, step, layer);
if (!output_choose_layers(output, result, &next_step)) {
@ -675,7 +685,8 @@ bool liftoff_display_apply(struct liftoff_display *display, drmModeAtomicReq *re
to hit bandwidth limits because too many planes are enabled. */
liftoff_list_for_each(plane, &display->planes, link) {
if (plane->layer == NULL) {
fprintf(stderr, "Disabling plane %d\n", plane->id);
liftoff_log(LIFTOFF_DEBUG,
"Disabling plane %d", plane->id);
if (!plane_apply(plane, NULL, req, &compatible)) {
return false;
}
@ -714,8 +725,9 @@ bool liftoff_display_apply(struct liftoff_display *display, drmModeAtomicReq *re
return false;
}
fprintf(stderr, "Found plane allocation for output %p "
"with score=%d\n", (void *)output, result.best_score);
liftoff_log(LIFTOFF_DEBUG,
"Found plane allocation for output %p with "
"score=%d", (void *)output, result.best_score);
/* Apply the best allocation */
i = 0;
@ -726,8 +738,9 @@ bool liftoff_display_apply(struct liftoff_display *display, drmModeAtomicReq *re
continue;
}
fprintf(stderr, "Assigning layer %p to plane %"PRIu32"\n",
(void *)layer, plane->id);
liftoff_log(LIFTOFF_DEBUG,
"Assigning layer %p to plane %"PRIu32,
(void *)layer, plane->id);
if (!plane_apply(plane, layer, req, &compatible)) {
return false;
}

View file

@ -1,6 +1,7 @@
#ifndef LIFTOFF_H
#define LIFTOFF_H
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
@ -49,4 +50,16 @@ void liftoff_layer_set_property(struct liftoff_layer *layer, const char *name,
*/
uint32_t liftoff_layer_get_plane_id(struct liftoff_layer *layer);
enum liftoff_log_importance {
LIFTOFF_SILENT,
LIFTOFF_DEBUG,
LIFTOFF_ERROR,
};
typedef void (*liftoff_log_func)(enum liftoff_log_importance importance,
const char *fmt, va_list args);
void liftoff_log_init(enum liftoff_log_importance verbosity,
liftoff_log_func callback);
#endif

15
include/log.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef LOG_H
#define LOG_H
#include <libliftoff.h>
#ifdef __GNUC__
#define _LIFTOFF_ATTRIB_PRINTF(start, end) __attribute__((format(printf, start, end)))
#else
#define _LIFTOFF_ATTRIB_PRINTF(start, end)
#endif
void liftoff_log(enum liftoff_log_importance verbosity,
const char *format, ...) _LIFTOFF_ATTRIB_PRINTF(2, 3);
#endif

View file

@ -1,8 +1,9 @@
#ifndef PRIVATE_H
#define PRIVATE_H
#include "libliftoff.h"
#include <libliftoff.h>
#include "list.h"
#include "log.h"
struct liftoff_display {
int drm_fd;

View file

@ -44,7 +44,8 @@ void liftoff_layer_set_property(struct liftoff_layer *layer, const char *name,
/* TODO: better error handling */
if (strcmp(name, "CRTC_ID") == 0) {
fprintf(stderr, "refusing to set a layer's CRTC_ID\n");
liftoff_log(LIFTOFF_ERROR,
"refusing to set a layer's CRTC_ID");
return;
}

33
log.c Normal file
View file

@ -0,0 +1,33 @@
#include <stdio.h>
#include "log.h"
static enum liftoff_log_importance log_importance = LIFTOFF_ERROR;
static void log_stderr(enum liftoff_log_importance verbosity, const char *fmt,
va_list args)
{
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
}
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;
}
}
void liftoff_log(enum liftoff_log_importance verbosity, const char *fmt, ...)
{
if (verbosity > log_importance) {
return;
}
va_list args;
va_start(args, fmt);
log_callback(verbosity, fmt, args);
va_end(args);
}

View file

@ -30,6 +30,7 @@ liftoff_lib = library(
'display.c',
'layer.c',
'list.c',
'log.c',
'output.c',
),
include_directories: liftoff_inc,

View file

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