blob: d9f1ecaae38e1011aee6ba463947b923e5af7595 [file] [log] [blame]
ajs274a4a42004-12-07 15:39:31 +00001/*
ajs274a4a42004-12-07 15:39:31 +00002 * Zebra logging funcions.
paul718e3742002-12-13 20:15:29 +00003 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
Everton Marques871dbcf2009-08-11 15:43:05 -03004 * Portions Copyright (c) 2008 Everton da Silva Marques <everton.marques@gmail.com>
paul718e3742002-12-13 20:15:29 +00005 *
6 * This file is part of GNU Zebra.
7 *
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with GNU Zebra; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
22 */
23
24#ifndef _ZEBRA_LOG_H
25#define _ZEBRA_LOG_H
26
27#include <syslog.h>
28
ajs5e764772004-12-03 19:03:33 +000029/* Here is some guidance on logging levels to use:
30 *
31 * LOG_DEBUG - For all messages that are enabled by optional debugging
32 * features, typically preceded by "if (IS...DEBUG...)"
33 * LOG_INFO - Information that may be of interest, but everything seems
34 * to be working properly.
35 * LOG_NOTICE - Only for message pertaining to daemon startup or shutdown.
36 * LOG_WARNING - Warning conditions: unexpected events, but the daemon believes
37 * it can continue to operate correctly.
38 * LOG_ERR - Error situations indicating malfunctions. Probably require
39 * attention.
40 *
41 * Note: LOG_CRIT, LOG_ALERT, and LOG_EMERG are currently not used anywhere,
42 * please use LOG_ERR instead.
43 */
44
paul718e3742002-12-13 20:15:29 +000045typedef enum
46{
47 ZLOG_NONE,
48 ZLOG_DEFAULT,
49 ZLOG_ZEBRA,
50 ZLOG_RIP,
51 ZLOG_BGP,
52 ZLOG_OSPF,
Paul Jakma57345092011-12-25 17:52:09 +010053 ZLOG_RIPNG,
54 ZLOG_BABEL,
paul718e3742002-12-13 20:15:29 +000055 ZLOG_OSPF6,
jardin9e867fe2003-12-23 08:56:18 +000056 ZLOG_ISIS,
Everton Marques871dbcf2009-08-11 15:43:05 -030057 ZLOG_PIM,
paul718e3742002-12-13 20:15:29 +000058 ZLOG_MASC
59} zlog_proto_t;
60
ajs274a4a42004-12-07 15:39:31 +000061/* If maxlvl is set to ZLOG_DISABLED, then no messages will be sent
62 to that logging destination. */
63#define ZLOG_DISABLED (LOG_EMERG-1)
64
65typedef enum
66{
67 ZLOG_DEST_SYSLOG = 0,
68 ZLOG_DEST_STDOUT,
69 ZLOG_DEST_MONITOR,
70 ZLOG_DEST_FILE
71} zlog_dest_t;
72#define ZLOG_NUM_DESTS (ZLOG_DEST_FILE+1)
73
paul718e3742002-12-13 20:15:29 +000074struct zlog
75{
ajs7d149b82004-11-28 23:00:01 +000076 const char *ident; /* daemon name (first arg to openlog) */
paul718e3742002-12-13 20:15:29 +000077 zlog_proto_t protocol;
ajs274a4a42004-12-07 15:39:31 +000078 int maxlvl[ZLOG_NUM_DESTS]; /* maximum priority to send to associated
79 logging destination */
80 int default_lvl; /* maxlvl to use if none is specified */
paul718e3742002-12-13 20:15:29 +000081 FILE *fp;
82 char *filename;
paul718e3742002-12-13 20:15:29 +000083 int facility; /* as per syslog facility */
ajs7d149b82004-11-28 23:00:01 +000084 int record_priority; /* should messages logged through stdio include the
85 priority of the message? */
86 int syslog_options; /* 2nd arg to openlog */
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +000087 int timestamp_precision; /* # of digits of subsecond precision */
paul718e3742002-12-13 20:15:29 +000088};
89
90/* Message structure. */
91struct message
92{
93 int key;
hassob04c6992004-10-04 19:10:31 +000094 const char *str;
paul718e3742002-12-13 20:15:29 +000095};
96
97/* Default logging strucutre. */
98extern struct zlog *zlog_default;
99
100/* Open zlog function */
paul8cc41982005-05-06 21:25:49 +0000101extern struct zlog *openzlog (const char *progname, zlog_proto_t protocol,
102 int syslog_options, int syslog_facility);
paul718e3742002-12-13 20:15:29 +0000103
104/* Close zlog function. */
paul8cc41982005-05-06 21:25:49 +0000105extern void closezlog (struct zlog *zl);
paul718e3742002-12-13 20:15:29 +0000106
107/* GCC have printf type attribute check. */
108#ifdef __GNUC__
109#define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
110#else
111#define PRINTF_ATTRIBUTE(a,b)
112#endif /* __GNUC__ */
113
114/* Generic function for zlog. */
Stephen Hemminger80c375e2008-08-08 15:15:23 -0700115extern void zlog (struct zlog *zl, int priority, const char *format, ...)
116 PRINTF_ATTRIBUTE(3, 4);
paul718e3742002-12-13 20:15:29 +0000117
118/* Handy zlog functions. */
paul8cc41982005-05-06 21:25:49 +0000119extern void zlog_err (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
120extern void zlog_warn (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
121extern void zlog_info (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
122extern void zlog_notice (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
123extern void zlog_debug (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
paul718e3742002-12-13 20:15:29 +0000124
125/* For bgpd's peer oriented log. */
Stephen Hemminger80c375e2008-08-08 15:15:23 -0700126extern void plog_err (struct zlog *, const char *format, ...)
127 PRINTF_ATTRIBUTE(2, 3);
128extern void plog_warn (struct zlog *, const char *format, ...)
129 PRINTF_ATTRIBUTE(2, 3);
130extern void plog_info (struct zlog *, const char *format, ...)
131 PRINTF_ATTRIBUTE(2, 3);
132extern void plog_notice (struct zlog *, const char *format, ...)
133 PRINTF_ATTRIBUTE(2, 3);
134extern void plog_debug (struct zlog *, const char *format, ...)
135 PRINTF_ATTRIBUTE(2, 3);
paul718e3742002-12-13 20:15:29 +0000136
David Lamparter615f9f12013-11-18 23:52:02 +0100137extern void zlog_thread_info (int log_level);
138
ajs274a4a42004-12-07 15:39:31 +0000139/* Set logging level for the given destination. If the log_level
140 argument is ZLOG_DISABLED, then the destination is disabled.
141 This function should not be used for file logging (use zlog_set_file
142 or zlog_reset_file instead). */
paul8cc41982005-05-06 21:25:49 +0000143extern void zlog_set_level (struct zlog *zl, zlog_dest_t, int log_level);
paul718e3742002-12-13 20:15:29 +0000144
ajs274a4a42004-12-07 15:39:31 +0000145/* Set logging to the given filename at the specified level. */
paul8cc41982005-05-06 21:25:49 +0000146extern int zlog_set_file (struct zlog *zl, const char *filename, int log_level);
ajs274a4a42004-12-07 15:39:31 +0000147/* Disable file logging. */
paul8cc41982005-05-06 21:25:49 +0000148extern int zlog_reset_file (struct zlog *zl);
paul718e3742002-12-13 20:15:29 +0000149
150/* Rotate log. */
paul8cc41982005-05-06 21:25:49 +0000151extern int zlog_rotate (struct zlog *);
paul718e3742002-12-13 20:15:29 +0000152
Leonid Rosenboim1e0ce7c2012-12-07 21:31:07 +0000153/* For hackey message lookup and check */
154#define LOOKUP_DEF(x, y, def) mes_lookup(x, x ## _max, y, def, #x)
155#define LOOKUP(x, y) LOOKUP_DEF(x, y, "(no item found)")
paul718e3742002-12-13 20:15:29 +0000156
Stephen Hemminger1423c802008-08-14 17:59:25 +0100157extern const char *lookup (const struct message *, int);
Stephen Hemminger8e4c0932009-05-15 09:47:34 -0700158extern const char *mes_lookup (const struct message *meslist,
Paul Jakma11486b52008-02-28 23:26:02 +0000159 int max, int index,
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400160 const char *no_item, const char *mesname);
paul718e3742002-12-13 20:15:29 +0000161
162extern const char *zlog_priority[];
ajs274a4a42004-12-07 15:39:31 +0000163extern const char *zlog_proto_names[];
paul718e3742002-12-13 20:15:29 +0000164
ajsca359762004-11-19 23:40:16 +0000165/* Safe version of strerror -- never returns NULL. */
166extern const char *safe_strerror(int errnum);
167
ajs59a06a92004-11-23 18:19:14 +0000168/* To be called when a fatal signal is caught. */
ajs31364272005-01-18 22:18:59 +0000169extern void zlog_signal(int signo, const char *action
170#ifdef SA_SIGINFO
171 , siginfo_t *siginfo, void *program_counter
172#endif
173 );
ajs59a06a92004-11-23 18:19:14 +0000174
ajs063ee522004-11-26 18:11:14 +0000175/* Log a backtrace. */
176extern void zlog_backtrace(int priority);
177
178/* Log a backtrace, but in an async-signal-safe way. Should not be
179 called unless the program is about to exit or abort, since it messes
ajs239c26f2005-01-17 15:22:28 +0000180 up the state of zlog file pointers. If program_counter is non-NULL,
181 that is logged in addition to the current backtrace. */
182extern void zlog_backtrace_sigsafe(int priority, void *program_counter);
ajs063ee522004-11-26 18:11:14 +0000183
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000184/* Puts a current timestamp in buf and returns the number of characters
185 written (not including the terminating NUL). The purpose of
186 this function is to avoid calls to localtime appearing all over the code.
187 It caches the most recent localtime result and can therefore
188 avoid multiple calls within the same second. If buflen is too small,
189 *buf will be set to '\0', and 0 will be returned. */
190extern size_t quagga_timestamp(int timestamp_precision /* # subsecond digits */,
191 char *buf, size_t buflen);
192
193/* structure useful for avoiding repeated rendering of the same timestamp */
194struct timestamp_control {
195 size_t len; /* length of rendered timestamp */
196 int precision; /* configuration parameter */
197 int already_rendered; /* should be initialized to 0 */
198 char buf[40]; /* will contain the rendered timestamp */
199};
200
ajs274a4a42004-12-07 15:39:31 +0000201/* Defines for use in command construction: */
202
203#define LOG_LEVELS "(emergencies|alerts|critical|errors|warnings|notifications|informational|debugging)"
204
205#define LOG_LEVEL_DESC \
206 "System is unusable\n" \
207 "Immediate action needed\n" \
208 "Critical conditions\n" \
209 "Error conditions\n" \
210 "Warning conditions\n" \
211 "Normal but significant conditions\n" \
212 "Informational messages\n" \
213 "Debugging messages\n"
214
215#define LOG_FACILITIES "(kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7)"
216
217#define LOG_FACILITY_DESC \
218 "Kernel\n" \
219 "User process\n" \
220 "Mail system\n" \
221 "System daemons\n" \
222 "Authorization system\n" \
223 "Syslog itself\n" \
224 "Line printer system\n" \
225 "USENET news\n" \
226 "Unix-to-Unix copy system\n" \
227 "Cron/at facility\n" \
228 "Local use\n" \
229 "Local use\n" \
230 "Local use\n" \
231 "Local use\n" \
232 "Local use\n" \
233 "Local use\n" \
234 "Local use\n" \
235 "Local use\n"
236
paul718e3742002-12-13 20:15:29 +0000237#endif /* _ZEBRA_LOG_H */