2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 14:35:26 +00:00

isc_logchannel, isc_logchannellist, and isc_logmessage now use the isc list

macros.

ISC_LOG_DEBUGONLY flag for a channel added, to mark a channel that should
only be used if the debugging level is non-zero.

Use ISC_LOG_DEBUGONLY for default_debug.

REQUIRE the context is valid for isc_logconfig_get, and ENSURE that
the configuration returned is non-NULL.

Other miscellaneous cleanups.
This commit is contained in:
David Lawrence
2000-03-01 17:31:56 +00:00
parent e02c696ea5
commit a40cc3249f

View File

@@ -15,7 +15,7 @@
* SOFTWARE. * SOFTWARE.
*/ */
/* $Id: log.c,v 1.16 2000/02/26 19:57:01 tale Exp $ */ /* $Id: log.c,v 1.17 2000/03/01 17:31:56 tale Exp $ */
/* Principal Authors: DCL */ /* Principal Authors: DCL */
@@ -43,10 +43,11 @@
#define LCFG_MAGIC 0x4C636667U /* Lcfg. */ #define LCFG_MAGIC 0x4C636667U /* Lcfg. */
#define VALID_CONFIG(lcfg) ((lcfg) != NULL && (lcfg)->magic == LCFG_MAGIC) #define VALID_CONFIG(lcfg) ((lcfg) != NULL && (lcfg)->magic == LCFG_MAGIC)
/*
* XXXDCL make dynamic?
*/
#define LOG_BUFFER_SIZE (8 * 1024) #define LOG_BUFFER_SIZE (8 * 1024)
typedef struct isc_logchannel isc_logchannel_t;
/* /*
* This is the structure that holds each named channel. A simple linked * This is the structure that holds each named channel. A simple linked
* list chains all of the channels together, so an individual channel is * list chains all of the channels together, so an individual channel is
@@ -56,14 +57,15 @@ typedef struct isc_logchannel isc_logchannel_t;
* from the head of the list are only done when isc_log_usechannel() is * from the head of the list are only done when isc_log_usechannel() is
* called, which should also be very infrequent. * called, which should also be very infrequent.
*/ */
typedef struct isc_logchannel isc_logchannel_t;
struct isc_logchannel { struct isc_logchannel {
char * name; char * name;
unsigned int type; unsigned int type;
int level; int level;
unsigned int flags; unsigned int flags;
isc_logdestination_t destination; isc_logdestination_t destination;
/* XXXDCL should be an ISC_LINK, so this can be an ISC_LIST */ ISC_LINK(isc_logchannel_t) link;
isc_logchannel_t * next;
}; };
/* /*
@@ -77,10 +79,9 @@ struct isc_logchannel {
typedef struct isc_logchannellist isc_logchannellist_t; typedef struct isc_logchannellist isc_logchannellist_t;
struct isc_logchannellist { struct isc_logchannellist {
isc_logmodule_t * module; isc_logmodule_t * module;
isc_logchannel_t * channel; isc_logchannel_t * channel;
/* XXXDCL should be an ISC_LINK, so this can be an ISC_LIST */ ISC_LINK(isc_logchannellist_t) link;
isc_logchannellist_t * next;
}; };
/* /*
@@ -96,13 +97,17 @@ struct isc_logmessage {
}; };
/* /*
* XXX describe * The isc_logconfig structure is used to store the configurable information
* about where messages are actually supposed to be sent -- the information
* that could changed based on some configuration file, as opposed to the
* the category/module specification of isc_log_[v]write[1] that is compiled
* into a program, or the debug_level which is dynamic state information.
*/ */
struct isc_logconfig { struct isc_logconfig {
unsigned int magic; unsigned int magic;
isc_log_t * lctx; isc_log_t * lctx;
isc_logchannel_t * channels; ISC_LIST(isc_logchannel_t) channels;
isc_logchannellist_t ** channellists; ISC_LIST(isc_logchannellist_t) *channellists;
unsigned int channellist_count; unsigned int channellist_count;
unsigned int duplicate_interval; unsigned int duplicate_interval;
}; };
@@ -117,12 +122,12 @@ struct isc_logconfig {
* Unfortunately, the lock cannot guard against a _different_ logging * Unfortunately, the lock cannot guard against a _different_ logging
* context in the same program competing for syslog's attention. Thus * context in the same program competing for syslog's attention. Thus
* There Can Be Only One, but this is not enforced. * There Can Be Only One, but this is not enforced.
* XXX enforce it? * XXXDCL enforce it?
* *
* Note that the category and module information is not locked. * Note that the category and module information is not locked.
* This is because in the usual case, only one isc_log_t is ever created * This is because in the usual case, only one isc_log_t is ever created
* in a program, and the category/module registration happens only once. * in a program, and the category/module registration happens only once.
* XXX it might be wise to add more locking overall. * XXXDCL it might be wise to add more locking overall.
*/ */
struct isc_log { struct isc_log {
/* Not locked. */ /* Not locked. */
@@ -131,23 +136,23 @@ struct isc_log {
unsigned int category_count; unsigned int category_count;
unsigned int module_count; unsigned int module_count;
int debug_level; int debug_level;
char buffer[LOG_BUFFER_SIZE];
ISC_LIST(isc_logmessage_t) messages;
isc_mutex_t lock; isc_mutex_t lock;
/* Locked by isc_log lock. */ /* Locked by isc_log lock. */
isc_logconfig_t * logconfig; isc_logconfig_t * logconfig;
char buffer[LOG_BUFFER_SIZE];
ISC_LIST(isc_logmessage_t) messages;
}; };
/* /*
* Used when ISC_LOG_PRINTLEVEL is enabled for a channel. * Used when ISC_LOG_PRINTLEVEL is enabled for a channel.
*/ */
static char *log_level_strings[] = { static const char *log_level_strings[] = {
"debug", "info", "notice", "warning", "error", "critical" "debug", "info", "notice", "warning", "error", "critical"
}; };
/* /*
* Used to convert ISC_LOG_* priorities into syslog priorities. * Used to convert ISC_LOG_* priorities into syslog priorities.
* XXXDCL NT * XXXDCL This will need modification for NT.
*/ */
static const int syslog_map[] = { static const int syslog_map[] = {
LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_WARNING, LOG_ERR, LOG_CRIT LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_WARNING, LOG_ERR, LOG_CRIT
@@ -168,7 +173,8 @@ isc_logcategory_t isc_categories[] = {
/* /*
* This essentially static structure must be filled in at run time, * This essentially static structure must be filled in at run time,
* so that the default_debug channel's structure can be addressed. * because its channel member is pointed to a channel that is created
* dynamically with isc_log_createchannel.
*/ */
isc_logchannellist_t default_channel; isc_logchannellist_t default_channel;
@@ -214,12 +220,12 @@ isc_result_t
isc_log_create(isc_mem_t *mctx, isc_log_t **lctxp, isc_logconfig_t **lcfgp) { isc_log_create(isc_mem_t *mctx, isc_log_t **lctxp, isc_logconfig_t **lcfgp) {
isc_log_t *lctx; isc_log_t *lctx;
isc_logconfig_t *lcfg = NULL; isc_logconfig_t *lcfg = NULL;
isc_result_t result = ISC_R_SUCCESS; isc_result_t result;
REQUIRE(mctx != NULL); REQUIRE(mctx != NULL);
REQUIRE(lctxp != NULL && *lctxp == NULL); REQUIRE(lctxp != NULL && *lctxp == NULL);
lctx = (isc_log_t *)isc_mem_get(mctx, sizeof(*lctx)); lctx = isc_mem_get(mctx, sizeof(*lctx));
if (lctx != NULL) { if (lctx != NULL) {
lctx->mctx = mctx; lctx->mctx = mctx;
lctx->category_count = 0; lctx->category_count = 0;
@@ -268,15 +274,16 @@ isc_logconfig_create(isc_log_t *lctx, isc_logconfig_t **lcfgp) {
REQUIRE(lcfgp != NULL && *lcfgp == NULL); REQUIRE(lcfgp != NULL && *lcfgp == NULL);
REQUIRE(VALID_CONTEXT(lctx)); REQUIRE(VALID_CONTEXT(lctx));
lcfg = (isc_logconfig_t *)isc_mem_get(lctx->mctx, sizeof(*lcfg)); lcfg = isc_mem_get(lctx->mctx, sizeof(*lcfg));
if (lcfg != NULL) { if (lcfg != NULL) {
lcfg->lctx = lctx; lcfg->lctx = lctx;
lcfg->channels = NULL;
lcfg->channellists = NULL; lcfg->channellists = NULL;
lcfg->channellist_count = 0; lcfg->channellist_count = 0;
lcfg->duplicate_interval = 0; lcfg->duplicate_interval = 0;
ISC_LIST_INIT(lcfg->channels);
/* /*
* Normally the magic number is the last thing set in the * Normally the magic number is the last thing set in the
* structure, but isc_log_createchannel() needs a valid * structure, but isc_log_createchannel() needs a valid
@@ -312,10 +319,10 @@ isc_logconfig_create(isc_log_t *lctx, isc_logconfig_t **lcfgp) {
} }
/* /*
* Set the default category's channel to default_stderr. * Set the default category's channel to default_stderr, which
* XXX I find this odd. * is at the head of the channels list because it was just created.
*/ */
default_channel.channel = lcfg->channels; default_channel.channel = ISC_LIST_HEAD(lcfg->channels);
if (result == ISC_R_SUCCESS) { if (result == ISC_R_SUCCESS) {
destination.file.stream = stderr; destination.file.stream = stderr;
@@ -326,7 +333,8 @@ isc_logconfig_create(isc_log_t *lctx, isc_logconfig_t **lcfgp) {
ISC_LOG_TOFILEDESC, ISC_LOG_TOFILEDESC,
ISC_LOG_DYNAMIC, ISC_LOG_DYNAMIC,
&destination, &destination,
ISC_LOG_PRINTTIME); ISC_LOG_PRINTTIME|
ISC_LOG_DEBUGONLY);
} }
if (result == ISC_R_SUCCESS) if (result == ISC_R_SUCCESS)
@@ -347,6 +355,10 @@ isc_logconfig_create(isc_log_t *lctx, isc_logconfig_t **lcfgp) {
isc_logconfig_t * isc_logconfig_t *
isc_logconfig_get(isc_log_t *lctx) { isc_logconfig_get(isc_log_t *lctx) {
REQUIRE(VALID_CONTEXT(lctx));
ENSURE(lctx->logconfig != NULL);
return (lctx->logconfig); return (lctx->logconfig);
} }
@@ -423,8 +435,8 @@ void
isc_logconfig_destroy(isc_logconfig_t **lcfgp) { isc_logconfig_destroy(isc_logconfig_t **lcfgp) {
isc_logconfig_t *lcfg; isc_logconfig_t *lcfg;
isc_mem_t *mctx; isc_mem_t *mctx;
isc_logchannel_t *channel, *next_channel; isc_logchannel_t *channel;
isc_logchannellist_t *item, *next_item; isc_logchannellist_t *item;
unsigned int i; unsigned int i;
REQUIRE(lcfgp != NULL && VALID_CONFIG(*lcfgp)); REQUIRE(lcfgp != NULL && VALID_CONFIG(*lcfgp));
@@ -439,9 +451,8 @@ isc_logconfig_destroy(isc_logconfig_t **lcfgp) {
mctx = lcfg->lctx->mctx; mctx = lcfg->lctx->mctx;
for (channel = lcfg->channels; channel != NULL; while ((channel = ISC_LIST_HEAD(lcfg->channels)) != NULL) {
channel = next_channel) { ISC_LIST_UNLINK(lcfg->channels, channel, link);
next_channel = channel->next;
if (channel->type == ISC_LOG_TOFILE) { if (channel->type == ISC_LOG_TOFILE) {
isc_mem_free(mctx, FILE_NAME(channel)); isc_mem_free(mctx, FILE_NAME(channel));
@@ -455,14 +466,14 @@ isc_logconfig_destroy(isc_logconfig_t **lcfgp) {
} }
for (i = 0; i < lcfg->channellist_count; i++) for (i = 0; i < lcfg->channellist_count; i++)
for (item = lcfg->channellists[i]; item != NULL; while ((item = ISC_LIST_HEAD(lcfg->channellists[i])) != NULL) {
item = next_item) { ISC_LIST_UNLINK(lcfg->channellists[i], item, link);
next_item = item->next; isc_mem_put(mctx, item, sizeof(*item));
isc_mem_put(mctx, item, sizeof(*item));
} }
isc_mem_put(mctx, lcfg->channellists, isc_mem_put(mctx, lcfg->channellists,
lcfg->channellist_count * sizeof(isc_logchannellist_t **)); lcfg->channellist_count *
sizeof(ISC_LIST(isc_logchannellist_t)));
lcfg->duplicate_interval = 0; lcfg->duplicate_interval = 0;
lcfg->magic = 0; lcfg->magic = 0;
@@ -526,13 +537,13 @@ isc_log_createchannel(isc_logconfig_t *lcfg, const char *name,
type == ISC_LOG_TOFILEDESC || type == ISC_LOG_TONULL); type == ISC_LOG_TOFILEDESC || type == ISC_LOG_TONULL);
REQUIRE(destination != NULL || type == ISC_LOG_TONULL); REQUIRE(destination != NULL || type == ISC_LOG_TONULL);
REQUIRE(level >= ISC_LOG_CRITICAL); REQUIRE(level >= ISC_LOG_CRITICAL);
REQUIRE((flags & ~ISC_LOG_PRINTALL) == 0); REQUIRE((flags & ~(ISC_LOG_PRINTALL | ISC_LOG_DEBUGONLY)) == 0);
/* XXXDCL find duplicate names? */ /* XXXDCL find duplicate names? */
mctx = lcfg->lctx->mctx; mctx = lcfg->lctx->mctx;
channel = (isc_logchannel_t *)isc_mem_get(mctx, sizeof(*channel)); channel = isc_mem_get(mctx, sizeof(*channel));
if (channel == NULL) if (channel == NULL)
return (ISC_R_NOMEMORY); return (ISC_R_NOMEMORY);
@@ -581,8 +592,7 @@ isc_log_createchannel(isc_logconfig_t *lcfg, const char *name,
return (ISC_R_UNEXPECTED); return (ISC_R_UNEXPECTED);
} }
channel->next = lcfg->channels; ISC_LIST_PREPEND(lcfg->channels, channel, link);
lcfg->channels = channel;
/* /*
* If default_stderr was redefined, make the default category * If default_stderr was redefined, make the default category
@@ -611,8 +621,8 @@ isc_log_usechannel(isc_logconfig_t *lcfg, const char *name,
REQUIRE(category == NULL || category->id < lctx->category_count); REQUIRE(category == NULL || category->id < lctx->category_count);
REQUIRE(module == NULL || module->id < lctx->module_count); REQUIRE(module == NULL || module->id < lctx->module_count);
for (channel = lcfg->channels; channel != NULL; for (channel = ISC_LIST_HEAD(lcfg->channels); channel != NULL;
channel = channel->next) channel = ISC_LIST_NEXT(channel, link))
if (strcmp(name, channel->name) == 0) if (strcmp(name, channel->name) == 0)
break; break;
@@ -732,8 +742,9 @@ isc_log_closefilelogs(isc_log_t *lctx) {
REQUIRE(VALID_CONTEXT(lctx)); REQUIRE(VALID_CONTEXT(lctx));
for (channel = lctx->logconfig->channels; channel != NULL; for (channel = ISC_LIST_HEAD(lctx->logconfig->channels);
channel = channel->next) channel != NULL;
channel = ISC_LIST_NEXT(channel, link))
if (channel->type == ISC_LOG_TOFILE && if (channel->type == ISC_LOG_TOFILE &&
FILE_STREAM(channel) != NULL) { FILE_STREAM(channel) != NULL) {
@@ -769,16 +780,13 @@ assignchannel(isc_logconfig_t *lcfg, unsigned int category_id,
if (result != ISC_R_SUCCESS) if (result != ISC_R_SUCCESS)
return (result); return (result);
new_item = (isc_logchannellist_t *)isc_mem_get(lctx->mctx, new_item = isc_mem_get(lctx->mctx, sizeof(*new_item));
sizeof(*new_item));
if (new_item == NULL) if (new_item == NULL)
return (ISC_R_NOMEMORY); return (ISC_R_NOMEMORY);
new_item->channel = channel; new_item->channel = channel;
new_item->module = module; new_item->module = module;
new_item->next = lcfg->channellists[category_id]; ISC_LIST_PREPEND(lcfg->channellists[category_id], new_item, link);
lcfg->channellists[category_id] = new_item;
return (ISC_R_SUCCESS); return (ISC_R_SUCCESS);
} }
@@ -787,7 +795,7 @@ static isc_result_t
sync_channellist(isc_logconfig_t *lcfg) { sync_channellist(isc_logconfig_t *lcfg) {
int bytes; int bytes;
isc_log_t *lctx; isc_log_t *lctx;
isc_logchannellist_t **lists; void *lists;
REQUIRE(VALID_CONFIG(lcfg)); REQUIRE(VALID_CONFIG(lcfg));
@@ -798,9 +806,9 @@ sync_channellist(isc_logconfig_t *lcfg) {
if (lctx->category_count == lcfg->channellist_count) if (lctx->category_count == lcfg->channellist_count)
return (ISC_R_SUCCESS); return (ISC_R_SUCCESS);
bytes = lctx->category_count * sizeof(isc_logchannellist_t *); bytes = lctx->category_count * sizeof(ISC_LIST(isc_logchannellist_t));
lists = (isc_logchannellist_t **)isc_mem_get(lctx->mctx, bytes); lists = isc_mem_get(lctx->mctx, bytes);
if (lists == NULL) if (lists == NULL)
return (ISC_R_NOMEMORY); return (ISC_R_NOMEMORY);
@@ -809,7 +817,7 @@ sync_channellist(isc_logconfig_t *lcfg) {
if (lcfg->channellist_count != 0) { if (lcfg->channellist_count != 0) {
bytes = lcfg->channellist_count * bytes = lcfg->channellist_count *
sizeof(isc_logchannellist_t *); sizeof(ISC_LIST(isc_logchannellist_t));
memcpy(lists, lcfg->channellists, bytes); memcpy(lists, lcfg->channellists, bytes);
isc_mem_put(lctx->mctx, lcfg->channellists, bytes); isc_mem_put(lctx->mctx, lcfg->channellists, bytes);
} }
@@ -872,9 +880,6 @@ roll_log(isc_logchannel_t *channel) {
char new[FILENAME_MAX + 1]; char new[FILENAME_MAX + 1];
char *path; char *path;
/*
* XXXDCL versions = 0 & versions == ISC_LOG_ROLLINFINITE do not work.
*/
/* /*
* Do nothing (not even excess version trimming) if ISC_LOG_ROLLNEVER * Do nothing (not even excess version trimming) if ISC_LOG_ROLLNEVER
* is specified. Apparently complete external control over the log * is specified. Apparently complete external control over the log
@@ -1031,11 +1036,11 @@ isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category,
lcfg = lctx->logconfig; lcfg = lctx->logconfig;
category_channels = lcfg->channellists[category->id]; category_channels = ISC_LIST_HEAD(lcfg->channellists[category->id]);
/* /*
* XXX duplicate filtering (do not write multiple times to same source * XXXDCL add duplicate filtering? (To not write multiple times to
* via various channels) * the same source via various channels).
*/ */
do { do {
/* /*
@@ -1046,12 +1051,13 @@ isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category,
break; break;
if (category_channels == NULL && ! matched && if (category_channels == NULL && ! matched &&
category_channels != lcfg->channellists[0]) category_channels != ISC_LIST_HEAD(lcfg->channellists[0]))
/* /*
* No category/module pair was explicitly configured. * No category/module pair was explicitly configured.
* Try the category named "default". * Try the category named "default".
*/ */
category_channels = lcfg->channellists[0]; category_channels =
ISC_LIST_HEAD(lcfg->channellists[0]);
if (category_channels == NULL && ! matched) if (category_channels == NULL && ! matched)
/* /*
@@ -1063,14 +1069,19 @@ isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category,
if (category_channels->module != NULL && if (category_channels->module != NULL &&
category_channels->module != module) { category_channels->module != module) {
category_channels = category_channels->next; category_channels = ISC_LIST_NEXT(category_channels,
link);
continue; continue;
} }
matched = ISC_TRUE; matched = ISC_TRUE;
channel = category_channels->channel; channel = category_channels->channel;
category_channels = category_channels->next; category_channels = ISC_LIST_NEXT(category_channels, link);
if (((channel->flags & ISC_LOG_DEBUGONLY) > 0) &&
lctx->debug_level == 0)
continue;
if (channel->level == ISC_LOG_DYNAMIC) { if (channel->level == ISC_LOG_DYNAMIC) {
if (lctx->debug_level < level) if (lctx->debug_level < level)