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)
*/
int ldb_close(struct ldb_context *ldb)
{
+ ldb_debug(ldb, LDB_DEBUG_TRACE, "ldb_close");
return ldb->ops->close(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;
}
/*
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;
}
/*
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;
}
*/
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;
}
/*
--- /dev/null
+/*
+ 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);
+}
+
}
}
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);
/* 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;
}
}
}
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;
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 \
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
/* memory allocation info */
struct ldb_alloc_ops alloc_ops;
+
+ /* memory allocation info */
+ struct ldb_debug_ops debug_ops;
};
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))
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;
}
exit(1);
}
+ ldb_set_debug_stderr(ldb);
+
if (argc == 0) {
usage();
}
exit(1);
}
+ ldb_set_debug_stderr(ldb);
+
for (i=0;i<argc;i++) {
ret = ldb_delete(ldb, argv[i]);
if (ret != 0) {
exit(1);
}
+ ldb_set_debug_stderr(ldb);
+
ret = ldb_search(ldb, basedn, scope, expression, NULL, &msgs);
if (ret == -1) {
exit(1);
}
+ ldb_set_debug_stderr(ldb);
+
if (argc == 0) {
usage();
exit(1);
exit(1);
}
+ ldb_set_debug_stderr(ldb);
+
if (interactive) {
char line[1024];
while (fgets(line, sizeof(line), stdin)) {
exit(1);
}
+ ldb_set_debug_stderr(ldb);
+
srandom(1);
start_test(ldb, nrecords, nsearches);
*/
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
return -1;
}
+ ldb_set_debug(sam_db, samdb_debug, NULL);
+
return 0;
}