ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1 | /* |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 2 | * Zebra logging funcions. |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3 | * 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> |
Timo Teräs | dafa05e | 2017-01-19 17:27:01 +0200 | [diff] [blame^] | 27 | #include <stdio.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 28 | |
ajs | 5e76477 | 2004-12-03 19:03:33 +0000 | [diff] [blame] | 29 | /* 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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 45 | typedef enum |
| 46 | { |
| 47 | ZLOG_NONE, |
| 48 | ZLOG_DEFAULT, |
| 49 | ZLOG_ZEBRA, |
| 50 | ZLOG_RIP, |
| 51 | ZLOG_BGP, |
| 52 | ZLOG_OSPF, |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 53 | ZLOG_RIPNG, |
| 54 | ZLOG_BABEL, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 55 | ZLOG_OSPF6, |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 56 | ZLOG_ISIS, |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 57 | ZLOG_PIM, |
Timo Teräs | dafa05e | 2017-01-19 17:27:01 +0200 | [diff] [blame^] | 58 | ZLOG_MASC, |
| 59 | ZLOG_NHRP, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 60 | } zlog_proto_t; |
| 61 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 62 | /* If maxlvl is set to ZLOG_DISABLED, then no messages will be sent |
| 63 | to that logging destination. */ |
| 64 | #define ZLOG_DISABLED (LOG_EMERG-1) |
| 65 | |
| 66 | typedef enum |
| 67 | { |
| 68 | ZLOG_DEST_SYSLOG = 0, |
| 69 | ZLOG_DEST_STDOUT, |
| 70 | ZLOG_DEST_MONITOR, |
| 71 | ZLOG_DEST_FILE |
| 72 | } zlog_dest_t; |
| 73 | #define ZLOG_NUM_DESTS (ZLOG_DEST_FILE+1) |
| 74 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 75 | struct zlog |
| 76 | { |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 77 | const char *ident; /* daemon name (first arg to openlog) */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 78 | zlog_proto_t protocol; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 79 | int maxlvl[ZLOG_NUM_DESTS]; /* maximum priority to send to associated |
| 80 | logging destination */ |
| 81 | int default_lvl; /* maxlvl to use if none is specified */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 82 | FILE *fp; |
| 83 | char *filename; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | int facility; /* as per syslog facility */ |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 85 | int record_priority; /* should messages logged through stdio include the |
| 86 | priority of the message? */ |
| 87 | int syslog_options; /* 2nd arg to openlog */ |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 88 | int timestamp_precision; /* # of digits of subsecond precision */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /* Message structure. */ |
| 92 | struct message |
| 93 | { |
| 94 | int key; |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 95 | const char *str; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | /* Default logging strucutre. */ |
| 99 | extern struct zlog *zlog_default; |
| 100 | |
| 101 | /* Open zlog function */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 102 | extern struct zlog *openzlog (const char *progname, zlog_proto_t protocol, |
| 103 | int syslog_options, int syslog_facility); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 104 | |
| 105 | /* Close zlog function. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 106 | extern void closezlog (struct zlog *zl); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 107 | |
| 108 | /* GCC have printf type attribute check. */ |
| 109 | #ifdef __GNUC__ |
| 110 | #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b))) |
| 111 | #else |
| 112 | #define PRINTF_ATTRIBUTE(a,b) |
| 113 | #endif /* __GNUC__ */ |
| 114 | |
| 115 | /* Generic function for zlog. */ |
Stephen Hemminger | 80c375e | 2008-08-08 15:15:23 -0700 | [diff] [blame] | 116 | extern void zlog (struct zlog *zl, int priority, const char *format, ...) |
| 117 | PRINTF_ATTRIBUTE(3, 4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 118 | |
| 119 | /* Handy zlog functions. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 120 | extern void zlog_err (const char *format, ...) PRINTF_ATTRIBUTE(1, 2); |
| 121 | extern void zlog_warn (const char *format, ...) PRINTF_ATTRIBUTE(1, 2); |
| 122 | extern void zlog_info (const char *format, ...) PRINTF_ATTRIBUTE(1, 2); |
| 123 | extern void zlog_notice (const char *format, ...) PRINTF_ATTRIBUTE(1, 2); |
| 124 | extern void zlog_debug (const char *format, ...) PRINTF_ATTRIBUTE(1, 2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 125 | |
| 126 | /* For bgpd's peer oriented log. */ |
Stephen Hemminger | 80c375e | 2008-08-08 15:15:23 -0700 | [diff] [blame] | 127 | extern void plog_err (struct zlog *, const char *format, ...) |
| 128 | PRINTF_ATTRIBUTE(2, 3); |
| 129 | extern void plog_warn (struct zlog *, const char *format, ...) |
| 130 | PRINTF_ATTRIBUTE(2, 3); |
| 131 | extern void plog_info (struct zlog *, const char *format, ...) |
| 132 | PRINTF_ATTRIBUTE(2, 3); |
| 133 | extern void plog_notice (struct zlog *, const char *format, ...) |
| 134 | PRINTF_ATTRIBUTE(2, 3); |
| 135 | extern void plog_debug (struct zlog *, const char *format, ...) |
| 136 | PRINTF_ATTRIBUTE(2, 3); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 137 | |
David Lamparter | 615f9f1 | 2013-11-18 23:52:02 +0100 | [diff] [blame] | 138 | extern void zlog_thread_info (int log_level); |
| 139 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 140 | /* Set logging level for the given destination. If the log_level |
| 141 | argument is ZLOG_DISABLED, then the destination is disabled. |
| 142 | This function should not be used for file logging (use zlog_set_file |
| 143 | or zlog_reset_file instead). */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 144 | 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] | 145 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 146 | /* Set logging to the given filename at the specified level. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 147 | 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] | 148 | /* Disable file logging. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 149 | extern int zlog_reset_file (struct zlog *zl); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 150 | |
| 151 | /* Rotate log. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 152 | extern int zlog_rotate (struct zlog *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 153 | |
Leonid Rosenboim | 1e0ce7c | 2012-12-07 21:31:07 +0000 | [diff] [blame] | 154 | /* For hackey message lookup and check */ |
| 155 | #define LOOKUP_DEF(x, y, def) mes_lookup(x, x ## _max, y, def, #x) |
| 156 | #define LOOKUP(x, y) LOOKUP_DEF(x, y, "(no item found)") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 157 | |
Stephen Hemminger | 1423c80 | 2008-08-14 17:59:25 +0100 | [diff] [blame] | 158 | extern const char *lookup (const struct message *, int); |
Stephen Hemminger | 8e4c093 | 2009-05-15 09:47:34 -0700 | [diff] [blame] | 159 | extern const char *mes_lookup (const struct message *meslist, |
Paul Jakma | 11486b5 | 2008-02-28 23:26:02 +0000 | [diff] [blame] | 160 | int max, int index, |
Dmitrij Tejblum | 51abba5 | 2011-09-21 17:41:41 +0400 | [diff] [blame] | 161 | const char *no_item, const char *mesname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 162 | |
| 163 | extern const char *zlog_priority[]; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 164 | extern const char *zlog_proto_names[]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 165 | |
ajs | ca35976 | 2004-11-19 23:40:16 +0000 | [diff] [blame] | 166 | /* Safe version of strerror -- never returns NULL. */ |
| 167 | extern const char *safe_strerror(int errnum); |
| 168 | |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 169 | /* To be called when a fatal signal is caught. */ |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 170 | extern void zlog_signal(int signo, const char *action |
| 171 | #ifdef SA_SIGINFO |
| 172 | , siginfo_t *siginfo, void *program_counter |
| 173 | #endif |
| 174 | ); |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 175 | |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 176 | /* Log a backtrace. */ |
| 177 | extern void zlog_backtrace(int priority); |
| 178 | |
| 179 | /* Log a backtrace, but in an async-signal-safe way. Should not be |
| 180 | called unless the program is about to exit or abort, since it messes |
ajs | 239c26f | 2005-01-17 15:22:28 +0000 | [diff] [blame] | 181 | up the state of zlog file pointers. If program_counter is non-NULL, |
| 182 | that is logged in addition to the current backtrace. */ |
| 183 | extern void zlog_backtrace_sigsafe(int priority, void *program_counter); |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 184 | |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 185 | /* Puts a current timestamp in buf and returns the number of characters |
| 186 | written (not including the terminating NUL). The purpose of |
| 187 | this function is to avoid calls to localtime appearing all over the code. |
| 188 | It caches the most recent localtime result and can therefore |
| 189 | avoid multiple calls within the same second. If buflen is too small, |
| 190 | *buf will be set to '\0', and 0 will be returned. */ |
Christian Franke | 880e31c | 2016-05-03 19:59:40 +0200 | [diff] [blame] | 191 | #define QUAGGA_TIMESTAMP_LEN 40 |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 192 | extern size_t quagga_timestamp(int timestamp_precision /* # subsecond digits */, |
| 193 | char *buf, size_t buflen); |
| 194 | |
Daniel Walton | c8af680 | 2015-10-21 06:56:44 -0700 | [diff] [blame] | 195 | extern void zlog_hexdump(void *mem, unsigned int len); |
| 196 | |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 197 | /* structure useful for avoiding repeated rendering of the same timestamp */ |
| 198 | struct timestamp_control { |
| 199 | size_t len; /* length of rendered timestamp */ |
| 200 | int precision; /* configuration parameter */ |
| 201 | int already_rendered; /* should be initialized to 0 */ |
Christian Franke | 880e31c | 2016-05-03 19:59:40 +0200 | [diff] [blame] | 202 | char buf[QUAGGA_TIMESTAMP_LEN]; /* will contain the rendered timestamp */ |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 203 | }; |
| 204 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 205 | /* Defines for use in command construction: */ |
| 206 | |
| 207 | #define LOG_LEVELS "(emergencies|alerts|critical|errors|warnings|notifications|informational|debugging)" |
| 208 | |
| 209 | #define LOG_LEVEL_DESC \ |
| 210 | "System is unusable\n" \ |
| 211 | "Immediate action needed\n" \ |
| 212 | "Critical conditions\n" \ |
| 213 | "Error conditions\n" \ |
| 214 | "Warning conditions\n" \ |
| 215 | "Normal but significant conditions\n" \ |
| 216 | "Informational messages\n" \ |
| 217 | "Debugging messages\n" |
| 218 | |
| 219 | #define LOG_FACILITIES "(kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7)" |
| 220 | |
| 221 | #define LOG_FACILITY_DESC \ |
| 222 | "Kernel\n" \ |
| 223 | "User process\n" \ |
| 224 | "Mail system\n" \ |
| 225 | "System daemons\n" \ |
| 226 | "Authorization system\n" \ |
| 227 | "Syslog itself\n" \ |
| 228 | "Line printer system\n" \ |
| 229 | "USENET news\n" \ |
| 230 | "Unix-to-Unix copy system\n" \ |
| 231 | "Cron/at facility\n" \ |
| 232 | "Local use\n" \ |
| 233 | "Local use\n" \ |
| 234 | "Local use\n" \ |
| 235 | "Local use\n" \ |
| 236 | "Local use\n" \ |
| 237 | "Local use\n" \ |
| 238 | "Local use\n" \ |
| 239 | "Local use\n" |
| 240 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 241 | #endif /* _ZEBRA_LOG_H */ |