Add logging mechanism

Copied from libinput

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
This commit is contained in:
Benjamin Tissoires
2015-08-14 16:09:29 -04:00
parent 5c206ff205
commit 0ed0e7a18d
3 changed files with 93 additions and 0 deletions

View File

@@ -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 */

View File

@@ -23,6 +23,7 @@
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#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="<invalid priority>"; 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;
}

View File

@@ -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