log.c revision 135446
1/*
2 * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2002  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.1.10.4 2004/03/08 09:04:14 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#ifndef ISC_FACILITY
29#define ISC_FACILITY LOG_DAEMON
30#endif
31
32/*
33 * When adding a new category, be sure to add the appropriate
34 * #define to <named/log.h>.
35 */
36static isc_logcategory_t categories[] = {
37	{ "",		 		0 },
38	{ "client",	 		0 },
39	{ "network",	 		0 },
40	{ "update",	 		0 },
41	{ "queries",	 		0 },
42	{ "unmatched",	 		0 },
43	{ "update-security",		0 },
44	{ NULL, 			0 }
45};
46
47/*
48 * When adding a new module, be sure to add the appropriate
49 * #define to <dns/log.h>.
50 */
51static isc_logmodule_t modules[] = {
52	{ "main",	 		0 },
53	{ "client",	 		0 },
54	{ "server",		 	0 },
55	{ "query",		 	0 },
56	{ "interfacemgr",	 	0 },
57	{ "update",	 		0 },
58	{ "xfer-in",	 		0 },
59	{ "xfer-out",	 		0 },
60	{ "notify",	 		0 },
61	{ "control",	 		0 },
62	{ "lwresd",	 		0 },
63	{ NULL, 			0 }
64};
65
66isc_result_t
67ns_log_init(isc_boolean_t safe) {
68	isc_result_t result;
69	isc_logconfig_t *lcfg = NULL;
70
71	ns_g_categories = categories;
72	ns_g_modules = modules;
73
74	/*
75	 * Setup a logging context.
76	 */
77	result = isc_log_create(ns_g_mctx, &ns_g_lctx, &lcfg);
78	if (result != ISC_R_SUCCESS)
79		return (result);
80
81	isc_log_registercategories(ns_g_lctx, ns_g_categories);
82	isc_log_registermodules(ns_g_lctx, ns_g_modules);
83	isc_log_setcontext(ns_g_lctx);
84	dns_log_init(ns_g_lctx);
85	dns_log_setcontext(ns_g_lctx);
86	cfg_log_init(ns_g_lctx);
87
88	if (safe)
89		result = ns_log_setsafechannels(lcfg);
90	else
91		result = ns_log_setdefaultchannels(lcfg);
92	if (result != ISC_R_SUCCESS)
93		goto cleanup;
94
95	result = ns_log_setdefaultcategory(lcfg);
96	if (result != ISC_R_SUCCESS)
97		goto cleanup;
98
99	return (ISC_R_SUCCESS);
100
101 cleanup:
102	isc_log_destroy(&ns_g_lctx);
103	isc_log_setcontext(NULL);
104	dns_log_setcontext(NULL);
105
106	return (result);
107}
108
109isc_result_t
110ns_log_setdefaultchannels(isc_logconfig_t *lcfg) {
111	isc_result_t result;
112	isc_logdestination_t destination;
113
114	/*
115	 * By default, the logging library makes "default_debug" log to
116	 * stderr.  In BIND, we want to override this and log to named.run
117	 * instead, unless the the -g option was given.
118	 */
119	if (! ns_g_logstderr) {
120		destination.file.stream = NULL;
121		destination.file.name = "named.run";
122		destination.file.versions = ISC_LOG_ROLLNEVER;
123		destination.file.maximum_size = 0;
124		result = isc_log_createchannel(lcfg, "default_debug",
125					       ISC_LOG_TOFILE,
126					       ISC_LOG_DYNAMIC,
127					       &destination,
128					       ISC_LOG_PRINTTIME|
129					       ISC_LOG_DEBUGONLY);
130		if (result != ISC_R_SUCCESS)
131			goto cleanup;
132	}
133
134#if ISC_FACILITY != LOG_DAEMON
135	destination.facility = ISC_FACILITY;
136	result = isc_log_createchannel(lcfg, "default_syslog",
137				       ISC_LOG_TOSYSLOG, ISC_LOG_INFO,
138				       &destination, 0);
139	if (result != ISC_R_SUCCESS)
140		goto cleanup;
141#endif
142
143	/*
144	 * Set the initial debug level.
145	 */
146	isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
147
148	result = ISC_R_SUCCESS;
149
150 cleanup:
151	return (result);
152}
153
154isc_result_t
155ns_log_setsafechannels(isc_logconfig_t *lcfg) {
156	isc_result_t result;
157
158	if (! ns_g_logstderr) {
159		result = isc_log_createchannel(lcfg, "default_debug",
160					       ISC_LOG_TONULL,
161					       ISC_LOG_DYNAMIC,
162					       NULL, 0);
163		if (result != ISC_R_SUCCESS)
164			goto cleanup;
165
166		/*
167		 * Setting the debug level to zero should get the output
168		 * discarded a bit faster.
169		 */
170		isc_log_setdebuglevel(ns_g_lctx, 0);
171	} else {
172		isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
173	}
174
175	result = ISC_R_SUCCESS;
176
177 cleanup:
178	return (result);
179}
180
181isc_result_t
182ns_log_setdefaultcategory(isc_logconfig_t *lcfg) {
183	isc_result_t result;
184
185	if (! ns_g_logstderr) {
186		result = isc_log_usechannel(lcfg, "default_syslog",
187					    ISC_LOGCATEGORY_DEFAULT, NULL);
188		if (result != ISC_R_SUCCESS)
189			goto cleanup;
190	}
191
192	result = isc_log_usechannel(lcfg, "default_debug",
193				    ISC_LOGCATEGORY_DEFAULT, NULL);
194	if (result != ISC_R_SUCCESS)
195		goto cleanup;
196
197	result = ISC_R_SUCCESS;
198
199 cleanup:
200	return (result);
201}
202
203isc_result_t
204ns_log_setunmatchedcategory(isc_logconfig_t *lcfg) {
205	isc_result_t result;
206
207	result = isc_log_usechannel(lcfg, "null",
208				    NS_LOGCATEGORY_UNMATCHED, NULL);
209	return (result);
210}
211
212void
213ns_log_shutdown(void) {
214	isc_log_destroy(&ns_g_lctx);
215	isc_log_setcontext(NULL);
216	dns_log_setcontext(NULL);
217}
218