4 * Copyright (C) 2004, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 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.
20 /* Id: log_test.c,v 1.26 2007/06/19 23:46:59 tbox Exp */
22 /* Principal Authors: DCL */
29 #include <isc/commandline.h>
31 #include <isc/string.h>
35 #define TEST_FILE "/tmp/test_log"
36 #define SYSLOG_FILE "/var/log/daemon.log"
37 #define FILE_VERSIONS 10
39 char usage
[] = "Usage: %s [-m] [-s syslog_logfile] [-r file_versions]\n";
41 #define CHECK(expr) result = expr; \
42 if (result != ISC_R_SUCCESS) { \
43 fprintf(stderr, "%s: " #expr "%s: exiting\n", \
44 progname, isc_result_totext(result)); \
48 main(int argc
, char **argv
) {
49 const char *progname
, *syslog_file
, *message
;
50 int ch
, i
, file_versions
, stderr_line
;
51 isc_boolean_t show_final_mem
= ISC_FALSE
;
53 isc_logconfig_t
*lcfg
;
56 isc_logdestination_t destination
;
57 const isc_logcategory_t
*category
;
58 const isc_logmodule_t
*module
;
60 progname
= strrchr(*argv
, '/');
66 syslog_file
= SYSLOG_FILE
;
67 file_versions
= FILE_VERSIONS
;
69 while ((ch
= isc_commandline_parse(argc
, argv
, "ms:r:")) != -1) {
72 show_final_mem
= ISC_TRUE
;
75 syslog_file
= isc_commandline_argument
;
78 file_versions
= atoi(isc_commandline_argument
);
79 if (file_versions
< 0 &&
80 file_versions
!= ISC_LOG_ROLLNEVER
&&
81 file_versions
!= ISC_LOG_ROLLINFINITE
) {
82 fprintf(stderr
, "%s: file rotations must be "
83 "%d (ISC_LOG_ROLLNEVER),\n\t"
84 "%d (ISC_LOG_ROLLINFINITE) "
87 ISC_LOG_ROLLINFINITE
);
92 fprintf(stderr
, usage
, progname
);
97 argc
-= isc_commandline_index
;
98 argv
+= isc_commandline_index
;
101 fprintf(stderr
, usage
, progname
);
105 fprintf(stderr
, "EXPECT:\n%s%d%s%s%s",
106 "8 lines to stderr (first 4 numbered, #3 repeated)\n",
107 file_versions
== 0 || file_versions
== ISC_LOG_ROLLNEVER
? 1 :
108 file_versions
> 0 ? file_versions
+ 1 : FILE_VERSIONS
+ 1,
109 " " TEST_FILE
" files, and\n",
110 "2 lines to syslog\n",
111 "lines ending with exclamation marks are errors\n\n");
113 isc_log_opensyslog(progname
, LOG_PID
, LOG_DAEMON
);
119 CHECK(isc_mem_create(0, 0, &mctx
));
120 CHECK(isc_log_create(mctx
, &lctx
, &lcfg
));
122 CHECK(isc_log_settag(lcfg
, progname
));
124 isc_log_setcontext(lctx
);
126 dns_log_setcontext(lctx
);
129 * Test isc_log_categorybyname and isc_log_modulebyname.
131 category
= isc_log_categorybyname(lctx
, "notify");
132 if (category
!= NULL
)
133 fprintf(stderr
, "%s category found. (expected)\n",
136 fprintf(stderr
, "notify category not found!\n");
138 module
= isc_log_modulebyname(lctx
, "xyzzy");
140 fprintf(stderr
, "%s module found!\n", module
->name
);
142 fprintf(stderr
, "xyzzy module not found. (expected)\n");
145 * Create a file channel to test file opening, size limiting and
149 destination
.file
.name
= TEST_FILE
;
150 destination
.file
.maximum_size
= 1;
151 destination
.file
.versions
= file_versions
;
153 CHECK(isc_log_createchannel(lcfg
, "file_test", ISC_LOG_TOFILE
,
154 ISC_LOG_INFO
, &destination
,
158 ISC_LOG_PRINTCATEGORY
|
159 ISC_LOG_PRINTMODULE
));
162 * Create a dynamic debugging channel to a file descriptor.
164 destination
.file
.stream
= stderr
;
166 CHECK(isc_log_createchannel(lcfg
, "debug_test", ISC_LOG_TOFILEDESC
,
167 ISC_LOG_DYNAMIC
, &destination
,
173 * Test the usability of the four predefined logging channels.
175 CHECK(isc_log_usechannel(lcfg
, "default_syslog",
176 DNS_LOGCATEGORY_DATABASE
,
177 DNS_LOGMODULE_CACHE
));
178 CHECK(isc_log_usechannel(lcfg
, "default_stderr",
179 DNS_LOGCATEGORY_DATABASE
,
180 DNS_LOGMODULE_CACHE
));
181 CHECK(isc_log_usechannel(lcfg
, "default_debug",
182 DNS_LOGCATEGORY_DATABASE
,
183 DNS_LOGMODULE_CACHE
));
184 CHECK(isc_log_usechannel(lcfg
, "null",
185 DNS_LOGCATEGORY_DATABASE
,
189 * Use the custom channels.
191 CHECK(isc_log_usechannel(lcfg
, "file_test",
192 DNS_LOGCATEGORY_GENERAL
,
195 CHECK(isc_log_usechannel(lcfg
, "debug_test",
196 DNS_LOGCATEGORY_GENERAL
,
197 DNS_LOGMODULE_RBTDB
));
199 fprintf(stderr
, "\n==> stderr begin\n");
202 * Write to the internal default by testing both a category for which
203 * no channel has been specified and a category which was specified
204 * but not with the named module.
208 isc_log_write(lctx
, DNS_LOGCATEGORY_SECURITY
, DNS_LOGMODULE_RBT
,
209 ISC_LOG_CRITICAL
, "%s (%d)",
210 "Unspecified category and unspecified module to stderr",
212 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBT
,
213 ISC_LOG_CRITICAL
, "%s (%d)",
214 "Specified category and unspecified module to stderr",
218 * Write to default_syslog, default_stderr and default_debug.
220 isc_log_write(lctx
, DNS_LOGCATEGORY_DATABASE
, DNS_LOGMODULE_CACHE
,
221 ISC_LOG_WARNING
, "%s (%d twice)",
222 "Using the predefined channels to syslog+stderr",
226 * Write to predefined null channel.
228 isc_log_write(lctx
, DNS_LOGCATEGORY_DATABASE
, DNS_LOGMODULE_RBTDB
,
229 ISC_LOG_INFO
, "This is to null and should not appear!");
232 * Reset the internal default to use syslog instead of stderr,
235 CHECK(isc_log_usechannel(lcfg
, "default_syslog",
236 ISC_LOGCATEGORY_DEFAULT
, NULL
));
237 isc_log_write(lctx
, DNS_LOGCATEGORY_SECURITY
, DNS_LOGMODULE_RBT
,
238 ISC_LOG_ERROR
, "%s%s",
239 "This message to the redefined default category should ",
240 "be second in syslog");
242 * Write to the file channel.
244 if (file_versions
>= 0 || file_versions
== ISC_LOG_ROLLINFINITE
) {
247 * If file_versions is 0 or ISC_LOG_ROLLINFINITE, write
248 * the "should not appear" and "should be in file" messages
249 * to ensure they get rolled.
251 if (file_versions
<= 0)
252 file_versions
= FILE_VERSIONS
;
255 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
,
256 DNS_LOGMODULE_DB
, ISC_LOG_NOTICE
,
257 "This should be rolled over "
260 for (i
= file_versions
- 1; i
>= 0; i
--)
261 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
,
262 DNS_LOGMODULE_DB
, ISC_LOG_NOTICE
,
263 "should be in file %d/%d", i
,
266 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
,
267 DNS_LOGMODULE_DB
, ISC_LOG_NOTICE
,
268 "should be in base file");
270 file_versions
= FILE_VERSIONS
;
271 for (i
= 1; i
<= file_versions
; i
++)
272 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
,
273 DNS_LOGMODULE_DB
, ISC_LOG_NOTICE
,
274 "This is message %d in the log file", i
);
279 * Write a debugging message to a category that has no
280 * debugging channels for the named module.
282 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_DB
,
284 "This debug message should not appear!");
287 * Write debugging messages to a dynamic debugging channel.
289 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBTDB
,
290 ISC_LOG_CRITICAL
, "This critical message should "
291 "not appear because the debug level is 0!");
293 isc_log_setdebuglevel(lctx
, 3);
295 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBTDB
,
296 ISC_LOG_DEBUG(1), "%s (%d)",
297 "Dynamic debugging to stderr", stderr_line
++);
298 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBTDB
,
300 "This debug level is too high and should not appear!");
303 * Test out the duplicate filtering using the debug_test channel.
305 isc_log_setduplicateinterval(lcfg
, 10);
306 message
= "This message should appear only once on stderr";
308 isc_log_write1(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBTDB
,
309 ISC_LOG_CRITICAL
, "%s", message
);
310 isc_log_write1(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBTDB
,
311 ISC_LOG_CRITICAL
, message
);
313 isc_log_setduplicateinterval(lcfg
, 1);
314 message
= "This message should appear twice on stderr";
316 isc_log_write1(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBTDB
,
317 ISC_LOG_CRITICAL
, message
);
319 isc_log_write1(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBTDB
,
320 ISC_LOG_CRITICAL
, message
);
323 * Review where everything went.
327 system("head " TEST_FILE
"*; rm -f " TEST_FILE
"*");
329 freopen(syslog_file
, "r", stdin
);
330 fprintf(stderr
, "\n==> %s <==\n", syslog_file
);
334 isc_log_destroy(&lctx
);
337 isc_mem_stats(mctx
, stderr
);