]>
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 struct xrefdata_logmsg
{
58 struct xrefdata xrefdata
;
60 /* nothing more here right now */
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.
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__)
75 static inline void zlog(int prio
, const char *fmt
, ...)
85 static inline void zlog_ref(const struct xref_logmsg
*xref
,
91 vzlogx(xref
, xref
->priority
, fmt
, ap
);
95 #define _zlog_ecref(ec_, prio, msg, ...) \
97 static struct xrefdata _xrefdata = { \
101 .hashu32 = {(prio), (ec_)}, \
103 static const struct xref_logmsg _xref __attribute__( \
105 .xref = XREF_INIT(XREFT_LOGMSG, &_xrefdata, __func__), \
106 .fmtstring = (msg), \
107 .priority = (prio), \
109 .args = (#__VA_ARGS__), \
111 XREF_LINK(_xref.xref); \
112 zlog_ref(&_xref, (msg), ##__VA_ARGS__); \
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__)
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__)
126 #define flog_err_sys(ferr_id, format, ...) \
127 _zlog_ecref(ferr_id, LOG_ERR, format, ## __VA_ARGS__)
129 extern void zlog_sigsafe(const char *text
, size_t len
);
131 /* extra priority value to disable a target without deleting it */
132 #define ZLOG_DISABLED (LOG_EMERG-1)
134 /* zlog_msg encapsulates a particular logging call from somewhere in the code.
135 * The same struct is passed around to all zlog_targets.
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.
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
148 extern int zlog_msg_prio(struct zlog_msg
*msg
);
149 extern const struct xref_logmsg
*zlog_msg_xref(struct zlog_msg
*msg
);
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)
155 * calling this with NULL textlen is likely wrong.
156 * use "%.*s", (int)textlen, text when passing to printf-like functions
158 extern const char *zlog_msg_text(struct zlog_msg
*msg
, size_t *textlen
);
160 extern void zlog_msg_args(struct zlog_msg
*msg
, size_t *hdrlen
,
161 size_t *n_argpos
, const struct fmt_outpos
**argpos
);
163 /* timestamp formatting control flags */
165 /* sub-second digit count */
166 #define ZLOG_TS_PREC 0xfU
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!)
172 #define ZLOG_TS_ISO8601 (1 << 8)
173 #define ZLOG_TS_LEGACY (1 << 9)
175 /* default is local time zone */
176 #define ZLOG_TS_UTC (1 << 10)
178 extern size_t zlog_msg_ts(struct zlog_msg
*msg
, struct fbuf
*out
,
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
,
185 /* currently just returns the current PID/TID since we never write another
188 extern void zlog_msg_pid(struct zlog_msg
*msg
, intmax_t *pid
, intmax_t *tid
);
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.
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
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").
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.
210 PREDECL_ATOMLIST(zlog_targets
);
212 struct zlog_targets_item head
;
216 void (*logfn
)(struct zlog_target
*zt
, struct zlog_msg
*msg
[],
219 /* for crash handlers, set to NULL if log target can't write crash logs
220 * without possibly deadlocking (AS-Safe)
222 * text is not \0 terminated & split up into lines (e.g. no \n)
224 void (*logfn_sigsafe
)(struct zlog_target
*zt
, const char *text
,
227 struct rcu_head rcu_head
;
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
,
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
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.)
243 extern struct zlog_target
*zlog_target_replace(struct zlog_target
*oldzt
,
244 struct zlog_target
*newzt
);
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)
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
),
254 (progname
, protoname
, instance
, uid
, gid
));
256 extern void zlog_fini(void);
257 DECLARE_KOOH(zlog_fini
, (), ());
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);
264 /* for tools & test programs, i.e. anything not a daemon.
265 * (no cleanup needed at exit)
267 extern void zlog_aux_init(const char *prefix
, int prio_min
);
268 DECLARE_HOOK(zlog_aux_init
, (const char *prefix
, int prio_min
),
271 extern void zlog_startup_end(void);
273 extern void zlog_tls_buffer_init(void);
274 extern void zlog_tls_buffer_flush(void);
275 extern void zlog_tls_buffer_fini(void);
277 /* Enable or disable 'immediate' output - default is to buffer messages. */
278 extern void zlog_set_immediate(bool set_p
);
280 extern const char *zlog_priority_str(int priority
);
286 #endif /* _FRR_ZLOG_H */