[02/12] soccr: Add logging

Submitted by Pavel Emelianov on Aug. 5, 2016, 2:59 p.m.

Details

Message ID 57A4A9C0.9070001@virtuozzo.com
State Accepted
Series "Introduce library for socket C/R"
Commit cdcf4b899be21e79e0e963caeddbee7e74e22580
Headers show

Commit Message

Pavel Emelianov Aug. 5, 2016, 2:59 p.m.
In order to get messages from libsoccr I propose to
introduce a printing callback that can be set by library
user and that will be called by libsoccr.

Signed-off-by: Pavel Emelyanov <xemul@virtuozzo.com>
---
 soccr/soccr.c | 13 +++++++++++++
 soccr/soccr.h |  9 +++++++++
 2 files changed, 22 insertions(+)

Patch hide | download patch | download mbox

diff --git a/soccr/soccr.c b/soccr/soccr.c
index f33c50c..98b8f47 100644
--- a/soccr/soccr.c
+++ b/soccr/soccr.c
@@ -1 +1,14 @@ 
 #include "soccr.h"
+
+static void (*log)(unsigned int loglevel, const char *format, ...)
+	__attribute__ ((__format__ (__printf__, 2, 3)));
+static unsigned int log_level = 0;
+
+void libsoccr_set_log(unsigned int level, void (*fn)(unsigned int level, const char *fmt, ...))
+{
+	log_level = level;
+	log = fn;
+}
+
+#define loge(msg, ...) do { if (log && (log_level >= SOCCR_LOG_ERR)) log(SOCCR_LOG_ERR, msg, ##__VA_ARGS__); } while (0)
+#define logd(msg, ...) do { if (log && (log_level >= SOCCR_LOG_DBG)) log(SOCCR_LOG_DBG, msg, ##__VA_ARGS__); } while (0)
diff --git a/soccr/soccr.h b/soccr/soccr.h
index eaae1a2..07d7947 100644
--- a/soccr/soccr.h
+++ b/soccr/soccr.h
@@ -1,3 +1,12 @@ 
 #ifndef __LIBSOCCR_H__
 #define __LIBSOCCR_H__
+#include <linux/types.h>
+
+struct libsoccr_sk;
+
+void libsoccr_set_log(unsigned int level, void (*fn)(unsigned int level, const char *fmt, ...));
+
+#define SOCCR_LOG_ERR	1
+#define SOCCR_LOG_DBG	2
+
 #endif