]>
Commit | Line | Data |
---|---|---|
274a4a44 | 1 | /* |
274a4a44 | 2 | * Zebra logging funcions. |
718e3744 | 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 | * | |
896014f4 DL |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 20 | */ |
21 | ||
22 | #ifndef _ZEBRA_LOG_H | |
23 | #define _ZEBRA_LOG_H | |
24 | ||
25 | #include <syslog.h> | |
5894e76d | 26 | #include <stdint.h> |
d34cb7f0 | 27 | #include <stdbool.h> |
2fb975da | 28 | #include <stdio.h> |
77d9c926 EDP |
29 | #include <stdarg.h> |
30 | #include "lib/hook.h" | |
31 | ||
5e244469 RW |
32 | #ifdef __cplusplus |
33 | extern "C" { | |
34 | #endif | |
35 | ||
77d9c926 EDP |
36 | /* Hook for external logging function */ |
37 | DECLARE_HOOK(zebra_ext_log, (int priority, const char *format, va_list args), | |
38 | (priority, format, args)); | |
718e3744 | 39 | |
5e764774 | 40 | /* Here is some guidance on logging levels to use: |
41 | * | |
42 | * LOG_DEBUG - For all messages that are enabled by optional debugging | |
43 | * features, typically preceded by "if (IS...DEBUG...)" | |
44 | * LOG_INFO - Information that may be of interest, but everything seems | |
45 | * to be working properly. | |
46 | * LOG_NOTICE - Only for message pertaining to daemon startup or shutdown. | |
47 | * LOG_WARNING - Warning conditions: unexpected events, but the daemon believes | |
48 | * it can continue to operate correctly. | |
49 | * LOG_ERR - Error situations indicating malfunctions. Probably require | |
50 | * attention. | |
51 | * | |
52 | * Note: LOG_CRIT, LOG_ALERT, and LOG_EMERG are currently not used anywhere, | |
53 | * please use LOG_ERR instead. | |
54 | */ | |
55 | ||
274a4a44 | 56 | /* If maxlvl is set to ZLOG_DISABLED, then no messages will be sent |
57 | to that logging destination. */ | |
58 | #define ZLOG_DISABLED (LOG_EMERG-1) | |
59 | ||
d62a17ae | 60 | typedef enum { |
61 | ZLOG_DEST_SYSLOG = 0, | |
62 | ZLOG_DEST_STDOUT, | |
63 | ZLOG_DEST_MONITOR, | |
64 | ZLOG_DEST_FILE | |
274a4a44 | 65 | } zlog_dest_t; |
66 | #define ZLOG_NUM_DESTS (ZLOG_DEST_FILE+1) | |
67 | ||
d34cb7f0 DL |
68 | extern bool zlog_startup_stderr; |
69 | ||
718e3744 | 70 | /* Message structure. */ |
d62a17ae | 71 | struct message { |
72 | int key; | |
73 | const char *str; | |
718e3744 | 74 | }; |
75 | ||
718e3744 | 76 | /* Open zlog function */ |
d62a17ae | 77 | extern void openzlog(const char *progname, const char *protoname, |
78 | uint16_t instance, int syslog_options, | |
79 | int syslog_facility); | |
718e3744 | 80 | |
81 | /* Close zlog function. */ | |
d62a17ae | 82 | extern void closezlog(void); |
718e3744 | 83 | |
84 | /* GCC have printf type attribute check. */ | |
85 | #ifdef __GNUC__ | |
86 | #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b))) | |
87 | #else | |
88 | #define PRINTF_ATTRIBUTE(a,b) | |
89 | #endif /* __GNUC__ */ | |
90 | ||
718e3744 | 91 | /* Handy zlog functions. */ |
d62a17ae | 92 | extern void zlog_err(const char *format, ...) PRINTF_ATTRIBUTE(1, 2); |
93 | extern void zlog_warn(const char *format, ...) PRINTF_ATTRIBUTE(1, 2); | |
94 | extern void zlog_info(const char *format, ...) PRINTF_ATTRIBUTE(1, 2); | |
95 | extern void zlog_notice(const char *format, ...) PRINTF_ATTRIBUTE(1, 2); | |
96 | extern void zlog_debug(const char *format, ...) PRINTF_ATTRIBUTE(1, 2); | |
e412d3b8 | 97 | extern void zlog(int priority, const char *format, ...) PRINTF_ATTRIBUTE(2, 3); |
718e3744 | 98 | |
7b526b61 | 99 | /* For logs which have error codes associated with them */ |
af4c2728 | 100 | #define flog_err(ferr_id, format, ...) \ |
d01b92fd | 101 | zlog_err("[EC %" PRIu32 "] " format, ferr_id, ##__VA_ARGS__) |
09c866e3 QY |
102 | #define flog_err_sys(ferr_id, format, ...) \ |
103 | flog_err(ferr_id, format, ##__VA_ARGS__) | |
2311712a | 104 | #define flog_warn(ferr_id, format, ...) \ |
d01b92fd | 105 | zlog_warn("[EC %" PRIu32 "] " format, ferr_id, ##__VA_ARGS__) |
c650e48c RW |
106 | #define flog(priority, ferr_id, format, ...) \ |
107 | zlog(priority, "[EC %" PRIu32 "] " format, ferr_id, ##__VA_ARGS__) | |
7b526b61 | 108 | |
d62a17ae | 109 | extern void zlog_thread_info(int log_level); |
d1265948 | 110 | |
274a4a44 | 111 | /* Set logging level for the given destination. If the log_level |
112 | argument is ZLOG_DISABLED, then the destination is disabled. | |
113 | This function should not be used for file logging (use zlog_set_file | |
114 | or zlog_reset_file instead). */ | |
d62a17ae | 115 | extern void zlog_set_level(zlog_dest_t, int log_level); |
718e3744 | 116 | |
274a4a44 | 117 | /* Set logging to the given filename at the specified level. */ |
d62a17ae | 118 | extern int zlog_set_file(const char *filename, int log_level); |
274a4a44 | 119 | /* Disable file logging. */ |
d62a17ae | 120 | extern int zlog_reset_file(void); |
718e3744 | 121 | |
122 | /* Rotate log. */ | |
d62a17ae | 123 | extern int zlog_rotate(void); |
718e3744 | 124 | |
d62a17ae | 125 | const char *lookup_msg(const struct message *mz, int kz, const char *nf); |
718e3744 | 126 | |
ca359769 | 127 | /* Safe version of strerror -- never returns NULL. */ |
128 | extern const char *safe_strerror(int errnum); | |
129 | ||
59a06a91 | 130 | /* To be called when a fatal signal is caught. */ |
31364274 | 131 | extern void zlog_signal(int signo, const char *action |
132 | #ifdef SA_SIGINFO | |
d62a17ae | 133 | , |
134 | siginfo_t *siginfo, void *program_counter | |
31364274 | 135 | #endif |
9d303b37 | 136 | ); |
59a06a91 | 137 | |
063ee52a | 138 | /* Log a backtrace. */ |
139 | extern void zlog_backtrace(int priority); | |
140 | ||
141 | /* Log a backtrace, but in an async-signal-safe way. Should not be | |
142 | called unless the program is about to exit or abort, since it messes | |
239c26fd | 143 | up the state of zlog file pointers. If program_counter is non-NULL, |
144 | that is logged in addition to the current backtrace. */ | |
145 | extern void zlog_backtrace_sigsafe(int priority, void *program_counter); | |
063ee52a | 146 | |
1ed72e0b AS |
147 | /* Puts a current timestamp in buf and returns the number of characters |
148 | written (not including the terminating NUL). The purpose of | |
149 | this function is to avoid calls to localtime appearing all over the code. | |
150 | It caches the most recent localtime result and can therefore | |
151 | avoid multiple calls within the same second. If buflen is too small, | |
152 | *buf will be set to '\0', and 0 will be returned. */ | |
ae616d60 | 153 | #define QUAGGA_TIMESTAMP_LEN 40 |
1ed72e0b AS |
154 | extern size_t quagga_timestamp(int timestamp_precision /* # subsecond digits */, |
155 | char *buf, size_t buflen); | |
156 | ||
a9b5cbe5 | 157 | extern void zlog_hexdump(const void *mem, unsigned int len); |
d62a17ae | 158 | extern const char *zlog_sanitize(char *buf, size_t bufsz, const void *in, |
159 | size_t inlen); | |
99a6c6cd | 160 | |
65efcfce | 161 | |
d62a17ae | 162 | extern int vzlog_test(int priority); |
65efcfce | 163 | |
1ed72e0b AS |
164 | /* structure useful for avoiding repeated rendering of the same timestamp */ |
165 | struct timestamp_control { | |
d62a17ae | 166 | size_t len; /* length of rendered timestamp */ |
167 | int precision; /* configuration parameter */ | |
168 | int already_rendered; /* should be initialized to 0 */ | |
169 | char buf[QUAGGA_TIMESTAMP_LEN]; /* will contain the rendered timestamp | |
9d303b37 | 170 | */ |
1ed72e0b AS |
171 | }; |
172 | ||
274a4a44 | 173 | /* Defines for use in command construction: */ |
174 | ||
d62a17ae | 175 | #define LOG_LEVEL_DESC \ |
176 | "System is unusable\n" \ | |
177 | "Immediate action needed\n" \ | |
178 | "Critical conditions\n" \ | |
179 | "Error conditions\n" \ | |
180 | "Warning conditions\n" \ | |
181 | "Normal but significant conditions\n" \ | |
182 | "Informational messages\n" \ | |
183 | "Debugging messages\n" | |
184 | ||
185 | #define LOG_FACILITY_DESC \ | |
186 | "Kernel\n" \ | |
187 | "User process\n" \ | |
188 | "Mail system\n" \ | |
189 | "System daemons\n" \ | |
190 | "Authorization system\n" \ | |
191 | "Syslog itself\n" \ | |
192 | "Line printer system\n" \ | |
193 | "USENET news\n" \ | |
194 | "Unix-to-Unix copy system\n" \ | |
195 | "Cron/at facility\n" \ | |
196 | "Local use\n" \ | |
197 | "Local use\n" \ | |
198 | "Local use\n" \ | |
199 | "Local use\n" \ | |
200 | "Local use\n" \ | |
201 | "Local use\n" \ | |
202 | "Local use\n" \ | |
203 | "Local use\n" | |
274a4a44 | 204 | |
5e244469 RW |
205 | #ifdef __cplusplus |
206 | } | |
207 | #endif | |
208 | ||
718e3744 | 209 | #endif /* _ZEBRA_LOG_H */ |