log.h 2.36 KB
Newer Older
1
/*  Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

    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/>.
 */

#pragma once

#include "contrib/sockaddr.h"
#include "knot/common/log.h"
Jan Včelák's avatar
Jan Včelák committed
21 22 23 24 25 26 27 28
#include "libknot/dname.h"

enum log_operation {
	LOG_OPERATION_AXFR,
	LOG_OPERATION_IXFR,
	LOG_OPERATION_NOTIFY,
	LOG_OPERATION_REFRESH,
	LOG_OPERATION_UPDATE,
29
	LOG_OPERATION_PARENT,
Jan Včelák's avatar
Jan Včelák committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
};

enum log_direction {
	LOG_DIRECTION_IN,
	LOG_DIRECTION_OUT,
};

static inline const char *log_operation_name(enum log_operation 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";
50 51
	case LOG_OPERATION_PARENT:
		return "parent DS check";
Jan Včelák's avatar
Jan Včelák committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	default:
		return "?";
	}
}

static inline const char *log_direction_name(enum log_direction direction)
{
	switch (direction) {
	case LOG_DIRECTION_IN:
		return "incoming";
	case LOG_DIRECTION_OUT:
		return "outgoing";
	default:
		return "?";
	}
}
68 69

/*!
Jan Včelák's avatar
Jan Včelák committed
70 71 72 73 74 75 76 77 78 79 80
 * \brief Generate log message for server communication.
 *
 * If this macro was a function:
 *
 * void ns_log(int priority, const knot_dname_t *zone, enum log_operation op,
 *             enum log_direction dir, const struct sockaddr *remote,
 *             const char *fmt, ...);
 *
 * Example output:
 *
 * [example.com] NOTIFY, outgoing, 2001:db8::1@53: serial 123
81 82
 *
 */
Jan Včelák's avatar
Jan Včelák committed
83 84 85 86
#define ns_log(priority, zone, op, dir, remote, fmt, ...) \
	do { \
		char address[SOCKADDR_STRLEN] = ""; \
		sockaddr_tostr(address, sizeof(address), remote); \
87
		log_fmt_zone(priority, LOG_SOURCE_ZONE, zone, "%s, %s, %s: " fmt, \
Jan Včelák's avatar
Jan Včelák committed
88 89
		             log_operation_name(op), log_direction_name(dir), address, \
		             ## __VA_ARGS__); \
90
	} while (0)