1/*	$NetBSD: named-journalprint.c,v 1.2.6.1 2012/06/05 21:15:48 bouyer Exp $	*/
2
3/*
4 * Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001  Internet Software Consortium.
6 *
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.
10 *
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.
18 */
19
20/* Id: named-journalprint.c,v 1.2 2009/12/04 21:59:23 marka Exp  */
21
22/*! \file */
23#include <config.h>
24
25#include <isc/log.h>
26#include <isc/mem.h>
27#include <isc/util.h>
28
29#include <dns/journal.h>
30#include <dns/log.h>
31#include <dns/result.h>
32#include <dns/types.h>
33
34#include <stdlib.h>
35
36/*
37 * Setup logging to use stderr.
38 */
39static isc_result_t
40setup_logging(isc_mem_t *mctx, FILE *errout, isc_log_t **logp) {
41	isc_logdestination_t destination;
42	isc_logconfig_t *logconfig = NULL;
43	isc_log_t *log = NULL;
44
45	RUNTIME_CHECK(isc_log_create(mctx, &log, &logconfig) == ISC_R_SUCCESS);
46	isc_log_setcontext(log);
47	dns_log_init(log);
48	dns_log_setcontext(log);
49
50	destination.file.stream = errout;
51	destination.file.name = NULL;
52	destination.file.versions = ISC_LOG_ROLLNEVER;
53	destination.file.maximum_size = 0;
54	RUNTIME_CHECK(isc_log_createchannel(logconfig, "stderr",
55					    ISC_LOG_TOFILEDESC,
56					    ISC_LOG_DYNAMIC,
57					    &destination, 0) == ISC_R_SUCCESS);
58	RUNTIME_CHECK(isc_log_usechannel(logconfig, "stderr",
59					 NULL, NULL) == ISC_R_SUCCESS);
60
61	*logp = log;
62	return (ISC_R_SUCCESS);
63}
64
65int
66main(int argc, char **argv) {
67	char *file;
68	isc_mem_t *mctx = NULL;
69	isc_result_t result;
70	isc_log_t *lctx = NULL;
71
72	if (argc != 2) {
73		printf("usage: %s journal\n", argv[0]);
74		return(1);
75	}
76
77	file = argv[1];
78
79	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
80	RUNTIME_CHECK(setup_logging(mctx, stderr, &lctx) == ISC_R_SUCCESS);
81
82	result = dns_journal_print(mctx, file, stdout);
83	if (result == DNS_R_NOJOURNAL)
84		fprintf(stderr, "%s\n", dns_result_totext(result));
85	isc_log_destroy(&lctx);
86	isc_mem_detach(&mctx);
87	return(result != ISC_R_SUCCESS ? 1 : 0);
88}
89