r18900: Next attempt to fix the Solaris build. Not sure about whether to merge this
[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 2 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, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb debug
29  *
30  *  Description: functions for printing debug messages
31  *
32  *  Author: Andrew Tridgell
33  */
34
35 #include "includes.h"
36 #include "ldb/include/includes.h"
37
38 /*
39   this allows the user to choose their own debug function
40 */
41 int ldb_set_debug(struct ldb_context *ldb,
42                   void (*debug)(void *context, enum ldb_debug_level level, 
43                                 const char *fmt, va_list ap),
44                   void *context)
45 {
46         ldb->debug_ops.debug = debug;
47         ldb->debug_ops.context = context;
48         return 0;
49 }
50
51 /*
52   debug function for ldb_set_debug_stderr
53 */
54 static void ldb_debug_stderr(void *context, enum ldb_debug_level level, 
55                              const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
56 static void ldb_debug_stderr(void *context, enum ldb_debug_level level, 
57                              const char *fmt, va_list ap)
58 {
59         if (level <= LDB_DEBUG_WARNING) {
60                 vfprintf(stderr, fmt, ap);
61         }
62 }
63
64 /*
65   convenience function to setup debug messages on stderr
66   messages of level LDB_DEBUG_WARNING and higher are printed
67 */
68 int ldb_set_debug_stderr(struct ldb_context *ldb)
69 {
70         return ldb_set_debug(ldb, ldb_debug_stderr, ldb);
71 }
72
73 /*
74   log a message
75 */
76 void ldb_debug(struct ldb_context *ldb, enum ldb_debug_level level, const char *fmt, ...)
77 {
78         va_list ap;
79         if (ldb->debug_ops.debug == NULL) {
80                 ldb_set_debug_stderr(ldb);
81         }
82         va_start(ap, fmt);
83         ldb->debug_ops.debug(ldb->debug_ops.context, level, fmt, ap);
84         va_end(ap);
85 }
86
87
88 /*
89   log a message, and set the ldb error string to the same message
90 */
91 void ldb_debug_set(struct ldb_context *ldb, enum ldb_debug_level level, 
92                    const char *fmt, ...)
93 {
94         va_list ap;
95         char *msg;
96         va_start(ap, fmt);
97         msg = talloc_vasprintf(ldb, fmt, ap);
98         va_end(ap);
99         if (msg != NULL) {
100                 ldb_set_errstring(ldb, msg);
101                 ldb_debug(ldb, level, "%s", msg);
102         }
103         talloc_free(msg);
104 }
105