1 /* $NetBSD: log.c,v 1.8 2014/12/10 04:37:59 christos Exp $ */
4 * Copyright (C) 2004-2007, 2009, 2011-2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
23 * \author Principal Authors: DCL */
32 #include <sys/types.h> /* dev_t FreeBSD 2.1 */
37 #include <isc/magic.h>
40 #include <isc/print.h>
42 #include <isc/stdio.h>
43 #include <isc/string.h>
47 #define LCTX_MAGIC ISC_MAGIC('L', 'c', 't', 'x')
48 #define VALID_CONTEXT(lctx) ISC_MAGIC_VALID(lctx, LCTX_MAGIC)
50 #define LCFG_MAGIC ISC_MAGIC('L', 'c', 'f', 'g')
51 #define VALID_CONFIG(lcfg) ISC_MAGIC_VALID(lcfg, LCFG_MAGIC)
54 * XXXDCL make dynamic?
56 #define LOG_BUFFER_SIZE (8 * 1024)
59 #define PATH_MAX 1024 /* AIX and others don't define this. */
63 * This is the structure that holds each named channel. A simple linked
64 * list chains all of the channels together, so an individual channel is
65 * found by doing strcmp()s with the names down the list. Their should
66 * be no performance penalty from this as it is expected that the number
67 * of named channels will be no more than a dozen or so, and name lookups
68 * from the head of the list are only done when isc_log_usechannel() is
69 * called, which should also be very infrequent.
71 typedef struct isc_logchannel isc_logchannel_t
;
73 struct isc_logchannel
{
78 isc_logdestination_t destination
;
79 ISC_LINK(isc_logchannel_t
) link
;
83 * The logchannellist structure associates categories and modules with
84 * channels. First the appropriate channellist is found based on the
85 * category, and then each structure in the linked list is checked for
86 * a matching module. It is expected that the number of channels
87 * associated with any given category will be very short, no more than
88 * three or four in the more unusual cases.
90 typedef struct isc_logchannellist isc_logchannellist_t
;
92 struct isc_logchannellist
{
93 const isc_logmodule_t
* module
;
94 isc_logchannel_t
* channel
;
95 ISC_LINK(isc_logchannellist_t
) link
;
99 * This structure is used to remember messages for pruning via
100 * isc_log_[v]write1().
102 typedef struct isc_logmessage isc_logmessage_t
;
104 struct isc_logmessage
{
107 ISC_LINK(isc_logmessage_t
) link
;
111 * The isc_logconfig structure is used to store the configurable information
112 * about where messages are actually supposed to be sent -- the information
113 * that could changed based on some configuration file, as opposed to the
114 * the category/module specification of isc_log_[v]write[1] that is compiled
115 * into a program, or the debug_level which is dynamic state information.
117 struct isc_logconfig
{
120 ISC_LIST(isc_logchannel_t
) channels
;
121 ISC_LIST(isc_logchannellist_t
) *channellists
;
122 unsigned int channellist_count
;
123 unsigned int duplicate_interval
;
126 isc_boolean_t dynamic
;
130 * This isc_log structure provides the context for the isc_log functions.
131 * The log context locks itself in isc_log_doit, the internal backend to
132 * isc_log_write. The locking is necessary both to provide exclusive access
133 * to the buffer into which the message is formatted and to guard against
134 * competing threads trying to write to the same syslog resource. (On
135 * some systems, such as BSD/OS, stdio is thread safe but syslog is not.)
136 * Unfortunately, the lock cannot guard against a _different_ logging
137 * context in the same program competing for syslog's attention. Thus
138 * There Can Be Only One, but this is not enforced.
141 * Note that the category and module information is not locked.
142 * This is because in the usual case, only one isc_log_t is ever created
143 * in a program, and the category/module registration happens only once.
144 * XXXDCL it might be wise to add more locking overall.
150 isc_logcategory_t
* categories
;
151 unsigned int category_count
;
152 isc_logmodule_t
* modules
;
153 unsigned int module_count
;
156 /* Locked by isc_log lock. */
157 isc_logconfig_t
* logconfig
;
158 char buffer
[LOG_BUFFER_SIZE
];
159 ISC_LIST(isc_logmessage_t
) messages
;
163 * Used when ISC_LOG_PRINTLEVEL is enabled for a channel.
165 static const char *log_level_strings
[] = {
175 * Used to convert ISC_LOG_* priorities into syslog priorities.
176 * XXXDCL This will need modification for NT.
178 static const int syslog_map
[] = {
188 * When adding new categories, a corresponding ISC_LOGCATEGORY_foo
189 * definition needs to be added to <isc/log.h>.
191 * The default category is provided so that the internal default can
192 * be overridden. Since the default is always looked up as the first
193 * channellist in the log context, it must come first in isc_categories[].
195 LIBISC_EXTERNAL_DATA isc_logcategory_t isc_categories
[] = {
196 { "default", 0 }, /* "default" must come first. */
202 * See above comment for categories on LIBISC_EXTERNAL_DATA, and apply it to modules.
204 LIBISC_EXTERNAL_DATA isc_logmodule_t isc_modules
[] = {
215 * This essentially constant structure must be filled in at run time,
216 * because its channel member is pointed to a channel that is created
217 * dynamically with isc_log_createchannel.
219 static isc_logchannellist_t default_channel
;
222 * libisc logs to this context.
224 LIBISC_EXTERNAL_DATA isc_log_t
*isc_lctx
= NULL
;
227 * Forward declarations.
230 assignchannel(isc_logconfig_t
*lcfg
, unsigned int category_id
,
231 const isc_logmodule_t
*module
, isc_logchannel_t
*channel
);
234 sync_channellist(isc_logconfig_t
*lcfg
);
237 greatest_version(isc_logchannel_t
*channel
, int *greatest
);
240 roll_log(isc_logchannel_t
*channel
);
243 isc_log_doit(isc_log_t
*lctx
, isc_logcategory_t
*category
,
244 isc_logmodule_t
*module
, int level
, isc_boolean_t write_once
,
245 isc_msgcat_t
*msgcat
, int msgset
, int msg
,
246 const char *format
, va_list args
)
247 ISC_FORMAT_PRINTF(9, 0);
251 * Convenience macros.
254 #define FACILITY(channel) (channel->destination.facility)
255 #define FILE_NAME(channel) (channel->destination.file.name)
256 #define FILE_STREAM(channel) (channel->destination.file.stream)
257 #define FILE_VERSIONS(channel) (channel->destination.file.versions)
258 #define FILE_MAXSIZE(channel) (channel->destination.file.maximum_size)
259 #define FILE_MAXREACHED(channel) (channel->destination.file.maximum_reached)
263 **** Public interfaces.
267 * Establish a new logging context, with default channels.
270 isc_log_create(isc_mem_t
*mctx
, isc_log_t
**lctxp
, isc_logconfig_t
**lcfgp
) {
272 isc_logconfig_t
*lcfg
= NULL
;
275 REQUIRE(mctx
!= NULL
);
276 REQUIRE(lctxp
!= NULL
&& *lctxp
== NULL
);
277 REQUIRE(lcfgp
== NULL
|| *lcfgp
== NULL
);
279 lctx
= isc_mem_get(mctx
, sizeof(*lctx
));
282 isc_mem_attach(mctx
, &lctx
->mctx
);
283 lctx
->categories
= NULL
;
284 lctx
->category_count
= 0;
285 lctx
->modules
= NULL
;
286 lctx
->module_count
= 0;
287 lctx
->debug_level
= 0;
289 ISC_LIST_INIT(lctx
->messages
);
291 result
= isc_mutex_init(&lctx
->lock
);
292 if (result
!= ISC_R_SUCCESS
) {
293 isc_mem_putanddetach(&mctx
, lctx
, sizeof(*lctx
));
298 * Normally setting the magic number is the last step done
299 * in a creation function, but a valid log context is needed
300 * by isc_log_registercategories and isc_logconfig_create.
301 * If either fails, the lctx is destroyed and not returned
304 lctx
->magic
= LCTX_MAGIC
;
306 isc_log_registercategories(lctx
, isc_categories
);
307 isc_log_registermodules(lctx
, isc_modules
);
308 result
= isc_logconfig_create(lctx
, &lcfg
);
311 result
= ISC_R_NOMEMORY
;
313 if (result
== ISC_R_SUCCESS
)
314 result
= sync_channellist(lcfg
);
316 if (result
== ISC_R_SUCCESS
) {
317 lctx
->logconfig
= lcfg
;
325 isc_logconfig_destroy(&lcfg
);
327 isc_log_destroy(&lctx
);
334 isc_logconfig_create(isc_log_t
*lctx
, isc_logconfig_t
**lcfgp
) {
335 isc_logconfig_t
*lcfg
;
336 isc_logdestination_t destination
;
337 isc_result_t result
= ISC_R_SUCCESS
;
338 int level
= ISC_LOG_INFO
;
340 REQUIRE(lcfgp
!= NULL
&& *lcfgp
== NULL
);
341 REQUIRE(VALID_CONTEXT(lctx
));
343 lcfg
= isc_mem_get(lctx
->mctx
, sizeof(*lcfg
));
347 lcfg
->channellists
= NULL
;
348 lcfg
->channellist_count
= 0;
349 lcfg
->duplicate_interval
= 0;
350 lcfg
->highest_level
= level
;
352 lcfg
->dynamic
= ISC_FALSE
;
354 ISC_LIST_INIT(lcfg
->channels
);
357 * Normally the magic number is the last thing set in the
358 * structure, but isc_log_createchannel() needs a valid
359 * config. If the channel creation fails, the lcfg is not
360 * returned to the caller.
362 lcfg
->magic
= LCFG_MAGIC
;
365 result
= ISC_R_NOMEMORY
;
368 * Create the default channels:
369 * default_syslog, default_stderr, default_debug and null.
371 if (result
== ISC_R_SUCCESS
) {
372 destination
.facility
= LOG_DAEMON
;
373 result
= isc_log_createchannel(lcfg
, "default_syslog",
374 ISC_LOG_TOSYSLOG
, level
,
378 if (result
== ISC_R_SUCCESS
) {
379 destination
.file
.stream
= stderr
;
380 destination
.file
.name
= NULL
;
381 destination
.file
.versions
= ISC_LOG_ROLLNEVER
;
382 destination
.file
.maximum_size
= 0;
383 result
= isc_log_createchannel(lcfg
, "default_stderr",
390 if (result
== ISC_R_SUCCESS
) {
392 * Set the default category's channel to default_stderr,
393 * which is at the head of the channels list because it was
396 default_channel
.channel
= ISC_LIST_HEAD(lcfg
->channels
);
398 destination
.file
.stream
= stderr
;
399 destination
.file
.name
= NULL
;
400 destination
.file
.versions
= ISC_LOG_ROLLNEVER
;
401 destination
.file
.maximum_size
= 0;
402 result
= isc_log_createchannel(lcfg
, "default_debug",
409 if (result
== ISC_R_SUCCESS
)
410 result
= isc_log_createchannel(lcfg
, "null",
415 if (result
== ISC_R_SUCCESS
)
420 isc_logconfig_destroy(&lcfg
);
426 isc_logconfig_get(isc_log_t
*lctx
) {
427 REQUIRE(VALID_CONTEXT(lctx
));
429 ENSURE(lctx
->logconfig
!= NULL
);
431 return (lctx
->logconfig
);
435 isc_logconfig_use(isc_log_t
*lctx
, isc_logconfig_t
*lcfg
) {
436 isc_logconfig_t
*old_cfg
;
439 REQUIRE(VALID_CONTEXT(lctx
));
440 REQUIRE(VALID_CONFIG(lcfg
));
441 REQUIRE(lcfg
->lctx
== lctx
);
444 * Ensure that lcfg->channellist_count == lctx->category_count.
445 * They won't be equal if isc_log_usechannel has not been called
446 * since any call to isc_log_registercategories.
448 result
= sync_channellist(lcfg
);
449 if (result
!= ISC_R_SUCCESS
)
454 old_cfg
= lctx
->logconfig
;
455 lctx
->logconfig
= lcfg
;
459 isc_logconfig_destroy(&old_cfg
);
461 return (ISC_R_SUCCESS
);
465 isc_log_destroy(isc_log_t
**lctxp
) {
467 isc_logconfig_t
*lcfg
;
469 isc_logmessage_t
*message
;
471 REQUIRE(lctxp
!= NULL
&& VALID_CONTEXT(*lctxp
));
476 if (lctx
->logconfig
!= NULL
) {
477 lcfg
= lctx
->logconfig
;
478 lctx
->logconfig
= NULL
;
479 isc_logconfig_destroy(&lcfg
);
482 DESTROYLOCK(&lctx
->lock
);
484 while ((message
= ISC_LIST_HEAD(lctx
->messages
)) != NULL
) {
485 ISC_LIST_UNLINK(lctx
->messages
, message
, link
);
487 isc_mem_put(mctx
, message
,
488 sizeof(*message
) + strlen(message
->text
) + 1);
491 lctx
->buffer
[0] = '\0';
492 lctx
->debug_level
= 0;
493 lctx
->categories
= NULL
;
494 lctx
->category_count
= 0;
495 lctx
->modules
= NULL
;
496 lctx
->module_count
= 0;
500 isc_mem_putanddetach(&mctx
, lctx
, sizeof(*lctx
));
506 isc_logconfig_destroy(isc_logconfig_t
**lcfgp
) {
507 isc_logconfig_t
*lcfg
;
509 isc_logchannel_t
*channel
;
510 isc_logchannellist_t
*item
;
514 REQUIRE(lcfgp
!= NULL
&& VALID_CONFIG(*lcfgp
));
519 * This function cannot be called with a logconfig that is in
520 * use by a log context.
522 REQUIRE(lcfg
->lctx
!= NULL
&& lcfg
->lctx
->logconfig
!= lcfg
);
524 mctx
= lcfg
->lctx
->mctx
;
526 while ((channel
= ISC_LIST_HEAD(lcfg
->channels
)) != NULL
) {
527 ISC_LIST_UNLINK(lcfg
->channels
, channel
, link
);
529 if (channel
->type
== ISC_LOG_TOFILE
) {
531 * The filename for the channel may have ultimately
532 * started its life in user-land as a const string,
533 * but in isc_log_createchannel it gets copied
534 * into writable memory and is not longer truly const.
536 DE_CONST(FILE_NAME(channel
), filename
);
537 isc_mem_free(mctx
, filename
);
539 if (FILE_STREAM(channel
) != NULL
)
540 (void)fclose(FILE_STREAM(channel
));
543 isc_mem_free(mctx
, channel
->name
);
544 isc_mem_put(mctx
, channel
, sizeof(*channel
));
547 for (i
= 0; i
< lcfg
->channellist_count
; i
++)
548 while ((item
= ISC_LIST_HEAD(lcfg
->channellists
[i
])) != NULL
) {
549 ISC_LIST_UNLINK(lcfg
->channellists
[i
], item
, link
);
550 isc_mem_put(mctx
, item
, sizeof(*item
));
553 if (lcfg
->channellist_count
> 0)
554 isc_mem_put(mctx
, lcfg
->channellists
,
555 lcfg
->channellist_count
*
556 sizeof(ISC_LIST(isc_logchannellist_t
)));
558 lcfg
->dynamic
= ISC_FALSE
;
559 if (lcfg
->tag
!= NULL
)
560 isc_mem_free(lcfg
->lctx
->mctx
, lcfg
->tag
);
562 lcfg
->highest_level
= 0;
563 lcfg
->duplicate_interval
= 0;
566 isc_mem_put(mctx
, lcfg
, sizeof(*lcfg
));
572 isc_log_registercategories(isc_log_t
*lctx
, isc_logcategory_t categories
[]) {
573 isc_logcategory_t
*catp
;
575 REQUIRE(VALID_CONTEXT(lctx
));
576 REQUIRE(categories
!= NULL
&& categories
[0].name
!= NULL
);
579 * XXXDCL This somewhat sleazy situation of using the last pointer
580 * in one category array to point to the next array exists because
581 * this registration function returns void and I didn't want to have
582 * change everything that used it by making it return an isc_result_t.
583 * It would need to do that if it had to allocate memory to store
584 * pointers to each array passed in.
586 if (lctx
->categories
== NULL
)
587 lctx
->categories
= categories
;
591 * Adjust the last (NULL) pointer of the already registered
592 * categories to point to the incoming array.
594 for (catp
= lctx
->categories
; catp
->name
!= NULL
; )
595 if (catp
->id
== UINT_MAX
)
597 * The name pointer points to the next array.
600 DE_CONST(catp
->name
, catp
);
604 catp
->name
= (void *)categories
;
609 * Update the id number of the category with its new global id.
611 for (catp
= categories
; catp
->name
!= NULL
; catp
++)
612 catp
->id
= lctx
->category_count
++;
616 isc_log_categorybyname(isc_log_t
*lctx
, const char *name
) {
617 isc_logcategory_t
*catp
;
619 REQUIRE(VALID_CONTEXT(lctx
));
620 REQUIRE(name
!= NULL
);
622 for (catp
= lctx
->categories
; catp
->name
!= NULL
; )
623 if (catp
->id
== UINT_MAX
)
625 * catp is neither modified nor returned to the
626 * caller, so removing its const qualifier is ok.
628 DE_CONST(catp
->name
, catp
);
630 if (strcmp(catp
->name
, name
) == 0)
639 isc_log_registermodules(isc_log_t
*lctx
, isc_logmodule_t modules
[]) {
640 isc_logmodule_t
*modp
;
642 REQUIRE(VALID_CONTEXT(lctx
));
643 REQUIRE(modules
!= NULL
&& modules
[0].name
!= NULL
);
646 * XXXDCL This somewhat sleazy situation of using the last pointer
647 * in one category array to point to the next array exists because
648 * this registration function returns void and I didn't want to have
649 * change everything that used it by making it return an isc_result_t.
650 * It would need to do that if it had to allocate memory to store
651 * pointers to each array passed in.
653 if (lctx
->modules
== NULL
)
654 lctx
->modules
= modules
;
658 * Adjust the last (NULL) pointer of the already registered
659 * modules to point to the incoming array.
661 for (modp
= lctx
->modules
; modp
->name
!= NULL
; )
662 if (modp
->id
== UINT_MAX
)
664 * The name pointer points to the next array.
667 DE_CONST(modp
->name
, modp
);
671 modp
->name
= (void *)modules
;
676 * Update the id number of the module with its new global id.
678 for (modp
= modules
; modp
->name
!= NULL
; modp
++)
679 modp
->id
= lctx
->module_count
++;
683 isc_log_modulebyname(isc_log_t
*lctx
, const char *name
) {
684 isc_logmodule_t
*modp
;
686 REQUIRE(VALID_CONTEXT(lctx
));
687 REQUIRE(name
!= NULL
);
689 for (modp
= lctx
->modules
; modp
->name
!= NULL
; )
690 if (modp
->id
== UINT_MAX
)
692 * modp is neither modified nor returned to the
693 * caller, so removing its const qualifier is ok.
695 DE_CONST(modp
->name
, modp
);
697 if (strcmp(modp
->name
, name
) == 0)
706 isc_log_createchannel(isc_logconfig_t
*lcfg
, const char *name
,
707 unsigned int type
, int level
,
708 const isc_logdestination_t
*destination
,
711 isc_logchannel_t
*channel
;
714 REQUIRE(VALID_CONFIG(lcfg
));
715 REQUIRE(name
!= NULL
);
716 REQUIRE(type
== ISC_LOG_TOSYSLOG
|| type
== ISC_LOG_TOFILE
||
717 type
== ISC_LOG_TOFILEDESC
|| type
== ISC_LOG_TONULL
);
718 REQUIRE(destination
!= NULL
|| type
== ISC_LOG_TONULL
);
719 REQUIRE(level
>= ISC_LOG_CRITICAL
);
721 (unsigned int)~(ISC_LOG_PRINTALL
| ISC_LOG_DEBUGONLY
)) == 0);
723 /* XXXDCL find duplicate names? */
725 mctx
= lcfg
->lctx
->mctx
;
727 channel
= isc_mem_get(mctx
, sizeof(*channel
));
729 return (ISC_R_NOMEMORY
);
731 channel
->name
= isc_mem_strdup(mctx
, name
);
732 if (channel
->name
== NULL
) {
733 isc_mem_put(mctx
, channel
, sizeof(*channel
));
734 return (ISC_R_NOMEMORY
);
737 channel
->type
= type
;
738 channel
->level
= level
;
739 channel
->flags
= flags
;
740 ISC_LINK_INIT(channel
, link
);
743 case ISC_LOG_TOSYSLOG
:
744 FACILITY(channel
) = destination
->facility
;
749 * The file name is copied because greatest_version wants
750 * to scribble on it, so it needs to be definitely in
754 isc_mem_strdup(mctx
, destination
->file
.name
);
755 FILE_STREAM(channel
) = NULL
;
756 FILE_VERSIONS(channel
) = destination
->file
.versions
;
757 FILE_MAXSIZE(channel
) = destination
->file
.maximum_size
;
758 FILE_MAXREACHED(channel
) = ISC_FALSE
;
761 case ISC_LOG_TOFILEDESC
:
762 FILE_NAME(channel
) = NULL
;
763 FILE_STREAM(channel
) = destination
->file
.stream
;
764 FILE_MAXSIZE(channel
) = 0;
765 FILE_VERSIONS(channel
) = ISC_LOG_ROLLNEVER
;
773 isc_mem_free(mctx
, channel
->name
);
774 isc_mem_put(mctx
, channel
, sizeof(*channel
));
775 return (ISC_R_UNEXPECTED
);
778 ISC_LIST_PREPEND(lcfg
->channels
, channel
, link
);
781 * If default_stderr was redefined, make the default category
782 * point to the new default_stderr.
784 if (strcmp(name
, "default_stderr") == 0)
785 default_channel
.channel
= channel
;
787 return (ISC_R_SUCCESS
);
791 isc_log_usechannel(isc_logconfig_t
*lcfg
, const char *name
,
792 const isc_logcategory_t
*category
,
793 const isc_logmodule_t
*module
)
796 isc_logchannel_t
*channel
;
797 isc_result_t result
= ISC_R_SUCCESS
;
800 REQUIRE(VALID_CONFIG(lcfg
));
801 REQUIRE(name
!= NULL
);
805 REQUIRE(category
== NULL
|| category
->id
< lctx
->category_count
);
806 REQUIRE(module
== NULL
|| module
->id
< lctx
->module_count
);
808 for (channel
= ISC_LIST_HEAD(lcfg
->channels
); channel
!= NULL
;
809 channel
= ISC_LIST_NEXT(channel
, link
))
810 if (strcmp(name
, channel
->name
) == 0)
814 return (ISC_R_NOTFOUND
);
816 if (category
!= NULL
)
817 result
= assignchannel(lcfg
, category
->id
, module
, channel
);
821 * Assign to all categories. Note that this includes
822 * the default channel.
824 for (i
= 0; i
< lctx
->category_count
; i
++) {
825 result
= assignchannel(lcfg
, i
, module
, channel
);
826 if (result
!= ISC_R_SUCCESS
)
834 isc_log_write(isc_log_t
*lctx
, isc_logcategory_t
*category
,
835 isc_logmodule_t
*module
, int level
, const char *format
, ...)
840 * Contract checking is done in isc_log_doit().
843 va_start(args
, format
);
844 isc_log_doit(lctx
, category
, module
, level
, ISC_FALSE
,
845 NULL
, 0, 0, format
, args
);
850 isc_log_vwrite(isc_log_t
*lctx
, isc_logcategory_t
*category
,
851 isc_logmodule_t
*module
, int level
,
852 const char *format
, va_list args
)
855 * Contract checking is done in isc_log_doit().
857 isc_log_doit(lctx
, category
, module
, level
, ISC_FALSE
,
858 NULL
, 0, 0, format
, args
);
862 isc_log_write1(isc_log_t
*lctx
, isc_logcategory_t
*category
,
863 isc_logmodule_t
*module
, int level
, const char *format
, ...)
868 * Contract checking is done in isc_log_doit().
871 va_start(args
, format
);
872 isc_log_doit(lctx
, category
, module
, level
, ISC_TRUE
,
873 NULL
, 0, 0, format
, args
);
878 isc_log_vwrite1(isc_log_t
*lctx
, isc_logcategory_t
*category
,
879 isc_logmodule_t
*module
, int level
,
880 const char *format
, va_list args
)
883 * Contract checking is done in isc_log_doit().
885 isc_log_doit(lctx
, category
, module
, level
, ISC_TRUE
,
886 NULL
, 0, 0, format
, args
);
890 isc_log_iwrite(isc_log_t
*lctx
, isc_logcategory_t
*category
,
891 isc_logmodule_t
*module
, int level
,
892 isc_msgcat_t
*msgcat
, int msgset
, int msg
,
893 const char *format
, ...)
898 * Contract checking is done in isc_log_doit().
901 va_start(args
, format
);
902 isc_log_doit(lctx
, category
, module
, level
, ISC_FALSE
,
903 msgcat
, msgset
, msg
, format
, args
);
908 isc_log_ivwrite(isc_log_t
*lctx
, isc_logcategory_t
*category
,
909 isc_logmodule_t
*module
, int level
,
910 isc_msgcat_t
*msgcat
, int msgset
, int msg
,
911 const char *format
, va_list args
)
914 * Contract checking is done in isc_log_doit().
916 isc_log_doit(lctx
, category
, module
, level
, ISC_FALSE
,
917 msgcat
, msgset
, msg
, format
, args
);
921 isc_log_iwrite1(isc_log_t
*lctx
, isc_logcategory_t
*category
,
922 isc_logmodule_t
*module
, int level
,
923 isc_msgcat_t
*msgcat
, int msgset
, int msg
,
924 const char *format
, ...)
929 * Contract checking is done in isc_log_doit().
932 va_start(args
, format
);
933 isc_log_doit(lctx
, category
, module
, level
, ISC_TRUE
,
934 msgcat
, msgset
, msg
, format
, args
);
939 isc_log_ivwrite1(isc_log_t
*lctx
, isc_logcategory_t
*category
,
940 isc_logmodule_t
*module
, int level
,
941 isc_msgcat_t
*msgcat
, int msgset
, int msg
,
942 const char *format
, va_list args
)
945 * Contract checking is done in isc_log_doit().
947 isc_log_doit(lctx
, category
, module
, level
, ISC_TRUE
,
948 msgcat
, msgset
, msg
, format
, args
);
952 isc_log_setcontext(isc_log_t
*lctx
) {
957 isc_log_setdebuglevel(isc_log_t
*lctx
, unsigned int level
) {
958 isc_logchannel_t
*channel
;
960 REQUIRE(VALID_CONTEXT(lctx
));
964 lctx
->debug_level
= level
;
966 * Close ISC_LOG_DEBUGONLY channels if level is zero.
968 if (lctx
->debug_level
== 0)
969 for (channel
= ISC_LIST_HEAD(lctx
->logconfig
->channels
);
971 channel
= ISC_LIST_NEXT(channel
, link
))
972 if (channel
->type
== ISC_LOG_TOFILE
&&
973 (channel
->flags
& ISC_LOG_DEBUGONLY
) != 0 &&
974 FILE_STREAM(channel
) != NULL
) {
975 (void)fclose(FILE_STREAM(channel
));
976 FILE_STREAM(channel
) = NULL
;
982 isc_log_getdebuglevel(isc_log_t
*lctx
) {
983 REQUIRE(VALID_CONTEXT(lctx
));
985 return (lctx
->debug_level
);
989 isc_log_setduplicateinterval(isc_logconfig_t
*lcfg
, unsigned int interval
) {
990 REQUIRE(VALID_CONFIG(lcfg
));
992 lcfg
->duplicate_interval
= interval
;
996 isc_log_getduplicateinterval(isc_logconfig_t
*lcfg
) {
997 REQUIRE(VALID_CONTEXT(lcfg
));
999 return (lcfg
->duplicate_interval
);
1003 isc_log_settag(isc_logconfig_t
*lcfg
, const char *tag
) {
1004 REQUIRE(VALID_CONFIG(lcfg
));
1006 if (tag
!= NULL
&& *tag
!= '\0') {
1007 if (lcfg
->tag
!= NULL
)
1008 isc_mem_free(lcfg
->lctx
->mctx
, lcfg
->tag
);
1009 lcfg
->tag
= isc_mem_strdup(lcfg
->lctx
->mctx
, tag
);
1010 if (lcfg
->tag
== NULL
)
1011 return (ISC_R_NOMEMORY
);
1014 if (lcfg
->tag
!= NULL
)
1015 isc_mem_free(lcfg
->lctx
->mctx
, lcfg
->tag
);
1019 return (ISC_R_SUCCESS
);
1023 isc_log_gettag(isc_logconfig_t
*lcfg
) {
1024 REQUIRE(VALID_CONFIG(lcfg
));
1029 /* XXXDCL NT -- This interface will assuredly be changing. */
1031 isc_log_opensyslog(const char *tag
, int options
, int facility
) {
1032 (void)openlog(tag
, options
, facility
);
1036 isc_log_closefilelogs(isc_log_t
*lctx
) {
1037 isc_logchannel_t
*channel
;
1039 REQUIRE(VALID_CONTEXT(lctx
));
1042 for (channel
= ISC_LIST_HEAD(lctx
->logconfig
->channels
);
1044 channel
= ISC_LIST_NEXT(channel
, link
))
1046 if (channel
->type
== ISC_LOG_TOFILE
&&
1047 FILE_STREAM(channel
) != NULL
) {
1048 (void)fclose(FILE_STREAM(channel
));
1049 FILE_STREAM(channel
) = NULL
;
1051 UNLOCK(&lctx
->lock
);
1055 **** Internal functions
1059 assignchannel(isc_logconfig_t
*lcfg
, unsigned int category_id
,
1060 const isc_logmodule_t
*module
, isc_logchannel_t
*channel
)
1062 isc_logchannellist_t
*new_item
;
1064 isc_result_t result
;
1066 REQUIRE(VALID_CONFIG(lcfg
));
1070 REQUIRE(category_id
< lctx
->category_count
);
1071 REQUIRE(module
== NULL
|| module
->id
< lctx
->module_count
);
1072 REQUIRE(channel
!= NULL
);
1075 * Ensure lcfg->channellist_count == lctx->category_count.
1077 result
= sync_channellist(lcfg
);
1078 if (result
!= ISC_R_SUCCESS
)
1081 new_item
= isc_mem_get(lctx
->mctx
, sizeof(*new_item
));
1082 if (new_item
== NULL
)
1083 return (ISC_R_NOMEMORY
);
1085 new_item
->channel
= channel
;
1086 new_item
->module
= module
;
1087 ISC_LIST_INITANDPREPEND(lcfg
->channellists
[category_id
],
1091 * Remember the highest logging level set by any channel in the
1092 * logging config, so isc_log_doit() can quickly return if the
1093 * message is too high to be logged by any channel.
1095 if (channel
->type
!= ISC_LOG_TONULL
) {
1096 if (lcfg
->highest_level
< channel
->level
)
1097 lcfg
->highest_level
= channel
->level
;
1098 if (channel
->level
== ISC_LOG_DYNAMIC
)
1099 lcfg
->dynamic
= ISC_TRUE
;
1102 return (ISC_R_SUCCESS
);
1106 * This would ideally be part of isc_log_registercategories(), except then
1107 * that function would have to return isc_result_t instead of void.
1110 sync_channellist(isc_logconfig_t
*lcfg
) {
1115 REQUIRE(VALID_CONFIG(lcfg
));
1119 REQUIRE(lctx
->category_count
!= 0);
1121 if (lctx
->category_count
== lcfg
->channellist_count
)
1122 return (ISC_R_SUCCESS
);
1124 bytes
= lctx
->category_count
* sizeof(ISC_LIST(isc_logchannellist_t
));
1126 lists
= isc_mem_get(lctx
->mctx
, bytes
);
1129 return (ISC_R_NOMEMORY
);
1131 memset(lists
, 0, bytes
);
1133 if (lcfg
->channellist_count
!= 0) {
1134 bytes
= lcfg
->channellist_count
*
1135 sizeof(ISC_LIST(isc_logchannellist_t
));
1136 memmove(lists
, lcfg
->channellists
, bytes
);
1137 isc_mem_put(lctx
->mctx
, lcfg
->channellists
, bytes
);
1140 lcfg
->channellists
= lists
;
1141 lcfg
->channellist_count
= lctx
->category_count
;
1143 return (ISC_R_SUCCESS
);
1147 greatest_version(isc_logchannel_t
*channel
, int *greatestp
) {
1148 /* XXXDCL HIGHLY NT */
1149 char *basename
, *digit_end
;
1150 const char *dirname
;
1151 int version
, greatest
= -1;
1154 isc_result_t result
;
1160 REQUIRE(channel
->type
== ISC_LOG_TOFILE
);
1163 * It is safe to DE_CONST the file.name because it was copied
1164 * with isc_mem_strdup in isc_log_createchannel.
1166 basename
= strrchr(FILE_NAME(channel
), sep
);
1168 basename2
= strrchr(FILE_NAME(channel
), '\\');
1169 if ((basename
!= NULL
&& basename2
!= NULL
&& basename2
> basename
) ||
1170 (basename
== NULL
&& basename2
!= NULL
)) {
1171 basename
= basename2
;
1175 if (basename
!= NULL
) {
1177 dirname
= FILE_NAME(channel
);
1179 DE_CONST(FILE_NAME(channel
), basename
);
1182 basenamelen
= strlen(basename
);
1185 result
= isc_dir_open(&dir
, dirname
);
1188 * Replace the file separator if it was taken out.
1190 if (basename
!= FILE_NAME(channel
))
1191 *(basename
- 1) = sep
;
1194 * Return if the directory open failed.
1196 if (result
!= ISC_R_SUCCESS
)
1199 while (isc_dir_read(&dir
) == ISC_R_SUCCESS
) {
1200 if (dir
.entry
.length
> basenamelen
&&
1201 strncmp(dir
.entry
.name
, basename
, basenamelen
) == 0 &&
1202 dir
.entry
.name
[basenamelen
] == '.') {
1204 version
= strtol(&dir
.entry
.name
[basenamelen
+ 1],
1206 if (*digit_end
== '\0' && version
> greatest
)
1210 isc_dir_close(&dir
);
1212 *greatestp
= ++greatest
;
1214 return (ISC_R_SUCCESS
);
1218 roll_log(isc_logchannel_t
*channel
) {
1220 char current
[PATH_MAX
+ 1];
1221 char new[PATH_MAX
+ 1];
1223 isc_result_t result
;
1226 * Do nothing (not even excess version trimming) if ISC_LOG_ROLLNEVER
1227 * is specified. Apparently complete external control over the log
1230 if (FILE_VERSIONS(channel
) == ISC_LOG_ROLLNEVER
)
1231 return (ISC_R_SUCCESS
);
1233 path
= FILE_NAME(channel
);
1236 * Set greatest_version to the greatest existing version
1237 * (not the maximum requested version). This is 1 based even
1238 * though the file names are 0 based, so an oldest log of log.1
1239 * is a greatest_version of 2.
1241 result
= greatest_version(channel
, &greatest
);
1242 if (result
!= ISC_R_SUCCESS
)
1246 * Now greatest should be set to the highest version number desired.
1247 * Since the highest number is one less than FILE_VERSIONS(channel)
1248 * when not doing infinite log rolling, greatest will need to be
1249 * decremented when it is equal to -- or greater than --
1250 * FILE_VERSIONS(channel). When greatest is less than
1251 * FILE_VERSIONS(channel), it is already suitable for use as
1252 * the maximum version number.
1255 if (FILE_VERSIONS(channel
) == ISC_LOG_ROLLINFINITE
||
1256 FILE_VERSIONS(channel
) > greatest
)
1260 * When greatest is >= FILE_VERSIONS(channel), it needs to
1261 * be reduced until it is FILE_VERSIONS(channel) - 1.
1262 * Remove any excess logs on the way to that value.
1264 while (--greatest
>= FILE_VERSIONS(channel
)) {
1265 n
= snprintf(current
, sizeof(current
), "%s.%d",
1267 if (n
>= (int)sizeof(current
) || n
< 0)
1268 result
= ISC_R_NOSPACE
;
1270 result
= isc_file_remove(current
);
1271 if (result
!= ISC_R_SUCCESS
&&
1272 result
!= ISC_R_FILENOTFOUND
)
1274 "unable to remove log file '%s.%d': %s",
1276 isc_result_totext(result
));
1279 for (i
= greatest
; i
> 0; i
--) {
1280 result
= ISC_R_SUCCESS
;
1281 n
= snprintf(current
, sizeof(current
), "%s.%d", path
, i
- 1);
1282 if (n
>= (int)sizeof(current
) || n
< 0)
1283 result
= ISC_R_NOSPACE
;
1284 if (result
== ISC_R_SUCCESS
) {
1285 n
= snprintf(new, sizeof(new), "%s.%d", path
, i
);
1286 if (n
>= (int)sizeof(new) || n
< 0)
1287 result
= ISC_R_NOSPACE
;
1289 if (result
== ISC_R_SUCCESS
)
1290 result
= isc_file_rename(current
, new);
1291 if (result
!= ISC_R_SUCCESS
&&
1292 result
!= ISC_R_FILENOTFOUND
)
1294 "unable to rename log file '%s.%d' to "
1295 "'%s.%d': %s", path
, i
- 1, path
, i
,
1296 isc_result_totext(result
));
1299 if (FILE_VERSIONS(channel
) != 0) {
1300 n
= snprintf(new, sizeof(new), "%s.0", path
);
1301 if (n
>= (int)sizeof(new) || n
< 0)
1302 result
= ISC_R_NOSPACE
;
1304 result
= isc_file_rename(path
, new);
1305 if (result
!= ISC_R_SUCCESS
&&
1306 result
!= ISC_R_FILENOTFOUND
)
1308 "unable to rename log file '%s' to '%s.0': %s",
1309 path
, path
, isc_result_totext(result
));
1311 result
= isc_file_remove(path
);
1312 if (result
!= ISC_R_SUCCESS
&&
1313 result
!= ISC_R_FILENOTFOUND
)
1314 syslog(LOG_ERR
, "unable to remove log file '%s': %s",
1315 path
, isc_result_totext(result
));
1318 return (ISC_R_SUCCESS
);
1322 isc_log_open(isc_logchannel_t
*channel
) {
1323 struct stat statbuf
;
1324 isc_boolean_t regular_file
;
1325 isc_boolean_t roll
= ISC_FALSE
;
1326 isc_result_t result
= ISC_R_SUCCESS
;
1329 REQUIRE(channel
->type
== ISC_LOG_TOFILE
);
1330 REQUIRE(FILE_STREAM(channel
) == NULL
);
1332 path
= FILE_NAME(channel
);
1334 REQUIRE(path
!= NULL
&& *path
!= '\0');
1337 * Determine type of file; only regular files will be
1338 * version renamed, and only if the base file exists
1339 * and either has no size limit or has reached its size limit.
1341 if (stat(path
, &statbuf
) == 0) {
1342 regular_file
= S_ISREG(statbuf
.st_mode
) ? ISC_TRUE
: ISC_FALSE
;
1343 /* XXXDCL if not regular_file complain? */
1344 if ((FILE_MAXSIZE(channel
) == 0 &&
1345 FILE_VERSIONS(channel
) != ISC_LOG_ROLLNEVER
) ||
1346 (FILE_MAXSIZE(channel
) > 0 &&
1347 statbuf
.st_size
>= FILE_MAXSIZE(channel
)))
1348 roll
= regular_file
;
1349 } else if (errno
== ENOENT
) {
1350 regular_file
= ISC_TRUE
;
1353 result
= ISC_R_INVALIDFILE
;
1358 if (result
== ISC_R_SUCCESS
&& roll
) {
1359 if (FILE_VERSIONS(channel
) == ISC_LOG_ROLLNEVER
)
1360 return (ISC_R_MAXSIZE
);
1361 result
= roll_log(channel
);
1362 if (result
!= ISC_R_SUCCESS
) {
1363 if ((channel
->flags
& ISC_LOG_OPENERR
) == 0) {
1365 "isc_log_open: roll_log '%s' "
1368 isc_result_totext(result
));
1369 channel
->flags
|= ISC_LOG_OPENERR
;
1375 result
= isc_stdio_open(path
, "a", &FILE_STREAM(channel
));
1381 isc_log_wouldlog(isc_log_t
*lctx
, int level
) {
1383 * Try to avoid locking the mutex for messages which can't
1384 * possibly be logged to any channels -- primarily debugging
1385 * messages that the debug level is not high enough to print.
1387 * If the level is (mathematically) less than or equal to the
1388 * highest_level, or if there is a dynamic channel and the level is
1389 * less than or equal to the debug level, the main loop must be
1390 * entered to see if the message should really be output.
1392 * NOTE: this is UNLOCKED access to the logconfig. However,
1393 * the worst thing that can happen is that a bad decision is made
1394 * about returning without logging, and that's not a big concern,
1395 * because that's a risk anyway if the logconfig is being
1396 * dynamically changed.
1399 if (lctx
== NULL
|| lctx
->logconfig
== NULL
)
1402 return (ISC_TF(level
<= lctx
->logconfig
->highest_level
||
1403 (lctx
->logconfig
->dynamic
&&
1404 level
<= lctx
->debug_level
)));
1408 isc_log_doit(isc_log_t
*lctx
, isc_logcategory_t
*category
,
1409 isc_logmodule_t
*module
, int level
, isc_boolean_t write_once
,
1410 isc_msgcat_t
*msgcat
, int msgset
, int msg
,
1411 const char *format
, va_list args
)
1414 char time_string
[64];
1415 char level_string
[24];
1416 const char *iformat
;
1417 struct stat statbuf
;
1418 isc_boolean_t matched
= ISC_FALSE
;
1419 isc_boolean_t printtime
, printtag
, printcolon
;
1420 isc_boolean_t printcategory
, printmodule
, printlevel
;
1421 isc_logconfig_t
*lcfg
;
1422 isc_logchannel_t
*channel
;
1423 isc_logchannellist_t
*category_channels
;
1424 isc_result_t result
;
1426 REQUIRE(lctx
== NULL
|| VALID_CONTEXT(lctx
));
1427 REQUIRE(category
!= NULL
);
1428 REQUIRE(module
!= NULL
);
1429 REQUIRE(level
!= ISC_LOG_DYNAMIC
);
1430 REQUIRE(format
!= NULL
);
1433 * Programs can use libraries that use this logging code without
1434 * wanting to do any logging, thus the log context is allowed to
1440 REQUIRE(category
->id
< lctx
->category_count
);
1441 REQUIRE(module
->id
< lctx
->module_count
);
1443 if (! isc_log_wouldlog(lctx
, level
))
1447 iformat
= isc_msgcat_get(msgcat
, msgset
, msg
, format
);
1451 time_string
[0] = '\0';
1452 level_string
[0] = '\0';
1456 lctx
->buffer
[0] = '\0';
1458 lcfg
= lctx
->logconfig
;
1460 category_channels
= ISC_LIST_HEAD(lcfg
->channellists
[category
->id
]);
1463 * XXXDCL add duplicate filtering? (To not write multiple times to
1464 * the same source via various channels).
1468 * If the channel list end was reached and a match was made,
1469 * everything is finished.
1471 if (category_channels
== NULL
&& matched
)
1474 if (category_channels
== NULL
&& ! matched
&&
1475 category_channels
!= ISC_LIST_HEAD(lcfg
->channellists
[0]))
1477 * No category/module pair was explicitly configured.
1478 * Try the category named "default".
1481 ISC_LIST_HEAD(lcfg
->channellists
[0]);
1483 if (category_channels
== NULL
&& ! matched
)
1485 * No matching module was explicitly configured
1486 * for the category named "default". Use the internal
1489 category_channels
= &default_channel
;
1491 if (category_channels
->module
!= NULL
&&
1492 category_channels
->module
!= module
) {
1493 category_channels
= ISC_LIST_NEXT(category_channels
,
1500 channel
= category_channels
->channel
;
1501 category_channels
= ISC_LIST_NEXT(category_channels
, link
);
1503 if (((channel
->flags
& ISC_LOG_DEBUGONLY
) != 0) &&
1504 lctx
->debug_level
== 0)
1507 if (channel
->level
== ISC_LOG_DYNAMIC
) {
1508 if (lctx
->debug_level
< level
)
1510 } else if (channel
->level
< level
)
1513 if ((channel
->flags
& ISC_LOG_PRINTTIME
) != 0 &&
1514 time_string
[0] == '\0') {
1518 isc_time_formattimestamp(&isctime
, time_string
,
1519 sizeof(time_string
));
1522 if ((channel
->flags
& ISC_LOG_PRINTLEVEL
) != 0 &&
1523 level_string
[0] == '\0') {
1524 if (level
< ISC_LOG_CRITICAL
)
1525 snprintf(level_string
, sizeof(level_string
),
1526 isc_msgcat_get(isc_msgcat
,
1531 else if (level
> ISC_LOG_DYNAMIC
)
1532 snprintf(level_string
, sizeof(level_string
),
1533 "%s %d: ", log_level_strings
[0],
1536 snprintf(level_string
, sizeof(level_string
),
1537 "%s: ", log_level_strings
[-level
]);
1541 * Only format the message once.
1543 if (lctx
->buffer
[0] == '\0') {
1544 (void)vsnprintf(lctx
->buffer
, sizeof(lctx
->buffer
),
1548 * Check for duplicates.
1551 isc_logmessage_t
*message
, *new;
1553 isc_interval_t interval
;
1555 isc_interval_set(&interval
,
1556 lcfg
->duplicate_interval
, 0);
1559 * 'oldest' is the age of the oldest messages
1560 * which fall within the duplicate_interval
1564 if (isc_time_subtract(&oldest
, &interval
, &oldest
)
1567 * Can't effectively do the checking
1568 * without having a valid time.
1572 message
=ISC_LIST_HEAD(lctx
->messages
);
1574 while (message
!= NULL
) {
1575 if (isc_time_compare(&message
->time
,
1578 * This message is older
1579 * than the duplicate_interval,
1580 * so it should be dropped from
1583 * Setting the interval to be
1584 * to be longer will obviously
1585 * not cause the expired
1586 * message to spring back into
1589 new = ISC_LIST_NEXT(message
,
1592 ISC_LIST_UNLINK(lctx
->messages
,
1595 isc_mem_put(lctx
->mctx
,
1597 sizeof(*message
) + 1 +
1598 strlen(message
->text
));
1605 * This message is in the duplicate
1606 * filtering interval ...
1608 if (strcmp(lctx
->buffer
, message
->text
)
1611 * ... and it is a duplicate.
1612 * Unlock the mutex and
1613 * get the hell out of Dodge.
1615 UNLOCK(&lctx
->lock
);
1619 message
= ISC_LIST_NEXT(message
, link
);
1623 * It wasn't in the duplicate interval,
1624 * so add it to the message list.
1626 new = isc_mem_get(lctx
->mctx
,
1627 sizeof(isc_logmessage_t
) +
1628 strlen(lctx
->buffer
) + 1);
1631 * Put the text immediately after
1632 * the struct. The strcpy is safe.
1634 new->text
= (char *)(new + 1);
1635 strcpy(new->text
, lctx
->buffer
);
1637 TIME_NOW(&new->time
);
1639 ISC_LINK_INIT(new, link
);
1640 ISC_LIST_APPEND(lctx
->messages
,
1646 printtime
= ISC_TF((channel
->flags
& ISC_LOG_PRINTTIME
)
1648 printtag
= ISC_TF((channel
->flags
&
1649 (ISC_LOG_PRINTTAG
|ISC_LOG_PRINTPREFIX
))
1650 != 0 && lcfg
->tag
!= NULL
);
1651 printcolon
= ISC_TF((channel
->flags
& ISC_LOG_PRINTTAG
)
1652 != 0 && lcfg
->tag
!= NULL
);
1653 printcategory
= ISC_TF((channel
->flags
& ISC_LOG_PRINTCATEGORY
)
1655 printmodule
= ISC_TF((channel
->flags
& ISC_LOG_PRINTMODULE
)
1657 printlevel
= ISC_TF((channel
->flags
& ISC_LOG_PRINTLEVEL
)
1660 switch (channel
->type
) {
1661 case ISC_LOG_TOFILE
:
1662 if (FILE_MAXREACHED(channel
)) {
1664 * If the file can be rolled, OR
1665 * If the file no longer exists, OR
1666 * If the file is less than the maximum size,
1667 * (such as if it had been renamed and
1668 * a new one touched, or it was truncated
1670 * ... then close it to trigger reopening.
1672 if (FILE_VERSIONS(channel
) !=
1673 ISC_LOG_ROLLNEVER
||
1674 (stat(FILE_NAME(channel
), &statbuf
) != 0 &&
1676 statbuf
.st_size
< FILE_MAXSIZE(channel
)) {
1677 (void)fclose(FILE_STREAM(channel
));
1678 FILE_STREAM(channel
) = NULL
;
1679 FILE_MAXREACHED(channel
) = ISC_FALSE
;
1687 if (FILE_STREAM(channel
) == NULL
) {
1688 result
= isc_log_open(channel
);
1689 if (result
!= ISC_R_SUCCESS
&&
1690 result
!= ISC_R_MAXSIZE
&&
1691 (channel
->flags
& ISC_LOG_OPENERR
) == 0) {
1693 "isc_log_open '%s' failed: %s",
1695 isc_result_totext(result
));
1696 channel
->flags
|= ISC_LOG_OPENERR
;
1698 if (result
!= ISC_R_SUCCESS
)
1700 channel
->flags
&= ~ISC_LOG_OPENERR
;
1704 case ISC_LOG_TOFILEDESC
:
1705 fprintf(FILE_STREAM(channel
),
1706 "%s%s%s%s%s%s%s%s%s%s\n",
1707 printtime
? time_string
: "",
1708 printtime
? " " : "",
1709 printtag
? lcfg
->tag
: "",
1710 printcolon
? ": " : "",
1711 printcategory
? category
->name
: "",
1712 printcategory
? ": " : "",
1713 printmodule
? (module
!= NULL
? module
->name
1716 printmodule
? ": " : "",
1717 printlevel
? level_string
: "",
1720 fflush(FILE_STREAM(channel
));
1723 * If the file now exceeds its maximum size
1724 * threshold, note it so that it will not be logged
1727 if (FILE_MAXSIZE(channel
) > 0) {
1728 INSIST(channel
->type
== ISC_LOG_TOFILE
);
1730 /* XXXDCL NT fstat/fileno */
1731 /* XXXDCL complain if fstat fails? */
1732 if (fstat(fileno(FILE_STREAM(channel
)),
1734 statbuf
.st_size
> FILE_MAXSIZE(channel
))
1735 FILE_MAXREACHED(channel
) = ISC_TRUE
;
1740 case ISC_LOG_TOSYSLOG
:
1742 syslog_level
= LOG_DEBUG
;
1743 else if (level
< ISC_LOG_CRITICAL
)
1744 syslog_level
= LOG_CRIT
;
1746 syslog_level
= syslog_map
[-level
];
1748 (void)syslog(FACILITY(channel
) | syslog_level
,
1749 "%s%s%s%s%s%s%s%s%s%s",
1750 printtime
? time_string
: "",
1751 printtime
? " " : "",
1752 printtag
? lcfg
->tag
: "",
1753 printcolon
? ": " : "",
1754 printcategory
? category
->name
: "",
1755 printcategory
? ": " : "",
1756 printmodule
? (module
!= NULL
1760 printmodule
? ": " : "",
1761 printlevel
? level_string
: "",
1765 case ISC_LOG_TONULL
:
1772 UNLOCK(&lctx
->lock
);