summaryrefslogtreecommitdiffstats
path: root/lib/ldb/common/ldb_debug.c
blob: d5e9e7aa295ed4222b33878febf4aafa9c2220de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* 
   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 3 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, see <http://www.gnu.org/licenses/>.
*/

/*
 *  Name: ldb
 *
 *  Component: ldb debug
 *
 *  Description: functions for printing debug messages
 *
 *  Author: Andrew Tridgell
 */

#include "ldb_private.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) PRINTF_ATTRIBUTE(3,0);
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);
		fprintf(stderr, "\n");
	}
}

static void ldb_debug_stderr_all(void *context, enum ldb_debug_level level, 
			     const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
static void ldb_debug_stderr_all(void *context, enum ldb_debug_level level, 
			     const char *fmt, va_list ap)
{
	vfprintf(stderr, fmt, ap);
	fprintf(stderr, "\n");
}

/*
  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 (va_list helper for ldb_tevent_debug)
*/
void ldb_vdebug(struct ldb_context *ldb, enum ldb_debug_level level, const char *fmt, va_list ap)
{
	if (ldb->debug_ops.debug == NULL) {
		if (ldb->flags & LDB_FLG_ENABLE_TRACING) {
			ldb_set_debug(ldb, ldb_debug_stderr_all, ldb);
		} else {
			ldb_set_debug_stderr(ldb);
		}
	}
	ldb->debug_ops.debug(ldb->debug_ops.context, level, fmt, ap);
}

/*
  log a message
*/
void ldb_debug(struct ldb_context *ldb, enum ldb_debug_level level, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	ldb_vdebug(ldb, level, fmt, ap);
	va_end(ap);
}

/*
  add to an accumulated log message
 */
void ldb_debug_add(struct ldb_context *ldb, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	if (ldb->partial_debug == NULL) {
		ldb->partial_debug = talloc_vasprintf(ldb, fmt, ap);
	} else {
		ldb->partial_debug = talloc_vasprintf_append(ldb->partial_debug, 
							     fmt, ap);
	}
	va_end(ap);
}

/*
  send the accumulated log message, and free it
 */
void ldb_debug_end(struct ldb_context *ldb, enum ldb_debug_level level)
{
	ldb_debug(ldb, level, "%s", ldb->partial_debug);
	talloc_free(ldb->partial_debug);
	ldb->partial_debug = NULL;
}

/*
  log a message, and set the ldb error string to the same message
*/
void ldb_debug_set(struct ldb_context *ldb, enum ldb_debug_level level, 
		   const char *fmt, ...)
{
	va_list ap;
	char *msg;
	va_start(ap, fmt);
	msg = talloc_vasprintf(ldb, fmt, ap);
	va_end(ap);
	if (msg != NULL) {
		ldb_set_errstring(ldb, msg);
		ldb_debug(ldb, level, "%s", msg);
	}
	talloc_free(msg);
}