From ba80e320544a64c0b1f7e5a32d5330b2031189c8 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sat, 19 Oct 2019 13:35:14 +0300 Subject: [PATCH] Introduce liftoff_log_errno Like perror, but uses the liftoff logging infrastructure. --- display.c | 11 ++++++++--- include/log.h | 1 + layer.c | 3 ++- log.c | 7 +++++++ plane.c | 5 +++++ 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/display.c b/display.c index 4db975c..5dc2b76 100644 --- a/display.c +++ b/display.c @@ -18,6 +18,7 @@ struct liftoff_display *liftoff_display_create(int drm_fd) display = calloc(1, sizeof(*display)); if (display == NULL) { + liftoff_log_errno(LIFTOFF_ERROR, "calloc"); return NULL; } @@ -26,18 +27,21 @@ struct liftoff_display *liftoff_display_create(int drm_fd) display->drm_fd = dup(drm_fd); if (display->drm_fd < 0) { + liftoff_log_errno(LIFTOFF_ERROR, "dup"); liftoff_display_destroy(display); return NULL; } drm_res = drmModeGetResources(drm_fd); if (drm_res == NULL) { + liftoff_log_errno(LIFTOFF_ERROR, "drmModeGetResources"); liftoff_display_destroy(display); return NULL; } display->crtcs = malloc(drm_res->count_crtcs * sizeof(uint32_t)); if (display->crtcs == NULL) { + liftoff_log_errno(LIFTOFF_ERROR, "malloc"); drmModeFreeResources(drm_res); liftoff_display_destroy(display); return NULL; @@ -51,6 +55,7 @@ struct liftoff_display *liftoff_display_create(int drm_fd) /* TODO: allow users to choose which layers to hand over */ drm_plane_res = drmModeGetPlaneResources(drm_fd); if (drm_plane_res == NULL) { + liftoff_log_errno(LIFTOFF_ERROR, "drmModeGetPlaneResources"); liftoff_display_destroy(display); return NULL; } @@ -100,7 +105,7 @@ static bool plane_set_prop(struct liftoff_plane *plane, drmModeAtomicReq *req, prop->name, value); ret = drmModeAtomicAddProperty(req, plane->id, prop->id, value); if (ret < 0) { - perror("drmModeAtomicAddProperty"); + liftoff_log_errno(LIFTOFF_ERROR, "drmModeAtomicAddProperty"); return false; } @@ -515,7 +520,7 @@ static bool display_test_commit(struct liftoff_display *display, } else if (-ret == EINVAL || -ret == ERANGE) { *compatible = false; } else { - perror("drmModeAtomicCommit"); + liftoff_log_errno(LIFTOFF_ERROR, "drmModeAtomicCommit"); *compatible = false; return false; } @@ -753,7 +758,7 @@ bool liftoff_display_apply(struct liftoff_display *display, drmModeAtomicReq *re step.alloc = malloc(result.planes_len * sizeof(*step.alloc)); result.best = malloc(result.planes_len * sizeof(*result.best)); if (step.alloc == NULL || result.best == NULL) { - perror("malloc"); + liftoff_log_errno(LIFTOFF_ERROR, "malloc"); return false; } diff --git a/include/log.h b/include/log.h index c83a041..c53fd9d 100644 --- a/include/log.h +++ b/include/log.h @@ -11,5 +11,6 @@ void liftoff_log(enum liftoff_log_importance verbosity, const char *format, ...) _LIFTOFF_ATTRIB_PRINTF(2, 3); +void liftoff_log_errno(enum liftoff_log_importance verbosity, const char *msg); #endif diff --git a/layer.c b/layer.c index 49400ac..4ab5d28 100644 --- a/layer.c +++ b/layer.c @@ -9,6 +9,7 @@ struct liftoff_layer *liftoff_layer_create(struct liftoff_output *output) layer = calloc(1, sizeof(*layer)); if (layer == NULL) { + liftoff_log_errno(LIFTOFF_ERROR, "calloc"); return NULL; } layer->output = output; @@ -57,7 +58,7 @@ void liftoff_layer_set_property(struct liftoff_layer *layer, const char *name, props = realloc(layer->props, (layer->props_len + 1) * sizeof(struct liftoff_layer_property)); if (props == NULL) { - perror("realloc"); + liftoff_log_errno(LIFTOFF_ERROR, "realloc"); return; } layer->props = props; diff --git a/log.c b/log.c index cb9cc9c..275b571 100644 --- a/log.c +++ b/log.c @@ -1,4 +1,6 @@ +#include #include +#include #include "log.h" static enum liftoff_log_importance log_importance = LIFTOFF_ERROR; @@ -31,3 +33,8 @@ void liftoff_log(enum liftoff_log_importance verbosity, const char *fmt, ...) log_callback(verbosity, fmt, args); va_end(args); } + +void liftoff_log_errno(enum liftoff_log_importance verbosity, const char *msg) +{ + liftoff_log(verbosity, "%s: %s", msg, strerror(errno)); +} diff --git a/plane.c b/plane.c index a445dc5..2ead855 100644 --- a/plane.c +++ b/plane.c @@ -43,11 +43,13 @@ struct liftoff_plane *plane_create(struct liftoff_display *display, uint32_t id) plane = calloc(1, sizeof(*plane)); if (plane == NULL) { + liftoff_log_errno(LIFTOFF_ERROR, "calloc"); return NULL; } drm_plane = drmModeGetPlane(display->drm_fd, id); if (drm_plane == NULL) { + liftoff_log_errno(LIFTOFF_ERROR, "drmModeGetPlane"); return NULL; } plane->id = drm_plane->plane_id; @@ -57,11 +59,13 @@ struct liftoff_plane *plane_create(struct liftoff_display *display, uint32_t id) drm_props = drmModeObjectGetProperties(display->drm_fd, id, DRM_MODE_OBJECT_PLANE); if (drm_props == NULL) { + liftoff_log_errno(LIFTOFF_ERROR, "drmModeObjectGetProperties"); return NULL; } plane->props = calloc(drm_props->count_props, sizeof(struct liftoff_plane_property)); if (plane->props == NULL) { + liftoff_log_errno(LIFTOFF_ERROR, "calloc"); drmModeFreeObjectProperties(drm_props); return NULL; } @@ -69,6 +73,7 @@ struct liftoff_plane *plane_create(struct liftoff_display *display, uint32_t id) drm_prop = drmModeGetProperty(display->drm_fd, drm_props->props[i]); if (drm_prop == NULL) { + liftoff_log_errno(LIFTOFF_ERROR, "drmModeGetProperty"); drmModeFreeObjectProperties(drm_props); return NULL; }