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