From 2918b5bda6a55c301eb87992b5f2acd7176d0737 Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Tue, 16 May 2000 03:37:39 +0000 Subject: [PATCH] ISC_LOG_PRINTTAG/isc_log_settag()/isc_log_gettag() added --- bin/tests/log_test.c | 83 ++++++++++++++++--------------- lib/isc/include/isc/log.h | 64 ++++++++++++++++++++---- lib/isc/log.c | 102 ++++++++++++++++++++++++-------------- 3 files changed, 162 insertions(+), 87 deletions(-) diff --git a/bin/tests/log_test.c b/bin/tests/log_test.c index 24929c49fe..cc35e4c937 100644 --- a/bin/tests/log_test.c +++ b/bin/tests/log_test.c @@ -15,7 +15,7 @@ * SOFTWARE. */ -/* $Id: log_test.c,v 1.15 2000/05/08 20:12:42 tale Exp $ */ +/* $Id: log_test.c,v 1.16 2000/05/16 03:33:51 tale Exp $ */ /* Principal Authors: DCL */ @@ -36,7 +36,7 @@ char usage[] = "Usage: %s [-m] [-s syslog_logfile] [-r file_versions]\n"; -#define CHECK_ISC(expr) result = expr; \ +#define CHECK(expr) result = expr; \ if (result != ISC_R_SUCCESS) { \ fprintf(stderr, "%s: " #expr "%s: exiting\n", \ progname, isc_result_totext(result)); \ @@ -113,8 +113,10 @@ main (int argc, char **argv) { mctx = NULL; lctx = NULL; - CHECK_ISC(isc_mem_create(0, 0, &mctx)); - CHECK_ISC(isc_log_create(mctx, &lctx, &lcfg)); + CHECK(isc_mem_create(0, 0, &mctx)); + CHECK(isc_log_create(mctx, &lctx, &lcfg)); + + isc_log_settag(lcfg, progname); isc_log_setcontext(lctx); dns_log_init(lctx); @@ -125,13 +127,14 @@ main (int argc, char **argv) { */ category = isc_log_categorybyname(lctx, "xfer-in"); if (category != NULL) - fprintf(stderr, "%s category found.\n", category->name); + fprintf(stderr, "%s category found. (expected)\n", + category->name); else fprintf(stderr, "xfer-in category not found!\n"); module = isc_log_modulebyname(lctx, "xyzzy"); if (module != NULL) - fprintf(stderr, "%s module found! (error)\n", module->name); + fprintf(stderr, "%s module found!\n", module->name); else fprintf(stderr, "xyzzy module not found. (expected)\n"); @@ -144,50 +147,51 @@ main (int argc, char **argv) { destination.file.maximum_size = 1; destination.file.versions = file_versions; - CHECK_ISC(isc_log_createchannel(lcfg, "file_test", ISC_LOG_TOFILE, - ISC_LOG_INFO, &destination, - ISC_LOG_PRINTTIME| - ISC_LOG_PRINTLEVEL| - ISC_LOG_PRINTCATEGORY| - ISC_LOG_PRINTMODULE)); + CHECK(isc_log_createchannel(lcfg, "file_test", ISC_LOG_TOFILE, + ISC_LOG_INFO, &destination, + ISC_LOG_PRINTTIME| + ISC_LOG_PRINTTAG| + ISC_LOG_PRINTLEVEL| + ISC_LOG_PRINTCATEGORY| + ISC_LOG_PRINTMODULE)); /* * Create a dynamic debugging channel to a file descriptor. */ destination.file.stream = stderr; - CHECK_ISC(isc_log_createchannel(lcfg, "debug_test", ISC_LOG_TOFILEDESC, - ISC_LOG_DYNAMIC, &destination, - ISC_LOG_PRINTTIME| - ISC_LOG_PRINTLEVEL| - ISC_LOG_DEBUGONLY)); + CHECK(isc_log_createchannel(lcfg, "debug_test", ISC_LOG_TOFILEDESC, + ISC_LOG_DYNAMIC, &destination, + ISC_LOG_PRINTTIME| + ISC_LOG_PRINTLEVEL| + ISC_LOG_DEBUGONLY)); /* * Test the usability of the four predefined logging channels. */ - CHECK_ISC(isc_log_usechannel(lcfg, "default_syslog", - DNS_LOGCATEGORY_DATABASE, - DNS_LOGMODULE_CACHE)); - CHECK_ISC(isc_log_usechannel(lcfg, "default_stderr", - DNS_LOGCATEGORY_DATABASE, - DNS_LOGMODULE_CACHE)); - CHECK_ISC(isc_log_usechannel(lcfg, "default_debug", - DNS_LOGCATEGORY_DATABASE, - DNS_LOGMODULE_CACHE)); - CHECK_ISC(isc_log_usechannel(lcfg, "null", - DNS_LOGCATEGORY_DATABASE, - NULL)); + CHECK(isc_log_usechannel(lcfg, "default_syslog", + DNS_LOGCATEGORY_DATABASE, + DNS_LOGMODULE_CACHE)); + CHECK(isc_log_usechannel(lcfg, "default_stderr", + DNS_LOGCATEGORY_DATABASE, + DNS_LOGMODULE_CACHE)); + CHECK(isc_log_usechannel(lcfg, "default_debug", + DNS_LOGCATEGORY_DATABASE, + DNS_LOGMODULE_CACHE)); + CHECK(isc_log_usechannel(lcfg, "null", + DNS_LOGCATEGORY_DATABASE, + NULL)); /* * Use the custom channels. */ - CHECK_ISC(isc_log_usechannel(lcfg, "file_test", - DNS_LOGCATEGORY_GENERAL, - DNS_LOGMODULE_DB)); + CHECK(isc_log_usechannel(lcfg, "file_test", + DNS_LOGCATEGORY_GENERAL, + DNS_LOGMODULE_DB)); - CHECK_ISC(isc_log_usechannel(lcfg, "debug_test", - DNS_LOGCATEGORY_GENERAL, - DNS_LOGMODULE_RBTDB)); + CHECK(isc_log_usechannel(lcfg, "debug_test", + DNS_LOGCATEGORY_GENERAL, + DNS_LOGMODULE_RBTDB)); fprintf(stderr, "\n==> stderr begin\n"); @@ -225,9 +229,8 @@ main (int argc, char **argv) { * Reset the internal default to use syslog instead of stderr, * and test it. */ - CHECK_ISC(isc_log_usechannel(lcfg, "default_syslog", - ISC_LOGCATEGORY_DEFAULT, - NULL)); + CHECK(isc_log_usechannel(lcfg, "default_syslog", + ISC_LOGCATEGORY_DEFAULT, NULL)); isc_log_write(lctx, DNS_LOGCATEGORY_SECURITY, DNS_LOGMODULE_RBT, ISC_LOG_ERROR, "%s%s", "This message to the redefined default category should ", @@ -254,12 +257,12 @@ main (int argc, char **argv) { for (i = file_versions - 1; i >= 0; i--) isc_log_write(lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_DB, ISC_LOG_NOTICE, - "This should be in file %d/%d", i, + "should be in file %d/%d", i, file_versions - 1); isc_log_write(lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_DB, ISC_LOG_NOTICE, - "This should be in the base file"); + "should be in base file"); } else { file_versions = FILE_VERSIONS; diff --git a/lib/isc/include/isc/log.h b/lib/isc/include/isc/log.h index 9c02eab037..c3cbb21914 100644 --- a/lib/isc/include/isc/log.h +++ b/lib/isc/include/isc/log.h @@ -15,7 +15,7 @@ * SOFTWARE. */ -/* $Id: log.h,v 1.19 2000/05/03 21:09:34 explorer Exp $ */ +/* $Id: log.h,v 1.20 2000/05/16 03:37:39 tale Exp $ */ #ifndef ISC_LOG_H #define ISC_LOG_H 1 @@ -58,8 +58,9 @@ ISC_LANG_BEGINDECLS #define ISC_LOG_PRINTLEVEL 0x0002 #define ISC_LOG_PRINTCATEGORY 0x0004 #define ISC_LOG_PRINTMODULE 0x0008 -#define ISC_LOG_PRINTALL 0x000F -#define ISC_LOG_DEBUGONLY 0x0010 +#define ISC_LOG_PRINTTAG 0x0010 +#define ISC_LOG_PRINTALL 0x001F +#define ISC_LOG_DEBUGONLY 0x1000 /* * Other options. @@ -390,8 +391,8 @@ isc_log_createchannel(isc_logconfig_t *lcfg, const char *name, * call by defining a new channel and then calling isc_log_usechannel() * for ISC_LOGCATEGORY_DEFAULT.) * - * Specifying ISC_LOG_PRINTTIME for syslog is allowed, but probably - * not what you wanted to do. + * Specifying ISC_LOG_PRINTTIME or ISC_LOG_PRINTTAG for syslog is allowed, + * but probably not what you wanted to do. * * ISC_LOG_DEBUGONLY will mark the channel as usable only when the * debug level of the logging context (see isc_log_setdebuglevel) @@ -637,9 +638,6 @@ isc_log_setduplicateinterval(isc_logconfig_t *lcfg, unsigned int interval); * * Requires: * lctx is a valid logging context. - * - * Ensures: - * The duplicate interval is set to the current */ unsigned int @@ -650,8 +648,54 @@ isc_log_getduplicateinterval(isc_logconfig_t *lcfg); * Requires: * lctx is a valid logging context. * - * Ensures: - * The current duplicate filtering interval is returned. + * Returns: + * The current duplicate filtering interval. + */ + +void +isc_log_settag(isc_logconfig_t *lcfg, char *tag); +/* + * Set the program name or other identifier for ISC_LOG_PRINTTAG. + * + * Requires: + * lcfg is a valid logging configuration. + * + * Notes: + * If this function has not set the tag to a non-NULL, non-empty value, + * then the ISC_LOG_PRINTTAG channel flag will not print anything. + * Unlike some implementations of syslog on Unix systems, you *must* set + * the tag in order to get it logged. It is not implicitly derived from + * the program name (which is pretty impossible to infer portably). + * + * Setting the tag to NULL or the empty string will also cause the + * ISC_LOG_PRINTTAG channel flag to not print anything. If tag equals the + * empty string, calls to isc_log_gettag will return NULL. + * + * Because the name is used by ISC_LOG_PRINTTAG, it should not be + * altered or destroyed after isc_log_settag(). + * + * XXXDCL when creating a new isc_logconfig_t, it might be nice if the tag + * of the currently active isc_logconfig_t was inherited. this does not + * currently happen. + */ + +char * +isc_log_gettag(isc_logconfig_t *lcfg); +/* + * Get the current identifier printed with ISC_LOG_PRINTTAG. + * + * Requires: + * lcfg is a valid logging configuration. + * + * Notes: + * Since isc_log_settag() will not associate a zero-length string + * with the logging configuration, attempts to do so will cause + * this function to return NULL. However, a determined programmer + * will observe that (currently) a tag of length greater than zero + * could be set, and then modified to be zero length. + * + * Returns: + * A pointer to the current identifier, or NULL if none has been set. */ void diff --git a/lib/isc/log.c b/lib/isc/log.c index f46cc333d6..e746449853 100644 --- a/lib/isc/log.c +++ b/lib/isc/log.c @@ -15,7 +15,7 @@ * SOFTWARE. */ -/* $Id: log.c,v 1.30 2000/05/08 14:37:26 tale Exp $ */ +/* $Id: log.c,v 1.31 2000/05/16 03:37:37 tale Exp $ */ /* Principal Authors: DCL */ @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -36,9 +37,9 @@ #include #define LCTX_MAGIC 0x4C637478U /* Lctx. */ -#define VALID_CONTEXT(lctx) ((lctx) != NULL && (lctx)->magic == LCTX_MAGIC) +#define VALID_CONTEXT(lctx) ISC_MAGIC_VALID(lctx, LCTX_MAGIC) #define LCFG_MAGIC 0x4C636667U /* Lcfg. */ -#define VALID_CONFIG(lcfg) ((lcfg) != NULL && (lcfg)->magic == LCFG_MAGIC) +#define VALID_CONFIG(lcfg) ISC_MAGIC_VALID(lcfg, LCFG_MAGIC) /* * XXXDCL make dynamic? @@ -108,6 +109,7 @@ struct isc_logconfig { unsigned int channellist_count; unsigned int duplicate_interval; int highest_level; + char * tag; isc_boolean_t dynamic; }; @@ -310,6 +312,7 @@ isc_logconfig_create(isc_log_t *lctx, isc_logconfig_t **lcfgp) { lcfg->channellist_count = 0; lcfg->duplicate_interval = 0; lcfg->highest_level = ISC_LOG_CRITICAL; + lcfg->tag = NULL; lcfg->dynamic = ISC_FALSE; ISC_LIST_INIT(lcfg->channels); @@ -507,6 +510,7 @@ isc_logconfig_destroy(isc_logconfig_t **lcfgp) { sizeof(ISC_LIST(isc_logchannellist_t))); lcfg->dynamic = ISC_FALSE; + lcfg->tag = NULL; lcfg->highest_level = 0; lcfg->duplicate_interval = 0; lcfg->magic = 0; @@ -840,6 +844,23 @@ isc_log_getduplicateinterval(isc_logconfig_t *lcfg) { return (lcfg->duplicate_interval); } +void +isc_log_settag(isc_logconfig_t *lcfg, char *tag) { + REQUIRE(VALID_CONFIG(lcfg)); + + if (tag != NULL && *tag != '\0') + lcfg->tag = tag; + else + lcfg->tag = NULL; +} + +char * +isc_log_gettag(isc_logconfig_t *lcfg) { + REQUIRE(VALID_CONFIG(lcfg)); + + return (lcfg->tag); +} + /* XXXDCL NT -- This interface will assuredly be changing. */ void isc_log_opensyslog(const char *tag, int options, int facility) { @@ -1126,8 +1147,11 @@ isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category, int syslog_level; char time_string[64]; char level_string[24]; + char pid_string[sizeof("[99999]")]; struct stat statbuf; isc_boolean_t matched = ISC_FALSE; + isc_boolean_t printtime, printtag; + isc_boolean_t printcategory, printmodule, printlevel; isc_logconfig_t *lcfg; isc_logchannel_t *channel; isc_logchannellist_t *category_channels; @@ -1169,8 +1193,9 @@ isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category, REQUIRE(level != ISC_LOG_DYNAMIC); REQUIRE(format != NULL); - time_string[0] = '\0'; + time_string[0] = '\0'; level_string[0] = '\0'; + pid_string[0] = '\0'; lctx->buffer[0] = '\0'; LOCK(&lctx->lock); @@ -1220,7 +1245,7 @@ isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category, channel = category_channels->channel; category_channels = ISC_LIST_NEXT(category_channels, link); - if (((channel->flags & ISC_LOG_DEBUGONLY) > 0) && + if (((channel->flags & ISC_LOG_DEBUGONLY) != 0) && lctx->debug_level == 0) continue; @@ -1230,7 +1255,7 @@ isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category, } else if (channel->level < level) continue; - if ((channel->flags & ISC_LOG_PRINTTIME) && + if ((channel->flags & ISC_LOG_PRINTTIME) != 0 && time_string[0] == '\0') { result = isc_time_now(&time); @@ -1269,7 +1294,7 @@ isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category, } - if ((channel->flags & ISC_LOG_PRINTLEVEL) && + if ((channel->flags & ISC_LOG_PRINTLEVEL) != 0 && level_string[0] == '\0') { if (level < ISC_LOG_CRITICAL) sprintf(level_string, "level %d: ", level); @@ -1391,6 +1416,17 @@ isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category, } } + printtime = ISC_TF((channel->flags & ISC_LOG_PRINTTIME) + != 0); + printtag = ISC_TF((channel->flags & ISC_LOG_PRINTTAG) + != 0 && lcfg->tag != NULL); + printcategory = ISC_TF((channel->flags & ISC_LOG_PRINTCATEGORY) + != 0); + printmodule = ISC_TF((channel->flags & ISC_LOG_PRINTMODULE) + != 0); + printlevel = ISC_TF((channel->flags & ISC_LOG_PRINTLEVEL) + != 0); + switch (channel->type) { case ISC_LOG_TOFILE: if (FILE_STREAM(channel) == NULL) { @@ -1405,21 +1441,17 @@ isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category, /* FALLTHROUGH */ case ISC_LOG_TOFILEDESC: - fprintf(FILE_STREAM(channel), "%s%s%s%s%s%s%s\n", - (channel->flags & ISC_LOG_PRINTTIME) ? - time_string : "", - (channel->flags & ISC_LOG_PRINTCATEGORY) ? - category->name : "", - (channel->flags & ISC_LOG_PRINTCATEGORY) ? - ": " : "", - (channel->flags & ISC_LOG_PRINTMODULE) ? - (module != NULL ? module->name : - "no_module") - : "", - (channel->flags & ISC_LOG_PRINTMODULE) ? - ": " : "", - (channel->flags & ISC_LOG_PRINTLEVEL) ? - level_string : "", + fprintf(FILE_STREAM(channel), "%s%s%s%s%s%s%s%s%s\n", + printtime ? time_string : "", + printtag ? lcfg->tag : "", + printtag ? ": " : "", + printcategory ? category->name : "", + printcategory ? ": " : "", + printmodule ? (module != NULL ? module->name + : "no_module") + : "", + printmodule ? ": " : "", + printlevel ? level_string : "", lctx->buffer); fflush(FILE_STREAM(channel)); @@ -1453,21 +1485,17 @@ isc_log_doit(isc_log_t *lctx, isc_logcategory_t *category, syslog_level = syslog_map[-level]; syslog(FACILITY(channel) | syslog_level, - "%s%s%s%s%s%s%s", - (channel->flags & ISC_LOG_PRINTTIME) ? - time_string : "", - (channel->flags & ISC_LOG_PRINTCATEGORY) ? - category->name : "", - (channel->flags & ISC_LOG_PRINTCATEGORY) ? - ": " : "", - (channel->flags & ISC_LOG_PRINTMODULE) ? - (module != NULL ? module->name : - "no_module") - : "", - (channel->flags & ISC_LOG_PRINTMODULE) ? - ": " : "", - (channel->flags & ISC_LOG_PRINTLEVEL) ? - level_string : "", + "%s%s%s%s%s%s%s%s%s", + printtime ? time_string : "", + printtag ? lcfg->tag : "", + printtag ? ": " : "", + printcategory ? category->name : "", + printcategory ? ": " : "", + printmodule ? (module != NULL ? module->name + : "no_module") + : "", + printmodule ? ": " : "", + printlevel ? level_string : "", lctx->buffer); break;