[PATCH libevdev 8/9] Log to stdout by default

Peter Hutterer peter.hutterer at who-t.net
Tue Sep 3 00:07:46 PDT 2013


The logging we do use atm inside the library is largely
to spot application errors. Log that to stdout by default so
it doesn't get lost.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 libevdev/libevdev.c | 30 +++++++++++++++++++++++++++---
 libevdev/libevdev.h |  4 ++--
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/libevdev/libevdev.c b/libevdev/libevdev.c
index dcf2beb..b121eb2 100644
--- a/libevdev/libevdev.c
+++ b/libevdev/libevdev.c
@@ -49,11 +49,32 @@ init_event_queue(struct libevdev *dev)
 }
 
 static void
-libevdev_noop_log_func(enum libevdev_log_priority priority,
+libevdev_dflt_log_func(enum libevdev_log_priority priority,
 		       void *data,
 		       const char *file, int line, const char *func,
 		       const char *format, va_list args)
 {
+	const char *prefix;
+	switch(priority) {
+		case LIBEVDEV_LOG_ERROR: prefix = "libevdev error"; break;
+		case LIBEVDEV_LOG_INFO: prefix = "libevdev info"; break;
+		case LIBEVDEV_LOG_DEBUG:
+					prefix = "libevdev debug";
+					break;
+		default:
+					break;
+	}
+	/* default logging format:
+	   libevev error in libevdev_some_func: blah blah
+	   libevev info in libevdev_some_func: blah blah
+	   libevev debug in file.c:123:libevdev_some_func: blah blah
+	 */
+
+	printf("%s in ", prefix);
+	if (priority == LIBEVDEV_LOG_DEBUG)
+		printf("%s:%d:", file, line);
+	printf("%s: ", func);
+	vprintf(format, args);
 }
 
 /*
@@ -61,7 +82,7 @@ libevdev_noop_log_func(enum libevdev_log_priority priority,
  */
 struct logdata log_data = {
 	LIBEVDEV_LOG_INFO,
-	libevdev_noop_log_func,
+	libevdev_dflt_log_func,
 	NULL,
 };
 
@@ -73,6 +94,9 @@ log_msg(enum libevdev_log_priority priority,
 {
 	va_list args;
 
+	if (!log_data.handler)
+		return;
+
 	va_start(args, format);
 	log_data.handler(priority, data, file, line, func, format, args);
 	va_end(args);
@@ -137,7 +161,7 @@ libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
 LIBEVDEV_EXPORT void
 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
 {
-	log_data.handler = logfunc ? logfunc : libevdev_noop_log_func;
+	log_data.handler = logfunc;
 	log_data.userdata = data;
 }
 
diff --git a/libevdev/libevdev.h b/libevdev/libevdev.h
index 36ecdd1..844a1eb 100644
--- a/libevdev/libevdev.h
+++ b/libevdev/libevdev.h
@@ -377,10 +377,10 @@ typedef void (*libevdev_log_func_t)(enum libevdev_log_priority priority,
 
 /**
  * Set a printf-style logging handler for library-internal logging. The default
- * logging function is a noop.
+ * logging function is to stdout.
  *
  * @param logfunc The logging function for this device. If NULL, the current
- * logging function is unset.
+ * logging function is unset and no logging is performed.
  * @param data User-specific data passed to the log handler.
  *
  * @note This function may be called before libevdev_set_fd().
-- 
1.8.2.1



More information about the Input-tools mailing list