Re: [PATCH libinput] Add a customizable log handler
On Fri, Feb 14, 2014 at 10:16:02AM +1000, Peter Hutterer wrote: The previous log handler wasn't actually hooked up to anything. Add a public API for the log handler with priority filtering, defaulting to priority 'error' and stderr as output stream. And to keep the diff down and convenience up, provide a few simple wrappers for logging. The generic is log_msg(), but let's use log_info, log_error, etc. Signed-off-by: Peter Hutterer peter.hutte...@who-t.net Looks good to me. Reviewed-by: Jonas Ådahl jad...@gmail.com --- src/libinput-private.h | 7 ++ src/libinput-util.c| 20 -- src/libinput.c | 62 ++ src/libinput.h | 76 ++ src/path.c | 4 +- test/Makefile.am | 7 +- test/log.c | 169 + 7 files changed, 322 insertions(+), 23 deletions(-) create mode 100644 test/log.c diff --git a/src/libinput-private.h b/src/libinput-private.h index 0d7de90..1fff7de 100644 --- a/src/libinput-private.h +++ b/src/libinput-private.h @@ -74,6 +74,13 @@ typedef void (*libinput_source_dispatch_t)(void *data); struct libinput_source; +#define log_debug(...) log_msg(LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__) +#define log_info(...) log_msg(LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__) +#define log_error(...) log_msg(LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__) + +void +log_msg(enum libinput_log_priority priority, const char *format, ...); + int libinput_init(struct libinput *libinput, const struct libinput_interface *interface, diff --git a/src/libinput-util.c b/src/libinput-util.c index a3534e1..eeb9786 100644 --- a/src/libinput-util.c +++ b/src/libinput-util.c @@ -35,26 +35,6 @@ #include libinput-util.h #include libinput-private.h -static FILE *g_log_file = NULL; - -void -set_logging_enabled(int enabled) -{ - g_log_file = enabled ? stdout : NULL; -} - -void -log_info(const char *format, ...) -{ - va_list ap; - - if (g_log_file) { - va_start(ap, format); - vfprintf(g_log_file, format, ap); - va_end(ap); - } -} - void list_init(struct list *list) { diff --git a/src/libinput.c b/src/libinput.c index cfce2c5..b4879af 100644 --- a/src/libinput.c +++ b/src/libinput.c @@ -78,6 +78,68 @@ struct libinput_event_touch { }; static void +libinput_default_log_func(enum libinput_log_priority priority, + void *data, + const char *format, va_list args) +{ + const char *prefix; + + switch(priority) { + case LIBINPUT_LOG_PRIORITY_DEBUG: prefix = debug; break; + case LIBINPUT_LOG_PRIORITY_INFO: prefix = info; break; + case LIBINPUT_LOG_PRIORITY_ERROR: prefix = error; break; + default: prefix=invalid priority; break; + } + + fprintf(stderr, libinput %s: , prefix); + vfprintf(stderr, format, args); +} + +struct log_data { + enum libinput_log_priority priority; + libinput_log_handler handler; + void *user_data; +}; + +static struct log_data log_data = { + .priority = LIBINPUT_LOG_PRIORITY_ERROR, + .handler = libinput_default_log_func, + .user_data = NULL, +}; + +void +log_msg(enum libinput_log_priority priority, const char *format, ...) +{ + va_list args; + + if (log_data.handler log_data.priority = priority) { + va_start(args, format); + log_data.handler(priority, log_data.user_data, format, args); + va_end(args); + } +} + +LIBINPUT_EXPORT void +libinput_log_set_priority(enum libinput_log_priority priority) +{ + log_data.priority = priority; +} + +LIBINPUT_EXPORT enum libinput_log_priority +libinput_log_get_priority(void) +{ + return log_data.priority; +} + +LIBINPUT_EXPORT void +libinput_log_set_handler(libinput_log_handler log_handler, + void *user_data) +{ + log_data.handler = log_handler; + log_data.user_data = user_data; +} + +static void libinput_post_event(struct libinput *libinput, struct libinput_event *event); diff --git a/src/libinput.h b/src/libinput.h index e87b2b7..6bf538a 100644 --- a/src/libinput.h +++ b/src/libinput.h @@ -42,6 +42,15 @@ typedef int32_t li_fixed_t; /** + * Log priority for internal logging messages. + */ +enum libinput_log_priority { + LIBINPUT_LOG_PRIORITY_DEBUG = 10, + LIBINPUT_LOG_PRIORITY_INFO = 20, + LIBINPUT_LOG_PRIORITY_ERROR = 30, +}; + +/** * @ingroup device * * Capabilities on a device. A device may have one or more capabilities @@ -875,6 +884,73 @@ void libinput_destroy(struct libinput *libinput); /** + * @ingroup base + * + * Set the global log priority. Messages with priorities equal to or + * higher than the argument will be printed to the current log handler. + * + * The default
[PATCH libinput] Add a customizable log handler
The previous log handler wasn't actually hooked up to anything. Add a public API for the log handler with priority filtering, defaulting to priority 'error' and stderr as output stream. And to keep the diff down and convenience up, provide a few simple wrappers for logging. The generic is log_msg(), but let's use log_info, log_error, etc. Signed-off-by: Peter Hutterer peter.hutte...@who-t.net --- src/libinput-private.h | 7 ++ src/libinput-util.c| 20 -- src/libinput.c | 62 ++ src/libinput.h | 76 ++ src/path.c | 4 +- test/Makefile.am | 7 +- test/log.c | 169 + 7 files changed, 322 insertions(+), 23 deletions(-) create mode 100644 test/log.c diff --git a/src/libinput-private.h b/src/libinput-private.h index 0d7de90..1fff7de 100644 --- a/src/libinput-private.h +++ b/src/libinput-private.h @@ -74,6 +74,13 @@ typedef void (*libinput_source_dispatch_t)(void *data); struct libinput_source; +#define log_debug(...) log_msg(LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__) +#define log_info(...) log_msg(LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__) +#define log_error(...) log_msg(LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__) + +void +log_msg(enum libinput_log_priority priority, const char *format, ...); + int libinput_init(struct libinput *libinput, const struct libinput_interface *interface, diff --git a/src/libinput-util.c b/src/libinput-util.c index a3534e1..eeb9786 100644 --- a/src/libinput-util.c +++ b/src/libinput-util.c @@ -35,26 +35,6 @@ #include libinput-util.h #include libinput-private.h -static FILE *g_log_file = NULL; - -void -set_logging_enabled(int enabled) -{ - g_log_file = enabled ? stdout : NULL; -} - -void -log_info(const char *format, ...) -{ - va_list ap; - - if (g_log_file) { - va_start(ap, format); - vfprintf(g_log_file, format, ap); - va_end(ap); - } -} - void list_init(struct list *list) { diff --git a/src/libinput.c b/src/libinput.c index cfce2c5..b4879af 100644 --- a/src/libinput.c +++ b/src/libinput.c @@ -78,6 +78,68 @@ struct libinput_event_touch { }; static void +libinput_default_log_func(enum libinput_log_priority priority, + void *data, + const char *format, va_list args) +{ + const char *prefix; + + switch(priority) { + case LIBINPUT_LOG_PRIORITY_DEBUG: prefix = debug; break; + case LIBINPUT_LOG_PRIORITY_INFO: prefix = info; break; + case LIBINPUT_LOG_PRIORITY_ERROR: prefix = error; break; + default: prefix=invalid priority; break; + } + + fprintf(stderr, libinput %s: , prefix); + vfprintf(stderr, format, args); +} + +struct log_data { + enum libinput_log_priority priority; + libinput_log_handler handler; + void *user_data; +}; + +static struct log_data log_data = { + .priority = LIBINPUT_LOG_PRIORITY_ERROR, + .handler = libinput_default_log_func, + .user_data = NULL, +}; + +void +log_msg(enum libinput_log_priority priority, const char *format, ...) +{ + va_list args; + + if (log_data.handler log_data.priority = priority) { + va_start(args, format); + log_data.handler(priority, log_data.user_data, format, args); + va_end(args); + } +} + +LIBINPUT_EXPORT void +libinput_log_set_priority(enum libinput_log_priority priority) +{ + log_data.priority = priority; +} + +LIBINPUT_EXPORT enum libinput_log_priority +libinput_log_get_priority(void) +{ + return log_data.priority; +} + +LIBINPUT_EXPORT void +libinput_log_set_handler(libinput_log_handler log_handler, +void *user_data) +{ + log_data.handler = log_handler; + log_data.user_data = user_data; +} + +static void libinput_post_event(struct libinput *libinput, struct libinput_event *event); diff --git a/src/libinput.h b/src/libinput.h index e87b2b7..6bf538a 100644 --- a/src/libinput.h +++ b/src/libinput.h @@ -42,6 +42,15 @@ typedef int32_t li_fixed_t; /** + * Log priority for internal logging messages. + */ +enum libinput_log_priority { + LIBINPUT_LOG_PRIORITY_DEBUG = 10, + LIBINPUT_LOG_PRIORITY_INFO = 20, + LIBINPUT_LOG_PRIORITY_ERROR = 30, +}; + +/** * @ingroup device * * Capabilities on a device. A device may have one or more capabilities @@ -875,6 +884,73 @@ void libinput_destroy(struct libinput *libinput); /** + * @ingroup base + * + * Set the global log priority. Messages with priorities equal to or + * higher than the argument will be printed to the current log handler. + * + * The default log priority is LIBINPUT_LOG_PRIORITY_ERROR. + * + * @param priority The minimum priority of log messages to print. + * + * @see libinput_log_set_handler + */ +void +libinput_log_set_priority(enum libinput_log_priority