s4:lib/events: map TEVENT_DEBUG_TRACE to debug level 10
[ira/wip.git] / source4 / lib / events / tevent_s4.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Andrew Tridgell 2003
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "includes.h"
20 #include "lib/events/events.h"
21
22 /*
23   this is used to catch debug messages from events
24 */
25 static void ev_wrap_debug(void *context, enum tevent_debug_level level,
26                           const char *fmt, va_list ap)  PRINTF_ATTRIBUTE(3,0);
27
28 static void ev_wrap_debug(void *context, enum tevent_debug_level level,
29                           const char *fmt, va_list ap)
30 {
31         int samba_level = -1;
32         char *s = NULL;
33         switch (level) {
34         case TEVENT_DEBUG_FATAL:
35                 samba_level = 0;
36                 break;
37         case TEVENT_DEBUG_ERROR:
38                 samba_level = 1;
39                 break;
40         case TEVENT_DEBUG_WARNING:
41                 samba_level = 2;
42                 break;
43         case TEVENT_DEBUG_TRACE:
44                 samba_level = 10;
45                 break;
46
47         };
48         vasprintf(&s, fmt, ap);
49         if (!s) return;
50         DEBUG(samba_level, ("tevent: %s", s));
51         free(s);
52 }
53
54 /*
55   create a event_context structure. This must be the first events
56   call, and all subsequent calls pass this event_context as the first
57   element. Event handlers also receive this as their first argument.
58
59   This samba4 specific call sets the samba4 debug handler.
60 */
61 struct tevent_context *s4_event_context_init(TALLOC_CTX *mem_ctx)
62 {
63         struct tevent_context *ev;
64
65         ev = tevent_context_init_byname(mem_ctx, NULL);
66         if (ev) {
67                 tevent_set_debug(ev, ev_wrap_debug, NULL);
68         }
69         return ev;
70 }
71
72 /*
73   find an event context that is a parent of the given memory context,
74   or create a new event context as a child of the given context if
75   none is found
76
77   This should be used in preference to event_context_init() in places
78   where you would prefer to use the existing event context if possible
79   (which is most situations)
80 */
81 struct tevent_context *event_context_find(TALLOC_CTX *mem_ctx)
82 {
83         struct tevent_context *ev = talloc_find_parent_bytype(mem_ctx, struct tevent_context);
84         if (ev == NULL) {               
85                 ev = tevent_context_init(mem_ctx);
86         }
87         return ev;
88 }