]>
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 whether something is a log message or something else.
74 extern void vzlogx(const struct xref_logmsg
*xref
, int prio
, const char *fmt
,
75 va_list ap
) PRINTFRR(3, 0);
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)
188 extern size_t zlog_msg_ts(struct zlog_msg
*msg
, struct fbuf
*out
,
190 extern void zlog_msg_tsraw(struct zlog_msg
*msg
, struct timespec
*ts
);
192 /* "mmm dd hh:mm:ss" for RFC3164 syslog. Only ZLOG_TS_UTC for flags. */
193 extern size_t zlog_msg_ts_3164(struct zlog_msg
*msg
, struct fbuf
*out
,
196 /* currently just returns the current PID/TID since we never write another
199 extern void zlog_msg_pid(struct zlog_msg
*msg
, intmax_t *pid
, intmax_t *tid
);
201 /* This list & struct implements the actual logging targets. It is accessed
202 * lock-free from all threads, and thus MUST only be changed atomically, i.e.
205 * Since there's no atomic replace, the replacement action is an add followed
206 * by a delete. This means that during logging config changes, log messages
207 * may be duplicated in the log target that is being changed. The old entry
208 * being changed MUST also at the very least not crash or do other stupid
211 * This list and struct are NOT related to config. Logging config is kept
212 * separately, and results in creating appropriate zlog_target(s) to realize
213 * the config. Log targets may also be created from varying sources, e.g.
214 * command line options, or VTY commands ("log monitor").
216 * struct zlog_target is intended to be embedded into a larger structure that
217 * contains additional field for the specific logging target, e.g. an fd or
218 * additional options. It MUST be the first field in that larger struct.
221 PREDECL_ATOMLIST(zlog_targets
);
223 struct zlog_targets_item head
;
227 void (*logfn
)(struct zlog_target
*zt
, struct zlog_msg
*msg
[],
230 /* for crash handlers, set to NULL if log target can't write crash logs
231 * without possibly deadlocking (AS-Safe)
233 * text is not \0 terminated & split up into lines (e.g. no \n)
235 void (*logfn_sigsafe
)(struct zlog_target
*zt
, const char *text
,
238 struct rcu_head rcu_head
;
241 /* make a copy for RCUpdating. oldzt may be NULL to allocate a fresh one. */
242 extern struct zlog_target
*zlog_target_clone(struct memtype
*mt
,
243 struct zlog_target
*oldzt
,
246 /* update the zlog_targets list; both oldzt and newzt may be NULL. You
247 * still need to zlog_target_free() the old target afterwards if it wasn't
250 * Returns oldzt so you can zlog_target_free(zlog_target_replace(old, new));
251 * (Some log targets may need extra cleanup inbetween, but remember the old
252 * target MUST remain functional until the end of the current RCU cycle.)
254 extern struct zlog_target
*zlog_target_replace(struct zlog_target
*oldzt
,
255 struct zlog_target
*newzt
);
257 /* Mostly for symmetry for zlog_target_clone(), just rcu_free() internally. */
258 #define zlog_target_free(mt, zt) \
259 rcu_free(mt, zt, rcu_head)
261 extern void zlog_init(const char *progname
, const char *protoname
,
262 unsigned short instance
, uid_t uid
, gid_t gid
);
263 DECLARE_HOOK(zlog_init
, (const char *progname
, const char *protoname
,
264 unsigned short instance
, uid_t uid
, gid_t gid
),
265 (progname
, protoname
, instance
, uid
, gid
));
267 extern void zlog_fini(void);
268 DECLARE_KOOH(zlog_fini
, (), ());
270 extern void zlog_set_prefix_ec(bool enable
);
271 extern bool zlog_get_prefix_ec(void);
272 extern void zlog_set_prefix_xid(bool enable
);
273 extern bool zlog_get_prefix_xid(void);
275 /* for tools & test programs, i.e. anything not a daemon.
276 * (no cleanup needed at exit)
278 extern void zlog_aux_init(const char *prefix
, int prio_min
);
279 DECLARE_HOOK(zlog_aux_init
, (const char *prefix
, int prio_min
),
282 extern void zlog_startup_end(void);
284 extern void zlog_tls_buffer_init(void);
285 extern void zlog_tls_buffer_flush(void);
286 extern void zlog_tls_buffer_fini(void);
288 /* Enable or disable 'immediate' output - default is to buffer messages. */
289 extern void zlog_set_immediate(bool set_p
);
291 extern const char *zlog_priority_str(int priority
);
297 #endif /* _FRR_ZLOG_H */