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