Merge from vendor branch BIND:
[dragonfly.git] / contrib / bind-9.2.4rc7 / bin / named / log.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: log.c,v 1.33.2.2 2004/03/09 06:09:18 marka Exp $ */
19
20 #include <config.h>
21
22 #include <isc/result.h>
23
24 #include <isccfg/log.h>
25
26 #include <named/log.h>
27
28 /*
29  * When adding a new category, be sure to add the appropriate
30  * #define to <named/log.h>.
31  */
32 static isc_logcategory_t categories[] = {
33         { "",                           0 },
34         { "client",                     0 },
35         { "network",                    0 },
36         { "update",                     0 },
37         { "queries",                    0 },
38         { "unmatched",                  0 },
39         { NULL,                         0 }
40 };
41
42 /*
43  * When adding a new module, be sure to add the appropriate
44  * #define to <dns/log.h>.
45  */
46 static isc_logmodule_t modules[] = {
47         { "main",                       0 },
48         { "client",                     0 },
49         { "server",                     0 },
50         { "query",                      0 },
51         { "interfacemgr",               0 },
52         { "update",                     0 },
53         { "xfer-in",                    0 },
54         { "xfer-out",                   0 },
55         { "notify",                     0 },
56         { "control",                    0 },
57         { "lwresd",                     0 },
58         { NULL,                         0 }
59 };
60
61 isc_result_t
62 ns_log_init(isc_boolean_t safe) {
63         isc_result_t result;
64         isc_logconfig_t *lcfg = NULL;
65
66         ns_g_categories = categories;
67         ns_g_modules = modules;
68
69         /*
70          * Setup a logging context.
71          */
72         result = isc_log_create(ns_g_mctx, &ns_g_lctx, &lcfg);
73         if (result != ISC_R_SUCCESS)
74                 return (result);
75
76         isc_log_registercategories(ns_g_lctx, ns_g_categories);
77         isc_log_registermodules(ns_g_lctx, ns_g_modules);
78         isc_log_setcontext(ns_g_lctx);
79         dns_log_init(ns_g_lctx);
80         dns_log_setcontext(ns_g_lctx);
81         cfg_log_init(ns_g_lctx);
82
83         if (safe)
84                 result = ns_log_setsafechannels(lcfg);
85         else
86                 result = ns_log_setdefaultchannels(lcfg);
87         if (result != ISC_R_SUCCESS)
88                 goto cleanup;
89
90         result = ns_log_setdefaultcategory(lcfg);
91         if (result != ISC_R_SUCCESS)
92                 goto cleanup;
93
94         return (ISC_R_SUCCESS);
95
96  cleanup:
97         isc_log_destroy(&ns_g_lctx);
98         isc_log_setcontext(NULL);
99         dns_log_setcontext(NULL);
100
101         return (result);
102 }
103
104 isc_result_t
105 ns_log_setdefaultchannels(isc_logconfig_t *lcfg) {
106         isc_result_t result;
107         isc_logdestination_t destination;
108
109         /*
110          * By default, the logging library makes "default_debug" log to
111          * stderr.  In BIND, we want to override this and log to named.run
112          * instead, unless the the -g option was given.
113          */
114         if (! ns_g_logstderr) {
115                 destination.file.stream = NULL;
116                 destination.file.name = "named.run";
117                 destination.file.versions = ISC_LOG_ROLLNEVER;
118                 destination.file.maximum_size = 0;
119                 result = isc_log_createchannel(lcfg, "default_debug",
120                                                ISC_LOG_TOFILE,
121                                                ISC_LOG_DYNAMIC,
122                                                &destination,
123                                                ISC_LOG_PRINTTIME|
124                                                ISC_LOG_DEBUGONLY);
125                 if (result != ISC_R_SUCCESS)
126                         goto cleanup;
127         }
128
129         /*
130          * Set the initial debug level.
131          */
132         isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
133
134         result = ISC_R_SUCCESS;
135
136  cleanup:
137         return (result);
138 }
139
140 isc_result_t
141 ns_log_setsafechannels(isc_logconfig_t *lcfg) {
142         isc_result_t result;
143
144         if (! ns_g_logstderr) {
145                 result = isc_log_createchannel(lcfg, "default_debug",
146                                                ISC_LOG_TONULL,
147                                                ISC_LOG_DYNAMIC,
148                                                NULL, 0);
149                 if (result != ISC_R_SUCCESS)
150                         goto cleanup;
151
152                 /*
153                  * Setting the debug level to zero should get the output
154                  * discarded a bit faster.
155                  */
156                 isc_log_setdebuglevel(ns_g_lctx, 0);
157         } else {
158                 isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
159         }
160
161         result = ISC_R_SUCCESS;
162
163  cleanup:
164         return (result);
165 }
166
167 isc_result_t
168 ns_log_setdefaultcategory(isc_logconfig_t *lcfg) {
169         isc_result_t result;
170
171         if (! ns_g_logstderr) {
172                 result = isc_log_usechannel(lcfg, "default_syslog",
173                                             ISC_LOGCATEGORY_DEFAULT, NULL);
174                 if (result != ISC_R_SUCCESS)
175                         goto cleanup;
176         }
177
178         result = isc_log_usechannel(lcfg, "default_debug",
179                                     ISC_LOGCATEGORY_DEFAULT, NULL);
180         if (result != ISC_R_SUCCESS)
181                 goto cleanup;
182
183         result = ISC_R_SUCCESS;
184
185  cleanup:
186         return (result);
187 }
188
189 isc_result_t
190 ns_log_setunmatchedcategory(isc_logconfig_t *lcfg) {
191         isc_result_t result;
192
193         result = isc_log_usechannel(lcfg, "null",
194                                     NS_LOGCATEGORY_UNMATCHED, NULL);
195         return (result);
196 }
197
198 void
199 ns_log_shutdown(void) {
200         isc_log_destroy(&ns_g_lctx);
201         isc_log_setcontext(NULL);
202         dns_log_setcontext(NULL);
203 }