blob: ee34a4ad2d9dab986ae87caba0aff32f7540e0a9 [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
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#ifndef _ZEBRA_LOG_H
24#define _ZEBRA_LOG_H
25
26#include <syslog.h>
27
ajs5e764772004-12-03 19:03:33 +000028/* Here is some guidance on logging levels to use:
29 *
30 * LOG_DEBUG - For all messages that are enabled by optional debugging
31 * features, typically preceded by "if (IS...DEBUG...)"
32 * LOG_INFO - Information that may be of interest, but everything seems
33 * to be working properly.
34 * LOG_NOTICE - Only for message pertaining to daemon startup or shutdown.
35 * LOG_WARNING - Warning conditions: unexpected events, but the daemon believes
36 * it can continue to operate correctly.
37 * LOG_ERR - Error situations indicating malfunctions. Probably require
38 * attention.
39 *
40 * Note: LOG_CRIT, LOG_ALERT, and LOG_EMERG are currently not used anywhere,
41 * please use LOG_ERR instead.
42 */
43
paul718e3742002-12-13 20:15:29 +000044typedef enum
45{
46 ZLOG_NONE,
47 ZLOG_DEFAULT,
48 ZLOG_ZEBRA,
49 ZLOG_RIP,
50 ZLOG_BGP,
51 ZLOG_OSPF,
52 ZLOG_RIPNG,
53 ZLOG_OSPF6,
jardin9e867fe2003-12-23 08:56:18 +000054 ZLOG_ISIS,
paul718e3742002-12-13 20:15:29 +000055 ZLOG_MASC
56} zlog_proto_t;
57
ajs274a4a42004-12-07 15:39:31 +000058/* If maxlvl is set to ZLOG_DISABLED, then no messages will be sent
59 to that logging destination. */
60#define ZLOG_DISABLED (LOG_EMERG-1)
61
62typedef enum
63{
64 ZLOG_DEST_SYSLOG = 0,
65 ZLOG_DEST_STDOUT,
66 ZLOG_DEST_MONITOR,
67 ZLOG_DEST_FILE
68} zlog_dest_t;
69#define ZLOG_NUM_DESTS (ZLOG_DEST_FILE+1)
70
paul718e3742002-12-13 20:15:29 +000071struct zlog
72{
ajs7d149b82004-11-28 23:00:01 +000073 const char *ident; /* daemon name (first arg to openlog) */
paul718e3742002-12-13 20:15:29 +000074 zlog_proto_t protocol;
ajs274a4a42004-12-07 15:39:31 +000075 int maxlvl[ZLOG_NUM_DESTS]; /* maximum priority to send to associated
76 logging destination */
77 int default_lvl; /* maxlvl to use if none is specified */
paul718e3742002-12-13 20:15:29 +000078 FILE *fp;
79 char *filename;
paul718e3742002-12-13 20:15:29 +000080 int facility; /* as per syslog facility */
ajs7d149b82004-11-28 23:00:01 +000081 int record_priority; /* should messages logged through stdio include the
82 priority of the message? */
83 int syslog_options; /* 2nd arg to openlog */
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +000084 int timestamp_precision; /* # of digits of subsecond precision */
paul718e3742002-12-13 20:15:29 +000085};
86
87/* Message structure. */
88struct message
89{
90 int key;
hassob04c6992004-10-04 19:10:31 +000091 const char *str;
paul718e3742002-12-13 20:15:29 +000092};
93
94/* Default logging strucutre. */
95extern struct zlog *zlog_default;
96
97/* Open zlog function */
paul8cc41982005-05-06 21:25:49 +000098extern struct zlog *openzlog (const char *progname, zlog_proto_t protocol,
99 int syslog_options, int syslog_facility);
paul718e3742002-12-13 20:15:29 +0000100
101/* Close zlog function. */
paul8cc41982005-05-06 21:25:49 +0000102extern void closezlog (struct zlog *zl);
paul718e3742002-12-13 20:15:29 +0000103
104/* GCC have printf type attribute check. */
105#ifdef __GNUC__
106#define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
107#else
108#define PRINTF_ATTRIBUTE(a,b)
109#endif /* __GNUC__ */
110
111/* Generic function for zlog. */
Stephen Hemminger80c375e2008-08-08 15:15:23 -0700112extern void zlog (struct zlog *zl, int priority, const char *format, ...)
113 PRINTF_ATTRIBUTE(3, 4);
paul718e3742002-12-13 20:15:29 +0000114
115/* Handy zlog functions. */
paul8cc41982005-05-06 21:25:49 +0000116extern void zlog_err (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
117extern void zlog_warn (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
118extern void zlog_info (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
119extern void zlog_notice (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
120extern void zlog_debug (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
paul718e3742002-12-13 20:15:29 +0000121
122/* For bgpd's peer oriented log. */
Stephen Hemminger80c375e2008-08-08 15:15:23 -0700123extern void plog_err (struct zlog *, const char *format, ...)
124 PRINTF_ATTRIBUTE(2, 3);
125extern void plog_warn (struct zlog *, const char *format, ...)
126 PRINTF_ATTRIBUTE(2, 3);
127extern void plog_info (struct zlog *, const char *format, ...)
128 PRINTF_ATTRIBUTE(2, 3);
129extern void plog_notice (struct zlog *, const char *format, ...)
130 PRINTF_ATTRIBUTE(2, 3);
131extern void plog_debug (struct zlog *, const char *format, ...)
132 PRINTF_ATTRIBUTE(2, 3);
paul718e3742002-12-13 20:15:29 +0000133
ajs274a4a42004-12-07 15:39:31 +0000134/* Set logging level for the given destination. If the log_level
135 argument is ZLOG_DISABLED, then the destination is disabled.
136 This function should not be used for file logging (use zlog_set_file
137 or zlog_reset_file instead). */
paul8cc41982005-05-06 21:25:49 +0000138extern void zlog_set_level (struct zlog *zl, zlog_dest_t, int log_level);
paul718e3742002-12-13 20:15:29 +0000139
ajs274a4a42004-12-07 15:39:31 +0000140/* Set logging to the given filename at the specified level. */
paul8cc41982005-05-06 21:25:49 +0000141extern int zlog_set_file (struct zlog *zl, const char *filename, int log_level);
ajs274a4a42004-12-07 15:39:31 +0000142/* Disable file logging. */
paul8cc41982005-05-06 21:25:49 +0000143extern int zlog_reset_file (struct zlog *zl);
paul718e3742002-12-13 20:15:29 +0000144
145/* Rotate log. */
paul8cc41982005-05-06 21:25:49 +0000146extern int zlog_rotate (struct zlog *);
paul718e3742002-12-13 20:15:29 +0000147
148/* For hackey massage lookup and check */
Dmitrij Tejblum614da2b2011-09-21 17:41:41 +0400149#define LOOKUP(x, y) mes_lookup(x, x ## _max, y, "(no item found)", #x)
paul718e3742002-12-13 20:15:29 +0000150
Stephen Hemminger1423c802008-08-14 17:59:25 +0100151extern const char *lookup (const struct message *, int);
Stephen Hemminger8e4c0932009-05-15 09:47:34 -0700152extern const char *mes_lookup (const struct message *meslist,
Paul Jakma11486b52008-02-28 23:26:02 +0000153 int max, int index,
Dmitrij Tejblum614da2b2011-09-21 17:41:41 +0400154 const char *no_item, const char *mesname);
paul718e3742002-12-13 20:15:29 +0000155
156extern const char *zlog_priority[];
ajs274a4a42004-12-07 15:39:31 +0000157extern const char *zlog_proto_names[];
paul718e3742002-12-13 20:15:29 +0000158
ajsca359762004-11-19 23:40:16 +0000159/* Safe version of strerror -- never returns NULL. */
160extern const char *safe_strerror(int errnum);
161
ajs59a06a92004-11-23 18:19:14 +0000162/* To be called when a fatal signal is caught. */
ajs31364272005-01-18 22:18:59 +0000163extern void zlog_signal(int signo, const char *action
164#ifdef SA_SIGINFO
165 , siginfo_t *siginfo, void *program_counter
166#endif
167 );
ajs59a06a92004-11-23 18:19:14 +0000168
ajs063ee522004-11-26 18:11:14 +0000169/* Log a backtrace. */
170extern void zlog_backtrace(int priority);
171
172/* Log a backtrace, but in an async-signal-safe way. Should not be
173 called unless the program is about to exit or abort, since it messes
ajs239c26f2005-01-17 15:22:28 +0000174 up the state of zlog file pointers. If program_counter is non-NULL,
175 that is logged in addition to the current backtrace. */
176extern void zlog_backtrace_sigsafe(int priority, void *program_counter);
ajs063ee522004-11-26 18:11:14 +0000177
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000178/* Puts a current timestamp in buf and returns the number of characters
179 written (not including the terminating NUL). The purpose of
180 this function is to avoid calls to localtime appearing all over the code.
181 It caches the most recent localtime result and can therefore
182 avoid multiple calls within the same second. If buflen is too small,
183 *buf will be set to '\0', and 0 will be returned. */
184extern size_t quagga_timestamp(int timestamp_precision /* # subsecond digits */,
185 char *buf, size_t buflen);
186
187/* structure useful for avoiding repeated rendering of the same timestamp */
188struct timestamp_control {
189 size_t len; /* length of rendered timestamp */
190 int precision; /* configuration parameter */
191 int already_rendered; /* should be initialized to 0 */
192 char buf[40]; /* will contain the rendered timestamp */
193};
194
ajs274a4a42004-12-07 15:39:31 +0000195/* Defines for use in command construction: */
196
197#define LOG_LEVELS "(emergencies|alerts|critical|errors|warnings|notifications|informational|debugging)"
198
199#define LOG_LEVEL_DESC \
200 "System is unusable\n" \
201 "Immediate action needed\n" \
202 "Critical conditions\n" \
203 "Error conditions\n" \
204 "Warning conditions\n" \
205 "Normal but significant conditions\n" \
206 "Informational messages\n" \
207 "Debugging messages\n"
208
209#define LOG_FACILITIES "(kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7)"
210
211#define LOG_FACILITY_DESC \
212 "Kernel\n" \
213 "User process\n" \
214 "Mail system\n" \
215 "System daemons\n" \
216 "Authorization system\n" \
217 "Syslog itself\n" \
218 "Line printer system\n" \
219 "USENET news\n" \
220 "Unix-to-Unix copy system\n" \
221 "Cron/at facility\n" \
222 "Local use\n" \
223 "Local use\n" \
224 "Local use\n" \
225 "Local use\n" \
226 "Local use\n" \
227 "Local use\n" \
228 "Local use\n" \
229 "Local use\n"
230
paul718e3742002-12-13 20:15:29 +0000231#endif /* _ZEBRA_LOG_H */