r513: added a generic ldb debug system to allow the Samba debug functions to
authorAndrew Tridgell <tridge@samba.org>
Thu, 6 May 2004 09:55:05 +0000 (09:55 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 17:51:46 +0000 (12:51 -0500)
be cleanly interfaced to ldb
(This used to be commit 74b89d5f960d6b936751e3f057b4540eb80b79cd)

14 files changed:
source4/lib/ldb/Makefile.ldb
source4/lib/ldb/common/ldb.c
source4/lib/ldb/common/ldb_debug.c [new file with mode: 0644]
source4/lib/ldb/common/ldb_ldif.c
source4/lib/ldb/config.m4
source4/lib/ldb/include/ldb.h
source4/lib/ldb/ldb_ldap/ldb_ldap.c
source4/lib/ldb/tools/ldbadd.c
source4/lib/ldb/tools/ldbdel.c
source4/lib/ldb/tools/ldbedit.c
source4/lib/ldb/tools/ldbmodify.c
source4/lib/ldb/tools/ldbsearch.c
source4/lib/ldb/tools/ldbtest.c
source4/rpc_server/samr/samdb.c

index 0a0b95f7fe0b650fe4922195fed7585c170ce044..1623b74f6c60a0ef07a30a268af4e21a915113f4 100644 (file)
@@ -23,7 +23,7 @@ LDB_TDB_OBJ=ldb_tdb/ldb_match.o ldb_tdb/ldb_tdb.o \
 
 COMMON_OBJ=common/ldb.o common/ldb_ldif.o common/util.o \
           common/ldb_parse.o common/ldb_msg.o common/ldb_utf8.o \
-          common/ldb_alloc.o
+          common/ldb_alloc.o common/ldb_debug.o
 
 OBJS =  $(COMMON_OBJ) $(LDB_TDB_OBJ) $(TDB_OBJ) $(LDB_LDAP_OBJ)
 
index 86d0dd9e9bf1752bc82a72815e4afb9e71b20393..b8f61e017ac5193809618ebbb87f78f50bf5c3a0 100644 (file)
@@ -68,6 +68,7 @@ struct ldb_context *ldb_connect(const char *url, unsigned int flags,
 */
 int ldb_close(struct ldb_context *ldb)
 {
+       ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_close");
        return ldb->ops->close(ldb);
 }
 
@@ -83,7 +84,12 @@ int ldb_search(struct ldb_context *ldb,
               const char *expression,
               char * const *attrs, struct ldb_message ***res)
 {
-       return ldb->ops->search(ldb, base, scope, expression, attrs, res);
+       int ret;
+       ret = ldb->ops->search(ldb, base, scope, expression, attrs, res);
+
+       ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_search(%s) -> %d records\n", 
+                 expression, ret);
+       return ret;
 }
 
 /* 
@@ -102,7 +108,10 @@ int ldb_search_free(struct ldb_context *ldb, struct ldb_message **msgs)
 int ldb_add(struct ldb_context *ldb, 
            const struct ldb_message *message)
 {
-       return ldb->ops->add_record(ldb, message);
+       int ret;
+       ret = ldb->ops->add_record(ldb, message);
+       ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_add(%s) -> %d\n", message->dn, ret);
+       return ret;
 }
 
 /*
@@ -111,7 +120,11 @@ int ldb_add(struct ldb_context *ldb,
 int ldb_modify(struct ldb_context *ldb, 
               const struct ldb_message *message)
 {
-       return ldb->ops->modify_record(ldb, message);
+       int ret;
+       ret = ldb->ops->modify_record(ldb, message);
+       ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_modify(%s) -> %d\n", 
+                 message->dn, ret);
+       return ret;
 }
 
 
@@ -120,7 +133,10 @@ int ldb_modify(struct ldb_context *ldb,
 */
 int ldb_delete(struct ldb_context *ldb, const char *dn)
 {
-       return ldb->ops->delete_record(ldb, dn);
+       int ret;
+       ret = ldb->ops->delete_record(ldb, dn);
+       ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_delete(%s) -> %d\n", dn, ret);
+       return ret;
 }
 
 /*
diff --git a/source4/lib/ldb/common/ldb_debug.c b/source4/lib/ldb/common/ldb_debug.c
new file mode 100644 (file)
index 0000000..d59f928
--- /dev/null
@@ -0,0 +1,84 @@
+/* 
+   ldb database library
+
+   Copyright (C) Andrew Tridgell  2004
+
+     ** NOTE! The following LGPL license applies to the ldb
+     ** library. This does NOT imply that all of Samba is released
+     ** under the LGPL
+   
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
+/*
+ *  Name: ldb
+ *
+ *  Component: ldb debug
+ *
+ *  Description: functions for printing debug messages
+ *
+ *  Author: Andrew Tridgell
+ */
+
+#include "includes.h"
+
+
+/*
+  this allows the user to choose their own debug function
+*/
+int ldb_set_debug(struct ldb_context *ldb,
+                 void (*debug)(void *context, enum ldb_debug_level level, 
+                               const char *fmt, va_list ap),
+                 void *context)
+{
+       ldb->debug_ops.debug = debug;
+       ldb->debug_ops.context = context;
+       return 0;
+}
+
+/*
+  debug function for ldb_set_debug_stderr
+*/
+static void ldb_debug_stderr(void *context, enum ldb_debug_level level, 
+                            const char *fmt, va_list ap)
+{
+       if (level <= LDB_DEBUG_WARNING) {
+               vfprintf(stderr, fmt, ap);
+       }
+}
+
+/*
+  convenience function to setup debug messages on stderr
+  messages of level LDB_DEBUG_WARNING and higher are printed
+*/
+int ldb_set_debug_stderr(struct ldb_context *ldb)
+{
+       return ldb_set_debug(ldb, ldb_debug_stderr, ldb);
+}
+
+/*
+  log a message
+*/
+void ldb_debug(struct ldb_context *ldb, enum ldb_debug_level level, const char *fmt, ...)
+{
+       va_list ap;
+       if (ldb->debug_ops.debug == NULL) {
+               return;
+       }
+       va_start(ap, fmt);
+       ldb->debug_ops.debug(ldb->debug_ops.context, level, fmt, ap);
+       va_end(ap);
+}
+
index 451276c48d72e30b396c93fd55c2520675cee225..513e2dd3655171df230011257fc0fc8467bf3d19 100644 (file)
@@ -217,7 +217,8 @@ int ldif_write(struct ldb_context *ldb,
                        }
                }
                if (!ldb_changetypes[i].name) {
-                       fprintf(stderr,"Invalid changetype\n");
+                       ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d\n",
+                                 ldif->changetype);
                        return -1;
                }
                ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
