2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-28 04:58:04 +00:00

Add isc_log_createandusechannel() function to simplify usage

The new
isc_log_createandusechannel() function combines following calls:

    isc_log_createchannel()
    isc_log_usechannel()

calls into a single call that cannot fail and therefore can be used in
places where we know this cannot fail thus simplifying the error
handling.
This commit is contained in:
Ondřej Surý 2024-08-14 14:38:07 +02:00
parent 091d738c72
commit 679e90a57d
20 changed files with 210 additions and 338 deletions

View File

@ -539,16 +539,10 @@ checksrv(dns_zone_t *zone, const dns_name_t *name, const dns_name_t *owner) {
isc_result_t isc_result_t
setup_logging(FILE *errout) { setup_logging(FILE *errout) {
isc_logconfig_t *logconfig = isc_logconfig_get(); isc_logconfig_t *logconfig = isc_logconfig_get();
isc_logdestination_t destination = { isc_log_createandusechannel(
.file.stream = errout, logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
.file.versions = ISC_LOG_ROLLNEVER, ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_FILE(errout), 0,
}; ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, &destination, 0);
RUNTIME_CHECK(isc_log_usechannel(logconfig, "stderr",
ISC_LOGCATEGORY_ALL,
ISC_LOGMODULE_ALL) == ISC_R_SUCCESS);
return (ISC_R_SUCCESS); return (ISC_R_SUCCESS);
} }

View File

@ -308,67 +308,45 @@ static int loglevel = 0;
static void static void
setup_logging(FILE *errout) { setup_logging(FILE *errout) {
isc_result_t result;
int packetlevel = 10; int packetlevel = 10;
isc_logconfig_t *logconfig = isc_logconfig_get();
isc_logdestination_t destination = {
.file.stream = errout,
.file.versions = ISC_LOG_ROLLNEVER,
};
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, &destination,
ISC_LOG_PRINTPREFIX);
isc_log_setdebuglevel(loglevel); isc_log_setdebuglevel(loglevel);
isc_logconfig_t *logconfig = isc_logconfig_get();
isc_log_settag(logconfig, ";; "); isc_log_settag(logconfig, ";; ");
result = isc_log_usechannel(logconfig, "stderr", isc_log_createandusechannel(
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_ALL); logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
if (result != ISC_R_SUCCESS) { ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_FILE(errout),
fatal("Couldn't attach to log channel 'stderr'"); ISC_LOG_PRINTPREFIX, ISC_LOGCATEGORY_DEFAULT,
} ISC_LOGMODULE_DEFAULT);
if (resolve_trace && loglevel < 1) { if (resolve_trace && loglevel < 1) {
isc_log_createchannel(logconfig, "resolver", ISC_LOG_TOFILEDESC, isc_log_createandusechannel(
ISC_LOG_DEBUG(1), &destination, logconfig, "resolver", ISC_LOG_TOFILEDESC,
ISC_LOG_PRINTPREFIX); ISC_LOG_DEBUG(1), ISC_LOGDESTINATION_FILE(errout),
ISC_LOG_PRINTPREFIX, DNS_LOGCATEGORY_RESOLVER,
result = isc_log_usechannel(logconfig, "resolver", DNS_LOGMODULE_RESOLVER);
DNS_LOGCATEGORY_RESOLVER,
DNS_LOGMODULE_RESOLVER);
if (result != ISC_R_SUCCESS) {
fatal("Couldn't attach to log channel 'resolver'");
}
} }
if (validator_trace && loglevel < 3) { if (validator_trace && loglevel < 3) {
isc_log_createchannel(logconfig, "validator", isc_log_createandusechannel(
ISC_LOG_TOFILEDESC, ISC_LOG_DEBUG(3), logconfig, "validator", ISC_LOG_TOFILEDESC,
&destination, ISC_LOG_PRINTPREFIX); ISC_LOG_DEBUG(3), ISC_LOGDESTINATION_FILE(errout),
ISC_LOG_PRINTPREFIX, DNS_LOGCATEGORY_DNSSEC,
result = isc_log_usechannel(logconfig, "validator", DNS_LOGMODULE_VALIDATOR);
DNS_LOGCATEGORY_DNSSEC,
DNS_LOGMODULE_VALIDATOR);
if (result != ISC_R_SUCCESS) {
fatal("Couldn't attach to log channel 'validator'");
}
} }
if (send_trace) { if (send_trace) {
packetlevel = 11; packetlevel = 11;
} }
if ((message_trace || send_trace) && loglevel < packetlevel) { if ((message_trace || send_trace) && loglevel < packetlevel) {
isc_log_createchannel(logconfig, "messages", ISC_LOG_TOFILEDESC, isc_log_createandusechannel(
ISC_LOG_DEBUG(packetlevel), &destination, logconfig, "messages", ISC_LOG_TOFILEDESC,
ISC_LOG_PRINTPREFIX); ISC_LOG_DEBUG(packetlevel),
ISC_LOGDESTINATION_FILE(errout), ISC_LOG_PRINTPREFIX,
result = isc_log_usechannel(logconfig, "messages", DNS_LOGCATEGORY_RESOLVER, DNS_LOGMODULE_PACKETS);
DNS_LOGCATEGORY_RESOLVER,
DNS_LOGMODULE_PACKETS);
if (result != ISC_R_SUCCESS) {
fatal("Couldn't attach to log channel 'messagse'");
}
} }
} }

View File

