blob: 9d5668062e8c993269fd430b92d93f02f8e2b3b7 [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
ajs59a06a92004-11-23 18:19:14 +0000166static char *
167str_append(char *dst, int len, const char *src)
168{
169 while ((len-- > 0) && *src)
170 *dst++ = *src++;
171 return dst;
172}
173
174static char *
175num_append(char *s, int len, u_long x)
176{
177 char buf[30];
178 char *t = &buf[29];
179
180 *t = '\0';
181 while (x && (t > buf))
182 {
183 *--t = '0'+(x % 10);
184 x /= 10;
185 }
186 return str_append(s,len,t);
187}
188
189/* Note: the goal here is to use only async-signal-safe functions. */
190void
191zlog_signal(int signo, const char *action)
192{
193 time_t now;
194 char buf[sizeof("DEFAULT: Received signal S at T; aborting...")+60];
195 char *s = buf;
196
197#define LOC s,buf+sizeof(buf)-s
198
199 time(&now);
200 if (zlog_default)
201 {
202 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
203 *s++ = ':';
204 *s++ = ' ';
205 }
206 s = str_append(LOC,"Received signal ");
207 s = num_append(LOC,signo);
208 s = str_append(LOC," at ");
209 s = num_append(LOC,now);
210 s = str_append(LOC,"; ");
211 s = str_append(LOC,action);
212 *s++ = '\n';
213
214#define DUMP(FP) write(fileno(FP),buf,s-buf);
215 if (!zlog_default)
216 DUMP(stderr)
217 else
218 {
219 if ((zlog_default->flags & ZLOG_FILE) && zlog_default->fp)
220 DUMP(zlog_default->fp)
221 if (zlog_default->flags & ZLOG_STDOUT)
222 DUMP(stdout)
223 if (zlog_default->flags & ZLOG_STDERR)
224 DUMP(stderr)
225 /* Is there a signal-safe way to send a syslog message? */
226 }
227#undef DUMP
228
229 /* Now try for a backtrace. */
230#ifdef HAVE_GLIBC_BACKTRACE
231 {
232 void *array[20];
ajs101ec702004-11-24 18:05:15 +0000233 int size;
ajs59a06a92004-11-23 18:19:14 +0000234
ajsad4d9742004-11-24 18:20:30 +0000235 if ((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0)
236 return;
ajs59a06a92004-11-23 18:19:14 +0000237 s = buf;
238 s = str_append(LOC,"Backtrace for ");
239 s = num_append(LOC,size);
240 s = str_append(LOC," stack frames:\n");
241
242#define DUMP(FP) { \
243 write(fileno(FP),buf,s-buf); \
244 backtrace_symbols_fd(array, size, fileno(FP)); \
245}
246
247 if (!zlog_default)
248 DUMP(stderr)
249 else
250 {
251 if ((zlog_default->flags & ZLOG_FILE) && zlog_default->fp)
252 DUMP(zlog_default->fp)
253 if (zlog_default->flags & ZLOG_STDOUT)
254 DUMP(stdout)
255 if (zlog_default->flags & ZLOG_STDERR)
256 DUMP(stderr)
257 /* Is there a signal-safe way to send a syslog message? */
258 }
259#undef DUMP
260 }
261#endif /* HAVE_GLIBC_BACKTRACE */
262#undef LOC
263}
264
paul718e3742002-12-13 20:15:29 +0000265void
266zlog (struct zlog *zl, int priority, const char *format, ...)
267{
ajsd246bd92004-11-23 17:35:08 +0000268 va_list args;
paul718e3742002-12-13 20:15:29 +0000269
ajsd246bd92004-11-23 17:35:08 +0000270 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000271 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000272 va_end (args);
paul718e3742002-12-13 20:15:29 +0000273}
274
ajsd246bd92004-11-23 17:35:08 +0000275#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
276void \
277FUNCNAME(const char *format, ...) \
278{ \
279 va_list args; \
280 va_start(args, format); \
281 vzlog (NULL, PRIORITY, format, args); \
282 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000283}
284
ajsd246bd92004-11-23 17:35:08 +0000285ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000286
ajsd246bd92004-11-23 17:35:08 +0000287ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000288
ajsd246bd92004-11-23 17:35:08 +0000289ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000290
ajsd246bd92004-11-23 17:35:08 +0000291ZLOG_FUNC(zlog_notice, LOG_NOTICE)
292
293ZLOG_FUNC(zlog_debug, LOG_DEBUG)
294
295#undef ZLOG_FUNC
296
297#define PLOG_FUNC(FUNCNAME,PRIORITY) \
298void \
299FUNCNAME(struct zlog *zl, const char *format, ...) \
300{ \
301 va_list args; \
302 va_start(args, format); \
303 vzlog (zl, PRIORITY, format, args); \
304 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000305}
306
ajsd246bd92004-11-23 17:35:08 +0000307PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000308
ajsd246bd92004-11-23 17:35:08 +0000309PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000310
ajsd246bd92004-11-23 17:35:08 +0000311PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000312
ajsd246bd92004-11-23 17:35:08 +0000313PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000314
ajsd246bd92004-11-23 17:35:08 +0000315PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000316
ajsd246bd92004-11-23 17:35:08 +0000317#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000318
ajscee3df12004-11-24 17:14:49 +0000319void
320_zlog_assert_failed (const char *assertion, const char *file,
321 unsigned int line, const char *function)
322{
323 zlog_err("Assertion `%s' failed in file %s, line %u, function %s",
324 assertion,file,line,(function ? function : "?"));
325 abort();
326}
327
paul718e3742002-12-13 20:15:29 +0000328
329/* Open log stream */
330struct zlog *
331openzlog (const char *progname, int flags, zlog_proto_t protocol,
332 int syslog_flags, int syslog_facility)
333{
334 struct zlog *zl;
335
336 zl = XMALLOC(MTYPE_ZLOG, sizeof (struct zlog));
337 memset (zl, 0, sizeof (struct zlog));
338
339 zl->ident = progname;
340 zl->flags = flags;
341 zl->protocol = protocol;
342 zl->facility = syslog_facility;
343 zl->maskpri = LOG_DEBUG;
344 zl->record_priority = 0;
345
346 openlog (progname, syslog_flags, zl->facility);
347
348 return zl;
349}
350
351void
352closezlog (struct zlog *zl)
353{
354 closelog();
355 fclose (zl->fp);
356
357 XFREE (MTYPE_ZLOG, zl);
358}
359
360/* Called from command.c. */
361void
362zlog_set_flag (struct zlog *zl, int flags)
363{
364 if (zl == NULL)
365 zl = zlog_default;
366
367 zl->flags |= flags;
368}
369
370void
371zlog_reset_flag (struct zlog *zl, int flags)
372{
373 if (zl == NULL)
374 zl = zlog_default;
375
376 zl->flags &= ~flags;
377}
378
379int
ajsd246bd92004-11-23 17:35:08 +0000380zlog_set_file (struct zlog *zl, const char *filename)
paul718e3742002-12-13 20:15:29 +0000381{
382 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000383 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000384
385 /* There is opend file. */
386 zlog_reset_file (zl);
387
388 /* Set default zl. */
389 if (zl == NULL)
390 zl = zlog_default;
391
392 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000393 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000394 fp = fopen (filename, "a");
395 if (fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000396 {
397 umask(oldumask);
398 return 0;
399 }
400 umask(oldumask);
paul718e3742002-12-13 20:15:29 +0000401
402 /* Set flags. */
403 zl->filename = strdup (filename);
404 zl->flags |= ZLOG_FILE;
405 zl->fp = fp;
406
407 return 1;
408}
409
410/* Reset opend file. */
411int
412zlog_reset_file (struct zlog *zl)
413{
414 if (zl == NULL)
415 zl = zlog_default;
416
417 zl->flags &= ~ZLOG_FILE;
418
419 if (zl->fp)
420 fclose (zl->fp);
421 zl->fp = NULL;
422
423 if (zl->filename)
424 free (zl->filename);
425 zl->filename = NULL;
426
427 return 1;
428}
429
430/* Reopen log file. */
431int
432zlog_rotate (struct zlog *zl)
433{
434 FILE *fp;
435
436 if (zl == NULL)
437 zl = zlog_default;
438
439 if (zl->fp)
440 fclose (zl->fp);
441 zl->fp = NULL;
442
443 if (zl->filename)
444 {
gdtaa593d52003-12-22 20:15:53 +0000445 mode_t oldumask;
446
447 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000448 fp = fopen (zl->filename, "a");
449 if (fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000450 {
451 umask(oldumask);
452 return -1;
453 }
454 umask(oldumask);
paul718e3742002-12-13 20:15:29 +0000455 zl->fp = fp;
456 }
457
458 return 1;
459}
460
paul718e3742002-12-13 20:15:29 +0000461/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000462const char *
paul718e3742002-12-13 20:15:29 +0000463lookup (struct message *mes, int key)
464{
465 struct message *pnt;
466
467 for (pnt = mes; pnt->key != 0; pnt++)
468 if (pnt->key == key)
469 return pnt->str;
470
471 return "";
472}
473
474/* Very old hacky version of message lookup function. Still partly
hassob04c6992004-10-04 19:10:31 +0000475 used in bgpd and ospfd. FIXME Seems that it's not used any more. */
hasso8c328f12004-10-05 21:01:23 +0000476const char *
paul718e3742002-12-13 20:15:29 +0000477mes_lookup (struct message *meslist, int max, int index)
478{
479 if (index < 0 || index >= max)
480 {
481 zlog_err ("message index out of bound: %d", max);
482 return NULL;
483 }
484 return meslist[index].str;
485}
ajsca359762004-11-19 23:40:16 +0000486
487/* Wrapper around strerror to handle case where it returns NULL. */
488const char *
489safe_strerror(int errnum)
490{
491 const char *s = strerror(errnum);
492 return (s != NULL) ? s : "Unknown error";
493}