1 /* $NetBSD: log_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2004, 2007, 2011, 2014 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.29 2011/08/28 23:46:51 marka Exp */
22 /* Principal Authors: DCL */
29 #include <isc/commandline.h>
31 #include <isc/string.h>
36 #define TEST_FILE "/tmp/test_log"
37 #define SYSLOG_FILE "/var/log/daemon.log"
38 #define FILE_VERSIONS 10
40 char usage
[] = "Usage: %s [-m] [-s syslog_logfile] [-r file_versions]\n";
42 #define CHECK(expr) result = expr; \
43 if (result != ISC_R_SUCCESS) { \
44 fprintf(stderr, "%s: " #expr "%s: exiting\n", \
45 progname, isc_result_totext(result)); \
49 main(int argc
, char **argv
) {
50 const char *progname
, *syslog_file
, *message
;
51 int ch
, i
, file_versions
, stderr_line
;
52 isc_boolean_t show_final_mem
= ISC_FALSE
;
54 isc_logconfig_t
*lcfg
;
57 isc_logdestination_t destination
;
58 const isc_logcategory_t
*category
;
59 const isc_logmodule_t
*module
;
61 progname
= strrchr(*argv
, '/');
67 syslog_file
= SYSLOG_FILE
;
68 file_versions
= FILE_VERSIONS
;
70 while ((ch
= isc_commandline_parse(argc
, argv
, "ms:r:")) != -1) {
73 show_final_mem
= ISC_TRUE
;
76 syslog_file
= isc_commandline_argument
;
79 file_versions
= atoi(isc_commandline_argument
);
80 if (file_versions
< 0 &&
81 file_versions
!= ISC_LOG_ROLLNEVER
&&
82 file_versions
!= ISC_LOG_ROLLINFINITE
) {
83 fprintf(stderr
, "%s: file rotations must be "
84 "%d (ISC_LOG_ROLLNEVER),\n\t"
85 "%d (ISC_LOG_ROLLINFINITE) "
88 ISC_LOG_ROLLINFINITE
);
93 fprintf(stderr
, usage
, progname
);
98 argc
-= isc_commandline_index
;
99 argv
+= isc_commandline_index
;
103 fprintf(stderr
, usage
, progname
);
107 fprintf(stderr
, "EXPECT:\n%s%d%s%s%s",
108 "8 lines to stderr (first 4 numbered, #3 repeated)\n",
109 file_versions
== 0 || file_versions
== ISC_LOG_ROLLNEVER
? 1 :
110 file_versions
> 0 ? file_versions
+ 1 : FILE_VERSIONS
+ 1,
111 " " TEST_FILE
" files, and\n",
112 "2 lines to syslog\n",
113 "lines ending with exclamation marks are errors\n\n");
115 isc_log_opensyslog(progname
, LOG_PID
, LOG_DAEMON
);
121 CHECK(isc_mem_create(0, 0, &mctx
));
122 CHECK(isc_log_create(mctx
, &lctx
, &lcfg
));
124 CHECK(isc_log_settag(lcfg
, progname
));
126 isc_log_setcontext(lctx
);
128 dns_log_setcontext(lctx
);
131 * Test isc_log_categorybyname and isc_log_modulebyname.
133 category
= isc_log_categorybyname(lctx
, "notify");
134 if (category
!= NULL
)
135 fprintf(stderr
, "%s category found. (expected)\n",
138 fprintf(stderr
, "notify category not found!\n");
140 module
= isc_log_modulebyname(lctx
, "xyzzy");
142 fprintf(stderr
, "%s module found!\n", module
->name
);
144 fprintf(stderr
, "xyzzy module not found. (expected)\n");
147 * Create a file channel to test file opening, size limiting and
151 destination
.file
.name
= TEST_FILE
;
152 destination
.file
.maximum_size
= 1;
153 destination
.file
.versions
= file_versions
;
155 CHECK(isc_log_createchannel(lcfg
, "file_test", ISC_LOG_TOFILE
,
156 ISC_LOG_INFO
, &destination
,
160 ISC_LOG_PRINTCATEGORY
|
161 ISC_LOG_PRINTMODULE
));
164 * Create a dynamic debugging channel to a file descriptor.
166 destination
.file
.stream
= stderr
;
168 CHECK(isc_log_createchannel(lcfg
, "debug_test", ISC_LOG_TOFILEDESC
,
169 ISC_LOG_DYNAMIC
, &destination
,
175 * Test the usability of the four predefined logging channels.
177 CHECK(isc_log_usechannel(lcfg
, "default_syslog",
178 DNS_LOGCATEGORY_DATABASE
,
179 DNS_LOGMODULE_CACHE
));
180 CHECK(isc_log_usechannel(lcfg
, "default_stderr",
181 DNS_LOGCATEGORY_DATABASE
,
182 DNS_LOGMODULE_CACHE
));
183 CHECK(isc_log_usechannel(lcfg
, "default_debug",
184 DNS_LOGCATEGORY_DATABASE
,
185 DNS_LOGMODULE_CACHE
));
186 CHECK(isc_log_usechannel(lcfg
, "null",
187 DNS_LOGCATEGORY_DATABASE
,
191 * Use the custom channels.
193 CHECK(isc_log_usechannel(lcfg
, "file_test",
194 DNS_LOGCATEGORY_GENERAL
,
197 CHECK(isc_log_usechannel(lcfg
, "debug_test",
198 DNS_LOGCATEGORY_GENERAL
,
199 DNS_LOGMODULE_RBTDB
));
201 fprintf(stderr
, "\n==> stderr begin\n");
204 * Write to the internal default by testing both a category for which
205 * no channel has been specified and a category which was specified
206 * but not with the named module.
210 isc_log_write(lctx
, DNS_LOGCATEGORY_SECURITY
, DNS_LOGMODULE_RBT
,
211 ISC_LOG_CRITICAL
, "%s (%d)",
212 "Unspecified category and unspecified module to stderr",
214 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBT
,
215 ISC_LOG_CRITICAL
, "%s (%d)",
216 "Specified category and unspecified module to stderr",
220 * Write to default_syslog, default_stderr and default_debug.
222 isc_log_write(lctx
, DNS_LOGCATEGORY_DATABASE
, DNS_LOGMODULE_CACHE
,
223 ISC_LOG_WARNING
, "%s (%d twice)",
224 "Using the predefined channels to syslog+stderr",
228 * Write to predefined null channel.
230 isc_log_write(lctx
, DNS_LOGCATEGORY_DATABASE
, DNS_LOGMODULE_RBTDB
,
231 ISC_LOG_INFO
, "This is to null and should not appear!");
234 * Reset the internal default to use syslog instead of stderr,
237 CHECK(isc_log_usechannel(lcfg
, "default_syslog",
238 ISC_LOGCATEGORY_DEFAULT
, NULL
));
239 isc_log_write(lctx
, DNS_LOGCATEGORY_SECURITY
, DNS_LOGMODULE_RBT
,
240 ISC_LOG_ERROR
, "%s%s",
241 "This message to the redefined default category should ",
242 "be second in syslog");
244 * Write to the file channel.
246 if (file_versions
>= 0 || file_versions
== ISC_LOG_ROLLINFINITE
) {
249 * If file_versions is 0 or ISC_LOG_ROLLINFINITE, write
250 * the "should not appear" and "should be in file" messages
251 * to ensure they get rolled.
253 if (file_versions
<= 0)
254 file_versions
= FILE_VERSIONS
;
257 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
,
258 DNS_LOGMODULE_DB
, ISC_LOG_NOTICE
,
259 "This should be rolled over "
262 for (i
= file_versions
- 1; i
>= 0; i
--)
263 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
,
264 DNS_LOGMODULE_DB
, ISC_LOG_NOTICE
,
265 "should be in file %d/%d", i
,
268 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
,
269 DNS_LOGMODULE_DB
, ISC_LOG_NOTICE
,
270 "should be in base file");
272 file_versions
= FILE_VERSIONS
;
273 for (i
= 1; i
<= file_versions
; i
++)
274 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
,
275 DNS_LOGMODULE_DB
, ISC_LOG_NOTICE
,
276 "This is message %d in the log file", i
);
281 * Write a debugging message to a category that has no
282 * debugging channels for the named module.
284 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_DB
,
286 "This debug message should not appear!");
289 * Write debugging messages to a dynamic debugging channel.
291 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBTDB
,
292 ISC_LOG_CRITICAL
, "This critical message should "
293 "not appear because the debug level is 0!");
295 isc_log_setdebuglevel(lctx
, 3);
297 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBTDB
,
298 ISC_LOG_DEBUG(1), "%s (%d)",
299 "Dynamic debugging to stderr", stderr_line
++);
300 isc_log_write(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBTDB
,
302 "This debug level is too high and should not appear!");
305 * Test out the duplicate filtering using the debug_test channel.
307 isc_log_setduplicateinterval(lcfg
, 10);
308 message
= "This message should appear only once on stderr";
310 isc_log_write1(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBTDB
,
311 ISC_LOG_CRITICAL
, "%s", message
);
312 isc_log_write1(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBTDB
,
313 ISC_LOG_CRITICAL
, "%s", message
);
315 isc_log_setduplicateinterval(lcfg
, 1);
316 message
= "This message should appear twice on stderr";
318 isc_log_write1(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBTDB
,
319 ISC_LOG_CRITICAL
, "%s", message
);
321 isc_log_write1(lctx
, DNS_LOGCATEGORY_GENERAL
, DNS_LOGMODULE_RBTDB
,
322 ISC_LOG_CRITICAL
, "%s", message
);
325 * Review where everything went.
329 if (system("head " TEST_FILE
"*; rm -f " TEST_FILE
"*") != 0) {
330 fprintf(stderr
, "system(\"head " TEST_FILE
"*; rm -f "
331 TEST_FILE
"*\") failed\n");
335 /* This is highly system specific. */
336 if (freopen(syslog_file
, "r", stdin
) == NULL
) {
337 fprintf(stderr
, "freopen(%s, \"r\", stdin) failed\n",
341 fprintf(stderr
, "\n==> %s <==\n", syslog_file
);
342 if (system("tail -2") != 0) {
343 fprintf(stderr
, "system(\"tail -2\") failed\n");
349 isc_log_destroy(&lctx
);
352 isc_mem_stats(mctx
, stderr
);