From 0ed0e7a18dd086540b455bdbfbd218460f131f20 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Fri, 14 Aug 2015 16:09:29 -0400 Subject: [PATCH] Add logging mechanism Copied from libinput Signed-off-by: Benjamin Tissoires --- src/libratbag-private.h | 17 +++++++++++++++ src/libratbag.c | 47 +++++++++++++++++++++++++++++++++++++++++ src/libratbag.h | 29 +++++++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/src/libratbag-private.h b/src/libratbag-private.h index d14b21c3..2069625f 100644 --- a/src/libratbag-private.h +++ b/src/libratbag-private.h @@ -25,6 +25,23 @@ #define LIBRATBAG_PRIVATE_H #include "libratbag.h" +void ratbag_device_init(struct ratbag *rb, int fd); + +void +log_msg_va(struct libratbag *libratbag, + enum libratbag_log_priority priority, + const char *format, + va_list args); +void log_msg(struct libratbag *libratbag, + enum libratbag_log_priority priority, + const char *format, ...); + +#define log_debug(li_, ...) log_msg((li_), LIBRATBAG_LOG_PRIORITY_DEBUG, __VA_ARGS__) +#define log_info(li_, ...) log_msg((li_), LIBRATBAG_LOG_PRIORITY_INFO, __VA_ARGS__) +#define log_error(li_, ...) log_msg((li_), LIBRATBAG_LOG_PRIORITY_ERROR, __VA_ARGS__) +#define log_bug_kernel(li_, ...) log_msg((li_), LIBRATBAG_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__) +#define log_bug_libratbag(li_, ...) log_msg((li_), LIBRATBAG_LOG_PRIORITY_ERROR, "libratbag bug: " __VA_ARGS__) +#define log_bug_client(li_, ...) log_msg((li_), LIBRATBAG_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__) #endif /* LIBRATBAG_PRIVATE_H */ diff --git a/src/libratbag.c b/src/libratbag.c index 106aa4fc..de3620f3 100644 --- a/src/libratbag.c +++ b/src/libratbag.c @@ -23,6 +23,7 @@ #include "config.h" #include +#include #include #include "libratbag-private.h" @@ -30,8 +31,51 @@ struct libratbag { int refcount; + libratbag_log_handler log_handler; + enum libratbag_log_priority log_priority; }; +static void +libratbag_default_log_func(struct libratbag *libratbag, + enum libratbag_log_priority priority, + const char *format, va_list args) +{ + const char *prefix; + + switch(priority) { + case LIBRATBAG_LOG_PRIORITY_DEBUG: prefix = "debug"; break; + case LIBRATBAG_LOG_PRIORITY_INFO: prefix = "info"; break; + case LIBRATBAG_LOG_PRIORITY_ERROR: prefix = "error"; break; + default: prefix=""; break; + } + + fprintf(stderr, "libratbag %s: ", prefix); + vfprintf(stderr, format, args); +} + +void +log_msg_va(struct libratbag *libratbag, + enum libratbag_log_priority priority, + const char *format, + va_list args) +{ + if (libratbag->log_handler && + libratbag->log_priority <= priority) + libratbag->log_handler(libratbag, priority, format, args); +} + +void +log_msg(struct libratbag *libratbag, + enum libratbag_log_priority priority, + const char *format, ...) +{ + va_list args; + + va_start(args, format); + log_msg_va(libratbag, priority, format, args); + va_end(args); +} + LIBRATBAG_EXPORT struct libratbag * libratbag_create_context(void) { @@ -43,6 +87,9 @@ libratbag_create_context(void) libratbag->refcount = 1; + libratbag->log_handler = libratbag_default_log_func; + libratbag->log_priority = LIBRATBAG_LOG_PRIORITY_DEBUG; + return libratbag; } diff --git a/src/libratbag.h b/src/libratbag.h index 320599ed..e8ce75be 100644 --- a/src/libratbag.h +++ b/src/libratbag.h @@ -35,6 +35,15 @@ extern "C" { __attribute__ ((format (printf, _format, _args))) #define LIBRATBAG_ATTRIBUTE_DEPRECATED __attribute__ ((deprecated)) +/** + * Log priority for internal logging messages. + */ +enum libratbag_log_priority { + LIBRATBAG_LOG_PRIORITY_DEBUG = 10, + LIBRATBAG_LOG_PRIORITY_INFO = 20, + LIBRATBAG_LOG_PRIORITY_ERROR = 30, +}; + /** * @ingroup base * @struct libratbag @@ -44,6 +53,26 @@ extern "C" { */ struct libratbag; +/** + * @ingroup base + * + * Log handler type for custom logging. + * + * @param libratbag The libratbag context + * @param priority The priority of the current message + * @param format Message format in printf-style + * @param args Message arguments + * + * @see libratbag_log_set_priority + * @see libratbag_log_get_priority + * @see libratbag_log_set_handler + */ +typedef void (*libratbag_log_handler)(struct libratbag *libratbag, + enum libratbag_log_priority priority, + const char *format, va_list args) + LIBRATBAG_ATTRIBUTE_PRINTF(3, 0); + + /** * @defgroup base Initialization and manipulation of libratbag contexts * @defgroup device Querying and manipulating devices