ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1 | /* |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame^] | 2 | * $Id$ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3 | * |
| 4 | * Zebra logging funcions. |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 5 | * 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 | |
ajs | 5e76477 | 2004-12-03 19:03:33 +0000 | [diff] [blame] | 30 | /* 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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 46 | typedef 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, |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 56 | ZLOG_ISIS, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 57 | ZLOG_MASC |
| 58 | } zlog_proto_t; |
| 59 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 60 | /* 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 | |
| 64 | typedef 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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 73 | struct zlog |
| 74 | { |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 75 | const char *ident; /* daemon name (first arg to openlog) */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 76 | zlog_proto_t protocol; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 77 | 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 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 80 | FILE *fp; |
| 81 | char *filename; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 82 | int facility; /* as per syslog facility */ |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 83 | 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. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame^] | 86 | int timestamp_precision; /* # of digits of subsecond precision */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /* Message structure. */ |
| 90 | struct message |
| 91 | { |
| 92 | int key; |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 93 | const char *str; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | /* Default logging strucutre. */ |
| 97 | extern struct zlog *zlog_default; |
| 98 | |
| 99 | /* Open zlog function */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 100 | extern struct zlog *openzlog (const char *progname, zlog_proto_t protocol, |
| 101 | int syslog_options, int syslog_facility); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 102 | |
| 103 | /* Close zlog function. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 104 | extern void closezlog (struct zlog *zl); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 105 | |
| 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. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 114 | extern void zlog (struct zlog *zl, int priority, const char *format, ...) PRINTF_ATTRIBUTE(3, 4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 115 | |
| 116 | /* Handy zlog functions. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 117 | extern void zlog_err (const char *format, ...) PRINTF_ATTRIBUTE(1, 2); |
| 118 | extern void zlog_warn (const char *format, ...) PRINTF_ATTRIBUTE(1, 2); |
| 119 | extern void zlog_info (const char *format, ...) PRINTF_ATTRIBUTE(1, 2); |
| 120 | extern void zlog_notice (const char *format, ...) PRINTF_ATTRIBUTE(1, 2); |
| 121 | extern void zlog_debug (const char *format, ...) PRINTF_ATTRIBUTE(1, 2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 122 | |
| 123 | /* For bgpd's peer oriented log. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 124 | extern void plog_err (struct zlog *, const char *format, ...); |
| 125 | extern void plog_warn (struct zlog *, const char *format, ...); |
| 126 | extern void plog_info (struct zlog *, const char *format, ...); |
| 127 | extern void plog_notice (struct zlog *, const char *format, ...); |
| 128 | extern void plog_debug (struct zlog *, const char *format, ...); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 129 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 130 | /* 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). */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 134 | extern void zlog_set_level (struct zlog *zl, zlog_dest_t, int log_level); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 135 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 136 | /* Set logging to the given filename at the specified level. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 137 | extern int zlog_set_file (struct zlog *zl, const char *filename, int log_level); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 138 | /* Disable file logging. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 139 | extern int zlog_reset_file (struct zlog *zl); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 140 | |
| 141 | /* Rotate log. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 142 | extern int zlog_rotate (struct zlog *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 143 | |
| 144 | /* For hackey massage lookup and check */ |
| 145 | #define LOOKUP(x, y) mes_lookup(x, x ## _max, y) |
| 146 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 147 | extern const char *lookup (struct message *, int); |
| 148 | extern const char *mes_lookup (struct message *meslist, int max, int index); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 149 | |
| 150 | extern const char *zlog_priority[]; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 151 | extern const char *zlog_proto_names[]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 152 | |
ajs | ca35976 | 2004-11-19 23:40:16 +0000 | [diff] [blame] | 153 | /* Safe version of strerror -- never returns NULL. */ |
| 154 | extern const char *safe_strerror(int errnum); |
| 155 | |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 156 | /* To be called when a fatal signal is caught. */ |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 157 | extern void zlog_signal(int signo, const char *action |
| 158 | #ifdef SA_SIGINFO |
| 159 | , siginfo_t *siginfo, void *program_counter |
| 160 | #endif |
| 161 | ); |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 162 | |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 163 | /* Log a backtrace. */ |
| 164 | extern void zlog_backtrace(int priority); |
| 165 | |
| 166 | /* Log a backtrace, but in an async-signal-safe way. Should not be |
| 167 | called unless the program is about to exit or abort, since it messes |
ajs | 239c26f | 2005-01-17 15:22:28 +0000 | [diff] [blame] | 168 | up the state of zlog file pointers. If program_counter is non-NULL, |
| 169 | that is logged in addition to the current backtrace. */ |
| 170 | extern void zlog_backtrace_sigsafe(int priority, void *program_counter); |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 171 | |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame^] | 172 | /* Puts a current timestamp in buf and returns the number of characters |
| 173 | written (not including the terminating NUL). The purpose of |
| 174 | this function is to avoid calls to localtime appearing all over the code. |
| 175 | It caches the most recent localtime result and can therefore |
| 176 | avoid multiple calls within the same second. If buflen is too small, |
| 177 | *buf will be set to '\0', and 0 will be returned. */ |
| 178 | extern size_t quagga_timestamp(int timestamp_precision /* # subsecond digits */, |
| 179 | char *buf, size_t buflen); |
| 180 | |
| 181 | /* structure useful for avoiding repeated rendering of the same timestamp */ |
| 182 | struct timestamp_control { |
| 183 | size_t len; /* length of rendered timestamp */ |
| 184 | int precision; /* configuration parameter */ |
| 185 | int already_rendered; /* should be initialized to 0 */ |
| 186 | char buf[40]; /* will contain the rendered timestamp */ |
| 187 | }; |
| 188 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 189 | /* Defines for use in command construction: */ |
| 190 | |
| 191 | #define LOG_LEVELS "(emergencies|alerts|critical|errors|warnings|notifications|informational|debugging)" |
| 192 | |
| 193 | #define LOG_LEVEL_DESC \ |
| 194 | "System is unusable\n" \ |
| 195 | "Immediate action needed\n" \ |
| 196 | "Critical conditions\n" \ |
| 197 | "Error conditions\n" \ |
| 198 | "Warning conditions\n" \ |
| 199 | "Normal but significant conditions\n" \ |
| 200 | "Informational messages\n" \ |
| 201 | "Debugging messages\n" |
| 202 | |
| 203 | #define LOG_FACILITIES "(kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7)" |
| 204 | |
| 205 | #define LOG_FACILITY_DESC \ |
| 206 | "Kernel\n" \ |
| 207 | "User process\n" \ |
| 208 | "Mail system\n" \ |
| 209 | "System daemons\n" \ |
| 210 | "Authorization system\n" \ |
| 211 | "Syslog itself\n" \ |
| 212 | "Line printer system\n" \ |
| 213 | "USENET news\n" \ |
| 214 | "Unix-to-Unix copy system\n" \ |
| 215 | "Cron/at facility\n" \ |
| 216 | "Local use\n" \ |
| 217 | "Local use\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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 225 | #endif /* _ZEBRA_LOG_H */ |