2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 10:10:06 +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
setup_logging(FILE *errout) {
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, 0);
RUNTIME_CHECK(isc_log_usechannel(logconfig, "stderr",
ISC_LOGCATEGORY_ALL,
ISC_LOGMODULE_ALL) == ISC_R_SUCCESS);
isc_log_createandusechannel(
logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_FILE(errout), 0,
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
return (ISC_R_SUCCESS);
}

View File

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

View File

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

View File

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

View File

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

View File

@ -44,7 +44,6 @@ category_fromconf(const cfg_obj_t *ccat, isc_logconfig_t *logconfig) {
isc_result_t result;
const char *catname;
isc_logcategory_t category;
isc_logmodule_t module;
const cfg_obj_t *destinations = 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);
}
module = ISC_LOGMODULE_ALL;
destinations = cfg_tuple_get(ccat, "destinations");
for (element = cfg_list_first(destinations); element != NULL;
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);
result = isc_log_usechannel(logconfig, channelname, category,
module);
ISC_LOGMODULE_DEFAULT);
if (result != ISC_R_SUCCESS) {
isc_log_write(CFG_LOGCATEGORY_CONFIG,
NAMED_LOGMODULE_SERVER, ISC_LOG_ERROR,

View File

@ -811,10 +811,10 @@ setup_system(void *arg ISC_ATTR_UNUSED) {
ddebug("setup_system()");
logconfig = isc_logconfig_get();
result = isc_log_usechannel(logconfig, "default_debug",
ISC_LOGCATEGORY_ALL, ISC_LOGMODULE_ALL);
check_result(result, "isc_log_usechannel");
isc_log_createandusechannel(logconfig, "debug", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_STDERR,
ISC_LOG_PRINTTIME, ISC_LOGCATEGORY_DEFAULT,
ISC_LOGMODULE_DEFAULT);
isc_log_setdebuglevel(logdebuglevel);
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;
bool show_final_mem = false;
isc_logconfig_t *logconfig = NULL;
isc_logdestination_t logdest;
cfg_parser_t *pctx = NULL;
cfg_obj_t *config = NULL;
const char *keyname = NULL;
@ -955,16 +954,11 @@ main(int argc, char **argv) {
logconfig = isc_logconfig_get();
isc_log_settag(logconfig, progname);
logdest.file.stream = stderr;
logdest.file.name = NULL;
logdest.file.versions = ISC_LOG_ROLLNEVER;
logdest.file.maximum_size = 0;
isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC,
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));
isc_log_createandusechannel(
logconfig, "default_stderr", ISC_LOG_TOFILEDESC, ISC_LOG_INFO,
ISC_LOGDESTINATION_STDERR,
ISC_LOG_PRINTTAG | ISC_LOG_PRINTLEVEL, ISC_LOGCATEGORY_DEFAULT,
ISC_LOGMODULE_DEFAULT);
parse_config(rndc_mctx, keyname, &pctx, &config);

View File

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

View File

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

View File

@ -37,20 +37,11 @@ usage(void) {
*/
static void
setup_logging(FILE *errout) {
isc_logdestination_t destination;
isc_logconfig_t *logconfig = NULL;
logconfig = isc_logconfig_get();
destination.file.stream = errout;
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);
isc_logconfig_t *logconfig = isc_logconfig_get();
isc_log_createandusechannel(
logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_FILE(errout), 0,
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
}
int

View File

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

View File

@ -26,23 +26,7 @@
#include <isccfg/namedconf.h>
static void
check_result(isc_result_t result, const char *format, ...) {
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);
output(void *closure ISC_ATTR_UNUSED, const char *text, int textlen) {
(void)fwrite(text, 1, textlen, stdout);
}
@ -54,12 +38,19 @@ usage(void) {
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
main(int argc, char **argv) {
isc_result_t result;
isc_mem_t *mctx = NULL;
isc_logconfig_t *lcfg = NULL;
isc_logdestination_t destination;
cfg_parser_t *pctx = NULL;
cfg_obj_t *cfg = NULL;
cfg_type_t *type = NULL;
@ -71,20 +62,7 @@ main(int argc, char **argv) {
isc_mem_create(&mctx);
/*
* 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()");
setup_logging();
/*
* Set the initial debug level.

View File

@ -24,6 +24,7 @@
#include <isc/formatcheck.h>
#include <isc/lang.h>
#include <isc/types.h>
#include <isc/util.h>
typedef struct isc_logconfig isc_logconfig_t; /*%< Log Configuration */
@ -106,10 +107,9 @@ typedef enum {
typedef enum isc_logcategory isc_logcategory_t; /*%< Log Category */
enum isc_logcategory {
/*%
* Do not log directly to DEFAULT. Use another category.
* When in doubt, use GENERAL.
* Logging to DEFAULT will end with assertion failure. Use another
* category. When in doubt, use GENERAL.
*/
ISC_LOGCATEGORY_ALL = -2,
ISC_LOGCATEGORY_INVALID = -1,
/* isc categories */
ISC_LOGCATEGORY_DEFAULT = 0,
@ -162,10 +162,9 @@ enum isc_logcategory {
*/
typedef enum isc_logmodule isc_logmodule_t; /*%< Log Module */
enum isc_logmodule {
ISC_LOGMODULE_ALL = -2,
ISC_LOGMODULE_INVALID = -1,
/* isc modules */
ISC_LOGMODULE_NONE = 0,
ISC_LOGMODULE_DEFAULT = 0,
ISC_LOGMODULE_SOCKET,
ISC_LOGMODULE_TIME,
ISC_LOGMODULE_INTERFACE,
@ -234,10 +233,10 @@ enum isc_logmodule {
* The isc_logfile structure is initialized as part of an isc_logdestination
* before calling isc_log_createchannel().
*
* When defining an #ISC_LOG_TOFILE
* channel the name, versions and maximum_size should be set before calling
* isc_log_createchannel(). To define an #ISC_LOG_TOFILEDESC channel set only
* the stream before the call.
* When defining an #ISC_LOG_TOFILE channel, the name, versions and
* maximum_size should be set before calling isc_log_createchannel(). To
* define an #ISC_LOG_TOFILEDESC channel set only the stream before the
* call.
*
* Setting maximum_size to zero implies no maximum.
*/
@ -268,14 +267,16 @@ typedef union isc_logdestination {
int facility;
} isc_logdestination_t;
#define ISC_LOGDESTINATION_STDERR \
#define ISC_LOGDESTINATION_FILE(errout) \
(&(isc_logdestination_t){ \
.file = { \
.stream = stderr, \
.stream = errout, \
.versions = ISC_LOG_ROLLNEVER, \
.suffix = isc_log_rollsuffix_increment, \
} })
#define ISC_LOGDESTINATION_STDERR ISC_LOGDESTINATION_FILE(stderr)
#define ISC_LOGDESTINATION_SYSLOG(f) \
(&(isc_logdestination_t){ .facility = (f) })
@ -473,50 +474,52 @@ isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
* Requires:
*\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:
*\li #ISC_R_SUCCESS
* The channel will be used by the indicated category/module
* 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
* The channel will be used by the indicated category/module
* arguments.
*/
/* 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
* 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().
* 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
@ -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
* 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
isc_log_vwrite(isc_logcategory_t category, isc_logmodule_t module, int level,
const char *format, va_list args)
ISC_FORMAT_PRINTF(4, 0);
const char *format, va_list args) 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
isc_log_setdebuglevel(unsigned int level);

View File

@ -210,7 +210,7 @@ static const char *categories_description[] = {
*/
static const char *modules_description[] = {
/* isc modules */
[ISC_LOGMODULE_NONE] = "no_module",
[ISC_LOGMODULE_DEFAULT] = "no_module",
[ISC_LOGMODULE_SOCKET] = "socket",
[ISC_LOGMODULE_TIME] = "time",
[ISC_LOGMODULE_INTERFACE] = "interface",
@ -486,7 +486,7 @@ isc_log_createchannel(isc_logconfig_t *lcfg, const char *name,
REQUIRE(level >= ISC_LOG_CRITICAL);
REQUIRE((flags & ~permitted) == 0);
/* XXXDCL find duplicate names? */
/* FIXME: find duplicate names? */
mctx = lcfg->lctx->mctx;
@ -552,10 +552,9 @@ isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
const isc_logmodule_t module) {
REQUIRE(VALID_CONFIG(lcfg));
REQUIRE(name != NULL);
REQUIRE(category == ISC_LOGCATEGORY_ALL ||
(category >= 0 && category < ISC_LOGCATEGORY_MAX));
REQUIRE(module == ISC_LOGMODULE_ALL ||
(module >= 0 && module < ISC_LOGMODULE_MAX));
REQUIRE(category >= ISC_LOGCATEGORY_DEFAULT &&
category < ISC_LOGCATEGORY_MAX);
REQUIRE(module >= ISC_LOGMODULE_DEFAULT && module < ISC_LOGMODULE_MAX);
isc_logchannel_t *channel;
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);
}
if (category != ISC_LOGCATEGORY_ALL) {
if (category != ISC_LOGCATEGORY_DEFAULT) {
assignchannel(lcfg, category, module, channel);
} else {
/*
* Assign to all categories. Note that this includes
* 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);
}
}
@ -594,6 +595,18 @@ isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
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
isc_log_write(isc_logcategory_t category, isc_logmodule_t module, int level,
const char *format, ...) {
@ -721,10 +734,9 @@ assignchannel(isc_logconfig_t *lcfg, const isc_logcategory_t category,
isc_log_t *lctx = lcfg->lctx;
REQUIRE(category > ISC_LOGCATEGORY_INVALID &&
REQUIRE(category >= ISC_LOGCATEGORY_DEFAULT &&
category < ISC_LOGCATEGORY_MAX);
REQUIRE(module == ISC_LOGMODULE_ALL ||
(module > ISC_LOGMODULE_INVALID && module < ISC_LOGMODULE_MAX));
REQUIRE(module >= ISC_LOGMODULE_DEFAULT && module < ISC_LOGMODULE_MAX);
isc_logchannellist_t *new_item = isc_mem_get(lctx->mctx,
sizeof(*new_item));
@ -1257,8 +1269,9 @@ isc_log_doit(isc_logcategory_t category, isc_logmodule_t module, int level,
isc_result_t result;
REQUIRE(isc__lctx == NULL || VALID_CONTEXT(isc__lctx));
REQUIRE(category >= 0 && category < ISC_LOGCATEGORY_MAX);
REQUIRE(module >= 0 && module < ISC_LOGMODULE_MAX);
REQUIRE(category > ISC_LOGCATEGORY_DEFAULT &&
category < ISC_LOGCATEGORY_MAX);
REQUIRE(module > ISC_LOGMODULE_DEFAULT && module < ISC_LOGMODULE_MAX);
REQUIRE(level != ISC_LOG_DYNAMIC);
REQUIRE(format != NULL);
@ -1311,7 +1324,7 @@ isc_log_doit(isc_logcategory_t category, isc_logmodule_t module, int level,
category_channels = &default_channel;
}
if (category_channels->module != ISC_LOGMODULE_ALL &&
if (category_channels->module != ISC_LOGMODULE_DEFAULT &&
category_channels->module != module)
{
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);
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);
}
@ -2613,7 +2613,7 @@ isc__nmsocket_log(const isc_nmsocket_t *sock, int level, const char *fmt, ...) {
vsnprintf(msgbuf, sizeof(msgbuf), fmt, 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);
}
@ -2673,7 +2673,7 @@ isc__nm_received_proxy_header_log(isc_nmhandle_t *handle,
real_peer_fmt, real_local_fmt, proto);
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,
real_addresses_msg);
return;
@ -2686,7 +2686,7 @@ isc__nm_received_proxy_header_log(isc_nmhandle_t *handle,
switch (socktype) {
case 0:
isc_log_write(ISC_LOGCATEGORY_DEFAULT,
isc_log_write(ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_NETMGR, log_level,
"%s: command: PROXY (unspecified address "
"and socket type, %s)",
@ -2714,7 +2714,7 @@ isc__nm_received_proxy_header_log(isc_nmhandle_t *handle,
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,
"%s: command: PROXY, socket type: %s, source: "
"%s, destination: %s, TLVs: %s",

View File

@ -153,26 +153,15 @@ init_items(isc_mem_t *mctx) {
static 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
isc_log_setdebuglevel(7);
#endif
result = isc_log_usechannel(logconfig, "stderr",
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_ALL);
INSIST(result == ISC_R_SUCCESS);
isc_logconfig_t *logconfig = isc_logconfig_get();
isc_log_createandusechannel(
logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_STDERR,
ISC_LOG_PRINTPREFIX | ISC_LOG_PRINTTIME | ISC_LOG_ISO8601,
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
}
static void

View File

@ -67,27 +67,15 @@
static 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
isc_log_setdebuglevel(7);
#endif
result = isc_log_usechannel(logconfig, "stderr",
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_ALL);
assert_int_equal(result, ISC_R_SUCCESS);
isc_logconfig_t *logconfig = isc_logconfig_get();
isc_log_createandusechannel(
logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_STDERR,
ISC_LOG_PRINTPREFIX | ISC_LOG_PRINTTIME | ISC_LOG_ISO8601,
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
}
static struct {

View File

@ -38,21 +38,11 @@
#include <tests/isc.h>
ISC_SETUP_TEST_IMPL(group) {
isc_result_t result;
isc_logconfig_t *logconfig = isc_logconfig_get();
isc_logdestination_t destination = {
.file.stream = stderr,
.file.versions = ISC_LOG_ROLLNEVER,
};
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);
}
isc_log_createandusechannel(
logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_STDERR, 0,
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
return (0);
}

View File

@ -39,20 +39,11 @@
#include <tests/isc.h>
ISC_SETUP_TEST_IMPL(group) {
isc_result_t result;
isc_logconfig_t *logconfig = isc_logconfig_get();
isc_logdestination_t destination = {
.file.stream = stderr,
.file.versions = ISC_LOG_ROLLNEVER,
};
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);
}
isc_log_createandusechannel(
logconfig, "default_stderr", ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, ISC_LOGDESTINATION_STDERR, 0,
ISC_LOGCATEGORY_DEFAULT, ISC_LOGMODULE_DEFAULT);
return (0);
}