log.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004-2007, 2009, 2013, 2014  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1999-2002  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: log.c,v 1.49 2009/01/07 01:46:40 jinmei Exp $ */
00019 
00020 /*! \file */
00021 
00022 #include <config.h>
00023 
00024 #include <isc/result.h>
00025 
00026 #include <isccfg/log.h>
00027 
00028 #include <named/log.h>
00029 
00030 #ifndef ISC_FACILITY
00031 #define ISC_FACILITY LOG_DAEMON
00032 #endif
00033 
00034 /*%
00035  * When adding a new category, be sure to add the appropriate
00036  * \#define to <named/log.h> and to update the list in
00037  * bin/check/check-tool.c.
00038  */
00039 static isc_logcategory_t categories[] = {
00040         { "",                           0 },
00041         { "client",                     0 },
00042         { "network",                    0 },
00043         { "update",                     0 },
00044         { "queries",                    0 },
00045         { "unmatched",                  0 },
00046         { "update-security",            0 },
00047         { "query-errors",               0 },
00048         { NULL,                         0 }
00049 };
00050 
00051 /*%
00052  * When adding a new module, be sure to add the appropriate
00053  * \#define to <dns/log.h>.
00054  */
00055 static isc_logmodule_t modules[] = {
00056         { "main",                       0 },
00057         { "client",                     0 },
00058         { "server",                     0 },
00059         { "query",                      0 },
00060         { "interfacemgr",               0 },
00061         { "update",                     0 },
00062         { "xfer-in",                    0 },
00063         { "xfer-out",                   0 },
00064         { "notify",                     0 },
00065         { "control",                    0 },
00066         { "lwresd",                     0 },
00067         { NULL,                         0 }
00068 };
00069 
00070 isc_result_t
00071 ns_log_init(isc_boolean_t safe) {
00072         isc_result_t result;
00073         isc_logconfig_t *lcfg = NULL;
00074 
00075         ns_g_categories = categories;
00076         ns_g_modules = modules;
00077 
00078         /*
00079          * Setup a logging context.
00080          */
00081         result = isc_log_create(ns_g_mctx, &ns_g_lctx, &lcfg);
00082         if (result != ISC_R_SUCCESS)
00083                 return (result);
00084 
00085         /*
00086          * named-checktool.c:setup_logging() needs to be kept in sync.
00087          */
00088         isc_log_registercategories(ns_g_lctx, ns_g_categories);
00089         isc_log_registermodules(ns_g_lctx, ns_g_modules);
00090         isc_log_setcontext(ns_g_lctx);
00091         dns_log_init(ns_g_lctx);
00092         dns_log_setcontext(ns_g_lctx);
00093         cfg_log_init(ns_g_lctx);
00094 
00095         if (safe)
00096                 result = ns_log_setsafechannels(lcfg);
00097         else
00098                 result = ns_log_setdefaultchannels(lcfg);
00099         if (result != ISC_R_SUCCESS)
00100                 goto cleanup;
00101 
00102         result = ns_log_setdefaultcategory(lcfg);
00103         if (result != ISC_R_SUCCESS)
00104                 goto cleanup;
00105 
00106         return (ISC_R_SUCCESS);
00107 
00108  cleanup:
00109         isc_log_destroy(&ns_g_lctx);
00110         isc_log_setcontext(NULL);
00111         dns_log_setcontext(NULL);
00112 
00113         return (result);
00114 }
00115 
00116 isc_result_t
00117 ns_log_setdefaultchannels(isc_logconfig_t *lcfg) {
00118         isc_result_t result;
00119         isc_logdestination_t destination;
00120 
00121         /*
00122          * By default, the logging library makes "default_debug" log to
00123          * stderr.  In BIND, we want to override this and log to named.run
00124          * instead, unless the -g option was given.
00125          */
00126         if (! ns_g_logstderr) {
00127                 destination.file.stream = NULL;
00128                 destination.file.name = "named.run";
00129                 destination.file.versions = ISC_LOG_ROLLNEVER;
00130                 destination.file.maximum_size = 0;
00131                 result = isc_log_createchannel(lcfg, "default_debug",
00132                                                ISC_LOG_TOFILE,
00133                                                ISC_LOG_DYNAMIC,
00134                                                &destination,
00135                                                ISC_LOG_PRINTTIME|
00136                                                ISC_LOG_DEBUGONLY);
00137                 if (result != ISC_R_SUCCESS)
00138                         goto cleanup;
00139         }
00140 
00141         if (ns_g_logfile != NULL) {
00142                 destination.file.stream = NULL;
00143                 destination.file.name = ns_g_logfile;
00144                 destination.file.versions = ISC_LOG_ROLLNEVER;
00145                 destination.file.maximum_size = 0;
00146                 result = isc_log_createchannel(lcfg, "default_logfile",
00147                                                ISC_LOG_TOFILE,
00148                                                ISC_LOG_DYNAMIC,
00149                                                &destination,
00150                                                ISC_LOG_PRINTTIME|
00151                                                ISC_LOG_PRINTCATEGORY|
00152                                                ISC_LOG_PRINTLEVEL);
00153                 if (result != ISC_R_SUCCESS)
00154                         goto cleanup;
00155         }
00156 
00157 #if ISC_FACILITY != LOG_DAEMON
00158         destination.facility = ISC_FACILITY;
00159         result = isc_log_createchannel(lcfg, "default_syslog",
00160                                        ISC_LOG_TOSYSLOG, ISC_LOG_INFO,
00161                                        &destination, 0);
00162         if (result != ISC_R_SUCCESS)
00163                 goto cleanup;
00164 #endif
00165 
00166         /*
00167          * Set the initial debug level.
00168          */
00169         isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
00170 
00171         result = ISC_R_SUCCESS;
00172 
00173  cleanup:
00174         return (result);
00175 }
00176 
00177 isc_result_t
00178 ns_log_setsafechannels(isc_logconfig_t *lcfg) {
00179         isc_result_t result;
00180         isc_logdestination_t destination;
00181 
00182         if (! ns_g_logstderr) {
00183                 result = isc_log_createchannel(lcfg, "default_debug",
00184                                                ISC_LOG_TONULL,
00185                                                ISC_LOG_DYNAMIC,
00186                                                NULL, 0);
00187                 if (result != ISC_R_SUCCESS)
00188                         goto cleanup;
00189 
00190                 /*
00191                  * Setting the debug level to zero should get the output
00192                  * discarded a bit faster.
00193                  */
00194                 isc_log_setdebuglevel(ns_g_lctx, 0);
00195         } else {
00196                 isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
00197         }
00198 
00199         if (ns_g_logfile != NULL) {
00200                 destination.file.stream = NULL;
00201                 destination.file.name = ns_g_logfile;
00202                 destination.file.versions = ISC_LOG_ROLLNEVER;
00203                 destination.file.maximum_size = 0;
00204                 result = isc_log_createchannel(lcfg, "default_logfile",
00205                                                ISC_LOG_TOFILE,
00206                                                ISC_LOG_DYNAMIC,
00207                                                &destination,
00208                                                ISC_LOG_PRINTTIME|
00209                                                ISC_LOG_PRINTCATEGORY|
00210                                                ISC_LOG_PRINTLEVEL);
00211                 if (result != ISC_R_SUCCESS)
00212                         goto cleanup;
00213         }
00214 
00215 #if ISC_FACILITY != LOG_DAEMON
00216         destination.facility = ISC_FACILITY;
00217         result = isc_log_createchannel(lcfg, "default_syslog",
00218                                        ISC_LOG_TOSYSLOG, ISC_LOG_INFO,
00219                                        &destination, 0);
00220         if (result != ISC_R_SUCCESS)
00221                 goto cleanup;
00222 #endif
00223 
00224         result = ISC_R_SUCCESS;
00225 
00226  cleanup:
00227         return (result);
00228 }
00229 
00230 isc_result_t
00231 ns_log_setdefaultcategory(isc_logconfig_t *lcfg) {
00232         isc_result_t result = ISC_R_SUCCESS;
00233 
00234         result = isc_log_usechannel(lcfg, "default_debug",
00235                                     ISC_LOGCATEGORY_DEFAULT, NULL);
00236         if (result != ISC_R_SUCCESS)
00237                 goto cleanup;
00238 
00239         if (! ns_g_logstderr) {
00240                 if (ns_g_logfile != NULL)
00241                         result = isc_log_usechannel(lcfg, "default_logfile",
00242                                                     ISC_LOGCATEGORY_DEFAULT,
00243                                                     NULL);
00244                 else if (! ns_g_nosyslog)
00245                         result = isc_log_usechannel(lcfg, "default_syslog",
00246                                                     ISC_LOGCATEGORY_DEFAULT,
00247                                                     NULL);
00248         }
00249 
00250  cleanup:
00251         return (result);
00252 }
00253 
00254 isc_result_t
00255 ns_log_setunmatchedcategory(isc_logconfig_t *lcfg) {
00256         isc_result_t result;
00257 
00258         result = isc_log_usechannel(lcfg, "null",
00259                                     NS_LOGCATEGORY_UNMATCHED, NULL);
00260         return (result);
00261 }
00262 
00263 void
00264 ns_log_shutdown(void) {
00265         isc_log_destroy(&ns_g_lctx);
00266         isc_log_setcontext(NULL);
00267         dns_log_setcontext(NULL);
00268 }

Generated on Tue Apr 28 17:41:04 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha