summaryrefslogtreecommitdiffstats
path: root/src/knot/nameserver/log.h
blob: fc79bd3738249c4fa313f506d4e61a70364bf963 (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
/*  Copyright (C) 2022 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 <https://www.gnu.org/licenses/>.
 */

#pragma once

#include "contrib/sockaddr.h"
#include "knot/common/log.h"
#include "libknot/dname.h"

typedef enum {
	LOG_OPERATION_AXFR,
	LOG_OPERATION_IXFR,
	LOG_OPERATION_NOTIFY,
	LOG_OPERATION_REFRESH,
	LOG_OPERATION_UPDATE,
	LOG_OPERATION_DS_CHECK,
	LOG_OPERATION_DS_PUSH,
} log_operation_t;

typedef enum {
	LOG_DIRECTION_NONE,
	LOG_DIRECTION_IN,
	LOG_DIRECTION_OUT,
} log_direction_t;

static inline const char *log_operation_name(log_operation_t operation)
{
	switch (operation) {
	case LOG_OPERATION_AXFR:
		return "AXFR";
	case LOG_OPERATION_IXFR:
		return "IXFR";
	case LOG_OPERATION_NOTIFY:
		return "notify";
	case LOG_OPERATION_REFRESH:
		return "refresh";
	case LOG_OPERATION_UPDATE:
		return "DDNS";
	case LOG_OPERATION_DS_CHECK:
		return "DS check";
	case LOG_OPERATION_DS_PUSH:
		return "DS push";
	default:
		return "?";
	}
}

static inline const char *log_direction_name(log_direction_t direction)
{
	switch (direction) {
	case LOG_DIRECTION_IN:
		return ", incoming";
	case LOG_DIRECTION_OUT:
		return ", outgoing";
	case LOG_DIRECTION_NONE:
	default:
		return "";
	}
}

/*!
 * \brief Generate log message for server communication.
 *
 * Example output:
 *
 * [example.com] NOTIFY, outgoing, remote 2001:db8::1@53, serial 123
 */
#define ns_log(priority, zone, op, dir, remote, pool, fmt, ...) \
	do { \
		char address[SOCKADDR_STRLEN] = ""; \
		sockaddr_tostr(address, sizeof(address), (const struct sockaddr_storage *)remote); \
		log_fmt_zone(priority, LOG_SOURCE_ZONE, zone, NULL, "%s%s, remote %s%s, " fmt, \
		             log_operation_name(op), log_direction_name(dir), address, \
		             (pool) ? " pool" : "", ## __VA_ARGS__); \
	} while (0)