@ -1360,11 +1360,10 @@ setup_libs(void) {
isc_managers_create(&mctx, 1, &loopmgr, &netmgr); isc_managers_create(&mctx, 1, &loopmgr, &netmgr);
logconfig = isc_logconfig_get(); logconfig = isc_logconfig_get();
result = isc_log_usechannel(logconfig, "default_debug", isc_log_createandusechannel(logconfig, "debug", ISC_LOG_TOFILEDESC,
ISC_LOGCATEGORY_ALL, ISC_LOGMODULE_ALL); ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_STDERR,
ISC_LOG_PRINTTIME, ISC_LOGCATEGORY_DEFAULT,
check_result(result, "isc_log_usechannel"); ISC_LOGMODULE_DEFAULT);
isc_log_setdebuglevel(0); isc_log_setdebuglevel(0);
isc_mem_setname(mctx, "dig"); isc_mem_setname(mctx, "dig");

View File

@ -129,7 +129,6 @@ sig_format(dns_rdata_rrsig_t *sig, char *cp, unsigned int size) {
void void
setup_logging(void) { setup_logging(void) {
isc_logdestination_t destination;
isc_logconfig_t *logconfig = NULL; isc_logconfig_t *logconfig = NULL;
int level; int level;
@ -162,17 +161,11 @@ setup_logging(void) {
* - the program name and logging level are printed * - the program name and logging level are printed
* - no time stamp is printed * - no time stamp is printed
*/ */
destination.file.stream = stderr; isc_log_createandusechannel(
destination.file.name = NULL; logconfig, "default_stderr", ISC_LOG_TOFILEDESC, level,
destination.file.versions = ISC_LOG_ROLLNEVER; ISC_LOGDESTINATION_STDERR,
destination.file.maximum_size = 0; ISC_LOG_PRINTTAG | ISC_LOG_PRINTLEVEL, ISC_LOGCATEGORY_DEFAULT,
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC, level, ISC_LOGMODULE_DEFAULT);
&destination,
ISC_LOG_PRINTTAG | ISC_LOG_PRINTLEVEL);
RUNTIME_CHECK(isc_log_usechannel(logconfig, "stderr",
ISC_LOGCATEGORY_ALL,
ISC_LOGMODULE_ALL) == ISC_R_SUCCESS);
} }
static isc_stdtime_t static isc_stdtime_t

View File

@ -162,7 +162,6 @@ named_log_setdefaultsslkeylogfile(isc_logconfig_t *lcfg) {
.maximum_size = 100 * 1024 * 1024, .maximum_size = 100 * 1024 * 1024,
}, },
}; };
isc_result_t result;
if (sslkeylogfile_path == NULL || if (sslkeylogfile_path == NULL ||
strcmp(sslkeylogfile_path, "config") == 0) strcmp(sslkeylogfile_path, "config") == 0)
@ -170,12 +169,10 @@ named_log_setdefaultsslkeylogfile(isc_logconfig_t *lcfg) {
return; return;
} }
isc_log_createchannel(lcfg, "default_sslkeylogfile", ISC_LOG_TOFILE, isc_log_createandusechannel(lcfg, "default_sslkeylogfile",
ISC_LOG_INFO, &destination, 0); ISC_LOG_TOFILE, ISC_LOG_INFO, &destination,
result = isc_log_usechannel(lcfg, "default_sslkeylogfile", 0, ISC_LOGCATEGORY_SSLKEYLOG,
ISC_LOGCATEGORY_SSLKEYLOG, ISC_LOGMODULE_DEFAULT);
ISC_LOGMODULE_ALL);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
} }
isc_result_t isc_result_t
@ -183,7 +180,8 @@ named_log_setdefaultcategory(isc_logconfig_t *lcfg) {
isc_result_t result = ISC_R_SUCCESS; isc_result_t result = ISC_R_SUCCESS;
result = isc_log_usechannel(lcfg, "default_debug", result = isc_log_usechannel(lcfg, "default_debug",
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_ALL); ISC_LOGCATEGORY_DEFAULT,
ISC_LOGMODULE_DEFAULT);
if (result != ISC_R_SUCCESS) { if (result != ISC_R_SUCCESS) {
goto cleanup; goto cleanup;
} }
@ -192,11 +190,11 @@ named_log_setdefaultcategory(isc_logconfig_t *lcfg) {
if (named_g_logfile != NULL) { if (named_g_logfile != NULL) {
result = isc_log_usechannel(lcfg, "default_logfile", result = isc_log_usechannel(lcfg, "default_logfile",
ISC_LOGCATEGORY_DEFAULT, ISC_LOGCATEGORY_DEFAULT,
ISC_LOGMODULE_ALL); ISC_LOGMODULE_DEFAULT);
} else if (!named_g_nosyslog) { } else if (!named_g_nosyslog) {
result = isc_log_usechannel(lcfg, "default_syslog", result = isc_log_usechannel(lcfg, "default_syslog",
ISC_LOGCATEGORY_DEFAULT, ISC_LOGCATEGORY_DEFAULT,
ISC_LOGMODULE_ALL); ISC_LOGMODULE_DEFAULT);
} }
} }
@ -209,6 +207,6 @@ named_log_setunmatchedcategory(isc_logconfig_t *lcfg) {
isc_result_t result; isc_result_t result;
result = isc_log_usechannel(lcfg, "null", NAMED_LOGCATEGORY_UNMATCHED, result = isc_log_usechannel(lcfg, "null", NAMED_LOGCATEGORY_UNMATCHED,
ISC_LOGMODULE_ALL); ISC_LOGMODULE_DEFAULT);
return (result); return (result);
} }

View File

