From 68293565de0b799dcc51e001dabf53adf88ee7ad Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 6 May 2004 09:55:05 +0000 Subject: r513: added a generic ldb debug system to allow the Samba debug functions to be cleanly interfaced to ldb (This used to be commit 74b89d5f960d6b936751e3f057b4540eb80b79cd) --- source4/lib/ldb/Makefile.ldb | 2 +- source4/lib/ldb/common/ldb.c | 24 +++++++++-- source4/lib/ldb/common/ldb_debug.c | 84 +++++++++++++++++++++++++++++++++++++ source4/lib/ldb/common/ldb_ldif.c | 10 +++-- source4/lib/ldb/config.m4 | 1 + source4/lib/ldb/include/ldb.h | 29 +++++++++++++ source4/lib/ldb/ldb_ldap/ldb_ldap.c | 2 +- source4/lib/ldb/tools/ldbadd.c | 2 + source4/lib/ldb/tools/ldbdel.c | 2 + source4/lib/ldb/tools/ldbedit.c | 2 + source4/lib/ldb/tools/ldbmodify.c | 2 + source4/lib/ldb/tools/ldbsearch.c | 2 + source4/lib/ldb/tools/ldbtest.c | 2 + source4/rpc_server/samr/samdb.c | 17 ++++++++ 14 files changed, 171 insertions(+), 10 deletions(-) create mode 100644 source4/lib/ldb/common/ldb_debug.c (limited to 'source4') diff --git a/source4/lib/ldb/Makefile.ldb b/source4/lib/ldb/Makefile.ldb index 0a0b95f7fe..1623b74f6c 100644 --- a/source4/lib/ldb/Makefile.ldb +++ b/source4/lib/ldb/Makefile.ldb @@ -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) diff --git a/source4/lib/ldb/common/ldb.c b/source4/lib/ldb/common/ldb.c index 86d0dd9e9b..b8f61e017a 100644 --- a/source4/lib/ldb/common/ldb.c +++ b/source4/lib/ldb/common/ldb.c @@ -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 index 0000000000..d59f9284b0 --- /dev/null +++ b/source4/lib/ldb/common/ldb_debug.c @@ -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); +} + diff --git a/source4/lib/ldb/common/ldb_ldif.c b/source4/lib/ldb/common/ldb_ldif.c index 451276c48d..513e2dd365 100644 --- a/source4/lib/ldb/common/ldb_ldif.c +++ b/source4/lib/ldb/common/ldb_ldif.c @@ -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; diff --git a/source4/lib/ldb/config.m4 b/source4/lib/ldb/config.m4 index 975da5a4a6..f450acce00 100644 --- a/source4/lib/ldb/config.m4 +++ b/source4/lib/ldb/config.m4 @@ -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 \ diff --git a/source4/lib/ldb/include/ldb.h b/source4/lib/ldb/include/ldb.h index 852389d415..adb6c31952 100644 --- a/source4/lib/ldb/include/ldb.h +++ b/source4/lib/ldb/include/ldb.h @@ -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)) diff --git a/source4/lib/ldb/ldb_ldap/ldb_ldap.c b/source4/lib/ldb/ldb_ldap/ldb_ldap.c index 7e7d047f25..7e959e7854 100644 --- a/source4/lib/ldb/ldb_ldap/ldb_ldap.c +++ b/source4/lib/ldb/ldb_ldap/ldb_ldap.c @@ -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; } diff --git a/source4/lib/ldb/tools/ldbadd.c b/source4/lib/ldb/tools/ldbadd.c index 6d89f67e0f..a45021c1d9 100644 --- a/source4/lib/ldb/tools/ldbadd.c +++ b/source4/lib/ldb/tools/ldbadd.c @@ -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(); } diff --git a/source4/lib/ldb/tools/ldbdel.c b/source4/lib/ldb/tools/ldbdel.c index 48ee07ad25..880713b25a 100644 --- a/source4/lib/ldb/tools/ldbdel.c +++ b/source4/lib/ldb/tools/ldbdel.c @@ -85,6 +85,8 @@ static void usage(void) exit(1); } + ldb_set_debug_stderr(ldb); + for (i=0;i 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; } -- cgit