@@ -493,7 +494,8 @@ struct ldb_ldif *ldif_read(struct ldb_context *ldb,
        
        /* first line must be a dn */
        if (ldb_attr_cmp(attr, "dn") != 0) {
-               fprintf(stderr, "First line must be a dn not '%s'\n", attr);
+               ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'\n", 
+                         attr);
                goto failed;
        }
 
@@ -512,8 +514,8 @@ struct ldb_ldif *ldif_read(struct ldb_context *ldb,
                                }
                        }
                        if (!ldb_changetypes[i].name) {
-                               fprintf(stderr,"Bad changetype '%s'\n",
-                                       (char *)value.data);
+                               ldb_debug(ldb, LDB_DEBUG_ERROR, 
+                                         "Error: Bad ldif changetype '%s'\n",(char *)value.data);
                        }
                        flags = 0;
                        continue;
index 975da5a4a625a730c8a90f67c398a2cec0ce9dab..f450acce00b1f7aa6315643437bbd8986cbe7603 100644 (file)
@@ -9,6 +9,7 @@ SMB_SUBSYSTEM(LIBLDB,[lib/ldb/common/ldb.o],
                lib/ldb/common/util.o \
                lib/ldb/common/ldb_utf8.o \
                lib/ldb/common/ldb_alloc.o \
+               lib/ldb/common/ldb_debug.o \
                lib/ldb/ldb_tdb/ldb_search.o \
                lib/ldb/ldb_tdb/ldb_tdb.o \
                lib/ldb/ldb_tdb/ldb_pack.o \
index 852389d415b043b678cefb18011e3383fe3cd648..adb6c31952699834aa2e1ad07d7ffb4f3e4303a6 100644 (file)
@@ -152,6 +152,21 @@ struct ldb_alloc_ops {
        void *context;
 };
 
+/* debugging uses one of the following levels */
+enum ldb_debug_level {LDB_DEBUG_FATAL, LDB_DEBUG_ERROR, 
+                     LDB_DEBUG_WARNING, LDB_DEBUG_TRACE};
+
+/*
+  the user can optionally supply a debug function. The function
+  is based on the vfprintf() style of interface, but with the addition
+  of a severity level
+*/
+struct ldb_debug_ops {
+       void (*debug)(void *context, enum ldb_debug_level level, 
+                     const char *fmt, va_list ap);
+       void *context;
+};
+
 
 /*
   every ldb connection is started by establishing a ldb_context
@@ -165,6 +180,9 @@ struct ldb_context {
 
        /* memory allocation info */
        struct ldb_alloc_ops alloc_ops;
+
+       /* memory allocation info */
+       struct ldb_debug_ops debug_ops;
 };
 
 
