blob: 205b7c0d379750084cdbcf8ec3f42794195e3422 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Logging of zebra
2 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
24#include "log.h"
25#include "memory.h"
26#include "command.h"
27
28struct zlog *zlog_default = NULL;
29
30const char *zlog_proto_names[] =
31{
32 "NONE",
33 "DEFAULT",
34 "ZEBRA",
35 "RIP",
36 "BGP",
37 "OSPF",
38 "RIPNG",
39 "OSPF6",
jardin9e867fe2003-12-23 08:56:18 +000040 "ISIS",
paul718e3742002-12-13 20:15:29 +000041 "MASC",
42 NULL,
43};
44
45const char *zlog_priority[] =
46{
47 "emergencies",
48 "alerts",
49 "critical",
50 "errors",
51 "warnings",
52 "notifications",
53 "informational",
54 "debugging",
55 NULL,
56};
57
58
59
60/* For time string format. */
61#define TIME_BUF 27
62
63/* Utility routine for current time printing. */
64static void
65time_print (FILE *fp)
66{
67 int ret;
68 char buf [TIME_BUF];
69 time_t clock;
70 struct tm *tm;
71
72 time (&clock);
73 tm = localtime (&clock);
74
75 ret = strftime (buf, TIME_BUF, "%Y/%m/%d %H:%M:%S", tm);
76 if (ret == 0) {
77 zlog_warn ("strftime error");
78 }
79
80 fprintf (fp, "%s ", buf);
81}
82
83/* va_list version of zlog. */
ajsd246bd92004-11-23 17:35:08 +000084static void
85vzlog (struct zlog *zl, int priority, const char *format, va_list args)
paul718e3742002-12-13 20:15:29 +000086{
87 /* If zlog is not specified, use default one. */
88 if (zl == NULL)
89 zl = zlog_default;
90
91 /* When zlog_default is also NULL, use stderr for logging. */
92 if (zl == NULL)
93 {
94 time_print (stderr);
95 fprintf (stderr, "%s: ", "unknown");
ajsd246bd92004-11-23 17:35:08 +000096 vfprintf (stderr, format, args);
paul718e3742002-12-13 20:15:29 +000097 fprintf (stderr, "\n");
98 fflush (stderr);
99
100 /* In this case we return at here. */
101 return;
102 }
103
104 /* only log this information if it has not been masked out */
105 if ( priority > zl->maskpri )
106 return ;
107
108 /* Syslog output */
109 if (zl->flags & ZLOG_SYSLOG)
ajsd246bd92004-11-23 17:35:08 +0000110 {
111 va_list ac;
112 va_copy(ac, args);
113 vsyslog (priority|zlog_default->facility, format, ac);
114 va_end(ac);
115 }
paul718e3742002-12-13 20:15:29 +0000116
117 /* File output. */
118 if (zl->flags & ZLOG_FILE)
119 {
ajsd246bd92004-11-23 17:35:08 +0000120 va_list ac;
paul718e3742002-12-13 20:15:29 +0000121 time_print (zl->fp);
hassob04c6992004-10-04 19:10:31 +0000122 if (zl->record_priority)
123 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000124 fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000125 va_copy(ac, args);
126 vfprintf (zl->fp, format, ac);
127 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000128 fprintf (zl->fp, "\n");
129 fflush (zl->fp);
130 }
131
132 /* stdout output. */
133 if (zl->flags & ZLOG_STDOUT)
134 {
ajsd246bd92004-11-23 17:35:08 +0000135 va_list ac;
paul718e3742002-12-13 20:15:29 +0000136 time_print (stdout);
hassob04c6992004-10-04 19:10:31 +0000137 if (zl->record_priority)
138 fprintf (stdout, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000139 fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000140 va_copy(ac, args);
141 vfprintf (stdout, format, ac);
142 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000143 fprintf (stdout, "\n");
144 fflush (stdout);
145 }
146
147 /* stderr output. */
148 if (zl->flags & ZLOG_STDERR)
149 {
ajsd246bd92004-11-23 17:35:08 +0000150 va_list ac;
paul718e3742002-12-13 20:15:29 +0000151 time_print (stderr);
hassob04c6992004-10-04 19:10:31 +0000152 if (zl->record_priority)
153 fprintf (stderr, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000154 fprintf (stderr, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000155 va_copy(ac, args);
156 vfprintf (stderr, format, ac);
157 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000158 fprintf (stderr, "\n");
159 fflush (stderr);
160 }
161
162 /* Terminal monitor. */
ajsd246bd92004-11-23 17:35:08 +0000163 vty_log (zlog_proto_names[zl->protocol], format, args);
paul718e3742002-12-13 20:15:29 +0000164}
165
166void
167zlog (struct zlog *zl, int priority, const char *format, ...)
168{
ajsd246bd92004-11-23 17:35:08 +0000169 va_list args;
paul718e3742002-12-13 20:15:29 +0000170
ajsd246bd92004-11-23 17:35:08 +0000171 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000172 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000173 va_end (args);
paul718e3742002-12-13 20:15:29 +0000174}
175
ajsd246bd92004-11-23 17:35:08 +0000176#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
177void \
178FUNCNAME(const char *format, ...) \
179{ \
180 va_list args; \
181 va_start(args, format); \
182 vzlog (NULL, PRIORITY, format, args); \
183 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000184}
185
ajsd246bd92004-11-23 17:35:08 +0000186ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000187
ajsd246bd92004-11-23 17:35:08 +0000188ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000189
ajsd246bd92004-11-23 17:35:08 +0000190ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000191
ajsd246bd92004-11-23 17:35:08 +0000192ZLOG_FUNC(zlog_notice, LOG_NOTICE)
193
194ZLOG_FUNC(zlog_debug, LOG_DEBUG)
195
196#undef ZLOG_FUNC
197
198#define PLOG_FUNC(FUNCNAME,PRIORITY) \
199void \
200FUNCNAME(struct zlog *zl, const char *format, ...) \
201{ \
202 va_list args; \
203 va_start(args, format); \
204 vzlog (zl, PRIORITY, format, args); \
205 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000206}
207
ajsd246bd92004-11-23 17:35:08 +0000208PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000209
ajsd246bd92004-11-23 17:35:08 +0000210PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000211
ajsd246bd92004-11-23 17:35:08 +0000212PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000213
ajsd246bd92004-11-23 17:35:08 +0000214PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000215
ajsd246bd92004-11-23 17:35:08 +0000216PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000217
ajsd246bd92004-11-23 17:35:08 +0000218#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000219
220
221/* Open log stream */
222struct zlog *
223openzlog (const char *progname, int flags, zlog_proto_t protocol,
224 int syslog_flags, int syslog_facility)
225{
226 struct zlog *zl;
227
228 zl = XMALLOC(MTYPE_ZLOG, sizeof (struct zlog));
229 memset (zl, 0, sizeof (struct zlog));
230
231 zl->ident = progname;
232 zl->flags = flags;
233 zl->protocol = protocol;
234 zl->facility = syslog_facility;
235 zl->maskpri = LOG_DEBUG;
236 zl->record_priority = 0;
237
238 openlog (progname, syslog_flags, zl->facility);
239
240 return zl;
241}
242
243void
244closezlog (struct zlog *zl)
245{
246 closelog();
247 fclose (zl->fp);
248
249 XFREE (MTYPE_ZLOG, zl);
250}
251
252/* Called from command.c. */
253void
254zlog_set_flag (struct zlog *zl, int flags)
255{
256 if (zl == NULL)
257 zl = zlog_default;
258
259 zl->flags |= flags;
260}
261
262void
263zlog_reset_flag (struct zlog *zl, int flags)
264{
265 if (zl == NULL)
266 zl = zlog_default;
267
268 zl->flags &= ~flags;
269}
270
271int
ajsd246bd92004-11-23 17:35:08 +0000272zlog_set_file (struct zlog *zl, const char *filename)
paul718e3742002-12-13 20:15:29 +0000273{
274 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000275 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000276
277 /* There is opend file. */
278 zlog_reset_file (zl);
279
280 /* Set default zl. */
281 if (zl == NULL)
282 zl = zlog_default;
283
284 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000285 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000286 fp = fopen (filename, "a");
287 if (fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000288 {
289 umask(oldumask);
290 return 0;
291 }
292 umask(oldumask);
paul718e3742002-12-13 20:15:29 +0000293
294 /* Set flags. */
295 zl->filename = strdup (filename);
296 zl->flags |= ZLOG_FILE;
297 zl->fp = fp;
298
299 return 1;
300}
301
302/* Reset opend file. */
303int
304zlog_reset_file (struct zlog *zl)
305{
306 if (zl == NULL)
307 zl = zlog_default;
308
309 zl->flags &= ~ZLOG_FILE;
310
311 if (zl->fp)
312 fclose (zl->fp);
313 zl->fp = NULL;
314
315 if (zl->filename)
316 free (zl->filename);
317 zl->filename = NULL;
318
319 return 1;
320}
321
322/* Reopen log file. */
323int
324zlog_rotate (struct zlog *zl)
325{
326 FILE *fp;
327
328 if (zl == NULL)
329 zl = zlog_default;
330
331 if (zl->fp)
332 fclose (zl->fp);
333 zl->fp = NULL;
334
335 if (zl->filename)
336 {
gdtaa593d52003-12-22 20:15:53 +0000337 mode_t oldumask;
338
339 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000340 fp = fopen (zl->filename, "a");
341 if (fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000342 {
343 umask(oldumask);
344 return -1;
345 }
346 umask(oldumask);
paul718e3742002-12-13 20:15:29 +0000347 zl->fp = fp;
348 }
349
350 return 1;
351}
352
paul718e3742002-12-13 20:15:29 +0000353/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000354const char *
paul718e3742002-12-13 20:15:29 +0000355lookup (struct message *mes, int key)
356{
357 struct message *pnt;
358
359 for (pnt = mes; pnt->key != 0; pnt++)
360 if (pnt->key == key)
361 return pnt->str;
362
363 return "";
364}
365
366/* Very old hacky version of message lookup function. Still partly
hassob04c6992004-10-04 19:10:31 +0000367 used in bgpd and ospfd. FIXME Seems that it's not used any more. */
hasso8c328f12004-10-05 21:01:23 +0000368const char *
paul718e3742002-12-13 20:15:29 +0000369mes_lookup (struct message *meslist, int max, int index)
370{
371 if (index < 0 || index >= max)
372 {
373 zlog_err ("message index out of bound: %d", max);
374 return NULL;
375 }
376 return meslist[index].str;
377}
ajsca359762004-11-19 23:40:16 +0000378
379/* Wrapper around strerror to handle case where it returns NULL. */
380const char *
381safe_strerror(int errnum)
382{
383 const char *s = strerror(errnum);
384 return (s != NULL) ? s : "Unknown error";
385}