]>
git.proxmox.com Git - mirror_frr.git/blob - lib/zlog.h
2 * Copyright (c) 2015-19 David Lamparter, for NetDEF, Inc.
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.
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.
42 extern char zlog_prefix
[];
43 extern size_t zlog_prefixsz
;
44 extern int zlog_tmpdirfd
;
45 extern int zlog_instance
;
46 extern const char *zlog_progname
;
51 const char *fmtstring
;
57 /* whether flag was added in config mode or enable mode */
58 #define LOGMSG_FLAG_EPHEMERAL (1 << 0)
59 #define LOGMSG_FLAG_PERSISTENT (1 << 1)
61 struct xrefdata_logmsg
{
62 struct xrefdata xrefdata
;
67 /* These functions are set up to write to stdout/stderr without explicit
68 * initialization and/or before config load. There is no need to call e.g.
69 * fprintf(stderr, ...) just because it's "too early" at startup. Depending
70 * on context, it may still be the right thing to use fprintf though -- try to
71 * determine wether something is a log message or something else.
74 extern void vzlogx(const struct xref_logmsg
*xref
, int prio
,
75 const char *fmt
, va_list ap
);
76 #define vzlog(prio, ...) vzlogx(NULL, prio, __VA_ARGS__)
79 static inline void zlog(int prio
, const char *fmt
, ...)
89 static inline void zlog_ref(const struct xref_logmsg
*xref
,
95 vzlogx(xref
, xref
->priority
, fmt
, ap
);
99 #define _zlog_ecref(ec_, prio, msg, ...) \
101 static struct xrefdata_logmsg _xrefdata = { \
107 .hashu32 = {(prio), (ec_)}, \
110 static const struct xref_logmsg _xref __attribute__( \
112 .xref = XREF_INIT(XREFT_LOGMSG, &_xrefdata.xrefdata, \
114 .fmtstring = (msg), \
115 .priority = (prio), \
117 .args = (#__VA_ARGS__), \
119 XREF_LINK(_xref.xref); \
120 zlog_ref(&_xref, (msg), ##__VA_ARGS__); \
123 #define zlog_err(...) _zlog_ecref(0, LOG_ERR, __VA_ARGS__)
124 #define zlog_warn(...) _zlog_ecref(0, LOG_WARNING, __VA_ARGS__)
125 #define zlog_info(...) _zlog_ecref(0, LOG_INFO, __VA_ARGS__)
126 #define zlog_notice(...) _zlog_ecref(0, LOG_NOTICE, __VA_ARGS__)
127 #define zlog_debug(...) _zlog_ecref(0, LOG_DEBUG, __VA_ARGS__)
129 #define flog_err(ferr_id, format, ...) \
130 _zlog_ecref(ferr_id, LOG_ERR, format, ## __VA_ARGS__)
131 #define flog_warn(ferr_id, format, ...) \
132 _zlog_ecref(ferr_id, LOG_WARNING, format, ## __VA_ARGS__)
134 #define flog_err_sys(ferr_id, format, ...) \
135 _zlog_ecref(ferr_id, LOG_ERR, format, ## __VA_ARGS__)
137 extern void zlog_sigsafe(const char *text
, size_t len
);
139 /* extra priority value to disable a target without deleting it */
140 #define ZLOG_DISABLED (LOG_EMERG-1)
142 /* zlog_msg encapsulates a particular logging call from somewhere in the code.
143 * The same struct is passed around to all zlog_targets.
145 * This is used to defer formatting the log message until it is actually
146 * requested by one of the targets. If none of the targets needs the message
147 * formatted, the formatting call is avoided entirely.
149 * This struct is opaque / private to the core zlog code. Logging targets
150 * should use zlog_msg_* functions to get text / timestamps / ... for a
156 extern int zlog_msg_prio(struct zlog_msg
*msg
);
157 extern const struct xref_logmsg
*zlog_msg_xref(struct zlog_msg
*msg
);
159 /* text is NOT \0 terminated; instead there is a \n after textlen since the
160 * logging targets would jump extra hoops otherwise for a single byte. (the
161 * \n is not included in textlen)
163 * calling this with NULL textlen is likely wrong.
164 * use "%.*s", (int)textlen, text when passing to printf-like functions
166 extern const char *zlog_msg_text(struct zlog_msg
*msg
, size_t *textlen
);
168 extern void zlog_msg_args(struct zlog_msg
*msg
, size_t *hdrlen
,
169 size_t *n_argpos
, const struct fmt_outpos
**argpos
);
171 /* timestamp formatting control flags */
173 /* sub-second digit count */
174 #define ZLOG_TS_PREC 0xfU
176 /* 8601: 0000-00-00T00:00:00Z (if used with ZLOG_TS_UTC)
177 * 0000-00-00T00:00:00+00:00 (otherwise)
178 * Legacy: 0000/00/00 00:00:00 (no TZ indicated!)
180 #define ZLOG_TS_ISO8601 (1 << 8)
181 #define ZLOG_TS_LEGACY (1 << 9)
183 /* default is local time zone */
184 #define ZLOG_TS_UTC (1 << 10)
186 extern size_t zlog_msg_ts(struct zlog_msg
*msg
, struct fbuf
*out
,
189 /* "mmm dd hh:mm:ss" for RFC3164 syslog. Only ZLOG_TS_UTC for flags. */
190 extern size_t zlog_msg_ts_3164(struct zlog_msg
*msg
, struct fbuf
*out
,
193 /* currently just returns the current PID/TID since we never write another
196 extern void zlog_msg_pid(struct zlog_msg
*msg
, intmax_t *pid
, intmax_t *tid
);
198 /* This list & struct implements the actual logging targets. It is accessed
199 * lock-free from all threads, and thus MUST only be changed atomically, i.e.
202 * Since there's no atomic replace, the replacement action is an add followed
203 * by a delete. This means that during logging config changes, log messages
204 * may be duplicated in the log target that is being changed. The old entry
205 * being changed MUST also at the very least not crash or do other stupid
208 * This list and struct are NOT related to config. Logging config is kept
209 * separately, and results in creating appropriate zlog_target(s) to realize
210 * the config. Log targets may also be created from varying sources, e.g.
211 * command line options, or VTY commands ("log monitor").
213 * struct zlog_target is intended to be embedded into a larger structure that
214 * contains additional field for the specific logging target, e.g. an fd or
215 * additional options. It MUST be the first field in that larger struct.
218 PREDECL_ATOMLIST(zlog_targets
);
220 struct zlog_targets_item head
;
224 void (*logfn
)(struct zlog_target
*zt
, struct zlog_msg
*msg
[],
227 /* for crash handlers, set to NULL if log target can't write crash logs
228 * without possibly deadlocking (AS-Safe)
230 * text is not \0 terminated & split up into lines (e.g. no \n)
232 void (*logfn_sigsafe
)(struct zlog_target
*zt
, const char *text
,
235 struct rcu_head rcu_head
;
238 /* make a copy for RCUpdating. oldzt may be NULL to allocate a fresh one. */
239 extern struct zlog_target
*zlog_target_clone(struct memtype
*mt
,
240 struct zlog_target
*oldzt
,
243 /* update the zlog_targets list; both oldzt and newzt may be NULL. You
244 * still need to zlog_target_free() the old target afterwards if it wasn't
247 * Returns oldzt so you can zlog_target_free(zlog_target_replace(old, new));
248 * (Some log targets may need extra cleanup inbetween, but remember the old
249 * target MUST remain functional until the end of the current RCU cycle.)
251 extern struct zlog_target
*zlog_target_replace(struct zlog_target
*oldzt
,
252 struct zlog_target
*newzt
);
254 /* Mostly for symmetry for zlog_target_clone(), just rcu_free() internally. */
255 #define zlog_target_free(mt, zt) \
256 rcu_free(mt, zt, rcu_head)
258 extern void zlog_init(const char *progname
, const char *protoname
,
259 unsigned short instance
, uid_t uid
, gid_t gid
);
260 DECLARE_HOOK(zlog_init
, (const char *progname
, const char *protoname
,
261 unsigned short instance
, uid_t uid
, gid_t gid
),
262 (progname
, protoname
, instance
, uid
, gid
));
264 extern void zlog_fini(void);
265 DECLARE_KOOH(zlog_fini
, (), ());
267 extern void zlog_set_prefix_ec(bool enable
);
268 extern bool zlog_get_prefix_ec(void);
269 extern void zlog_set_prefix_xid(bool enable
);
270 extern bool zlog_get_prefix_xid(void);
272 /* for tools & test programs, i.e. anything not a daemon.
273 * (no cleanup needed at exit)
275 extern void zlog_aux_init(const char *prefix
, int prio_min
);
276 DECLARE_HOOK(zlog_aux_init
, (const char *prefix
, int prio_min
),
279 extern void zlog_startup_end(void);
281 extern void zlog_tls_buffer_init(void);
282 extern void zlog_tls_buffer_flush(void);
283 extern void zlog_tls_buffer_fini(void);
285 /* Enable or disable 'immediate' output - default is to buffer messages. */
286 extern void zlog_set_immediate(bool set_p
);
288 extern const char *zlog_priority_str(int priority
);
294 #endif /* _FRR_ZLOG_H */