r23798: updated old Temple Place FSF addresses to new URL
[kai/samba-autobuild/.git] / source3 / lib / ldb / common / ldb_debug.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 3 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldb debug
28  *
29  *  Description: functions for printing debug messages
30  *
31  *  Author: Andrew Tridgell
32  */
33
34 #include "includes.h"
35 #include "ldb/include/includes.h"
36
37 /*
38   this allows the user to choose their own debug function
39 */
40 int ldb_set_debug(struct ldb_context *ldb,
41                   void (*debug)(void *context, enum ldb_debug_level level, 
42                                 const char *fmt, va_list ap),
43                   void *context)
44 {
45         ldb->debug_ops.debug = debug;
46         ldb->debug_ops.context = context;
47         return 0;
48 }
49
50 /*
51   debug function for ldb_set_debug_stderr
52 */
53 static void ldb_debug_stderr(void *context, enum ldb_debug_level level, 
54                              const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
55 static void ldb_debug_stderr(void *context, enum ldb_debug_level level, 
56                              const char *fmt, va_list ap)
57 {
58         if (level <= LDB_DEBUG_WARNING) {
59                 vfprintf(stderr, fmt, ap);
60         }
61 }
62
63 /*
64   convenience function to setup debug messages on stderr
65   messages of level LDB_DEBUG_WARNING and higher are printed
66 */
67 int ldb_set_debug_stderr(struct ldb_context *ldb)
68 {
69         return ldb_set_debug(ldb, ldb_debug_stderr, ldb);
70 }
71
72 /*
73   log a message
74 */
75 void ldb_debug(struct ldb_context *ldb, enum ldb_debug_level level, const char *fmt, ...)
76 {
77         va_list ap;
78         if (ldb->debug_ops.debug == NULL) {
79                 ldb_set_debug_stderr(ldb);
80         }
81         va_start(ap, fmt);
82         ldb->debug_ops.debug(ldb->debug_ops.context, level, fmt, ap);
83         va_end(ap);
84 }
85
86
87 /*
88   log a message, and set the ldb error string to the same message
89 */
90 void ldb_debug_set(struct ldb_context *ldb, enum ldb_debug_level level, 
91                    const char *fmt, ...)
92 {
93         va_list ap;
94         char *msg;
95         va_start(ap, fmt);
96         msg = talloc_vasprintf(ldb, fmt, ap);
97         va_end(ap);
98         if (msg != NULL) {
99                 ldb_set_errstring(ldb, msg);
100                 ldb_debug(ldb, level, "%s", msg);
101         }
102         talloc_free(msg);
103 }
104