1 .\" $Id: logging.mdoc,v 1.3 2004/03/09 06:30:08 marka Exp $
3 .\" Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
4 .\" Copyright (c) 1995-1999 by Internet Software Consortium
6 .\" Permission to use, copy, modify, and distribute this software for any
7 .\" purpose with or without fee is hereby granted, provided that the above
8 .\" copyright notice and this permission notice appear in all copies.
10 .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
11 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
13 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 .\" OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 .\" The following six UNCOMMENTED lines are required.
20 .\"Os OPERATING_SYSTEM [version/release]
22 .\"Dt DOCUMENT_TITLE [section number] [volume]
23 .Dt LOGGING @SYSCALL_EXT@
26 .Nm log_close_stream ,
28 .Nm log_get_filename ,
32 .Nm log_free_context ,
34 .Nm log_remove_channel ,
36 .Nm log_category_is_active ,
37 .Nm log_new_syslog_channel ,
38 .Nm log_new_file_channel ,
39 .Nm log_set_file_owner ,
40 .Nm log_new_null_channel ,
41 .Nm log_inc_references ,
42 .Nm log_dec_references ,
46 .Fd #include <isc/logging.h>
48 .Fn log_open_stream "log_channel chan"
50 .Fn log_close_stream "log_channel chan"
52 .Fn log_get_stream "log_channel chan"
54 .Fn log_get_filename "log_channel chan"
56 .Fn log_vwrite "log_context lc" "int category" "int level" \
57 "const char *format" va_list args"
59 .Fn log_write "log_context lc" "int category" "int level" \
60 "const char *format" "..."
62 .Fn log_check_channel "log_context lc" "int level" "log_channel chan"
64 .Fn log_check "log_context lc" "int category" "int level"
66 .Fn log_new_context "int num_categories" "char **category_names" \
69 .Fn log_free_context "log_context lc"
71 .Fn log_add_channel "log_context lc" "int category" "log_channel chan"
73 .Fn log_remove_channel "log_context lc" "int category" "log_channel chan"
75 .Fn log_option "log_context lc" "int option" "int value"
77 .Fn log_category_is_active "log_context lc" "int category"
79 .Fn log_new_syslog_channel "unsigned int flags" "int level" "int facility"
81 .Fn log_new_file_channel "unsigned int flags" "int level" \
82 "char *name" "FILE *stream" "unsigned int versions" \
83 "unsigned long max_size"
85 .Fn log_set_file_owner "log_channel chan" "uid_t owner" "gid_t group"
87 .Fn log_new_null_channel "void"
89 .Fn log_inc_references "log_channel chan"
91 .Fn log_dec_references "log_channel chan"
93 .Fn log_free_channel "log_channel chan"
98 is flexible logging system which is based upon a set of concepts:
99 .Nm logging channels ,
102 .Nm logging contexts .
104 The basic building block is the
105 .Dq Nm logging channel ,
108 (logging level), which type of logging is to occur, and other
109 flags and information associated with technical aspects of the logging.
110 The set of priorities which are supported is shown below, in the section
111 .Sx Message Priorities .
112 A priority sets a threshold for message logging; a logging channel will
114 log those messages which are
115 .Em at least as important
116 as its priority indicates. (The fact that
120 under the current scheme, is an implementation detail; if a channel has
125 log messages with the
129 log messages with the
137 also has an indication of the type of logging performed. Currently,
143 .Bl -tag -width "log_syslog" -compact -offset indent
146 .Xr syslog 3 Ns -style
155 A new logging channel is created by calling either
156 .Fn log_new_syslog_channel ,
157 .Fn log_new_file_channel ,
159 .Fn log_new_null_channel ,
161 When a channel is no longer to be used, it can be freed using
162 .Fn log_free_channel .
168 channel types can include more information; for instance, a
169 .Dv log_syslog Ns -type
170 channel allows the specification of a
171 .Xr syslog 3 Ns -style
174 .Dv log_file Ns -type
175 channels allows the caller to set a maximum file size and number
177 .Fn log_new_syslog_channel
179 .Fn log_new_file_channel ,
181 Additionally, once a logging channel of type
183 is defined, the functions
187 can open or close the stream associated with the logging channel's logging
192 functions return the stream or filename, respectively, of such a logging
193 channel. Also unique to logging channels of type
196 .Fn log_set_file_owner
197 function, which tells the logging system what user and group ought to own
198 newly created files (which is only effective if the caller is privileged.)
202 determining both the number of such categories and any (optional) names.
203 Categories are like array indexes in C; if the caller declares
205 categories, then they are considered to run from 0 to
207 with this scheme, a category number would be invalid if it were negative or
208 greater than/equal to
210 Each category can have its own list of
212 associated with it; we say that such a channel is
214 the particular category.
216 Individual logging channels can appear in more than one category.
219 .Dq Nm logging context
222 associated with the context's
226 scheme is associated with a particular
227 .Nm logging context .
229 A logging channel may appear in more than one logging context, and in
230 multiple categories within each logging context.
235 .Fn log_remove_channel
236 to add or remove a logging channel to some category in a logging context.
237 To see if a given category in a logging context is being used, use the
239 .Fn log_category_is_active .
246 and various flags associated with the whole context; in order to alter the
247 flags or change the priority of a context, use
249 .Ss Message Priorities
252 (logging levels) are supported (they can also be found in the header file):
253 .Bd -literal -offset indent
254 #define log_critical (-5)
255 #define log_error (-4)
256 #define log_warning (-3)
257 #define log_notice (-2)
258 #define log_info (-1)
261 In the current implementation, logging messages which have a level greater
262 than 0 are considered to be debugging messages.
266 currently supported are different values of the enumerated type
268 (these are also listed in the header file):
269 .Bd -literal -offset indent
270 typedef enum { log_syslog, log_file, log_null } log_output_type;
272 .Ss Logging Channel Flags
273 There are several flags which can be set on a logging channel; the flags
274 and their meanings are as follows (they are also found in the header file):
275 .Bl -tag -width "LOG_USE_CONTEXT_LEVEL " -offset indent
276 .It Dv LOG_CHANNEL_BROKEN
277 This is set only when some portion of
287 way; versioning or truncation is requested on a non-normal file.
288 .It Dv LOG_CHANNEL_OFF
289 This is set for channels opened by
290 .Fn log_new_null_channel .
291 .It Dv LOG_CLOSE_STREAM
292 If this flag is set, then
296 stream of a logging channel which is being
298 (if the logging channel is of type
301 .It Dv LOG_PRINT_CATEGORY
304 will insert the category name, if available, into logging messages which are
305 logged to channels of type
309 .It Dv LOG_PRINT_LEVEL
312 will insert a string identifying the message priority level into the
313 information logged to channels of type
317 .It Dv LOG_REQUIRE_DEBUG
318 Only log debugging messages (i.e., those with a priority greater than zero).
322 will insert a timestamp into logging messages which are logged to channels of
328 Truncate logging file when re-opened
329 .Fn ( log_open_stream
334 a new file of the same name with the
337 .It Dv LOG_USE_CONTEXT_LEVEL
338 Use the logging context's priority or logging level, rather than the logging
339 channel's own priority. This can be useful for those channels which are
340 included in multiple logging contexts.
342 .Ss FUNCTION DESCRIPTIONS
345 is for use with channels which log to a file; i.e., logging channels with a
349 If the logging channel pointed to by
351 is valid, it attempts to open (and return) the stream associated with that
352 channel. If the stream is already opened, then it is returned; otherwise,
354 is used to test the filename for the stream.
356 At this point, if the logging file is supposed to have different
358 (i.e., incremented version numbers; higher numbers indicate older versions
359 of the logging file). If so, then any existing versions are
361 to have one version-number higher than previously, and the
363 filename for the stream is set to the
365 form of the name. Next, if the logging file is supposed to be truncated
370 field of the logging channel structure is set), then any file with the
372 filename for the stream is
375 If the logging file is
377 a regular file, and either of the above operations (version numbering
378 or truncation) is supposed to take place, a
380 file pointer is returned.
382 Finally, the filename associated with the logging channel is
384 using the appropriate flags and a mode which sets the read/write permissions
385 for the user, group, and others. The file descriptor returned by
389 with the append mode set, and the stream returned by this call is stored
392 structure and returned.
396 fails at any point, then the
397 .Dv LOG_CHANNEL_BROKEN
400 field of the logging channel pointed to by
406 contains pertinent information.
410 function closes the stream associated with the logging channel pointed to by
414 is valid and the stream exists and can be closed properly by
424 returns the stream associated with the logging channel pointed to by
428 and specifies a logging channel which has a
430 or stream associated with it.
434 function returns the name of the file associated with the logging channel
439 and specifies a logging channel which has a file associated with it.
443 function performs the actual logging of a message to the various logging
444 channels of a logging context
446 The message consists of an
447 .Xr fprint 3 Ns -style
451 (if any); it will be written to all logging channels in the given
453 which have a priority set to
457 priority value. If the
459 is not valid or has no logging channels, then the category defaults to 0.
461 There are a number of conditions under which a call to
463 will not result in actually logging the message: if there is no logging channel
464 at even the default category (0), or if a given channel is either
468 (i.e., its flags have
469 .Dv LOG_CHANNEL_BROKEN
472 set, respectively), or if the logging channel channel is of type
474 Additionally, if the logging channel's flag has
475 .Dv LOG_REQUIRE_DEBUG
476 set and the message is not a debugging message (i.e., has a level greater
477 than 0), then it will not be logged.
478 Finally, if the message's priority is less important than the
479 channel's logging level (the priority threshold), will not be logged.
481 If a logging channel's flag has
482 .Dv LOG_USE_CONTEXT_LEVEL
483 set, it will use the logging context's priority, rather than its own.
485 If all of these hurdles are passed, then only
489 channels actually can have logging. For channels which use
493 facility is used in conjunction with a potentially modified form of the
494 message's priority level, since
496 has its own system of priorities
497 .Pq Pa /usr/include/syslog.h .
498 All debug messages (priority >= 0) are mapped to
501 priority, all messages
507 and the priorities corresponding to the ones listed in the section
508 .Sx Message Priorities
509 are given the obvious corresponding
515 type logging channels, if the file size is greater than the maximum file
516 size, then no logging occurs. (The same thing happens if a
518 stream is encountered and
520 fails to open the channel's stream.)
522 For both logging to normal files and logging via
524 the value of the flags
526 .Dv LOG_PRINT_CATEGORY ,
529 are used in determining whether or not these items are included in the logged
534 function is merely a front-end to a call to
536 see the description of that function, above, for more information.
540 .Fn log_check_channel
541 are used to see if a contemplated logging call will actually generate any
542 output, which is useful when creating a log message involves non-trivial
545 will return non-zero if a call to
551 would generate output on any channels, and zero otherwise.
552 .Fn log_check_channel
553 will return non-zero if writing to the
557 would generate output.
562 .Nm logging context ,
563 and stores this in the
565 field of the argument
567 and opaque structure used internally. This new
570 .Dq Fa num_categories
572 .Dq Fa category_names
573 which are supplied; the latter can be
577 .Dq Fa category_names
580 be freed by the caller, if it is
581 .No non- Ns Dv NULL .
582 The initial logging flags and priority are both set to zero.
586 function is used to free the opaque structure
596 .No non- Ns Dv NULL .
597 For each of the various
600 .Dq Va num_categories
601 which were in the corresponding call to
602 .Fn log_new_context )
603 associated with the given
604 .Nm logging context ,
610 The opaque structure itself is then
622 free the memory associated with
624 since the logging library did not allocate the memory for it, originally;
625 it was supplied in the call to
626 .Fn log_new_context .
633 to the list of logging channels in the given
638 No checking is performed to see whether or not
640 is already present in the given
642 so multiple instances in a single
645 .Fn log_remove_channel ,
649 .Fn log_remove_channel
656 from the list of logging channels in the given
661 It also attempts to free the channel by calling
663 (see its description, below).
667 function is used to change the
669 of the indicated logging context
678 .Dv LOG_OPTION_DEBUG ;
679 in the first case, the log context's debugging level is reset to the
680 indicated level. If the
683 .Dv LOG_OPTION_DEBUG ,
686 results in setting the debug flag of the logging context, while a zero
688 means that the debug flag is reset.
691 .Fn log_category_is_active
692 test returns a 1 if the given
694 of the indicated logging context
696 has at least one logging channel, and 0, otherwise.
699 .Fn log_new_syslog_channel ,
700 .Fn log_new_file_channel ,
702 .Fn log_new_null_channel
703 create a new channel of the type specified (thus, the difference in arguments);
708 .Ft struct log_channel
710 is always set to the appropriate value.
713 .Fn log_new_syslog_channel
717 .Ft struct log_channel
721 i.e., a logging channel which will use
723 The new structure is filled out with the
730 field is initialized to zero.
732 .Sx Logging Channel Flags
734 .Sx Message Priorities ,
735 above, or the header file for information about acceptable values for
743 facility; see the appropriate system header file or manpage for more
747 .Fn log_new_file_channel "unsigned int flags" "int level" \
748 "char *name" "FILE *stream" "unsigned int versions" \
749 "unsigned long max_size"
751 .Fn log_new_null_channel
754 .Fn log_inc_references
756 .Fn log_dec_references
757 increment or decrements, respectively, the
759 field of the logging channel pointed to by
761 if it is a valid channel (and if the
763 field is strictly positive, in the case of
764 .Fn log_dec_references ) .
765 These functions are meant to track changes in the number of different clients
766 which refer to the given logging channel.
771 field of the logging channel pointed to by
773 if there are no more outstanding references to it. If the channel uses a file,
778 flag is set), and the filename, if
779 .No non- Ns Dv NULL ,
787 .\" The following requests should be uncommented and
788 .\" used where appropriate. This next request is
789 .\" for sections 2 and 3 function return values only.
791 .\" This next request is for sections 1, 6, 7 & 8 only
792 .Bl -tag -width "log_category_is_active()"
793 .It Fn log_open_stream
795 is returned under any of several error conditions:
801 .No non- Ns Dv log_file
803 .Pq Va errno No is set to Dv EINVAL ;
804 b) if either versioning or truncation is requested for a non-normal file
805 .Pq Va errno No is set to Dv EINVAL ;
814 is set by the call which failed
816 If some value other than
818 is returned, then it is a valid logging stream (either newly-opened or
820 .It Fn log_close_stream
821 -1 if the stream associated with
828 0 if successful or the logging channel pointed to by
832 or not a logging channel which has uses a file); in the latter case,
836 .It Fn log_get_stream
838 under the same conditions as those under which
839 .Fn log_close_stream ,
840 above, returns 0 (including the setting of
842 Otherwise, the stream associated with the logging channel is returned.
843 .It Fn log_get_filename
845 under the same conditions as those under which
846 .Fn log_close_stream ,
847 above, returns 0 (including the setting of
849 Otherwise, the name of the file associated with the logging channel is
851 .It Fn log_new_context
855 .Pq with Va errno No set to Dv ENOMEM .
858 containing the new structures and information.
859 .It Fn log_add_channel
867 is invalid (negative or greater than or equal to
868 .Va lcp->num_categories ) ,
876 .Pq with Va errno No set to Dv ENOMEM .
878 .It Fn log_remove_channel
886 is invalid, as under failure condition a) for
887 .Fn log_add_channel ,
888 above, including the setting of
890 b) no channel numbered
892 is found in the logging context indicated by
894 .Pq with Va errno No set to Dv ENOENT .
904 specifies an unknown logging option;
910 .It Fn log_category_is_active
915 .Pq with Va errno No set to Dv EINVAL ;
918 number is valid and there are logging channels in this
920 within the indicated logging context; 0 if the
922 number is invalid or there are no logging channels in this
924 within the indicated logging context.
925 .It Fn log_new_syslog_channel
930 .Pq with Va errno No set to ENOMEM ;
932 .Dv log_syslog Ns -type
934 .It Fn log_new_file_channel
939 .Pq with Va errno No set to ENOMEM ;
941 .Dv log_file Ns -type
943 .It Fn log_new_null_channel
948 .Pq with Va errno No set to ENOMEM ;
950 .Dv log_null Ns -type
952 .It Fn log_inc_references
957 .Pq with Va errno set to Dv EINVAL .
959 .It Fn log_dec_references
966 field is already <= 0
967 .Pq with Va errno set to Dv EINVAL .
969 .It Fn log_free_channel
970 -1 under the same conditions as
971 .Fn log_dec_references ,
972 above, including the setting of
978 .Bl -tag -width "isc/logging.h"
980 include file for logging library
982 .Xr syslog 3 Ns -style
986 .\" This next request is for sections 1, 6, 7 & 8 only
987 .\" (command return values (to shell) and
988 .\" fprintf/stderr type diagnostics)
990 .\" The next request is for sections 2 and 3 error
991 .\" and signal handling only.
993 This table shows which functions can return the indicated error in the
997 section, above, for more information.
998 .Bl -tag -width "(any0other0value)0"
1000 .Fn log_open_stream ,
1001 .Fn log_close_stream ,
1002 .Fn log_get_stream ,
1003 .Fn log_get_filename ,
1004 .Fn log_add_channel ,
1005 .Fn log_remove_channel ,
1007 .Fn log_category_is_active ,
1008 .Fn log_inc_references ,
1009 .Fn log_dec_references ,
1010 .Fn log_free_channel .
1012 .Fn log_remove_channel .
1014 .Fn log_new_context ,
1015 .Fn log_add_channel ,
1016 .Fn log_new_syslog_channel ,
1017 .Fn log_new_file_channel ,
1018 .Fn log_new_null_channel .
1019 .It (any other value)
1020 returned via a pass-through of an error code from
1027 and functions which call it
1028 .Pq currently, only Fn log_vwrite .
1034 .Fn log_free_context
1043 can also exit with a critical error logged via
1045 indicating a memory overrun
1047 .Xr @INDOT@named @SYS_OPS_EXT@ ,
1049 The HTML documentation includes a file,
1051 which has more information about this logging system.