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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/* Copyright (C) 2014-2021 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#pragma once
#include <stdarg.h>
#include <syslog.h>
#include "lib/defines.h"
#define LOG_DEFAULT_LEVEL LOG_NOTICE
#define LOG_GNUTLS_LEVEL 5
/* Targets */
typedef enum {
LOG_TARGET_SYSLOG = 0,
LOG_TARGET_STDERR = 1,
LOG_TARGET_STDOUT = 2,
} log_target_t;
/* Groups */
/* Don't forget add *_TAG below, log_group_names[] item (log.c) and generate
* new kres-gen.lua */
enum kr_log_group {
LOG_GRP_SYSTEM = 1, /* Must be first in enum. */
LOG_GRP_CACHE,
LOG_GRP_IO,
LOG_GRP_NETWORK,
LOG_GRP_TA,
LOG_GRP_TLS,
LOG_GRP_GNUTLS,
LOG_GRP_TLSCLIENT,
LOG_GRP_XDP,
LOG_GRP_ZIMPORT,
LOG_GRP_ZSCANNER,
LOG_GRP_DOH,
LOG_GRP_DNSSEC,
LOG_GRP_HINT,
LOG_GRP_PLAN,
LOG_GRP_ITERATOR,
LOG_GRP_VALIDATOR,
LOG_GRP_RESOLVER,
LOG_GRP_SELECTION,
LOG_GRP_ZCUT,
LOG_GRP_COOKIES,
LOG_GRP_STATISTICS,
LOG_GRP_REBIND,
LOG_GRP_WORKER,
LOG_GRP_POLICY,
LOG_GRP_TASENTINEL,
LOG_GRP_TASIGNALING,
LOG_GRP_TAUPDATE,
LOG_GRP_DAF,
LOG_GRP_DETECTTIMEJUMP,
LOG_GRP_DETECTTIMESKEW,
LOG_GRP_GRAPHITE,
LOG_GRP_PREFILL,
LOG_GRP_PRIMING,
LOG_GRP_SRVSTALE,
LOG_GRP_WATCHDOG,
LOG_GRP_NSID,
LOG_GRP_DNSTAP,
LOG_GRP_TESTS,
LOG_GRP_DOTAUTH,
LOG_GRP_HTTP,
LOG_GRP_CONTROL,
/* ^^ Add new log groups above ^^. */
LOG_GRP_DEVEL, /* Must be last entry in enum! */
};
typedef struct {
const char *g_name;
enum kr_log_group g_val;
} log_group_names_t;
#define LOG_GRP_SYSTEM_TAG "system"
#define LOG_GRP_CACHE_TAG "cache"
#define LOG_GRP_IO_TAG "io"
#define LOG_GRP_NETWORK_TAG "net"
#define LOG_GRP_TA_TAG "ta"
#define LOG_GRP_TLS_TAG "tls"
#define LOG_GRP_GNUTLS_TAG "gnutls"
#define LOG_GRP_TLSCLIENT_TAG "tls_cl"
#define LOG_GRP_XDP_TAG "xdp"
#define LOG_GRP_ZIMPORT_TAG "zimprt"
#define LOG_GRP_ZSCANNER_TAG "zscann"
#define LOG_GRP_DOH_TAG "doh"
#define LOG_GRP_DNSSEC_TAG "dnssec"
#define LOG_GRP_HINT_TAG "hint"
#define LOG_GRP_PLAN_TAG "plan"
#define LOG_GRP_ITERATOR_TAG "iterat"
#define LOG_GRP_VALIDATOR_TAG "valdtr"
#define LOG_GRP_RESOLVER_TAG "resolv"
#define LOG_GRP_SELECTION_TAG "select"
#define LOG_GRP_ZCUT_TAG "zoncut"
#define LOG_GRP_COOKIES_TAG "cookie"
#define LOG_GRP_STATISTICS_TAG "statis"
#define LOG_GRP_REBIND_TAG "rebind"
#define LOG_GRP_WORKER_TAG "worker"
#define LOG_GRP_POLICY_TAG "policy"
#define LOG_GRP_TASENTINEL_TAG "tasent"
#define LOG_GRP_TASIGNALING_TAG "tasign"
#define LOG_GRP_TAUPDATE_TAG "taupd"
#define LOG_GRP_DAF_TAG "daf"
#define LOG_GRP_DETECTTIMEJUMP_TAG "timejm"
#define LOG_GRP_DETECTTIMESKEW_TAG "timesk"
#define LOG_GRP_GRAPHITE_TAG "graphi"
#define LOG_GRP_PREFILL_TAG "prefil"
#define LOG_GRP_PRIMING_TAG "primin"
#define LOG_GRP_SRVSTALE_TAG "srvstl"
#define LOG_GRP_WATCHDOG_TAG "wtchdg"
#define LOG_GRP_NSID_TAG "nsid"
#define LOG_GRP_DNSTAP_TAG "dnstap"
#define LOG_GRP_TESTS_TAG "tests"
#define LOG_GRP_DOTAUTH_TAG "dotaut"
#define LOG_GRP_HTTP_TAG "http"
#define LOG_GRP_CONTROL_TAG "contrl"
#define LOG_GRP_DEVEL_TAG "devel"
KR_EXPORT
bool kr_log_group_is_set(enum kr_log_group group);
KR_EXPORT
void kr_log_add_group(enum kr_log_group group);
KR_EXPORT
void kr_log_del_group(enum kr_log_group group);
KR_EXPORT
const char *kr_log_grp2name(enum kr_log_group group);
KR_EXPORT
enum kr_log_group kr_log_name2grp(const char *name);
/* Log */
typedef int log_level_t;
KR_EXPORT
extern log_level_t kr_log_level;
KR_EXPORT
extern log_target_t kr_log_target;
KR_EXPORT KR_PRINTF(6)
void kr_log_fmt(enum kr_log_group group, log_level_t level, const char *file, const char *line,
const char *func, const char *fmt, ...);
KR_EXPORT
int kr_log_level_set(log_level_t level);
KR_EXPORT
log_level_t kr_log_level_get(void);
KR_EXPORT
void kr_log_init(log_level_t level, log_target_t target);
#define TO_STR_A(x) #x
#define TO_STR(x) TO_STR_A(x)
#define SD_JOURNAL_METADATA "CODE_FILE=" __FILE__, "CODE_LINE=" TO_STR(__LINE__), ""
#define kr_log_debug(grp, fmt, ...) \
kr_log_fmt(LOG_GRP_ ## grp, LOG_DEBUG, SD_JOURNAL_METADATA, \
"[%-6s] " fmt, LOG_GRP_ ## grp ## _TAG, ## __VA_ARGS__)
#define kr_log_info(grp, fmt, ...) \
kr_log_fmt(LOG_GRP_ ## grp, LOG_INFO, SD_JOURNAL_METADATA, \
"[%-6s] " fmt, LOG_GRP_ ## grp ## _TAG, ## __VA_ARGS__)
#define kr_log_notice(grp, fmt, ...) \
kr_log_fmt(LOG_GRP_ ## grp, LOG_NOTICE, SD_JOURNAL_METADATA, \
"[%-6s] " fmt, LOG_GRP_ ## grp ## _TAG, ## __VA_ARGS__)
#define kr_log_warning(grp, fmt, ...) \
kr_log_fmt(LOG_GRP_ ## grp, LOG_WARNING, SD_JOURNAL_METADATA, \
"[%-6s] " fmt, LOG_GRP_ ## grp ## _TAG, ## __VA_ARGS__)
#define kr_log_error(grp, fmt, ...) \
kr_log_fmt(LOG_GRP_ ## grp, LOG_ERR, SD_JOURNAL_METADATA, \
"[%-6s] " fmt, LOG_GRP_ ## grp ## _TAG, ## __VA_ARGS__)
#define kr_log_fatal(grp, fmt, ...) \
kr_log_fmt(LOG_GRP_ ## grp, LOG_CRIT, SD_JOURNAL_METADATA, \
"[%-6s] " fmt, LOG_GRP_ ## grp ## _TAG, ## __VA_ARGS__)
#define kr_log_deprecate(grp, fmt, ...) \
kr_log_fmt(LOG_GRP_ ## grp, LOG_WARNING,SD_JOURNAL_METADATA, \
"[%-6s] deprecation WARNING: " fmt, LOG_GRP_ ## grp ## _TAG, ## __VA_ARGS__)
#define KR_LOG_LEVEL_IS(exp) ((kr_log_level >= (exp)) ? true : false)
/* Syslog */
KR_EXPORT
char *kr_log_level2name(log_level_t level);
KR_EXPORT
log_level_t kr_log_name2level(const char *name);
#ifndef SYSLOG_NAMES
typedef struct _code {
char *c_name;
int c_val;
} syslog_code_t;
KR_EXPORT
extern syslog_code_t prioritynames[];
#endif
/* Misc. */
struct kr_request;
struct kr_query;
/**
* Log a message through the request log handler or stdout.
* Caller is responsible for detecting verbose mode, use QRVERBOSE() macro.
* @param qry_uid query ID to append to request ID, 0 means "no query"
* @param indent level of indentation between [req.qry][source] and message
* @param source message source
* @param fmt message format
*/
#define kr_log_req(req, qry_id, indent, grp, fmt, ...) \
kr_log_req1(req, qry_id, indent, LOG_GRP_ ## grp, LOG_GRP_ ## grp ## _TAG, fmt, ## __VA_ARGS__)
KR_EXPORT KR_PRINTF(6)
void kr_log_req1(const struct kr_request * const req, uint32_t qry_uid,
const unsigned int indent, enum kr_log_group group, const char *tag, const char *fmt, ...);
/**
* Log a message through the request log handler or stdout.
* Caller is responsible for detecting verbose mode, use QRVERBOSE() macro.
* @param qry current query
* @param source message source
* @param fmt message format
*/
#define kr_log_q(qry, grp, fmt, ...) kr_log_q1(qry, LOG_GRP_ ## grp, LOG_GRP_ ## grp ## _TAG, fmt, ## __VA_ARGS__)
KR_EXPORT KR_PRINTF(4)
void kr_log_q1(const struct kr_query *qry, enum kr_log_group group, const char *tag, const char *fmt, ...);
/** Return whether a particular log group in a request is in debug/verbose mode.
*
* Typically you use this as condition to compute some data to be logged,
* in case that's considered too expensive to do unless it really gets logged.
*
* The request can be NULL, and there's a _qry() shortand to specify query instead.
*/
#define kr_log_is_debug(grp, req) \
__builtin_expect(kr_log_is_debug_fun(LOG_GRP_ ## grp, (req)), false)
#define kr_log_is_debug_qry(grp, qry) kr_log_is_debug(grp, (qry) ? (qry)->request : NULL)
KR_EXPORT
bool kr_log_is_debug_fun(enum kr_log_group group, const struct kr_request *req);
|