2 * Copyright (C) 2004, 2006 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: log.c,v 1.70.2.8.2.14 2006/03/02 00:37:20 marka Exp $ */
20 /* Principal Authors: DCL */
29 #include <sys/types.h> /* dev_t FreeBSD 2.1 */
34 #include <isc/magic.h>
37 #include <isc/print.h>
39 #include <isc/stdio.h>
40 #include <isc/string.h>
44 #define LCTX_MAGIC ISC_MAGIC('L', 'c', 't', 'x')
45 #define VALID_CONTEXT(lctx) ISC_MAGIC_VALID(lctx, LCTX_MAGIC)
47 #define LCFG_MAGIC ISC_MAGIC('L', 'c', 'f', 'g')
48 #define VALID_CONFIG(lcfg) ISC_MAGIC_VALID(lcfg, LCFG_MAGIC)
51 * XXXDCL make dynamic?
53 #define LOG_BUFFER_SIZE (8 * 1024)
56 #define PATH_MAX 1024 /* AIX and others don't define this. */
60 * This is the structure that holds each named channel. A simple linked
61 * list chains all of the channels together, so an individual channel is
62 * found by doing strcmp()s with the names down the list. Their should
63 * be no peformance penalty from this as it is expected that the number
64 * of named channels will be no more than a dozen or so, and name lookups
65 * from the head of the list are only done when isc_log_usechannel() is
66 * called, which should also be very infrequent.
68 typedef struct isc_logchannel isc_logchannel_t
;
70 struct isc_logchannel
{
75 isc_logdestination_t destination
;
76 ISC_LINK(isc_logchannel_t
) link
;
80 * The logchannellist structure associates categories and modules with
81 * channels. First the appropriate channellist is found based on the
82 * category, and then each structure in the linked list is checked for
83 * a matching module. It is expected that the number of channels
84 * associated with any given category will be very short, no more than
85 * three or four in the more unusual cases.
87 typedef struct isc_logchannellist isc_logchannellist_t
;
89 struct isc_logchannellist
{
90 const isc_logmodule_t
* module
;
91 isc_logchannel_t
* channel
;
92 ISC_LINK(isc_logchannellist_t
) link
;
96 * This structure is used to remember messages for pruning via
97 * isc_log_[v]write1().
99 typedef struct isc_logmessage isc_logmessage_t
;
101 struct isc_logmessage
{
104 ISC_LINK(isc_logmessage_t
) link
;
108 * The isc_logconfig structure is used to store the configurable information
109 * about where messages are actually supposed to be sent -- the information
110 * that could changed based on some configuration file, as opposed to the
111 * the category/module specification of isc_log_[v]write[1] that is compiled
112 * into a program, or the debug_level which is dynamic state information.
114 struct isc_logconfig
{
117 ISC_LIST(isc_logchannel_t
) channels
;
118 ISC_LIST(isc_logchannellist_t
) *channellists
;
119 unsigned int channellist_count
;
120 unsigned int duplicate_interval
;
123 isc_boolean_t dynamic
;
127 * This isc_log structure provides the context for the isc_log functions.
128 * The log context locks itself in isc_log_doit, the internal backend to
129 * isc_log_write. The locking is necessary both to provide exclusive access
130 * to the the buffer into which the message is formatted and to guard against
131 * competing threads trying to write to the same syslog resource. (On
132 * some systems, such as BSD/OS, stdio is thread safe but syslog is not.)
133 * Unfortunately, the lock cannot guard against a _different_ logging
134 * context in the same program competing for syslog's attention. Thus
135 * There Can Be Only One, but this is not enforced.
138 * Note that the category and module information is not locked.
139 * This is because in the usual case, only one isc_log_t is ever created
140 * in a program, and the category/module registration happens only once.
141 * XXXDCL it might be wise to add more locking overall.
147 isc_logcategory_t
* categories
;
148 unsigned int category_count
;
149 isc_logmodule_t
* modules
;
150 unsigned int module_count
;
153 /* Locked by isc_log lock. */
154 isc_logconfig_t
* logconfig
;
155 char buffer
[LOG_BUFFER_SIZE
];
156 ISC_LIST(isc_logmessage_t
) messages
;
160 * Used when ISC_LOG_PRINTLEVEL is enabled for a channel.
162 static const char *log_level_strings
[] = {
172 * Used to convert ISC_LOG_* priorities into syslog priorities.
173 * XXXDCL This will need modification for NT.
175 static const int syslog_map
[] = {
185 * When adding new categories, a corresponding ISC_LOGCATEGORY_foo
186 * definition needs to be added to <isc/log.h>.
188 * The default category is provided so that the internal default can
189 * be overridden. Since the default is always looked up as the first
190 * channellist in the log context, it must come first in isc_categories[].
192 LIBISC_EXTERNAL_DATA isc_logcategory_t isc_categories
[] = {
193 { "default", 0 }, /* "default" must come first. */
199 * See above comment for categories, and apply it to modules.
201 LIBISC_EXTERNAL_DATA isc_logmodule_t isc_modules
[] = {
210 * This essentially constant structure must be filled in at run time,
211 * because its channel member is pointed to a channel that is created
212 * dynamically with isc_log_createchannel.
214 static isc_logchannellist_t default_channel
;
217 * libisc logs to this context.
219 LIBISC_EXTERNAL_DATA isc_log_t
*isc_lctx
= NULL
;
222 * Forward declarations.
225 assignchannel(isc_logconfig_t
*lcfg
, unsigned int category_id
,
226 const isc_logmodule_t
*module
, isc_logchannel_t
*channel
);
229 sync_channellist(isc_logconfig_t
*lcfg
);
232 greatest_version(isc_logchannel_t
*channel
, int *greatest
);
235 roll_log(isc_logchannel_t
*channel
);
238 isc_log_doit(isc_log_t
*lctx
, isc_logcategory_t
*category
,
239 isc_logmodule_t
*module
, int level
, isc_boolean_t write_once
,
240 isc_msgcat_t
*msgcat
, int msgset
, int msg
,
241 const char *format
, va_list args
)
242 ISC_FORMAT_PRINTF(9, 0);
245 * Convenience macros.
248 #define FACILITY(channel) (channel->destination.facility)
249 #define FILE_NAME(channel) (channel->destination.file.name)
250 #define FILE_STREAM(channel) (channel->destination.file.stream)
251 #define FILE_VERSIONS(channel) (channel->destination.file.versions)
252 #define FILE_MAXSIZE(channel) (channel->destination.file.maximum_size)
253 #define FILE_MAXREACHED(channel) (channel->destination.file.maximum_reached)
256 **** Public interfaces.
260 * Establish a new logging context, with default channels.
263 isc_log_create(isc_mem_t
*mctx
, isc_log_t
**lctxp
, isc_logconfig_t
**lcfgp
) {
265 isc_logconfig_t
*lcfg
= NULL
;
268 REQUIRE(mctx
!= NULL
);
269 REQUIRE(lctxp
!= NULL
&& *lctxp
== NULL
);
270 REQUIRE(lcfgp
== NULL
|| *lcfgp
== NULL
);
272 lctx
= isc_mem_get(mctx
, sizeof(*lctx
));
275 lctx
->categories
= NULL
;
276 lctx
->category_count
= 0;
277 lctx
->modules
= NULL
;
278 lctx
->module_count
= 0;
279 lctx
->debug_level
= 0;
281 ISC_LIST_INIT(lctx
->messages
);
283 RUNTIME_CHECK(isc_mutex_init(&lctx
->lock
) == ISC_R_SUCCESS
);
286 * Normally setting the magic number is the last step done
287 * in a creation function, but a valid log context is needed
288 * by isc_log_registercategories and isc_logconfig_create.
289 * If either fails, the lctx is destroyed and not returned
292 lctx
->magic
= LCTX_MAGIC
;
294 isc_log_registercategories(lctx
, isc_categories
);
295 isc_log_registermodules(lctx
, isc_modules
);
296 result
= isc_logconfig_create(lctx
, &lcfg
);
299 result
= ISC_R_NOMEMORY
;
301 if (result
== ISC_R_SUCCESS
)
302 result
= sync_channellist(lcfg
);
304 if (result
== ISC_R_SUCCESS
) {
305 lctx
->logconfig
= lcfg
;
313 isc_logconfig_destroy(&lcfg
);
315 isc_log_destroy(&lctx
);
322 isc_logconfig_create(isc_log_t
*lctx
, isc_logconfig_t
**lcfgp
) {
323 isc_logconfig_t
*lcfg
;
324 isc_logdestination_t destination
;
325 isc_result_t result
= ISC_R_SUCCESS
;
326 int level
= ISC_LOG_INFO
;
328 REQUIRE(lcfgp
!= NULL
&& *lcfgp
== NULL
);
329 REQUIRE(VALID_CONTEXT(lctx
));
331 lcfg
= isc_mem_get(lctx
->mctx
, sizeof(*lcfg
));
335 lcfg
->channellists
= NULL
;
336 lcfg
->channellist_count
= 0;
337 lcfg
->duplicate_interval
= 0;
338 lcfg
->highest_level
= level
;
340 lcfg
->dynamic
= ISC_FALSE
;
342 ISC_LIST_INIT(lcfg
->channels
);
345 * Normally the magic number is the last thing set in the
346 * structure, but isc_log_createchannel() needs a valid
347 * config. If the channel creation fails, the lcfg is not
348 * returned to the caller.
350 lcfg
->magic
= LCFG_MAGIC
;
353 result
= ISC_R_NOMEMORY
;
356 * Create the default channels:
357 * default_syslog, default_stderr, default_debug and null.
359 if (result
== ISC_R_SUCCESS
) {
360 destination
.facility
= LOG_DAEMON
;
361 result
= isc_log_createchannel(lcfg
, "default_syslog",
362 ISC_LOG_TOSYSLOG
, level
,
366 if (result
== ISC_R_SUCCESS
) {
367 destination
.file
.stream
= stderr
;
368 destination
.file
.name
= NULL
;
369 destination
.file
.versions
= ISC_LOG_ROLLNEVER
;
370 destination
.file
.maximum_size
= 0;
371 result
= isc_log_createchannel(lcfg
, "default_stderr",
378 if (result
== ISC_R_SUCCESS
) {
380 * Set the default category's channel to default_stderr,
381 * which is at the head of the channels list because it was
384 default_channel
.channel
= ISC_LIST_HEAD(lcfg
->channels
);
386 destination
.file
.stream
= stderr
;
387 destination
.file
.name
= NULL
;
388 destination
.file
.versions
= ISC_LOG_ROLLNEVER
;
389 destination
.file
.maximum_size
= 0;
390 result
= isc_log_createchannel(lcfg
, "default_debug",
397 if (result
== ISC_R_SUCCESS
)
398 result
= isc_log_createchannel(lcfg
, "null",
403 if (result
== ISC_R_SUCCESS
)
408 isc_logconfig_destroy(&lcfg
);
414 isc_logconfig_get(isc_log_t
*lctx
) {
415 REQUIRE(VALID_CONTEXT(lctx
));
417 ENSURE(lctx
->logconfig
!= NULL
);
419 return (lctx
->logconfig
);
423 isc_logconfig_use(isc_log_t
*lctx
, isc_logconfig_t
*lcfg
) {
424 isc_logconfig_t
*old_cfg
;
427 REQUIRE(VALID_CONTEXT(lctx
));
428 REQUIRE(VALID_CONFIG(lcfg
));
429 REQUIRE(lcfg
->lctx
== lctx
);
432 * Ensure that lcfg->channellist_count == lctx->category_count.
433 * They won't be equal if isc_log_usechannel has not been called
434 * since any call to isc_log_registercategories.
436 result
= sync_channellist(lcfg
);
437 if (result
!= ISC_R_SUCCESS
)
442 old_cfg
= lctx
->logconfig
;
443 lctx
->logconfig
= lcfg
;
447 isc_logconfig_destroy(&old_cfg
);
449 return (ISC_R_SUCCESS
);
453 isc_log_destroy(isc_log_t
**lctxp
) {
455 isc_logconfig_t
*lcfg
;
457 isc_logmessage_t
*message
;
459 REQUIRE(lctxp
!= NULL
&& VALID_CONTEXT(*lctxp
));
464 if (lctx
->logconfig
!= NULL
) {
465 lcfg
= lctx
->logconfig
;
466 lctx
->logconfig
= NULL
;
467 isc_logconfig_destroy(&lcfg
);
470 DESTROYLOCK(&lctx
->lock
);
472 while ((message
= ISC_LIST_HEAD(lctx
->messages
)) != NULL
) {
473 ISC_LIST_UNLINK(lctx
->messages
, message
, link
);
475 isc_mem_put(mctx
, message
,
476 sizeof(*message
) + strlen(message
->text
) + 1);
479 lctx
->buffer
[0] = '\0';
480 lctx
->debug_level
= 0;
481 lctx
->categories
= NULL
;
482 lctx
->category_count
= 0;
483 lctx
->modules
= NULL
;
484 lctx
->module_count
= 0;
488 isc_mem_put(mctx
, lctx
, sizeof(*lctx
));
494 isc_logconfig_destroy(isc_logconfig_t
**lcfgp
) {
495 isc_logconfig_t
*lcfg
;
497 isc_logchannel_t
*channel
;
498 isc_logchannellist_t
*item
;
502 REQUIRE(lcfgp
!= NULL
&& VALID_CONFIG(*lcfgp
));
507 * This function cannot be called with a logconfig that is in
508 * use by a log context.
510 REQUIRE(lcfg
->lctx
!= NULL
&& lcfg
->lctx
->logconfig
!= lcfg
);
512 mctx
= lcfg
->lctx
->mctx
;
514 while ((channel
= ISC_LIST_HEAD(lcfg
->channels
)) != NULL
) {
515 ISC_LIST_UNLINK(lcfg
->channels
, channel
, link
);
517 if (channel
->type
== ISC_LOG_TOFILE
) {
519 * The filename for the channel may have ultimately
520 * started its life in user-land as a const string,
521 * but in isc_log_createchannel it gets copied
522 * into writable memory and is not longer truly const.
524 DE_CONST(FILE_NAME(channel
), filename
);
525 isc_mem_free(mctx
, filename
);
527 if (FILE_STREAM(channel
) != NULL
)
528 (void)fclose(FILE_STREAM(channel
));
531 isc_mem_free(mctx
, channel
->name
);
532 isc_mem_put(mctx
, channel
, sizeof(*channel
));
535 for (i
= 0; i
< lcfg
->channellist_count
; i
++)
536 while ((item
= ISC_LIST_HEAD(lcfg
->channellists
[i
])) != NULL
) {
537 ISC_LIST_UNLINK(lcfg
->channellists
[i
], item
, link
);
538 isc_mem_put(mctx
, item
, sizeof(*item
));
541 if (lcfg
->channellist_count
> 0)
542 isc_mem_put(mctx
, lcfg
->channellists
,
543 lcfg
->channellist_count
*
544 sizeof(ISC_LIST(isc_logchannellist_t
)));
546 lcfg
->dynamic
= ISC_FALSE
;
547 if (lcfg
->tag
!= NULL
)
548 isc_mem_free(lcfg
->lctx
->mctx
, lcfg
->tag
);
550 lcfg
->highest_level
= 0;
551 lcfg
->duplicate_interval
= 0;
554 isc_mem_put(mctx
, lcfg
, sizeof(*lcfg
));
560 isc_log_registercategories(isc_log_t
*lctx
, isc_logcategory_t categories
[]) {
561 isc_logcategory_t
*catp
;
563 REQUIRE(VALID_CONTEXT(lctx
));
564 REQUIRE(categories
!= NULL
&& categories
[0].name
!= NULL
);
567 * XXXDCL This somewhat sleazy situation of using the last pointer
568 * in one category array to point to the next array exists because
569 * this registration function returns void and I didn't want to have
570 * change everything that used it by making it return an isc_result_t.
571 * It would need to do that if it had to allocate memory to store
572 * pointers to each array passed in.
574 if (lctx
->categories
== NULL
)
575 lctx
->categories
= categories
;
579 * Adjust the last (NULL) pointer of the already registered
580 * categories to point to the incoming array.
582 for (catp
= lctx
->categories
; catp
->name
!= NULL
; )
583 if (catp
->id
== UINT_MAX
)
585 * The name pointer points to the next array.
588 DE_CONST(catp
->name
, catp
);
592 catp
->name
= (void *)categories
;
597 * Update the id number of the category with its new global id.
599 for (catp
= categories
; catp
->name
!= NULL
; catp
++)
600 catp
->id
= lctx
->category_count
++;
604 isc_log_categorybyname(isc_log_t
*lctx
, const char *name
) {
605 isc_logcategory_t
*catp
;
607 REQUIRE(VALID_CONTEXT(lctx
));
608 REQUIRE(name
!= NULL
);
610 for (catp
= lctx
->categories
; catp
->name
!= NULL
; )
611 if (catp
->id
== UINT_MAX
)
613 * catp is neither modified nor returned to the
614 * caller, so removing its const qualifier is ok.
616 DE_CONST(catp
->name
, catp
);
618 if (strcmp(catp
->name
, name
) == 0)
627 isc_log_registermodules(isc_log_t
*lctx
, isc_logmodule_t modules
[]) {
628 isc_logmodule_t
*modp
;
630 REQUIRE(VALID_CONTEXT(lctx
));
631 REQUIRE(modules
!= NULL
&& modules
[0].name
!= NULL
);
634 * XXXDCL This somewhat sleazy situation of using the last pointer
635 * in one category array to point to the next array exists because
636 * this registration function returns void and I didn't want to have
637 * change everything that used it by making it return an isc_result_t.
638 * It would need to do that if it had to allocate memory to store
639 * pointers to each array passed in.
641 if (lctx
->modules
== NULL
)
642 lctx
->modules
= modules
;
646 * Adjust the last (NULL) pointer of the already registered
647 * modules to point to the incoming array.
649 for (modp
= lctx
->modules
; modp
->name
!= NULL
; )
650 if (modp
->id
== UINT_MAX
)
652 * The name pointer points to the next array.
655 DE_CONST(modp
->name
, modp
);
659 modp
->name
= (void *)modules
;
664 * Update the id number of the module with its new global id.
666 for (modp
= modules
; modp
->name
!= NULL
; modp
++)
667 modp
->id
= lctx
->module_count
++;
671 isc_log_modulebyname(isc_log_t
*lctx
, const char *name
) {
672 isc_logmodule_t
*modp
;
674 REQUIRE(VALID_CONTEXT(lctx
));
675 REQUIRE(name
!= NULL
);
677 for (modp
= lctx
->modules
; modp
->name
!= NULL
; )
678 if (modp
->id
== UINT_MAX
)
680 * modp is neither modified nor returned to the
681 * caller, so removing its const qualifier is ok.
683 DE_CONST(modp
->name
, modp
);
685 if (strcmp(modp
->name
, name
) == 0)
694 isc_log_createchannel(isc_logconfig_t
*lcfg
, const char *name
,
695 unsigned int type
, int level
,
696 const isc_logdestination_t
*destination
,
699 isc_logchannel_t
*channel
;
702 REQUIRE(VALID_CONFIG(lcfg
));
703 REQUIRE(name
!= NULL
);
704 REQUIRE(type
== ISC_LOG_TOSYSLOG
|| type
== ISC_LOG_TOFILE
||
705 type
== ISC_LOG_TOFILEDESC
|| type
== ISC_LOG_TONULL
);
706 REQUIRE(destination
!= NULL
|| type
== ISC_LOG_TONULL
);
707 REQUIRE(level
>= ISC_LOG_CRITICAL
);
709 (unsigned int)~(ISC_LOG_PRINTALL
| ISC_LOG_DEBUGONLY
)) == 0);
711 /* XXXDCL find duplicate names? */
713 mctx
= lcfg
->lctx
->mctx
;
715 channel
= isc_mem_get(mctx
, sizeof(*channel
));
717 return (ISC_R_NOMEMORY
);
719 channel
->name
= isc_mem_strdup(mctx
, name
);
720 if (channel
->name
== NULL
) {
721 isc_mem_put(mctx
, channel
, sizeof(*channel
));
722 return (ISC_R_NOMEMORY
);
725 channel
->type
= type
;
726 channel
->level
= level
;
727 channel
->flags
= flags
;
728 ISC_LINK_INIT(channel
, link
);
731 case ISC_LOG_TOSYSLOG
:
732 FACILITY(channel
) = destination
->facility
;
737 * The file name is copied because greatest_version wants
738 * to scribble on it, so it needs to be definitely in
742 isc_mem_strdup(mctx
, destination
->file
.name
);
743 FILE_STREAM(channel
) = NULL
;
744 FILE_VERSIONS(channel
) = destination
->file
.versions
;
745 FILE_MAXSIZE(channel
) = destination
->file
.maximum_size
;
746 FILE_MAXREACHED(channel
) = ISC_FALSE
;
749 case ISC_LOG_TOFILEDESC
:
750 FILE_NAME(channel
) = NULL
;
751 FILE_STREAM(channel
) = destination
->file
.stream
;
752 FILE_MAXSIZE(channel
) = 0;
753 FILE_VERSIONS(channel
) = ISC_LOG_ROLLNEVER
;
761 isc_mem_put(mctx
, channel
->name
, strlen(channel
->name
) + 1);
762 isc_mem_put(mctx
, channel
, sizeof(*channel
));
763 return (ISC_R_UNEXPECTED
);
766 ISC_LIST_PREPEND(lcfg
->channels
, channel
, link
);
769 * If default_stderr was redefined, make the default category
770 * point to the new default_stderr.
772 if (strcmp(name
, "default_stderr") == 0)
773 default_channel
.channel
= channel
;
775 return (ISC_R_SUCCESS
);
779 isc_log_usechannel(isc_logconfig_t
*lcfg
, const char *name
,
780 const isc_logcategory_t
*category
,
781 const isc_logmodule_t
*module
)
784 isc_logchannel_t
*channel
;
785 isc_result_t result
= ISC_R_SUCCESS
;
788 REQUIRE(VALID_CONFIG(lcfg
));
789 REQUIRE(name
!= NULL
);
793 REQUIRE(category
== NULL
|| category
->id
< lctx
->category_count
);
794 REQUIRE(module
== NULL
|| module
->id
< lctx
->module_count
);
796 for (channel
= ISC_LIST_HEAD(lcfg
->channels
); channel
!= NULL
;
797 channel
= ISC_LIST_NEXT(channel
, link
))
798 if (strcmp(name
, channel
->name
) == 0)
802 return (ISC_R_NOTFOUND
);
804 if (category
!= NULL
)
805 result
= assignchannel(lcfg
, category
->id
, module
, channel
);
809 * Assign to all categories. Note that this includes
810 * the default channel.
812 for (i
= 0; i
< lctx
->category_count
; i
++) {
813 result
= assignchannel(lcfg
, i
, module
, channel
);
814 if (result
!= ISC_R_SUCCESS
)
822 isc_log_write(isc_log_t
*lctx
, isc_logcategory_t
*category
,
823 isc_logmodule_t
*module
, int level
, const char *format
, ...)
828 * Contract checking is done in isc_log_doit().
831 va_start(args
, format
);
832 isc_log_doit(lctx
, category
, module
, level
, ISC_FALSE
,
833 NULL
, 0, 0, format
, args
);
838 isc_log_vwrite(isc_log_t
*lctx
, isc_logcategory_t
*category
,
839 isc_logmodule_t
*module
, int level
,
840 const char *format
, va_list args
)
843 * Contract checking is done in isc_log_doit().
845 isc_log_doit(lctx
, category
, module
, level
, ISC_FALSE
,
846 NULL
, 0, 0, format
, args
);
850 isc_log_write1(isc_log_t
*lctx
, isc_logcategory_t
*category
,
851 isc_logmodule_t
*module
, int level
, const char *format
, ...)
856 * Contract checking is done in isc_log_doit().
859 va_start(args
, format
);
860 isc_log_doit(lctx
, category
, module
, level
, ISC_TRUE
,
861 NULL
, 0, 0, format
, args
);
866 isc_log_vwrite1(isc_log_t
*lctx
, isc_logcategory_t
*category
,
867 isc_logmodule_t
*module
, int level
,
868 const char *format
, va_list args
)
871 * Contract checking is done in isc_log_doit().
873 isc_log_doit(lctx
, category
, module
, level
, ISC_TRUE
,
874 NULL
, 0, 0, format
, args
);
878 isc_log_iwrite(isc_log_t
*lctx
, isc_logcategory_t
*category
,
879 isc_logmodule_t
*module
, int level
,
880 isc_msgcat_t
*msgcat
, int msgset
, int msg
,
881 const char *format
, ...)
886 * Contract checking is done in isc_log_doit().
889 va_start(args
, format
);
890 isc_log_doit(lctx
, category
, module
, level
, ISC_FALSE
,
891 msgcat
, msgset
, msg
, format
, args
);
896 isc_log_ivwrite(isc_log_t
*lctx
, isc_logcategory_t
*category
,
897 isc_logmodule_t
*module
, int level
,
898 isc_msgcat_t
*msgcat
, int msgset
, int msg
,
899 const char *format
, va_list args
)
902 * Contract checking is done in isc_log_doit().
904 isc_log_doit(lctx
, category
, module
, level
, ISC_FALSE
,
905 msgcat
, msgset
, msg
, format
, args
);
909 isc_log_iwrite1(isc_log_t
*lctx
, isc_logcategory_t
*category
,
910 isc_logmodule_t
*module
, int level
,
911 isc_msgcat_t
*msgcat
, int msgset
, int msg
,
912 const char *format
, ...)
917 * Contract checking is done in isc_log_doit().
920 va_start(args
, format
);
921 isc_log_doit(lctx
, category
, module
, level
, ISC_TRUE
,
922 msgcat
, msgset
, msg
, format
, args
);
927 isc_log_ivwrite1(isc_log_t
*lctx
, isc_logcategory_t
*category
,
928 isc_logmodule_t
*module
, int level
,
929 isc_msgcat_t
*msgcat
, int msgset
, int msg
,
930 const char *format
, va_list args
)
933 * Contract checking is done in isc_log_doit().
935 isc_log_doit(lctx
, category
, module
, level
, ISC_TRUE
,
936 msgcat
, msgset
, msg
, format
, args
);
940 isc_log_setcontext(isc_log_t
*lctx
) {
945 isc_log_setdebuglevel(isc_log_t
*lctx
, unsigned int level
) {
946 isc_logchannel_t
*channel
;
948 REQUIRE(VALID_CONTEXT(lctx
));
952 lctx
->debug_level
= level
;
954 * Close ISC_LOG_DEBUGONLY channels if level is zero.
956 if (lctx
->debug_level
== 0)
957 for (channel
= ISC_LIST_HEAD(lctx
->logconfig
->channels
);
959 channel
= ISC_LIST_NEXT(channel
, link
))
960 if (channel
->type
== ISC_LOG_TOFILE
&&
961 (channel
->flags
& ISC_LOG_DEBUGONLY
) != 0 &&
962 FILE_STREAM(channel
) != NULL
) {
963 (void)fclose(FILE_STREAM(channel
));
964 FILE_STREAM(channel
) = NULL
;
970 isc_log_getdebuglevel(isc_log_t
*lctx
) {
971 REQUIRE(VALID_CONTEXT(lctx
));
973 return (lctx
->debug_level
);
977 isc_log_setduplicateinterval(isc_logconfig_t
*lcfg
, unsigned int interval
) {
978 REQUIRE(VALID_CONFIG(lcfg
));
980 lcfg
->duplicate_interval
= interval
;
984 isc_log_getduplicateinterval(isc_logconfig_t
*lcfg
) {
985 REQUIRE(VALID_CONTEXT(lcfg
));
987 return (lcfg
->duplicate_interval
);
991 isc_log_settag(isc_logconfig_t
*lcfg
, const char *tag
) {
992 REQUIRE(VALID_CONFIG(lcfg
));
994 if (tag
!= NULL
&& *tag
!= '\0') {
995 if (lcfg
->tag
!= NULL
)
996 isc_mem_free(lcfg
->lctx
->mctx
, lcfg
->tag
);
997 lcfg
->tag
= isc_mem_strdup(lcfg
->lctx
->mctx
, tag
);
998 if (lcfg
->tag
== NULL
)
999 return (ISC_R_NOMEMORY
);
1002 if (lcfg
->tag
!= NULL
)
1003 isc_mem_free(lcfg
->lctx
->mctx
, lcfg
->tag
);
1007 return (ISC_R_SUCCESS
);
1011 isc_log_gettag(isc_logconfig_t
*lcfg
) {
1012 REQUIRE(VALID_CONFIG(lcfg
));
1017 /* XXXDCL NT -- This interface will assuredly be changing. */
1019 isc_log_opensyslog(const char *tag
, int options
, int facility
) {
1020 (void)openlog(tag
, options
, facility
);
1024 isc_log_closefilelogs(isc_log_t
*lctx
) {
1025 isc_logchannel_t
*channel
;
1027 REQUIRE(VALID_CONTEXT(lctx
));
1030 for (channel
= ISC_LIST_HEAD(lctx
->logconfig
->channels
);
1032 channel
= ISC_LIST_NEXT(channel
, link
))
1034 if (channel
->type
== ISC_LOG_TOFILE
&&
1035 FILE_STREAM(channel
) != NULL
) {
1036 (void)fclose(FILE_STREAM(channel
));
1037 FILE_STREAM(channel
) = NULL
;
1039 UNLOCK(&lctx
->lock
);
1043 **** Internal functions
1047 assignchannel(isc_logconfig_t
*lcfg
, unsigned int category_id
,
1048 const isc_logmodule_t
*module
, isc_logchannel_t
*channel
)
1050 isc_logchannellist_t
*new_item
;
1052 isc_result_t result
;
1054 REQUIRE(VALID_CONFIG(lcfg
));
1058 REQUIRE(category_id
< lctx
->category_count
);
1059 REQUIRE(module
== NULL
|| module
->id
< lctx
->module_count
);
1060 REQUIRE(channel
!= NULL
);
1063 * Ensure lcfg->channellist_count == lctx->category_count.
1065 result
= sync_channellist(lcfg
);
1066 if (result
!= ISC_R_SUCCESS
)
1069 new_item
= isc_mem_get(lctx
->mctx
, sizeof(*new_item
));
1070 if (new_item
== NULL
)
1071 return (ISC_R_NOMEMORY
);
1073 new_item
->channel
= channel
;
1074 new_item
->module
= module
;
1075 ISC_LIST_INITANDPREPEND(lcfg
->channellists
[category_id
],
1079 * Remember the highest logging level set by any channel in the
1080 * logging config, so isc_log_doit() can quickly return if the
1081 * message is too high to be logged by any channel.
1083 if (channel
->type
!= ISC_LOG_TONULL
) {
1084 if (lcfg
->highest_level
< channel
->level
)
1085 lcfg
->highest_level
= channel
->level
;
1086 if (channel
->level
== ISC_LOG_DYNAMIC
)
1087 lcfg
->dynamic
= ISC_TRUE
;
1090 return (ISC_R_SUCCESS
);
1094 * This would ideally be part of isc_log_registercategories(), except then
1095 * that function would have to return isc_result_t instead of void.
1098 sync_channellist(isc_logconfig_t
*lcfg
) {
1103 REQUIRE(VALID_CONFIG(lcfg
));
1107 REQUIRE(lctx
->category_count
!= 0);
1109 if (lctx
->category_count
== lcfg
->channellist_count
)
1110 return (ISC_R_SUCCESS
);
1112 bytes
= lctx
->category_count
* sizeof(ISC_LIST(isc_logchannellist_t
));
1114 lists
= isc_mem_get(lctx
->mctx
, bytes
);
1117 return (ISC_R_NOMEMORY
);
1119 memset(lists
, 0, bytes
);
1121 if (lcfg
->channellist_count
!= 0) {
1122 bytes
= lcfg
->channellist_count
*
1123 sizeof(ISC_LIST(isc_logchannellist_t
));
1124 memcpy(lists
, lcfg
->channellists
, bytes
);
1125 isc_mem_put(lctx
->mctx
, lcfg
->channellists
, bytes
);
1128 lcfg
->channellists
= lists
;
1129 lcfg
->channellist_count
= lctx
->category_count
;
1131 return (ISC_R_SUCCESS
);
1135 greatest_version(isc_logchannel_t
*channel
, int *greatestp
) {
1136 /* XXXDCL HIGHLY NT */
1137 char *basename
, *digit_end
;
1138 const char *dirname
;
1139 int version
, greatest
= -1;
1140 unsigned int basenamelen
;
1142 isc_result_t result
;
1148 REQUIRE(channel
->type
== ISC_LOG_TOFILE
);
1151 * It is safe to DE_CONST the file.name because it was copied
1152 * with isc_mem_strdup in isc_log_createchannel.
1154 basename
= strrchr(FILE_NAME(channel
), sep
);
1156 basename2
= strrchr(FILE_NAME(channel
), '\\');
1157 if ((basename
!= NULL
&& basename2
!= NULL
&& basename2
> basename
) ||
1158 (basename
== NULL
&& basename2
!= NULL
)) {
1159 basename
= basename2
;
1163 if (basename
!= NULL
) {
1165 dirname
= FILE_NAME(channel
);
1167 DE_CONST(FILE_NAME(channel
), basename
);
1170 basenamelen
= strlen(basename
);
1173 result
= isc_dir_open(&dir
, dirname
);
1176 * Replace the file separator if it was taken out.
1178 if (basename
!= FILE_NAME(channel
))
1179 *(basename
- 1) = sep
;
1182 * Return if the directory open failed.
1184 if (result
!= ISC_R_SUCCESS
)
1187 while (isc_dir_read(&dir
) == ISC_R_SUCCESS
) {
1188 if (dir
.entry
.length
> basenamelen
&&
1189 strncmp(dir
.entry
.name
, basename
, basenamelen
) == 0 &&
1190 dir
.entry
.name
[basenamelen
] == '.') {
1192 version
= strtol(&dir
.entry
.name
[basenamelen
+ 1],
1194 if (*digit_end
== '\0' && version
> greatest
)
1198 isc_dir_close(&dir
);
1200 *greatestp
= ++greatest
;
1202 return (ISC_R_SUCCESS
);
1206 roll_log(isc_logchannel_t
*channel
) {
1208 char current
[PATH_MAX
+ 1];
1209 char new[PATH_MAX
+ 1];
1211 isc_result_t result
;
1214 * Do nothing (not even excess version trimming) if ISC_LOG_ROLLNEVER
1215 * is specified. Apparently complete external control over the log
1218 if (FILE_VERSIONS(channel
) == ISC_LOG_ROLLNEVER
)
1219 return (ISC_R_SUCCESS
);
1221 path
= FILE_NAME(channel
);
1224 * Set greatest_version to the greatest existing version
1225 * (not the maximum requested version). This is 1 based even
1226 * though the file names are 0 based, so an oldest log of log.1
1227 * is a greatest_version of 2.
1229 result
= greatest_version(channel
, &greatest
);
1230 if (result
!= ISC_R_SUCCESS
)
1234 * Now greatest should be set to the highest version number desired.
1235 * Since the highest number is one less than FILE_VERSIONS(channel)
1236 * when not doing infinite log rolling, greatest will need to be
1237 * decremented when it is equal to -- or greater than --
1238 * FILE_VERSIONS(channel). When greatest is less than
1239 * FILE_VERSIONS(channel), it is already suitable for use as
1240 * the maximum version number.
1243 if (FILE_VERSIONS(channel
) == ISC_LOG_ROLLINFINITE
||
1244 FILE_VERSIONS(channel
) > greatest
)
1248 * When greatest is >= FILE_VERSIONS(channel), it needs to
1249 * be reduced until it is FILE_VERSIONS(channel) - 1.
1250 * Remove any excess logs on the way to that value.
1252 while (--greatest
>= FILE_VERSIONS(channel
)) {
1253 n
= snprintf(current
, sizeof(current
), "%s.%d",
1255 if (n
>= (int)sizeof(current
) || n
< 0)
1256 result
= ISC_R_NOSPACE
;
1258 result
= isc_file_remove(current
);
1259 if (result
!= ISC_R_SUCCESS
&&
1260 result
!= ISC_R_FILENOTFOUND
)
1262 "unable to remove log file '%s.%d': %s",
1264 isc_result_totext(result
));
1267 for (i
= greatest
; i
> 0; i
--) {
1268 result
= ISC_R_SUCCESS
;
1269 n
= snprintf(current
, sizeof(current
), "%s.%d", path
, i
- 1);
1270 if (n
>= (int)sizeof(current
) || n
< 0)
1271 result
= ISC_R_NOSPACE
;
1272 if (result
== ISC_R_SUCCESS
) {
1273 n
= snprintf(new, sizeof(new), "%s.%d", path
, i
);
1274 if (n
>= (int)sizeof(new) || n
< 0)
1275 result
= ISC_R_NOSPACE
;
1277 if (result
== ISC_R_SUCCESS
)
1278 result
= isc_file_rename(current
, new);
1279 if (result
!= ISC_R_SUCCESS
&&
1280 result
!= ISC_R_FILENOTFOUND
)
1282 "unable to rename log file '%s.%d' to "
1283 "'%s.%d': %s", path
, i
- 1, path
, i
,
1284 isc_result_totext(result
));
1287 if (FILE_VERSIONS(channel
) != 0) {
1288 n
= snprintf(new, sizeof(new), "%s.0", path
);
1289 if (n
>= (int)sizeof(new) || n
< 0)
1290 result
= ISC_R_NOSPACE
;
1292 result
= isc_file_rename(path
, new);
1293 if (result
!= ISC_R_SUCCESS
&&
1294 result
!= ISC_R_FILENOTFOUND
)
1296 "unable to rename log file '%s' to '%s.0': %s",
1297 path
, path
, isc_result_totext(result
));
1299 result
= isc_file_remove(path
);
1300 if (result
!= ISC_R_SUCCESS
&&
1301 result
!= ISC_R_FILENOTFOUND
)
1302 syslog(LOG_ERR
, "unable to remove log file '%s': %s",
1303 path
, isc_result_totext(result
));
1306 return (ISC_R_SUCCESS
);
1310 isc_log_open(isc_logchannel_t
*channel
) {
1311 struct stat statbuf
;
1312 isc_boolean_t regular_file
;
1313 isc_boolean_t roll
= ISC_FALSE
;
1314 isc_result_t result
= ISC_R_SUCCESS
;
1317 REQUIRE(channel
->type
== ISC_LOG_TOFILE
);
1318 REQUIRE(FILE_STREAM(channel
) == NULL
);
1320 path
= FILE_NAME(channel
);
1322 REQUIRE(path
!= NULL
&& *path
!= '\0');
1325 * Determine type of file; only regular files will be
1326 * version renamed, and only if the base file exists
1327 * and either has no size limit or has reached its size limit.
1329 if (stat(path
, &statbuf
) == 0) {
1330 regular_file
= S_ISREG(statbuf
.st_mode
) ? ISC_TRUE
: ISC_FALSE
;
1331 /* XXXDCL if not regular_file complain? */
1332 if ((FILE_MAXSIZE(channel
) == 0 &&
1333 FILE_VERSIONS(channel
) != ISC_LOG_ROLLNEVER
) ||
1334 (FILE_MAXSIZE(channel
) > 0 &&
1335 statbuf
.st_size
>= FILE_MAXSIZE(channel
)))
1336 roll
= regular_file
;
1337 } else if (errno
== ENOENT
)
1338 regular_file
= ISC_TRUE
;
1340 result
= ISC_R_INVALIDFILE
;
1345 if (result
== ISC_R_SUCCESS
&& roll
) {
1346 if (FILE_VERSIONS(channel
) == ISC_LOG_ROLLNEVER
)
1347 return (ISC_R_MAXSIZE
);
1348 result
= roll_log(channel
);
1349 if (result
!= ISC_R_SUCCESS
) {
1350 if ((channel
->flags
& ISC_LOG_OPENERR
) == 0) {
1352 "isc_log_open: roll_log '%s' "
1355 isc_result_totext(result
));
1356 channel
->flags
|= ISC_LOG_OPENERR
;
1362 result
= isc_stdio_open(path
, "a", &FILE_STREAM(channel
));
1368 isc_log_wouldlog(isc_log_t
*lctx
, int level
) {
1370 * Try to avoid locking the mutex for messages which can't
1371 * possibly be logged to any channels -- primarily debugging
1372 * messages that the debug level is not high enough to print.
1374 * If the level is (mathematically) less than or equal to the
1375 * highest_level, or if there is a dynamic channel and the level is
1376 * less than or equal to the debug level, the main loop must be
1377 * entered to see if the message should really be output.
1379 * NOTE: this is UNLOCKED access to the logconfig. However,
1380 * the worst thing that can happen is that a bad decision is made
1381 * about returning without logging, and that's not a big concern,
1382 * because that's a risk anyway if the logconfig is being
1383 * dynamically changed.
1386 if (lctx
== NULL
|| lctx
->logconfig
== NULL
)
1389 return (ISC_TF(level
<= lctx
->logconfig
->highest_level
||
1390 (lctx
->logconfig
->dynamic
&&
1391 level
<= lctx
->debug_level
)));
1395 isc_log_doit(isc_log_t
*lctx
, isc_logcategory_t
*category
,
1396 isc_logmodule_t
*module
, int level
, isc_boolean_t write_once
,
1397 isc_msgcat_t
*msgcat
, int msgset
, int msg
,
1398 const char *format
, va_list args
)
1401 char time_string
[64];
1402 char level_string
[24];
1403 const char *iformat
;
1404 struct stat statbuf
;
1405 isc_boolean_t matched
= ISC_FALSE
;
1406 isc_boolean_t printtime
, printtag
;
1407 isc_boolean_t printcategory
, printmodule
, printlevel
;
1408 isc_logconfig_t
*lcfg
;
1409 isc_logchannel_t
*channel
;
1410 isc_logchannellist_t
*category_channels
;
1411 isc_result_t result
;
1413 REQUIRE(lctx
== NULL
|| VALID_CONTEXT(lctx
));
1414 REQUIRE(category
!= NULL
);
1415 REQUIRE(module
!= NULL
);
1416 REQUIRE(level
!= ISC_LOG_DYNAMIC
);
1417 REQUIRE(format
!= NULL
);
1420 * Programs can use libraries that use this logging code without
1421 * wanting to do any logging, thus the log context is allowed to
1427 REQUIRE(category
->id
< lctx
->category_count
);
1428 REQUIRE(module
->id
< lctx
->module_count
);
1430 if (! isc_log_wouldlog(lctx
, level
))
1434 iformat
= isc_msgcat_get(msgcat
, msgset
, msg
, format
);
1438 time_string
[0] = '\0';
1439 level_string
[0] = '\0';
1443 lctx
->buffer
[0] = '\0';
1445 lcfg
= lctx
->logconfig
;
1447 category_channels
= ISC_LIST_HEAD(lcfg
->channellists
[category
->id
]);
1450 * XXXDCL add duplicate filtering? (To not write multiple times to
1451 * the same source via various channels).
1455 * If the channel list end was reached and a match was made,
1456 * everything is finished.
1458 if (category_channels
== NULL
&& matched
)
1461 if (category_channels
== NULL
&& ! matched
&&
1462 category_channels
!= ISC_LIST_HEAD(lcfg
->channellists
[0]))
1464 * No category/module pair was explicitly configured.
1465 * Try the category named "default".
1468 ISC_LIST_HEAD(lcfg
->channellists
[0]);
1470 if (category_channels
== NULL
&& ! matched
)
1472 * No matching module was explicitly configured
1473 * for the category named "default". Use the internal
1476 category_channels
= &default_channel
;
1478 if (category_channels
->module
!= NULL
&&
1479 category_channels
->module
!= module
) {
1480 category_channels
= ISC_LIST_NEXT(category_channels
,
1487 channel
= category_channels
->channel
;
1488 category_channels
= ISC_LIST_NEXT(category_channels
, link
);
1490 if (((channel
->flags
& ISC_LOG_DEBUGONLY
) != 0) &&
1491 lctx
->debug_level
== 0)
1494 if (channel
->level
== ISC_LOG_DYNAMIC
) {
1495 if (lctx
->debug_level
< level
)
1497 } else if (channel
->level
< level
)
1500 if ((channel
->flags
& ISC_LOG_PRINTTIME
) != 0 &&
1501 time_string
[0] == '\0') {
1505 isc_time_formattimestamp(&isctime
, time_string
,
1506 sizeof(time_string
));
1509 if ((channel
->flags
& ISC_LOG_PRINTLEVEL
) != 0 &&
1510 level_string
[0] == '\0') {
1511 if (level
< ISC_LOG_CRITICAL
)
1512 snprintf(level_string
, sizeof(level_string
),
1513 isc_msgcat_get(isc_msgcat
,
1518 else if (level
> ISC_LOG_DYNAMIC
)
1519 snprintf(level_string
, sizeof(level_string
),
1520 "%s %d: ", log_level_strings
[0],
1523 snprintf(level_string
, sizeof(level_string
),
1524 "%s: ", log_level_strings
[-level
]);
1528 * Only format the message once.
1530 if (lctx
->buffer
[0] == '\0') {
1531 (void)vsnprintf(lctx
->buffer
, sizeof(lctx
->buffer
),
1535 * Check for duplicates.
1538 isc_logmessage_t
*message
, *new;
1540 isc_interval_t interval
;
1542 isc_interval_set(&interval
,
1543 lcfg
->duplicate_interval
, 0);
1546 * 'oldest' is the age of the oldest messages
1547 * which fall within the duplicate_interval
1551 if (isc_time_subtract(&oldest
, &interval
, &oldest
)
1554 * Can't effectively do the checking
1555 * without having a valid time.
1559 message
=ISC_LIST_HEAD(lctx
->messages
);
1561 while (message
!= NULL
) {
1562 if (isc_time_compare(&message
->time
,
1565 * This message is older
1566 * than the duplicate_interval,
1567 * so it should be dropped from
1570 * Setting the interval to be
1571 * to be longer will obviously
1572 * not cause the expired
1573 * message to spring back into
1576 new = ISC_LIST_NEXT(message
,
1579 ISC_LIST_UNLINK(lctx
->messages
,
1582 isc_mem_put(lctx
->mctx
,
1584 sizeof(*message
) + 1 +
1585 strlen(message
->text
));
1592 * This message is in the duplicate
1593 * filtering interval ...
1595 if (strcmp(lctx
->buffer
, message
->text
)
1598 * ... and it is a duplicate.
1599 * Unlock the mutex and
1600 * get the hell out of Dodge.
1602 UNLOCK(&lctx
->lock
);
1606 message
= ISC_LIST_NEXT(message
, link
);
1610 * It wasn't in the duplicate interval,
1611 * so add it to the message list.
1613 new = isc_mem_get(lctx
->mctx
,
1614 sizeof(isc_logmessage_t
) +
1615 strlen(lctx
->buffer
) + 1);
1618 * Put the text immediately after
1619 * the struct. The strcpy is safe.
1621 new->text
= (char *)(new + 1);
1622 strcpy(new->text
, lctx
->buffer
);
1624 TIME_NOW(&new->time
);
1626 ISC_LIST_APPEND(lctx
->messages
,
1632 printtime
= ISC_TF((channel
->flags
& ISC_LOG_PRINTTIME
)
1634 printtag
= ISC_TF((channel
->flags
& ISC_LOG_PRINTTAG
)
1635 != 0 && lcfg
->tag
!= NULL
);
1636 printcategory
= ISC_TF((channel
->flags
& ISC_LOG_PRINTCATEGORY
)
1638 printmodule
= ISC_TF((channel
->flags
& ISC_LOG_PRINTMODULE
)
1640 printlevel
= ISC_TF((channel
->flags
& ISC_LOG_PRINTLEVEL
)
1643 switch (channel
->type
) {
1644 case ISC_LOG_TOFILE
:
1645 if (FILE_MAXREACHED(channel
)) {
1647 * If the file can be rolled, OR
1648 * If the file no longer exists, OR
1649 * If the file is less than the maximum size,
1650 * (such as if it had been renamed and
1651 * a new one touched, or it was truncated
1653 * ... then close it to trigger reopening.
1655 if (FILE_VERSIONS(channel
) !=
1656 ISC_LOG_ROLLNEVER
||
1657 (stat(FILE_NAME(channel
), &statbuf
) != 0 &&
1659 statbuf
.st_size
< FILE_MAXSIZE(channel
)) {
1660 (void)fclose(FILE_STREAM(channel
));
1661 FILE_STREAM(channel
) = NULL
;
1662 FILE_MAXREACHED(channel
) = ISC_FALSE
;
1670 if (FILE_STREAM(channel
) == NULL
) {
1671 result
= isc_log_open(channel
);
1672 if (result
!= ISC_R_SUCCESS
&&
1673 result
!= ISC_R_MAXSIZE
&&
1674 (channel
->flags
& ISC_LOG_OPENERR
) == 0) {
1676 "isc_log_open '%s' failed: %s",
1678 isc_result_totext(result
));
1679 channel
->flags
|= ISC_LOG_OPENERR
;
1681 if (result
!= ISC_R_SUCCESS
)
1683 channel
->flags
&= ~ISC_LOG_OPENERR
;
1687 case ISC_LOG_TOFILEDESC
:
1688 fprintf(FILE_STREAM(channel
), "%s%s%s%s%s%s%s%s%s%s\n",
1689 printtime
? time_string
: "",
1690 printtime
? " " : "",
1691 printtag
? lcfg
->tag
: "",
1692 printtag
? ": " : "",
1693 printcategory
? category
->name
: "",
1694 printcategory
? ": " : "",
1695 printmodule
? (module
!= NULL
? module
->name
1698 printmodule
? ": " : "",
1699 printlevel
? level_string
: "",
1702 fflush(FILE_STREAM(channel
));
1705 * If the file now exceeds its maximum size
1706 * threshold, note it so that it will not be logged
1709 if (FILE_MAXSIZE(channel
) > 0) {
1710 INSIST(channel
->type
== ISC_LOG_TOFILE
);
1712 /* XXXDCL NT fstat/fileno */
1713 /* XXXDCL complain if fstat fails? */
1714 if (fstat(fileno(FILE_STREAM(channel
)),
1716 statbuf
.st_size
> FILE_MAXSIZE(channel
))
1717 FILE_MAXREACHED(channel
) = ISC_TRUE
;
1722 case ISC_LOG_TOSYSLOG
:
1724 syslog_level
= LOG_DEBUG
;
1725 else if (level
< ISC_LOG_CRITICAL
)
1726 syslog_level
= LOG_CRIT
;
1728 syslog_level
= syslog_map
[-level
];
1730 (void)syslog(FACILITY(channel
) | syslog_level
,
1731 "%s%s%s%s%s%s%s%s%s%s",
1732 printtime
? time_string
: "",
1733 printtime
? " " : "",
1734 printtag
? lcfg
->tag
: "",
1735 printtag
? ": " : "",
1736 printcategory
? category
->name
: "",
1737 printcategory
? ": " : "",
1738 printmodule
? (module
!= NULL
? module
->name
1741 printmodule
? ": " : "",
1742 printlevel
? level_string
: "",
1746 case ISC_LOG_TONULL
:
1753 UNLOCK(&lctx
->lock
);