@@ -308,6 +326,17 @@ int ldb_set_alloc(struct ldb_context *ldb,
                  void *(*alloc)(void *context, void *ptr, size_t size),
                  void *context);
 
+/*
+  this allows the user to set a debug function for error reporting
+*/
+int ldb_set_debug(struct ldb_context *ldb,
+                 void (*debug)(void *context, enum ldb_debug_level level, 
+                               const char *fmt, va_list ap),
+                 void *context);
+
+/* this sets up debug to print messages on stderr */
+int ldb_set_debug_stderr(struct ldb_context *ldb);
+
 
 /* these are used as type safe versions of the ldb allocation functions */
 #define ldb_malloc_p(ldb, type) (type *)ldb_malloc(ldb, sizeof(type))
index 7e7d047f2518d6edc18f0bee5244c5c2f3c70ab9..7e959e7854f3fbbe5935dd8a3b5522b10134519e 100644 (file)
@@ -248,7 +248,7 @@ static int lldb_search(struct ldb_context *ldb, const char *base,
 
                if (msg_count == count) {
                        /* hmm, got too many? */
-                       fprintf(stderr,"Too many messages?!\n");
+                       ldb_debug(ldb, LDB_DEBUG_FATAL, "Fatal: ldap message count inconsistent\n");
                        break;
                }
 
index 6d89f67e0fed96af921e0b9c1f0004c811ab23cb..a45021c1d965bf3363fc2c2cb022c9cb1cfd1a04 100644 (file)
@@ -115,6 +115,8 @@ static int process_file(struct ldb_context *ldb, FILE *f)
                exit(1);
        }
 
+       ldb_set_debug_stderr(ldb);
+
        if (argc == 0) {
                usage();
        }
index 48ee07ad256e125846516b88b71bee8f23948501..880713b25a3a023a8d7b0765bef2eceb7d41af96 100644 (file)
@@ -85,6 +85,8 @@ static void usage(void)
                exit(1);
        }
 
+       ldb_set_debug_stderr(ldb);
+
        for (i=0;i<argc;i++) {
                ret = ldb_delete(ldb, argv[i]);
                if (ret != 0) {
index 57c54cad405b81b1b69f271e304b83ca3ef53624..739c3b6301571300ba21e97ab6d379caa4506e5c 100644 (file)
@@ -351,6 +351,8 @@ static void usage(void)
                exit(1);
        }
 
+       ldb_set_debug_stderr(ldb);
+
        ret = ldb_search(ldb, basedn, scope, expression, NULL, &msgs);
 
        if (ret == -1) {
index 6ac8e366c779278472f6719233f58da2625c886c..9f7cbe4527bcfa8651fcd7245d396cef26356050 100644 (file)
@@ -117,6 +117,8 @@ static int process_file(struct ldb_context *ldb, FILE *f)
                exit(1);
        }
 
+       ldb_set_debug_stderr(ldb);
+
        if (argc == 0) {
                usage();
                exit(1);
index edda31b7937e9d244336a5a964b485e0777e50fd..f80f81b50e8fc82b481cf3f45212d853de729e85 100644 (file)
@@ -149,6 +149,8 @@ static int do_search(struct ldb_context *ldb,
                exit(1);
        }
 
+       ldb_set_debug_stderr(ldb);
+
        if (interactive) {
                char line[1024];
                while (fgets(line, sizeof(line), stdin)) {
index e7db7673875df16e08b0caf82608cd528fb6a7a2..fc224115f57fe64a9db3b4c4d7cec2ea3a0bde8b 100644 (file)
@@ -319,6 +319,8 @@ static void usage(void)
                exit(1);
        }
 
+       ldb_set_debug_stderr(ldb);
+
        srandom(1);
 
        start_test(ldb, nrecords, nsearches);
index c4909caeb00857d1392b1f2468be620c01ea8473..c0e0d12a7bd76519991fe725fb3141b59a2d9f8e 100644 (file)
 */
 static struct ldb_context *sam_db;
 
+/*
+  this is used to catch debug messages from ldb
+*/
+void samdb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
+{
+       char *s = NULL;
+       if (DEBUGLEVEL < 4 && level > LDB_DEBUG_WARNING) {
+               return;
+       }
+       vasprintf(&s, fmt, ap);
+       if (!s) return;
+       DEBUG(level, ("samdb: %s\n", s));
+       free(s);
+}
+
 /*
   connect to the SAM database
   return 0 on success, -1 on failure
@@ -47,6 +62,8 @@ int samdb_connect(void)
                return -1;
        }
 
+       ldb_set_debug(sam_db, samdb_debug, NULL);
+
        return 0;
 }