Merge branch 'master' of ssh://jra@git.samba.org/data/git/samba
[sfrench/samba-autobuild/.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 ev_set_debug(struct event_context *ev,
34                  void (*debug)(void *context, enum ev_debug_level level,
35                                 const char *fmt, va_list ap),
36                  void *context)
37 {
38         ev->debug_ops.debug = debug;
39         ev->debug_ops.context = context;
40         return 0;
41 }
42
43 /*
44   debug function for ev_set_debug_stderr
45 */
46 void ev_debug_stderr(void *context, enum ev_debug_level level,
47                             const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
48 void ev_debug_stderr(void *context, enum ev_debug_level level,
49                             const char *fmt, va_list ap)
50 {
51         if (level <= EV_DEBUG_WARNING) {
52                 vfprintf(stderr, fmt, ap);
53         }
54 }
55
56 /*
57   convenience function to setup debug messages on stderr
58   messages of level EV_DEBUG_WARNING and higher are printed
59 */
60 int ev_set_debug_stderr(struct event_context *ev)
61 {
62         return ev_set_debug(ev, ev_debug_stderr, ev);
63 }
64
65 /*
66  * log a message
67  *
68  * The default debug action is to ignore debugging messages.
69  * This is the most appropriate action for a library.
70  * Applications using the library must decide where to
71  * redirect debugging messages
72 */
73 void ev_debug(struct event_context *ev, enum ev_debug_level level, const char *fmt, ...)
74 {
75         va_list ap;
76         if (ev->debug_ops.debug == NULL) {
77                 return;
78         }
79         va_start(ap, fmt);
80         ev->debug_ops.debug(ev->debug_ops.context, level, fmt, ap);
81         va_end(ap);
82 }
83