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