libwbclient: Add debugging hooks.
authorKai Blin <kai@samba.org>
Mon, 1 Jun 2009 21:33:27 +0000 (23:33 +0200)
committerKai Blin <kai@samba.org>
Sat, 13 Jun 2009 07:39:01 +0000 (09:39 +0200)
nsswitch/libwbclient/wbc_async.c
nsswitch/libwbclient/wbc_async.h

index fb8d8b102d494091bc0b8d384a9cf405a033c9af..181d5463e94c34d91f5c7b78d1788ff4eb10e921 100644 (file)
@@ -82,11 +82,18 @@ wbcErr tevent_req_simple_recv_wbcerr(struct tevent_req *req)
        return WBC_ERR_SUCCESS;
 }
 
+struct wbc_debug_ops {
+       void (*debug)(void *context, enum wbcDebugLevel level,
+                     const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
+       void *context;
+};
+
 struct wb_context {
        struct tevent_queue *queue;
        int fd;
        bool is_priv;
        const char *dir;
+       struct wbc_debug_ops debug_ops;
 };
 
 static int make_nonstd_fd(int fd)
@@ -697,3 +704,71 @@ wbcErr wb_trans_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
        *presponse = talloc_move(mem_ctx, &state->wb_resp);
        return WBC_ERR_SUCCESS;
 }
+
+/********************************************************************
+ * Debug wrapper functions, modeled (with lot's of code copied as is)
+ * after the tevent debug wrapper functions
+ ********************************************************************/
+
+/*
+  this allows the user to choose their own debug function
+*/
+int wbcSetDebug(struct wb_context *wb_ctx,
+               void (*debug)(void *context,
+                             enum wbcDebugLevel level,
+                             const char *fmt,
+                             va_list ap) PRINTF_ATTRIBUTE(3,0),
+               void *context)
+{
+       wb_ctx->debug_ops.debug = debug;
+       wb_ctx->debug_ops.context = context;
+       return 0;
+}
+
+/*
+  debug function for wbcSetDebugStderr
+*/
+static void wbcDebugStderr(void *private_data,
+                          enum wbcDebugLevel level,
+                          const char *fmt,
+                          va_list ap) PRINTF_ATTRIBUTE(3,0);
+static void wbcDebugStderr(void *private_data,
+                          enum wbcDebugLevel level,
+                          const char *fmt, va_list ap)
+{
+       if (level <= WBC_DEBUG_WARNING) {
+               vfprintf(stderr, fmt, ap);
+       }
+}
+
+/*
+  convenience function to setup debug messages on stderr
+  messages of level WBC_DEBUG_WARNING and higher are printed
+*/
+int wbcSetDebugStderr(struct wb_context *wb_ctx)
+{
+       return wbcSetDebug(wb_ctx, wbcDebugStderr, wb_ctx);
+}
+
+/*
+ * log a message
+ *
+ * The default debug action is to ignore debugging messages.
+ * This is the most appropriate action for a library.
+ * Applications using the library must decide where to
+ * redirect debugging messages
+*/
+void wbcDebug(struct wb_context *wb_ctx, enum wbcDebugLevel level,
+             const char *fmt, ...)
+{
+       va_list ap;
+       if (!wb_ctx) {
+               return;
+       }
+       if (wb_ctx->debug_ops.debug == NULL) {
+               return;
+       }
+       va_start(ap, fmt);
+       wb_ctx->debug_ops.debug(wb_ctx->debug_ops.context, level, fmt, ap);
+       va_end(ap);
+}
index 607dd9de282dc4d1588c2c27f59fbc3f514a1be4..76e02cadf227afce1ca506ec70bc8bba3341674e 100644 (file)
@@ -32,6 +32,13 @@ struct wb_context;
 struct winbindd_request;
 struct winbindd_response;
 
+enum wbcDebugLevel {
+       WBC_DEBUG_FATAL,
+       WBC_DEBUG_ERROR,
+       WBC_DEBUG_WARNING,
+       WBC_DEBUG_TRACE
+};
+
 struct tevent_req *wb_trans_send(TALLOC_CTX *mem_ctx,
                                 struct tevent_context *ev,
                                 struct wb_context *wb_ctx, bool need_priv,
@@ -39,6 +46,15 @@ struct tevent_req *wb_trans_send(TALLOC_CTX *mem_ctx,
 wbcErr wb_trans_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
                     struct winbindd_response **presponse);
 struct wb_context *wb_context_init(TALLOC_CTX *mem_ctx, const char* dir);
+int wbcSetDebug(struct wb_context *wb_ctx,
+               void (*debug)(void *context,
+                             enum wbcDebugLevel level,
+                             const char *fmt,
+                             va_list ap) PRINTF_ATTRIBUTE(3,0),
+               void *context);
+int wbcSetDebugStderr(struct wb_context *wb_ctx);
+void wbcDebug(struct wb_context *wb_ctx, enum wbcDebugLevel level,
+             const char *fmt, ...) PRINTF_ATTRIBUTE(3,0);
 
 /* Definitions from wb_reqtrans.c */
 wbcErr map_wbc_err_from_errno(int error);