blob: 7432b25e41e6fab74588dd89562c3545196eb609 [file] [log] [blame]
ajs274a4a42004-12-07 15:39:31 +00001/*
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +00002 * $Id$
ajs274a4a42004-12-07 15:39:31 +00003 *
4 * Zebra logging funcions.
paul718e3742002-12-13 20:15:29 +00005 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
6 *
7 * This file is part of GNU Zebra.
8 *
9 * GNU Zebra is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * GNU Zebra is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with GNU Zebra; see the file COPYING. If not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 * 02111-1307, USA.
23 */
24
25#ifndef _ZEBRA_LOG_H
26#define _ZEBRA_LOG_H
27
28#include <syslog.h>
29
ajs5e764772004-12-03 19:03:33 +000030/* Here is some guidance on logging levels to use:
31 *
32 * LOG_DEBUG - For all messages that are enabled by optional debugging
33 * features, typically preceded by "if (IS...DEBUG...)"
34 * LOG_INFO - Information that may be of interest, but everything seems
35 * to be working properly.
36 * LOG_NOTICE - Only for message pertaining to daemon startup or shutdown.
37 * LOG_WARNING - Warning conditions: unexpected events, but the daemon believes
38 * it can continue to operate correctly.
39 * LOG_ERR - Error situations indicating malfunctions. Probably require
40 * attention.
41 *
42 * Note: LOG_CRIT, LOG_ALERT, and LOG_EMERG are currently not used anywhere,
43 * please use LOG_ERR instead.
44 */
45
paul718e3742002-12-13 20:15:29 +000046typedef enum
47{
48 ZLOG_NONE,
49 ZLOG_DEFAULT,
50 ZLOG_ZEBRA,
51 ZLOG_RIP,
52 ZLOG_BGP,
53 ZLOG_OSPF,
54 ZLOG_RIPNG,
55 ZLOG_OSPF6,
jardin9e867fe2003-12-23 08:56:18 +000056 ZLOG_ISIS,
paul718e3742002-12-13 20:15:29 +000057 ZLOG_MASC
58} zlog_proto_t;
59
ajs274a4a42004-12-07 15:39:31 +000060/* If maxlvl is set to ZLOG_DISABLED, then no messages will be sent
61 to that logging destination. */
62#define ZLOG_DISABLED (LOG_EMERG-1)
63
64typedef enum
65{
66 ZLOG_DEST_SYSLOG = 0,
67 ZLOG_DEST_STDOUT,
68 ZLOG_DEST_MONITOR,
69 ZLOG_DEST_FILE
70} zlog_dest_t;
71#define ZLOG_NUM_DESTS (ZLOG_DEST_FILE+1)
72
paul718e3742002-12-13 20:15:29 +000073struct zlog
74{
ajs7d149b82004-11-28 23:00:01 +000075 const char *ident; /* daemon name (first arg to openlog) */
paul718e3742002-12-13 20:15:29 +000076 zlog_proto_t protocol;
ajs274a4a42004-12-07 15:39:31 +000077 int maxlvl[ZLOG_NUM_DESTS]; /* maximum priority to send to associated
78 logging destination */
79 int default_lvl; /* maxlvl to use if none is specified */
paul718e3742002-12-13 20:15:29 +000080 FILE *fp;
81 char *filename;
paul718e3742002-12-13 20:15:29 +000082 int facility; /* as per syslog facility */
ajs7d149b82004-11-28 23:00:01 +000083 int record_priority; /* should messages logged through stdio include the
84 priority of the message? */
85 int syslog_options; /* 2nd arg to openlog */
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +000086 int timestamp_precision; /* # of digits of subsecond precision */
paul718e3742002-12-13 20:15:29 +000087};
88
89/* Message structure. */
90struct message
91{
92 int key;
hassob04c6992004-10-04 19:10:31 +000093 const char *str;
paul718e3742002-12-13 20:15:29 +000094};
95
96/* Default logging strucutre. */
97extern struct zlog *zlog_default;
98
99/* Open zlog function */
paul8cc41982005-05-06 21:25:49 +0000100extern struct zlog *openzlog (const char *progname, zlog_proto_t protocol,
101 int syslog_options, int syslog_facility);
paul718e3742002-12-13 20:15:29 +0000102
103/* Close zlog function. */
paul8cc41982005-05-06 21:25:49 +0000104extern void closezlog (struct zlog *zl);
paul718e3742002-12-13 20:15:29 +0000105
106/* GCC have printf type attribute check. */
107#ifdef __GNUC__
108#define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
109#else
110#define PRINTF_ATTRIBUTE(a,b)
111#endif /* __GNUC__ */
112
113/* Generic function for zlog. */
paul8cc41982005-05-06 21:25:49 +0000114extern void zlog (struct zlog *zl, int priority, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
paul718e3742002-12-13 20:15:29 +0000115
116/* Handy zlog functions. */
paul8cc41982005-05-06 21:25:49 +0000117extern void zlog_err (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
118extern void zlog_warn (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
119extern void zlog_info (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
120extern void zlog_notice (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
121extern void zlog_debug (const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
paul718e3742002-12-13 20:15:29 +0000122
123/* For bgpd's peer oriented log. */
paul8cc41982005-05-06 21:25:49 +0000124extern void plog_err (struct zlog *, const char *format, ...);
125extern void plog_warn (struct zlog *, const char *format, ...);
126extern void plog_info (struct zlog *, const char *format, ...);
127extern void plog_notice (struct zlog *, const char *format, ...);
128extern void plog_debug (struct zlog *, const char *format, ...);
paul718e3742002-12-13 20:15:29 +0000129
ajs274a4a42004-12-07 15:39:31 +0000130/* Set logging level for the given destination. If the log_level
131 argument is ZLOG_DISABLED, then the destination is disabled.
132 This function should not be used for file logging (use zlog_set_file
133 or zlog_reset_file instead). */
paul8cc41982005-05-06 21:25:49 +0000134extern void zlog_set_level (struct zlog *zl, zlog_dest_t, int log_level);
paul718e3742002-12-13 20:15:29 +0000135
ajs274a4a42004-12-07 15:39:31 +0000136/* Set logging to the given filename at the specified level. */
paul8cc41982005-05-06 21:25:49 +0000137extern int zlog_set_file (struct zlog *zl, const char *filename, int log_level);
ajs274a4a42004-12-07 15:39:31 +0000138/* Disable file logging. */
paul8cc41982005-05-06 21:25:49 +0000139extern int zlog_reset_file (struct zlog *zl);
paul718e3742002-12-13 20:15:29 +0000140
141/* Rotate log. */
paul8cc41982005-05-06 21:25:49 +0000142extern int zlog_rotate (struct zlog *);
paul718e3742002-12-13 20:15:29 +0000143
144/* For hackey massage lookup and check */
Paul Jakma11486b52008-02-28 23:26:02 +0000145#define LOOKUP(x, y) mes_lookup(x, x ## _max, y, "(no item found)")
paul718e3742002-12-13 20:15:29 +0000146
paul8cc41982005-05-06 21:25:49 +0000147extern const char *lookup (struct message *, int);
Paul Jakma11486b52008-02-28 23:26:02 +0000148extern const char *mes_lookup (struct message *meslist,
149 int max, int index,
150 const char *no_item);
paul718e3742002-12-13 20:15:29 +0000151
152extern const char *zlog_priority[];
ajs274a4a42004-12-07 15:39:31 +0000153extern const char *zlog_proto_names[];
paul718e3742002-12-13 20:15:29 +0000154
ajsca359762004-11-19 23:40:16 +0000155/* Safe version of strerror -- never returns NULL. */
156extern const char *safe_strerror(int errnum);
157
ajs59a06a92004-11-23 18:19:14 +0000158/* To be called when a fatal signal is caught. */
ajs31364272005-01-18 22:18:59 +0000159extern void zlog_signal(int signo, const char *action
160#ifdef SA_SIGINFO
161 , siginfo_t *siginfo, void *program_counter
162#endif
163 );
ajs59a06a92004-11-23 18:19:14 +0000164
ajs063ee522004-11-26 18:11:14 +0000165/* Log a backtrace. */
166extern void zlog_backtrace(int priority);
167
168/* Log a backtrace, but in an async-signal-safe way. Should not be
169 called unless the program is about to exit or abort, since it messes
ajs239c26f2005-01-17 15:22:28 +0000170 up the state of zlog file pointers. If program_counter is non-NULL,
171 that is logged in addition to the current backtrace. */
172extern void zlog_backtrace_sigsafe(int priority, void *program_counter);
ajs063ee522004-11-26 18:11:14 +0000173
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000174/* Puts a current timestamp in buf and returns the number of characters
175 written (not including the terminating NUL). The purpose of
176 this function is to avoid calls to localtime appearing all over the code.
177 It caches the most recent localtime result and can therefore
178 avoid multiple calls within the same second. If buflen is too small,
179 *buf will be set to '\0', and 0 will be returned. */
180extern size_t quagga_timestamp(int timestamp_precision /* # subsecond digits */,
181 char *buf, size_t buflen);
182
183/* structure useful for avoiding repeated rendering of the same timestamp */
184struct timestamp_control {
185 size_t len; /* length of rendered timestamp */
186 int precision; /* configuration parameter */
187 int already_rendered; /* should be initialized to 0 */
188 char buf[40]; /* will contain the rendered timestamp */
189};
190
ajs274a4a42004-12-07 15:39:31 +0000191/* Defines for use in command construction: */
192
193#define LOG_LEVELS "(emergencies|alerts|critical|errors|warnings|notifications|informational|debugging)"
194
195#define LOG_LEVEL_DESC \
196 "System is unusable\n" \
197 "Immediate action needed\n" \
198 "Critical conditions\n" \
199 "Error conditions\n" \
200 "Warning conditions\n" \
201 "Normal but significant conditions\n" \
202 "Informational messages\n" \
203 "Debugging messages\n"
204
205#define LOG_FACILITIES "(kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7)"
206
207#define LOG_FACILITY_DESC \
208 "Kernel\n" \
209 "User process\n" \
210 "Mail system\n" \
211 "System daemons\n" \
212 "Authorization system\n" \
213 "Syslog itself\n" \
214 "Line printer system\n" \
215 "USENET news\n" \
216 "Unix-to-Unix copy system\n" \
217 "Cron/at facility\n" \
218 "Local use\n" \
219 "Local use\n" \
220 "Local use\n" \
221 "Local use\n" \
222 "Local use\n" \
223 "Local use\n" \
224 "Local use\n" \
225 "Local use\n"
226
paul718e3742002-12-13 20:15:29 +0000227#endif /* _ZEBRA_LOG_H */