]> git.proxmox.com Git - mirror_frr.git/blame - lib/zlog.h
*: make sure `config.h` or `zebra.h` is first
[mirror_frr.git] / lib / zlog.h
CommitLineData
0bdeb5e5
DL
1/*
2 * Copyright (c) 2015-19 David Lamparter, for NetDEF, Inc.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#ifndef _FRR_ZLOG_H
18#define _FRR_ZLOG_H
19
20#include <stdarg.h>
21#include <stdbool.h>
22#include <stdint.h>
23#include <string.h>
24#include <syslog.h>
25#include <unistd.h>
26#include <sys/uio.h>
27
28#include "atomlist.h"
29#include "frrcu.h"
30#include "memory.h"
31#include "hook.h"
32
17e38209
RW
33#ifdef __cplusplus
34extern "C" {
35#endif
36
0bdeb5e5
DL
37extern char zlog_prefix[];
38extern size_t zlog_prefixsz;
39extern int zlog_tmpdirfd;
40
131879fb
DL
41struct xref_logmsg {
42 struct xref xref;
43
44 const char *fmtstring;
45 uint32_t priority;
46 uint32_t ec;
2621bb8b 47 const char *args;
131879fb
DL
48};
49
50struct xrefdata_logmsg {
51 struct xrefdata xrefdata;
52
53 /* nothing more here right now */
54};
55
0bdeb5e5
DL
56/* These functions are set up to write to stdout/stderr without explicit
57 * initialization and/or before config load. There is no need to call e.g.
58 * fprintf(stderr, ...) just because it's "too early" at startup. Depending
59 * on context, it may still be the right thing to use fprintf though -- try to
60 * determine wether something is a log message or something else.
61 */
62
131879fb
DL
63extern void vzlogx(const struct xref_logmsg *xref, int prio,
64 const char *fmt, va_list ap);
65#define vzlog(prio, ...) vzlogx(NULL, prio, __VA_ARGS__)
0bdeb5e5 66
c2527ed2 67PRINTFRR(2, 3)
0bdeb5e5
DL
68static inline void zlog(int prio, const char *fmt, ...)
69{
70 va_list ap;
71
72 va_start(ap, fmt);
73 vzlog(prio, fmt, ap);
74 va_end(ap);
75}
76
131879fb
DL
77PRINTFRR(2, 3)
78static inline void zlog_ref(const struct xref_logmsg *xref,
79 const char *fmt, ...)
80{
81 va_list ap;
82
83 va_start(ap, fmt);
84 vzlogx(xref, xref->priority, fmt, ap);
85 va_end(ap);
86}
87
c364a096
EDP
88#define _zlog_ecref(ec_, prio, msg, ...) \
89 do { \
131879fb 90 static struct xrefdata _xrefdata = { \
c364a096
EDP
91 .xref = NULL, \
92 .uid = {}, \
131879fb 93 .hashstr = (msg), \
c364a096 94 .hashu32 = {(prio), (ec_)}, \
131879fb 95 }; \
c364a096
EDP
96 static const struct xref_logmsg _xref __attribute__( \
97 (used)) = { \
131879fb
DL
98 .xref = XREF_INIT(XREFT_LOGMSG, &_xrefdata, __func__), \
99 .fmtstring = (msg), \
100 .priority = (prio), \
101 .ec = (ec_), \
2621bb8b 102 .args = (#__VA_ARGS__), \
131879fb
DL
103 }; \
104 XREF_LINK(_xref.xref); \
a3c67498 105 zlog_ref(&_xref, (msg), ##__VA_ARGS__); \
131879fb
DL
106 } while (0)
107
a3c67498
DL
108#define zlog_err(...) _zlog_ecref(0, LOG_ERR, __VA_ARGS__)
109#define zlog_warn(...) _zlog_ecref(0, LOG_WARNING, __VA_ARGS__)
110#define zlog_info(...) _zlog_ecref(0, LOG_INFO, __VA_ARGS__)
111#define zlog_notice(...) _zlog_ecref(0, LOG_NOTICE, __VA_ARGS__)
112#define zlog_debug(...) _zlog_ecref(0, LOG_DEBUG, __VA_ARGS__)
113
131879fb
DL
114#define flog_err(ferr_id, format, ...) \
115 _zlog_ecref(ferr_id, LOG_ERR, format, ## __VA_ARGS__)
116#define flog_warn(ferr_id, format, ...) \
117 _zlog_ecref(ferr_id, LOG_WARNING, format, ## __VA_ARGS__)
118
119#define flog_err_sys(ferr_id, format, ...) \
a3c67498 120 _zlog_ecref(ferr_id, LOG_ERR, format, ## __VA_ARGS__)
0bdeb5e5
DL
121
122extern void zlog_sigsafe(const char *text, size_t len);
123
124/* extra priority value to disable a target without deleting it */
125#define ZLOG_DISABLED (LOG_EMERG-1)
126
127/* zlog_msg encapsulates a particular logging call from somewhere in the code.
128 * The same struct is passed around to all zlog_targets.
129 *
130 * This is used to defer formatting the log message until it is actually
131 * requested by one of the targets. If none of the targets needs the message
132 * formatted, the formatting call is avoided entirely.
133 *
134 * This struct is opaque / private to the core zlog code. Logging targets
135 * should use zlog_msg_* functions to get text / timestamps / ... for a
136 * message.
137 */
138
139struct zlog_msg;
140
141extern int zlog_msg_prio(struct zlog_msg *msg);
131879fb 142extern const struct xref_logmsg *zlog_msg_xref(struct zlog_msg *msg);
0bdeb5e5
DL
143
144/* pass NULL as textlen if you don't need it. */
145extern const char *zlog_msg_text(struct zlog_msg *msg, size_t *textlen);
146
147/* timestamp formatting control flags */
148
149/* sub-second digit count */
150#define ZLOG_TS_PREC 0xfU
151
152/* 8601: 0000-00-00T00:00:00Z (if used with ZLOG_TS_UTC)
153 * 0000-00-00T00:00:00+00:00 (otherwise)
154 * Legacy: 0000/00/00 00:00:00 (no TZ indicated!)
155 */
156#define ZLOG_TS_ISO8601 (1 << 8)
157#define ZLOG_TS_LEGACY (1 << 9)
158
159/* default is local time zone */
160#define ZLOG_TS_UTC (1 << 10)
161
162extern size_t zlog_msg_ts(struct zlog_msg *msg, char *out, size_t outsz,
163 uint32_t flags);
164
165/* This list & struct implements the actual logging targets. It is accessed
166 * lock-free from all threads, and thus MUST only be changed atomically, i.e.
167 * RCU.
168 *
169 * Since there's no atomic replace, the replacement action is an add followed
170 * by a delete. This means that during logging config changes, log messages
171 * may be duplicated in the log target that is being changed. The old entry
172 * being changed MUST also at the very least not crash or do other stupid
173 * things.
174 *
175 * This list and struct are NOT related to config. Logging config is kept
176 * separately, and results in creating appropriate zlog_target(s) to realize
177 * the config. Log targets may also be created from varying sources, e.g.
178 * command line options, or VTY commands ("log monitor").
179 *
180 * struct zlog_target is intended to be embedded into a larger structure that
181 * contains additional field for the specific logging target, e.g. an fd or
182 * additional options. It MUST be the first field in that larger struct.
183 */
184
960b9a53 185PREDECL_ATOMLIST(zlog_targets);
0bdeb5e5
DL
186struct zlog_target {
187 struct zlog_targets_item head;
188
189 int prio_min;
190
191 void (*logfn)(struct zlog_target *zt, struct zlog_msg *msg[],
192 size_t nmsgs);
193
194 /* for crash handlers, set to NULL if log target can't write crash logs
195 * without possibly deadlocking (AS-Safe)
196 *
197 * text is not \0 terminated & split up into lines (e.g. no \n)
198 */
199 void (*logfn_sigsafe)(struct zlog_target *zt, const char *text,
200 size_t len);
201
202 struct rcu_head rcu_head;
203};
204
205/* make a copy for RCUpdating. oldzt may be NULL to allocate a fresh one. */
206extern struct zlog_target *zlog_target_clone(struct memtype *mt,
207 struct zlog_target *oldzt,
208 size_t size);
209
210/* update the zlog_targets list; both oldzt and newzt may be NULL. You
211 * still need to zlog_target_free() the old target afterwards if it wasn't
212 * NULL.
213 *
214 * Returns oldzt so you can zlog_target_free(zlog_target_replace(old, new));
215 * (Some log targets may need extra cleanup inbetween, but remember the old
216 * target MUST remain functional until the end of the current RCU cycle.)
217 */
218extern struct zlog_target *zlog_target_replace(struct zlog_target *oldzt,
219 struct zlog_target *newzt);
220
221/* Mostly for symmetry for zlog_target_clone(), just rcu_free() internally. */
222#define zlog_target_free(mt, zt) \
223 rcu_free(mt, zt, rcu_head)
224
225extern void zlog_init(const char *progname, const char *protoname,
226 unsigned short instance, uid_t uid, gid_t gid);
227DECLARE_HOOK(zlog_init, (const char *progname, const char *protoname,
228 unsigned short instance, uid_t uid, gid_t gid),
8451921b 229 (progname, protoname, instance, uid, gid));
0bdeb5e5
DL
230
231extern void zlog_fini(void);
8451921b 232DECLARE_KOOH(zlog_fini, (), ());
0bdeb5e5 233
a3c67498
DL
234extern void zlog_set_prefix_ec(bool enable);
235extern bool zlog_get_prefix_ec(void);
236extern void zlog_set_prefix_xid(bool enable);
237extern bool zlog_get_prefix_xid(void);
238
0bdeb5e5
DL
239/* for tools & test programs, i.e. anything not a daemon.
240 * (no cleanup needed at exit)
241 */
242extern void zlog_aux_init(const char *prefix, int prio_min);
243DECLARE_HOOK(zlog_aux_init, (const char *prefix, int prio_min),
8451921b 244 (prefix, prio_min));
0bdeb5e5
DL
245
246extern void zlog_startup_end(void);
247
248extern void zlog_tls_buffer_init(void);
249extern void zlog_tls_buffer_flush(void);
250extern void zlog_tls_buffer_fini(void);
251
17e38209
RW
252#ifdef __cplusplus
253}
254#endif
255
0bdeb5e5 256#endif /* _FRR_ZLOG_H */