Revert "s4:registry - "util" - make "reg_key_add_abs" consistent with "reg_key_del_abs""
[Samba.git] / source4 / lib / events / tevent_s4.c
blob838f20debeb10a7ae721e03fb3cf6a52192437cc
1 /*
2 Unix SMB/CIFS implementation.
3 Copyright (C) Andrew Tridgell 2003
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.
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.
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/>.
19 #include "includes.h"
20 #define TEVENT_DEPRECATED 1
21 #include "lib/events/events.h"
24 this is used to catch debug messages from events
26 static void ev_wrap_debug(void *context, enum tevent_debug_level level,
27 const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3,0);
29 static void ev_wrap_debug(void *context, enum tevent_debug_level level,
30 const char *fmt, va_list ap)
32 int samba_level = -1;
33 char *s = NULL;
34 switch (level) {
35 case TEVENT_DEBUG_FATAL:
36 samba_level = 0;
37 break;
38 case TEVENT_DEBUG_ERROR:
39 samba_level = 1;
40 break;
41 case TEVENT_DEBUG_WARNING:
42 samba_level = 2;
43 break;
44 case TEVENT_DEBUG_TRACE:
45 samba_level = 50;
46 break;
49 vasprintf(&s, fmt, ap);
50 if (!s) return;
51 DEBUG(samba_level, ("tevent: %s", s));
52 free(s);
56 create a event_context structure. This must be the first events
57 call, and all subsequent calls pass this event_context as the first
58 element. Event handlers also receive this as their first argument.
60 This samba4 specific call sets the samba4 debug handler.
62 struct tevent_context *s4_event_context_init(TALLOC_CTX *mem_ctx)
64 struct tevent_context *ev;
66 ev = tevent_context_init_byname(mem_ctx, NULL);
67 if (ev) {
68 tevent_set_debug(ev, ev_wrap_debug, NULL);
69 tevent_loop_allow_nesting(ev);
71 return ev;
74 static struct tevent_context *default_tevent_context;
76 /* set a default event context that will be used for
77 * event_context_find() if a parent event context is not found
79 void s4_event_context_set_default(struct tevent_context *ev)
81 default_tevent_context = ev;
85 find an event context that is a parent of the given memory context,
86 or create a new event context as a child of the given context if
87 none is found
89 This should be used in preference to event_context_init() in places
90 where you would prefer to use the existing event context if possible
91 (which is most situations)
93 struct tevent_context *event_context_find(TALLOC_CTX *mem_ctx)
95 struct tevent_context *ev = talloc_find_parent_bytype(mem_ctx, struct tevent_context);
96 if (ev == NULL) {
97 ev = default_tevent_context;
99 if (ev == NULL) {
100 ev = tevent_context_init(mem_ctx);
102 return ev;