@ -44,7 +44,6 @@ category_fromconf(const cfg_obj_t *ccat, isc_logconfig_t *logconfig) {
isc_result_t result; isc_result_t result;
const char *catname; const char *catname;
isc_logcategory_t category; isc_logcategory_t category;
isc_logmodule_t module;
const cfg_obj_t *destinations = NULL; const cfg_obj_t *destinations = NULL;
const cfg_listelt_t *element = NULL; const cfg_listelt_t *element = NULL;
@ -63,8 +62,6 @@ category_fromconf(const cfg_obj_t *ccat, isc_logconfig_t *logconfig) {
return (ISC_R_SUCCESS); return (ISC_R_SUCCESS);
} }
module = ISC_LOGMODULE_ALL;
destinations = cfg_tuple_get(ccat, "destinations"); destinations = cfg_tuple_get(ccat, "destinations");
for (element = cfg_list_first(destinations); element != NULL; for (element = cfg_list_first(destinations); element != NULL;
element = cfg_list_next(element)) element = cfg_list_next(element))
@ -73,7 +70,7 @@ category_fromconf(const cfg_obj_t *ccat, isc_logconfig_t *logconfig) {
const char *channelname = cfg_obj_asstring(channel); const char *channelname = cfg_obj_asstring(channel);
result = isc_log_usechannel(logconfig, channelname, category, result = isc_log_usechannel(logconfig, channelname, category,
module); ISC_LOGMODULE_DEFAULT);
if (result != ISC_R_SUCCESS) { if (result != ISC_R_SUCCESS) {
isc_log_write(CFG_LOGCATEGORY_CONFIG, isc_log_write(CFG_LOGCATEGORY_CONFIG,
NAMED_LOGMODULE_SERVER, ISC_LOG_ERROR, NAMED_LOGMODULE_SERVER, ISC_LOG_ERROR,

View File

@ -811,10 +811,10 @@ setup_system(void *arg ISC_ATTR_UNUSED) {
ddebug("setup_system()"); ddebug("setup_system()");
logconfig = isc_logconfig_get(); logconfig = isc_logconfig_get();
result = isc_log_usechannel(logconfig, "default_debug", isc_log_createandusechannel(logconfig, "debug", ISC_LOG_TOFILEDESC,
ISC_LOGCATEGORY_ALL, ISC_LOGMODULE_ALL); ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_STDERR,
check_result(result, "isc_log_usechannel"); ISC_LOG_PRINTTIME, ISC_LOGCATEGORY_DEFAULT,
ISC_LOGMODULE_DEFAULT);
isc_log_setdebuglevel(logdebuglevel); isc_log_setdebuglevel(logdebuglevel);
result = irs_resconf_load(gmctx, resolvconf, &resconf); result = irs_resconf_load(gmctx, resolvconf, &resconf);

View File

@ -807,7 +807,6 @@ main(int argc, char **argv) {
isc_result_t result = ISC_R_SUCCESS; isc_result_t result = ISC_R_SUCCESS;
bool show_final_mem = false; bool show_final_mem = false;
isc_logconfig_t *logconfig = NULL; isc_logconfig_t *logconfig = NULL;
isc_logdestination_t logdest;
cfg_parser_t *pctx = NULL; cfg_parser_t *pctx = NULL;
cfg_obj_t *config = NULL; cfg_obj_t *config = NULL;
const char *keyname = NULL; const char *keyname = NULL;
@ -955,16 +954,11 @@ main(int argc, char **argv) {
logconfig = isc_logconfig_get(); logconfig = isc_logconfig_get();
isc_log_settag(logconfig, progname); isc_log_settag(logconfig, progname);
logdest.file.stream = stderr; isc_log_createandusechannel(
logdest.file.name = NULL; logconfig, "default_stderr", ISC_LOG_TOFILEDESC, ISC_LOG_INFO,
logdest.file.versions = ISC_LOG_ROLLNEVER; ISC_LOGDESTINATION_STDERR,
logdest.file.maximum_size = 0; ISC_LOG_PRINTTAG | ISC_LOG_PRINTLEVEL, ISC_LOGCATEGORY_DEFAULT,
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC, ISC_LOGMODULE_DEFAULT);
ISC_LOG_INFO, &logdest,
ISC_LOG_PRINTTAG | ISC_LOG_PRINTLEVEL);
DO("enabling log channel",
isc_log_usechannel(logconfig, "stderr", ISC_LOGCATEGORY_ALL,
ISC_LOGMODULE_ALL));
parse_config(rndc_mctx, keyname, &pctx, &config); parse_config(rndc_mctx, keyname, &pctx, &config);

View File

@ -61,7 +61,6 @@ main(int argc, char **argv) {
isc_result_t result; isc_result_t result;
char *origin, *file1, *file2, *journal; char *origin, *file1, *file2, *journal;
dns_db_t *olddb = NULL, *newdb = NULL; dns_db_t *olddb = NULL, *newdb = NULL;
isc_logdestination_t destination;
isc_logconfig_t *logconfig = NULL; isc_logconfig_t *logconfig = NULL;
if (argc != 5) { if (argc != 5) {
@ -78,18 +77,10 @@ main(int argc, char **argv) {
isc_mem_create(&mctx); isc_mem_create(&mctx);
logconfig = isc_logconfig_get(); logconfig = isc_logconfig_get();
destination.file.stream = stderr; isc_log_createandusechannel(
destination.file.name = NULL; logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
destination.file.versions = ISC_LOG_ROLLNEVER; ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_STDERR, 0,
destination.file.maximum_size = 0; ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, &destination, 0);
result = isc_log_usechannel(logconfig, "stderr", ISC_LOGCATEGORY_ALL,
ISC_LOGMODULE_ALL);
if (result != ISC_R_SUCCESS) {
goto cleanup;
}
result = loadzone(&olddb, origin, file1); result = loadzone(&olddb, origin, file1);
if (result != ISC_R_SUCCESS) { if (result != ISC_R_SUCCESS) {

View File

@ -48,7 +48,6 @@ unsigned int bits = 2048U;
isc_mem_t *mctx; isc_mem_t *mctx;
isc_logconfig_t *logconfig; isc_logconfig_t *logconfig;
int level = ISC_LOG_WARNING; int level = ISC_LOG_WARNING;
isc_logdestination_t destination;
char filename[255]; char filename[255];
isc_result_t result; isc_result_t result;
isc_buffer_t buf; isc_buffer_t buf;
@ -110,17 +109,12 @@ main(int argc, char **argv) {
logconfig = isc_logconfig_get(); logconfig = isc_logconfig_get();
isc_log_settag(logconfig, "bigkey"); isc_log_settag(logconfig, "bigkey");
destination.file.stream = stderr; isc_log_createandusechannel(
destination.file.name = NULL; logconfig, "default_stderr", ISC_LOG_TOFILEDESC, level,
destination.file.versions = ISC_LOG_ROLLNEVER; ISC_LOGDESTINATION_STDERR,
destination.file.maximum_size = 0; ISC_LOG_PRINTTAG | ISC_LOG_PRINTLEVEL, ISC_LOGCATEGORY_DEFAULT,
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC, level, ISC_LOGMODULE_DEFAULT);
&destination,
ISC_LOG_PRINTTAG | ISC_LOG_PRINTLEVEL);
CHECK(isc_log_usechannel(logconfig, "stderr", NULL, NULL), "isc_log_"
"usechannel("
")");
name = dns_fixedname_initname(&fname); name = dns_fixedname_initname(&fname);
isc_buffer_constinit(&buf, "example.", strlen("example.")); isc_buffer_constinit(&buf, "example.", strlen("example."));
isc_buffer_add(&buf, strlen("example.")); isc_buffer_add(&buf, strlen("example."));

View File

@ -37,20 +37,11 @@ usage(void) {
*/ */
static void static void
setup_logging(FILE *errout) { setup_logging(FILE *errout) {
isc_logdestination_t destination; isc_logconfig_t *logconfig = isc_logconfig_get();
isc_logconfig_t *logconfig = NULL; isc_log_createandusechannel(
logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
logconfig = isc_logconfig_get(); ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_FILE(errout), 0,
destination.file.stream = errout; ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
destination.file.name = NULL;
destination.file.versions = ISC_LOG_ROLLNEVER;
destination.file.maximum_size = 0;
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, &destination, 0);
RUNTIME_CHECK(isc_log_usechannel(logconfig, "stderr",
ISC_LOGCATEGORY_ALL,
ISC_LOGMODULE_ALL) == ISC_R_SUCCESS);
} }
int int

View File

@ -1132,17 +1132,17 @@ the following steps need to be taken to initialize it.
null, and all other messages to syslog. null, and all other messages to syslog.
result = isc_log_usechannel(lcfg, "default_stderr", result = isc_log_usechannel(lcfg, "default_stderr",
DNS_LOGCATEGORY_SECURITY, NULL); DNS_LOGCATEGORY_SECURITY, ISC_LOGMODULE_DEFAULT);
if (result != ISC_R_SUCCESS) if (result != ISC_R_SUCCESS)
oops_it_didnt_work(); oops_it_didnt_work();
result = isc_log_usechannel(lcfg, "null", result = isc_log_usechannel(lcfg, "null",
DNS_LOGCATEGORY_DATABASE, NULL); DNS_LOGCATEGORY_DATABASE, ISC_LOGMODULE_DEFAULT);
if (result != ISC_R_SUCCESS) if (result != ISC_R_SUCCESS)
oops_it_didnt_work(); oops_it_didnt_work();
result = isc_log_usechannel(lcfg, "default_syslog", result = isc_log_usechannel(lcfg, "default_syslog",
ISC_LOGCATEGORY_DEFAULT, NULL); ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
if (result != ISC_R_SUCCESS) if (result != ISC_R_SUCCESS)
oops_it_didnt_work(); oops_it_didnt_work();

View File

@ -26,23 +26,7 @@
#include <isccfg/namedconf.h> #include <isccfg/namedconf.h>
static void static void
check_result(isc_result_t result, const char *format, ...) { output(void *closure ISC_ATTR_UNUSED, const char *text, int textlen) {
va_list args;
if (result == ISC_R_SUCCESS) {
return;
}
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fprintf(stderr, ": %s\n", isc_result_totext(result));
exit(EXIT_FAILURE);
}
static void
output(void *closure, const char *text, int textlen) {
UNUSED(closure);
(void)fwrite(text, 1, textlen, stdout); (void)fwrite(text, 1, textlen, stdout);
} }
@ -54,12 +38,19 @@ usage(void) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
static void
setup_logging(void) {
isc_logconfig_t *logconfig = isc_logconfig_get();
isc_log_createandusechannel(
logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_STDERR, ISC_LOG_PRINTTIME,
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
}
int int
main(int argc, char **argv) { main(int argc, char **argv) {
isc_result_t result; isc_result_t result;
isc_mem_t *mctx = NULL; isc_mem_t *mctx = NULL;
isc_logconfig_t *lcfg = NULL;
isc_logdestination_t destination;
cfg_parser_t *pctx = NULL; cfg_parser_t *pctx = NULL;
cfg_obj_t *cfg = NULL; cfg_obj_t *cfg = NULL;
cfg_type_t *type = NULL; cfg_type_t *type = NULL;
@ -71,20 +62,7 @@ main(int argc, char **argv) {
isc_mem_create(&mctx); isc_mem_create(&mctx);
/* setup_logging();
* Create and install the default channel.
*/
lcfg = isc_logconfig_get();
destination.file.stream = stderr;
destination.file.name = NULL;
destination.file.versions = ISC_LOG_ROLLNEVER;
destination.file.maximum_size = 0;
isc_log_createchannel(lcfg, "_default", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, &destination, ISC_LOG_PRINTTIME);
result = isc_log_usechannel(lcfg, "_default", ISC_LOGCATEGORY_ALL,
ISC_LOGMODULE_ALL);
check_result(result, "isc_log_usechannel()");
/* /*
* Set the initial debug level. * Set the initial debug level.

View File

@ -24,6 +24,7 @@
#include <isc/formatcheck.h> #include <isc/formatcheck.h>
#include <isc/lang.h> #include <isc/lang.h>
#include <isc/types.h> #include <isc/types.h>
#include <isc/util.h>
typedef struct isc_logconfig isc_logconfig_t; /*%< Log Configuration */ typedef struct isc_logconfig isc_logconfig_t; /*%< Log Configuration */
@ -106,10 +107,9 @@ typedef enum {
typedef enum isc_logcategory isc_logcategory_t; /*%< Log Category */ typedef enum isc_logcategory isc_logcategory_t; /*%< Log Category */
enum isc_logcategory { enum isc_logcategory {
/*% /*%
* Do not log directly to DEFAULT. Use another category. * Logging to DEFAULT will end with assertion failure. Use another
* When in doubt, use GENERAL. * category. When in doubt, use GENERAL.
*/ */
ISC_LOGCATEGORY_ALL = -2,
ISC_LOGCATEGORY_INVALID = -1, ISC_LOGCATEGORY_INVALID = -1,
/* isc categories */ /* isc categories */
ISC_LOGCATEGORY_DEFAULT = 0, ISC_LOGCATEGORY_DEFAULT = 0,
@ -162,10 +162,9 @@ enum isc_logcategory {
*/ */
typedef enum isc_logmodule isc_logmodule_t; /*%< Log Module */ typedef enum isc_logmodule isc_logmodule_t; /*%< Log Module */
enum isc_logmodule { enum isc_logmodule {
ISC_LOGMODULE_ALL = -2,
ISC_LOGMODULE_INVALID = -1, ISC_LOGMODULE_INVALID = -1,
/* isc modules */ /* isc modules */
ISC_LOGMODULE_NONE = 0, ISC_LOGMODULE_DEFAULT = 0,
ISC_LOGMODULE_SOCKET, ISC_LOGMODULE_SOCKET,
ISC_LOGMODULE_TIME, ISC_LOGMODULE_TIME,
ISC_LOGMODULE_INTERFACE, ISC_LOGMODULE_INTERFACE,
@ -234,10 +233,10 @@ enum isc_logmodule {
* The isc_logfile structure is initialized as part of an isc_logdestination * The isc_logfile structure is initialized as part of an isc_logdestination
* before calling isc_log_createchannel(). * before calling isc_log_createchannel().
* *
* When defining an #ISC_LOG_TOFILE * When defining an #ISC_LOG_TOFILE channel, the name, versions and
* channel the name, versions and maximum_size should be set before calling * maximum_size should be set before calling isc_log_createchannel(). To
* isc_log_createchannel(). To define an #ISC_LOG_TOFILEDESC channel set only * define an #ISC_LOG_TOFILEDESC channel set only the stream before the
* the stream before the call. * call.
* *
* Setting maximum_size to zero implies no maximum. * Setting maximum_size to zero implies no maximum.
*/ */
@ -268,14 +267,16 @@ typedef union isc_logdestination {
int facility; int facility;
} isc_logdestination_t; } isc_logdestination_t;
#define ISC_LOGDESTINATION_STDERR \ #define ISC_LOGDESTINATION_FILE(errout) \
(&(isc_logdestination_t){ \ (&(isc_logdestination_t){ \
.file = { \ .file = { \
.stream = stderr, \ .stream = errout, \
.versions = ISC_LOG_ROLLNEVER, \ .versions = ISC_LOG_ROLLNEVER, \
.suffix = isc_log_rollsuffix_increment, \ .suffix = isc_log_rollsuffix_increment, \
} }) } })
#define ISC_LOGDESTINATION_STDERR ISC_LOGDESTINATION_FILE(stderr)
#define ISC_LOGDESTINATION_SYSLOG(f) \ #define ISC_LOGDESTINATION_SYSLOG(f) \
(&(isc_logdestination_t){ .facility = (f) }) (&(isc_logdestination_t){ .facility = (f) })
@ -473,50 +474,52 @@ isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
* Requires: * Requires:
*\li lcfg is a valid logging configuration. *\li lcfg is a valid logging configuration.
* *
*\li category is NULL or has an id that is in the range of known ids. *\li category is ISC_LOGCATEGORY_DEFAULT or has an id that is in the range of
* known ids.
* *
* module is NULL or has an id that is in the range of known ids. * module is ISC_LOGMODULE_DEFAULT or has an id that is in the range of
* known ids.
* *
* Ensures: * Ensures:
*\li #ISC_R_SUCCESS * The channel will be used by the indicated category/module
* The channel will be used by the indicated category/module * arguments.
* arguments.
*
*\li #ISC_R_NOMEMORY
* If assignment for a specific category has been requested,
* the channel has not been associated with the indicated
* category/module arguments and no additional memory is
* used by the logging context.
* If assignment for all categories has been requested
* then _some_ may have succeeded (starting with category
* "default" and progressing through the order of categories
* passed to isc_log_registercategories()) and additional memory
* is being used by whatever assignments succeeded.
*
* Returns:
*\li #ISC_R_SUCCESS Success
*\li #ISC_R_NOMEMORY Resource limit: Out of memory
*/ */
/* Attention: next four comments PRECEDE code */ void
/*! isc_log_createandusechannel(isc_logconfig_t *lcfg, const char *name,
unsigned int type, int level,
const isc_logdestination_t *destination,
unsigned int flags,
const isc_logcategory_t category,
const isc_logmodule_t module);
/*%<
* The isc_log_createchannel() and isc_log_usechannel() functions, combined
* into one. (This is for use by utilities that have simpler logging
* requirements than named, and don't have to define and assign channels
* dynamically.)
*/
void
isc_log_write(isc_logcategory_t category, isc_logmodule_t module, int level,
const char *format, ...) ISC_FORMAT_PRINTF(4, 5);
/*%<
* \brief * \brief
* Write a message to the log channels. * Write a message to the log channels.
* *
* Notes: * Notes:
*\li lctx can be NULL; this is allowed so that programs which use
* libraries that use the ISC logging system are not required to
* also use it.
*
*\li The format argument is a printf(3) string, with additional arguments *\li The format argument is a printf(3) string, with additional arguments
* as necessary. * as necessary.
* *
* Requires: * Requires:
*\li lctx is a valid logging context.
*
*\li The category and module arguments must have ids that are in the *\li The category and module arguments must have ids that are in the
* range of known ids, as established by isc_log_registercategories() * range of known ids.
* and isc_log_registermodules(). *
*\li category != ISC_LOGCATEGORY_DEFAULT. ISC_LOGCATEGORY_DEFAULT is used
* only to define channels.
*
*\li module != ISC_LOGMODULE_DEFAULT. ISC_LOGMODULE_DEFAULT is used
* only to define channels.
* *
*\li level != #ISC_LOG_DYNAMIC. ISC_LOG_DYNAMIC is used only to define *\li level != #ISC_LOG_DYNAMIC. ISC_LOG_DYNAMIC is used only to define
* channels, and explicit debugging level must be identified for * channels, and explicit debugging level must be identified for
@ -532,49 +535,40 @@ isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
*\li Nothing. Failure to log a message is not construed as a *\li Nothing. Failure to log a message is not construed as a
* meaningful error. * meaningful error.
*/ */
void
isc_log_write(isc_logcategory_t category, isc_logmodule_t module, int level,
const char *format, ...)
ISC_FORMAT_PRINTF(4, 5);
/*%
* Write a message to the log channels.
*
* Notes:
*\li lctx can be NULL; this is allowed so that programs which use
* libraries that use the ISC logging system are not required to
* also use it.
*
*\li The format argument is a printf(3) string, with additional arguments
* as necessary.
*
* Requires:
*\li lctx is a valid logging context.
*
*\li The category and module arguments must have ids that are in the
* range of known ids, as established by isc_log_registercategories()
* and isc_log_registermodules().
*
*\li level != #ISC_LOG_DYNAMIC. ISC_LOG_DYNAMIC is used only to define
* channels, and explicit debugging level must be identified for
* isc_log_write() via ISC_LOG_DEBUG(level).
*
*\li format != NULL.
*
* Ensures:
*\li The log message is written to every channel associated with the
* indicated category/module pair.
*
* Returns:
*\li Nothing. Failure to log a message is not construed as a
* meaningful error.
*/
void void
isc_log_vwrite(isc_logcategory_t category, isc_logmodule_t module, int level, isc_log_vwrite(isc_logcategory_t category, isc_logmodule_t module, int level,
const char *format, va_list args) const char *format, va_list args) ISC_FORMAT_PRINTF(4, 0);
/*%<
ISC_FORMAT_PRINTF(4, 0); * Write a message to the log channels.
*
*\li The format argument is a printf(3) string, with additional arguments
* as necessary.
*
* Requires:
*\li The category and module arguments must have ids that are in the
* range of known ids.
*
*\li category != ISC_LOGCATEGORY_DEFAULT. ISC_LOGCATEGORY_DEFAULT is used
* only to define channels.
*
*\li module != ISC_LOGMODULE_DEFAULT. ISC_LOGMODULE_DEFAULT is used
* only to define channels.
*
*\li level != #ISC_LOG_DYNAMIC. ISC_LOG_DYNAMIC is used only to define
* channels, and explicit debugging level must be identified for
* isc_log_write() via ISC_LOG_DEBUG(level).
*
*\li format != NULL.
*
* Ensures:
*\li The log message is written to every channel associated with the
* indicated category/module pair.
*
* Returns:
*\li Nothing. Failure to log a message is not construed as a
* meaningful error.
*/
void void
isc_log_setdebuglevel(unsigned int level); isc_log_setdebuglevel(unsigned int level);

View File

@ -210,7 +210,7 @@ static const char *categories_description[] = {
*/ */
static const char *modules_description[] = { static const char *modules_description[] = {
/* isc modules */ /* isc modules */
[ISC_LOGMODULE_NONE] = "no_module", [ISC_LOGMODULE_DEFAULT] = "no_module",
[ISC_LOGMODULE_SOCKET] = "socket", [ISC_LOGMODULE_SOCKET] = "socket",
[ISC_LOGMODULE_TIME] = "time", [ISC_LOGMODULE_TIME] = "time",
[ISC_LOGMODULE_INTERFACE] = "interface", [ISC_LOGMODULE_INTERFACE] = "interface",
@ -486,7 +486,7 @@ isc_log_createchannel(isc_logconfig_t *lcfg, const char *name,
REQUIRE(level >= ISC_LOG_CRITICAL); REQUIRE(level >= ISC_LOG_CRITICAL);
REQUIRE((flags & ~permitted) == 0); REQUIRE((flags & ~permitted) == 0);
/* XXXDCL find duplicate names? */ /* FIXME: find duplicate names? */
mctx = lcfg->lctx->mctx; mctx = lcfg->lctx->mctx;
@ -552,10 +552,9 @@ isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
const isc_logmodule_t module) { const isc_logmodule_t module) {
REQUIRE(VALID_CONFIG(lcfg)); REQUIRE(VALID_CONFIG(lcfg));
REQUIRE(name != NULL); REQUIRE(name != NULL);
REQUIRE(category == ISC_LOGCATEGORY_ALL || REQUIRE(category >= ISC_LOGCATEGORY_DEFAULT &&
(category >= 0 && category < ISC_LOGCATEGORY_MAX)); category < ISC_LOGCATEGORY_MAX);
REQUIRE(module == ISC_LOGMODULE_ALL || REQUIRE(module >= ISC_LOGMODULE_DEFAULT && module < ISC_LOGMODULE_MAX);
(module >= 0 && module < ISC_LOGMODULE_MAX));
isc_logchannel_t *channel; isc_logchannel_t *channel;
for (channel = ISC_LIST_HEAD(lcfg->channels); channel != NULL; for (channel = ISC_LIST_HEAD(lcfg->channels); channel != NULL;
@ -570,14 +569,16 @@ isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
return (ISC_R_NOTFOUND); return (ISC_R_NOTFOUND);
} }
if (category != ISC_LOGCATEGORY_ALL) { if (category != ISC_LOGCATEGORY_DEFAULT) {
assignchannel(lcfg, category, module, channel); assignchannel(lcfg, category, module, channel);
} else { } else {
/* /*
* Assign to all categories. Note that this includes * Assign to all categories. Note that this includes
* the default channel. * the default channel.
*/ */
for (size_t i = 0; i < ISC_LOGCATEGORY_MAX; i++) { for (size_t i = ISC_LOGCATEGORY_DEFAULT;
i < ISC_LOGCATEGORY_MAX; i++)
{
assignchannel(lcfg, i, module, channel); assignchannel(lcfg, i, module, channel);
} }
} }
@ -594,6 +595,18 @@ isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
return (ISC_R_SUCCESS); return (ISC_R_SUCCESS);
} }
void
isc_log_createandusechannel(isc_logconfig_t *lcfg, const char *name,
unsigned int type, int level,
const isc_logdestination_t *destination,
unsigned int flags,
const isc_logcategory_t category,
const isc_logmodule_t module) {
isc_log_createchannel(lcfg, name, type, level, destination, flags);
RUNTIME_CHECK(isc_log_usechannel(lcfg, name, category, module) ==
ISC_R_SUCCESS);
}
void void
isc_log_write(isc_logcategory_t category, isc_logmodule_t module, int level, isc_log_write(isc_logcategory_t category, isc_logmodule_t module, int level,
const char *format, ...) { const char *format, ...) {
@ -721,10 +734,9 @@ assignchannel(isc_logconfig_t *lcfg, const isc_logcategory_t category,
isc_log_t *lctx = lcfg->lctx; isc_log_t *lctx = lcfg->lctx;
REQUIRE(category > ISC_LOGCATEGORY_INVALID && REQUIRE(category >= ISC_LOGCATEGORY_DEFAULT &&
category < ISC_LOGCATEGORY_MAX); category < ISC_LOGCATEGORY_MAX);
REQUIRE(module == ISC_LOGMODULE_ALL || REQUIRE(module >= ISC_LOGMODULE_DEFAULT && module < ISC_LOGMODULE_MAX);
(module > ISC_LOGMODULE_INVALID && module < ISC_LOGMODULE_MAX));
isc_logchannellist_t *new_item = isc_mem_get(lctx->mctx, isc_logchannellist_t *new_item = isc_mem_get(lctx->mctx,
sizeof(*new_item)); sizeof(*new_item));
@ -1257,8 +1269,9 @@ isc_log_doit(isc_logcategory_t category, isc_logmodule_t module, int level,
isc_result_t result; isc_result_t result;
REQUIRE(isc__lctx == NULL || VALID_CONTEXT(isc__lctx)); REQUIRE(isc__lctx == NULL || VALID_CONTEXT(isc__lctx));
REQUIRE(category >= 0 && category < ISC_LOGCATEGORY_MAX); REQUIRE(category > ISC_LOGCATEGORY_DEFAULT &&
REQUIRE(module >= 0 && module < ISC_LOGMODULE_MAX); category < ISC_LOGCATEGORY_MAX);
REQUIRE(module > ISC_LOGMODULE_DEFAULT && module < ISC_LOGMODULE_MAX);
REQUIRE(level != ISC_LOG_DYNAMIC); REQUIRE(level != ISC_LOG_DYNAMIC);
REQUIRE(format != NULL); REQUIRE(format != NULL);
@ -1311,7 +1324,7 @@ isc_log_doit(isc_logcategory_t category, isc_logmodule_t module, int level,
category_channels = &default_channel; category_channels = &default_channel;
} }
if (category_channels->module != ISC_LOGMODULE_ALL && if (category_channels->module != ISC_LOGMODULE_DEFAULT &&
category_channels->module != module) category_channels->module != module)
{ {
category_channels = ISC_LIST_NEXT(category_channels, category_channels = ISC_LIST_NEXT(category_channels,

View File

@ -2596,7 +2596,7 @@ isc__netmgr_log(const isc_nm_t *netmgr, int level, const char *fmt, ...) {
vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap); vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
va_end(ap); va_end(ap);
isc_log_write(ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_NETMGR, level, isc_log_write(ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_NETMGR, level,
"netmgr %p: %s", netmgr, msgbuf); "netmgr %p: %s", netmgr, msgbuf);
} }
@ -2613,7 +2613,7 @@ isc__nmsocket_log(const isc_nmsocket_t *sock, int level, const char *fmt, ...) {
vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap); vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
va_end(ap); va_end(ap);
isc_log_write(ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_NETMGR, level, isc_log_write(ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_NETMGR, level,
"socket %p: %s", sock, msgbuf); "socket %p: %s", sock, msgbuf);
} }
@ -2673,7 +2673,7 @@ isc__nm_received_proxy_header_log(isc_nmhandle_t *handle,
real_peer_fmt, real_local_fmt, proto); real_peer_fmt, real_local_fmt, proto);
if (cmd == ISC_PROXY2_CMD_LOCAL) { if (cmd == ISC_PROXY2_CMD_LOCAL) {
isc_log_write(ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_NETMGR, isc_log_write(ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_NETMGR,
log_level, "%s: command: LOCAL (%s)", common_msg, log_level, "%s: command: LOCAL (%s)", common_msg,
real_addresses_msg); real_addresses_msg);
return; return;
@ -2686,7 +2686,7 @@ isc__nm_received_proxy_header_log(isc_nmhandle_t *handle,
switch (socktype) { switch (socktype) {
case 0: case 0:
isc_log_write(ISC_LOGCATEGORY_DEFAULT, isc_log_write(ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_NETMGR, log_level, ISC_LOGMODULE_NETMGR, log_level,
"%s: command: PROXY (unspecified address " "%s: command: PROXY (unspecified address "
"and socket type, %s)", "and socket type, %s)",
@ -2714,7 +2714,7 @@ isc__nm_received_proxy_header_log(isc_nmhandle_t *handle,
dst_addr_msg = dst_addr_fmt; dst_addr_msg = dst_addr_fmt;
} }
isc_log_write(ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_NETMGR, isc_log_write(ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_NETMGR,
log_level, log_level,
"%s: command: PROXY, socket type: %s, source: " "%s: command: PROXY, socket type: %s, source: "
"%s, destination: %s, TLVs: %s", "%s, destination: %s, TLVs: %s",

View File

@ -153,26 +153,15 @@ init_items(isc_mem_t *mctx) {
static void static void
init_logging(void) { init_logging(void) {
isc_result_t result;
isc_logdestination_t destination;
isc_logconfig_t *logconfig = NULL;
logconfig = isc_logconfig_get();
destination.file.stream = stderr;
destination.file.name = NULL;
destination.file.versions = ISC_LOG_ROLLNEVER;
destination.file.maximum_size = 0;
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, &destination,
ISC_LOG_PRINTPREFIX | ISC_LOG_PRINTTIME |
ISC_LOG_ISO8601);
#if VERBOSE #if VERBOSE
isc_log_setdebuglevel(7); isc_log_setdebuglevel(7);
#endif #endif
isc_logconfig_t *logconfig = isc_logconfig_get();
result = isc_log_usechannel(logconfig, "stderr", isc_log_createandusechannel(
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_ALL); logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
INSIST(result == ISC_R_SUCCESS); ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_STDERR,
ISC_LOG_PRINTPREFIX | ISC_LOG_PRINTTIME | ISC_LOG_ISO8601,
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
} }
static void static void

View File

@ -67,27 +67,15 @@
static void static void
setup_logging(void) { setup_logging(void) {
isc_result_t result;
isc_logdestination_t destination;
isc_logconfig_t *logconfig = NULL;
logconfig = isc_logconfig_get();
destination.file.stream = stderr;
destination.file.name = NULL;
destination.file.versions = ISC_LOG_ROLLNEVER;
destination.file.maximum_size = 0;
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, &destination,
ISC_LOG_PRINTPREFIX | ISC_LOG_PRINTTIME |
ISC_LOG_ISO8601);
#if VERBOSE #if VERBOSE
isc_log_setdebuglevel(7); isc_log_setdebuglevel(7);
#endif #endif
isc_logconfig_t *logconfig = isc_logconfig_get();
result = isc_log_usechannel(logconfig, "stderr", isc_log_createandusechannel(
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_ALL); logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
assert_int_equal(result, ISC_R_SUCCESS); ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_STDERR,
ISC_LOG_PRINTPREFIX | ISC_LOG_PRINTTIME | ISC_LOG_ISO8601,
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
} }
static struct { static struct {

View File

@ -38,21 +38,11 @@
#include <tests/isc.h> #include <tests/isc.h>
ISC_SETUP_TEST_IMPL(group) { ISC_SETUP_TEST_IMPL(group) {
isc_result_t result;
isc_logconfig_t *logconfig = isc_logconfig_get(); isc_logconfig_t *logconfig = isc_logconfig_get();
isc_logdestination_t destination = { isc_log_createandusechannel(
.file.stream = stderr, logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
.file.versions = ISC_LOG_ROLLNEVER, ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_STDERR, 0,
}; ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, &destination, 0);
result = isc_log_usechannel(logconfig, "stderr", ISC_LOGCATEGORY_ALL,
ISC_LOGMODULE_ALL);
if (result != ISC_R_SUCCESS) {
return (-1);
}
return (0); return (0);
} }

View File

@ -39,20 +39,11 @@
#include <tests/isc.h> #include <tests/isc.h>
ISC_SETUP_TEST_IMPL(group) { ISC_SETUP_TEST_IMPL(group) {
isc_result_t result;
isc_logconfig_t *logconfig = isc_logconfig_get(); isc_logconfig_t *logconfig = isc_logconfig_get();
isc_logdestination_t destination = { isc_log_createandusechannel(
.file.stream = stderr, logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
.file.versions = ISC_LOG_ROLLNEVER, ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_STDERR, 0,
}; ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, &destination, 0);
result = isc_log_usechannel(logconfig, "stderr", ISC_LOGCATEGORY_ALL,
ISC_LOGMODULE_ALL);
if (result != ISC_R_SUCCESS) {
return (-1);
}
return (0); return (0);
} }