1 /*
2  * Zebra logging funcions.
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 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
20  */
21 
22 #ifndef _ZEBRA_LOG_H
23 #define _ZEBRA_LOG_H
24 
25 #include "zassert.h"
26 
27 #include <syslog.h>
28 #include <stdint.h>
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <stdarg.h>
32 
33 #include "lib/hook.h"
34 #include "lib/zlog.h"
35 #include "lib/zlog_targets.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /* Here is some guidance on logging levels to use:
42  *
43  * LOG_DEBUG	- For all messages that are enabled by optional debugging
44  *		  features, typically preceded by "if (IS...DEBUG...)"
45  * LOG_INFO	- Information that may be of interest, but everything seems
46  *		  to be working properly.
47  * LOG_NOTICE	- Only for message pertaining to daemon startup or shutdown.
48  * LOG_WARNING	- Warning conditions: unexpected events, but the daemon believes
49  *		  it can continue to operate correctly.
50  * LOG_ERR	- Error situations indicating malfunctions.  Probably require
51  *		  attention.
52  *
53  * Note: LOG_CRIT, LOG_ALERT, and LOG_EMERG are currently not used anywhere,
54  * please use LOG_ERR instead.
55  */
56 
57 extern void zlog_rotate(void);
58 
59 /* Message structure. */
60 struct message {
61 	int key;
62 	const char *str;
63 };
64 
65 /* For logs which have error codes associated with them */
66 #define flog_err(ferr_id, format, ...)                                        \
67 	zlog_err("[EC %u] " format, ferr_id, ##__VA_ARGS__)
68 #define flog_err_sys(ferr_id, format, ...)                                     \
69 	flog_err(ferr_id, format, ##__VA_ARGS__)
70 #define flog_warn(ferr_id, format, ...)                                        \
71 	zlog_warn("[EC %u] " format, ferr_id, ##__VA_ARGS__)
72 #define flog(priority, ferr_id, format, ...)                                   \
73 	zlog(priority, "[EC %u] " format, ferr_id, ##__VA_ARGS__)
74 
75 extern void zlog_thread_info(int log_level);
76 
77 #define ZLOG_FILTERS_MAX 100      /* Max # of filters at once */
78 #define ZLOG_FILTER_LENGTH_MAX 80 /* 80 character filter limit */
79 
80 struct zlog_cfg_filterfile {
81 	struct zlog_cfg_file parent;
82 };
83 
84 extern void zlog_filterfile_init(struct zlog_cfg_filterfile *zcf);
85 extern void zlog_filterfile_fini(struct zlog_cfg_filterfile *zcf);
86 
87 /* Add/Del/Dump log filters */
88 extern void zlog_filter_clear(void);
89 extern int zlog_filter_add(const char *filter);
90 extern int zlog_filter_del(const char *filter);
91 extern int zlog_filter_dump(char *buf, size_t max_size);
92 
93 const char *lookup_msg(const struct message *mz, int kz, const char *nf);
94 
95 /* Safe version of strerror -- never returns NULL. */
96 extern const char *safe_strerror(int errnum);
97 
98 /* To be called when a fatal signal is caught. */
99 extern void zlog_signal(int signo, const char *action, void *siginfo,
100 			void *program_counter);
101 
102 /* Log a backtrace. */
103 extern void zlog_backtrace(int priority);
104 
105 /* Log a backtrace, but in an async-signal-safe way.  Should not be
106    called unless the program is about to exit or abort, since it messes
107    up the state of zlog file pointers.  If program_counter is non-NULL,
108    that is logged in addition to the current backtrace. */
109 extern void zlog_backtrace_sigsafe(int priority, void *program_counter);
110 
111 /* Puts a current timestamp in buf and returns the number of characters
112    written (not including the terminating NUL).  The purpose of
113    this function is to avoid calls to localtime appearing all over the code.
114    It caches the most recent localtime result and can therefore
115    avoid multiple calls within the same second.  If buflen is too small,
116    *buf will be set to '\0', and 0 will be returned. */
117 #define QUAGGA_TIMESTAMP_LEN 40
118 extern size_t quagga_timestamp(int timestamp_precision /* # subsecond digits */,
119 			       char *buf, size_t buflen);
120 
121 extern void zlog_hexdump(const void *mem, size_t len);
122 extern const char *zlog_sanitize(char *buf, size_t bufsz, const void *in,
123 				 size_t inlen);
124 
125 /* Note: whenever a new route-type or zserv-command is added the
126  * corresponding {command,route}_types[] table in lib/log.c MUST be
127  * updated! */
128 
129 /* Map a route type to a string.  For example, ZEBRA_ROUTE_RIPNG -> "ripng". */
130 extern const char *zebra_route_string(unsigned int route_type);
131 /* Map a route type to a char.  For example, ZEBRA_ROUTE_RIPNG -> 'R'. */
132 extern char zebra_route_char(unsigned int route_type);
133 /* Map a zserv command type to the same string,
134  * e.g. ZEBRA_INTERFACE_ADD -> "ZEBRA_INTERFACE_ADD" */
135 /* Map a protocol name to its number. e.g. ZEBRA_ROUTE_BGP->9*/
136 extern int proto_name2num(const char *s);
137 /* Map redistribute X argument to protocol number.
138  * unlike proto_name2num, this accepts shorthands and takes
139  * an AFI value to restrict input */
140 extern int proto_redistnum(int afi, const char *s);
141 
142 extern const char *zserv_command_string(unsigned int command);
143 
144 
145 /* structure useful for avoiding repeated rendering of the same timestamp */
146 struct timestamp_control {
147 	size_t len;			/* length of rendered timestamp */
148 	int precision;			/* configuration parameter */
149 	int already_rendered;		/* should be initialized to 0 */
150 	char buf[QUAGGA_TIMESTAMP_LEN]; /* will contain the rendered timestamp
151 					   */
152 };
153 
154 /* Defines for use in command construction: */
155 
156 #define LOG_LEVEL_DESC                                                         \
157 	"System is unusable\n"                                                 \
158 	"Immediate action needed\n"                                            \
159 	"Critical conditions\n"                                                \
160 	"Error conditions\n"                                                   \
161 	"Warning conditions\n"                                                 \
162 	"Normal but significant conditions\n"                                  \
163 	"Informational messages\n"                                             \
164 	"Debugging messages\n"
165 
166 #define LOG_FACILITY_DESC                                                      \
167 	"Kernel\n"                                                             \
168 	"User process\n"                                                       \
169 	"Mail system\n"                                                        \
170 	"System daemons\n"                                                     \
171 	"Authorization system\n"                                               \
172 	"Syslog itself\n"                                                      \
173 	"Line printer system\n"                                                \
174 	"USENET news\n"                                                        \
175 	"Unix-to-Unix copy system\n"                                           \
176 	"Cron/at facility\n"                                                   \
177 	"Local use\n"                                                          \
178 	"Local use\n"                                                          \
179 	"Local use\n"                                                          \
180 	"Local use\n"                                                          \
181 	"Local use\n"                                                          \
182 	"Local use\n"                                                          \
183 	"Local use\n"                                                          \
184 	"Local use\n"
185 
186 #ifdef __cplusplus
187 }
188 #endif
189 
190 #endif /* _ZEBRA_LOG_H */
191