ad5212b0aebdcb24f778ba55bb19710066b077dd
[jra/samba/.git] / lib / tevent / tevent_debug.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2005
5    Copyright (C) Jelmer Vernooij 2005
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "replace.h"
22 #include "tevent.h"
23 #include "tevent_internal.h"
24
25 /********************************************************************
26  * Debug wrapper functions, modeled (with lot's of code copied as is)
27  * after the ev debug wrapper functions
28  ********************************************************************/
29
30 /*
31   this allows the user to choose their own debug function
32 */
33 int tevent_set_debug(struct tevent_context *ev,
34                      void (*debug)(void *context,
35                                    enum tevent_debug_level level,
36                                    const char *fmt,
37                                    va_list ap) PRINTF_ATTRIBUTE(3,0),
38                      void *context)
39 {
40         ev->debug_ops.debug = debug;
41         ev->debug_ops.context = context;
42         return 0;
43 }
44
45 /*
46   debug function for ev_set_debug_stderr
47 */
48 static void tevent_debug_stderr(void *private_data,
49                                 enum tevent_debug_level level,
50                                 const char *fmt,
51                                 va_list ap) PRINTF_ATTRIBUTE(3,0);
52 static void tevent_debug_stderr(void *private_data,
53                                 enum tevent_debug_level level,
54                                 const char *fmt, va_list ap)
55 {
56         if (level <= TEVENT_DEBUG_WARNING) {
57                 vfprintf(stderr, fmt, ap);
58         }
59 }
60
61 /*
62   convenience function to setup debug messages on stderr
63   messages of level TEVENT_DEBUG_WARNING and higher are printed
64 */
65 int tevent_set_debug_stderr(struct tevent_context *ev)
66 {
67         return tevent_set_debug(ev, tevent_debug_stderr, ev);
68 }
69
70 /*
71  * log a message
72  *
73  * The default debug action is to ignore debugging messages.
74  * This is the most appropriate action for a library.
75  * Applications using the library must decide where to
76  * redirect debugging messages
77 */
78 void tevent_debug(struct tevent_context *ev, enum tevent_debug_level level,
79                   const char *fmt, ...)
80 {
81         va_list ap;
82         if (!ev) {
83                 return;
84         }
85         if (ev->debug_ops.debug == NULL) {
86                 return;
87         }
88         va_start(ap, fmt);
89         ev->debug_ops.debug(ev->debug_ops.context, level, fmt, ap);
90         va_end(ap);
91 }
92