6df58aeaf0129686876b1a694a3fcc4ff20a4aa7
[obnox/samba/samba-obnox.git] / lib / util / tevent_debug.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 <tevent.h>
21
22 static void samba_tevent_debug(void *context,
23                                enum tevent_debug_level level,
24                                const char *fmt,
25                                va_list ap)  PRINTF_ATTRIBUTE(3,0);
26
27 static void samba_tevent_debug(void *context,
28                                enum tevent_debug_level level,
29                                const char *fmt,
30                                va_list ap)
31 {
32         int samba_level = -1;
33
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;
47         };
48
49         if (CHECK_DEBUGLVL(samba_level)) {
50                 const char *name = (const char *)context;
51                 char *message = NULL;
52                 int ret;
53
54                 ret = vasprintf(&message, fmt, ap);
55                 if (ret == -1) {
56                         return;
57                 }
58
59                 if (name == NULL) {
60                         name = "samba_tevent";
61                 }
62
63                 DEBUG(samba_level, ("%s: %s", name, message));
64                 free(message);
65         }
66 }
67
68 struct tevent_context *samba_tevent_context_init(TALLOC_CTX *mem_ctx)
69 {
70         struct tevent_context *ev;
71
72         ev = tevent_context_init(mem_ctx);
73         if (ev) {
74                 tevent_set_debug(ev, samba_tevent_debug, NULL);
75         }
76
77         return ev;
78 }