summaryrefslogtreecommitdiff
path: root/src/knot/conf/logconf.c
blob: 427e76272bf67d96a0cc930e1be5371ec7a9f933 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include "knot/other/debug.h"
#include "knot/conf/logconf.h"
#include "knot/conf/conf.h"
#include "common/log.h"
#include "common/lists.h"
#include "knot/knot.h"

int log_conf_hook(const struct conf_t *conf, void *data)
{
	// Data not used
	int ret = 0;
	UNUSED(data);

	// Check if log declaration exists, otherwise ignore
	if (conf->logs_count < 1) {
		return KNOT_EINVAL;
	}

	// Find maximum log facility id
	node *n = 0; size_t files = 0;
	WALK_LIST(n, conf->logs) {
		conf_log_t* log = (conf_log_t*)n;
		if (log->type == LOGT_FILE) {
			++files;
		}
	}

	// Initialize logsystem
	log_truncate();
	if ((ret = log_setup(files)) < 0) {
		return ret;
	}

	// Setup logs
	int loaded_sections = 0;
	n = 0;
	WALK_LIST(n, conf->logs) {

		// Calculate offset
		conf_log_t* log = (conf_log_t*)n;
		int facility = log->type;
		if (facility == LOGT_FILE) {
			facility = log_open_file(log->file);
			if (facility < 0) {
				log_server_error("Failed to open "
				                 "logfile '%s'.\n", log->file);
				continue;
			}
		}

		// Auto-assign fatal errors to syslog or stderr
		if (facility <= LOGT_STDERR) {
			int mask = LOG_MASK(LOG_FATAL);
			log_levels_add(facility, LOG_ANY, mask);
			loaded_sections |= 1 << facility;
		}

		// Setup sources mapping
		node *m = 0;
		WALK_LIST(m, log->map) {

			// Assign mapped level
			conf_log_map_t *map = (conf_log_map_t*)m;
			log_levels_add(facility, map->source, map->prios);
		}
	}

	// Load defaults for syslog or stderr
	int bmask = LOG_MASK(LOG_ERR)|LOG_MASK(LOG_FATAL);
	if (!(loaded_sections & (1 << LOGT_SYSLOG))) {
		log_levels_set(LOGT_SYSLOG, LOG_ANY, bmask);
	}
	if (!(loaded_sections & (1 << LOGT_STDERR))) {
		log_levels_set(LOGT_STDERR, LOG_ANY, bmask);
	}

	return KNOT_EOK;
}