blob: 2efc30f68a0cfa7f2c0b89bb0b90d8743cbf59cc [file] [log] [blame]
ajs274a4a42004-12-07 15:39:31 +00001/*
ajs31364272005-01-18 22:18:59 +00002 * $Id: log.c,v 1.23 2005/01/18 22:18:59 ajs Exp $
ajs274a4a42004-12-07 15:39:31 +00003 *
4 * Logging of zebra
paul718e3742002-12-13 20:15:29 +00005 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
6 *
7 * This file is part of GNU Zebra.
8 *
9 * GNU Zebra is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * GNU Zebra is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with GNU Zebra; see the file COPYING. If not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 * 02111-1307, USA.
23 */
24
25#include <zebra.h>
26
27#include "log.h"
28#include "memory.h"
29#include "command.h"
ajs7d149b82004-11-28 23:00:01 +000030#ifndef SUNOS_5
31#include <sys/un.h>
32#endif
paul718e3742002-12-13 20:15:29 +000033
34struct zlog *zlog_default = NULL;
35
36const char *zlog_proto_names[] =
37{
38 "NONE",
39 "DEFAULT",
40 "ZEBRA",
41 "RIP",
42 "BGP",
43 "OSPF",
44 "RIPNG",
45 "OSPF6",
jardin9e867fe2003-12-23 08:56:18 +000046 "ISIS",
paul718e3742002-12-13 20:15:29 +000047 "MASC",
48 NULL,
49};
50
51const char *zlog_priority[] =
52{
53 "emergencies",
54 "alerts",
55 "critical",
56 "errors",
57 "warnings",
58 "notifications",
59 "informational",
60 "debugging",
61 NULL,
62};
63
64
65
66/* For time string format. */
67#define TIME_BUF 27
68
69/* Utility routine for current time printing. */
70static void
71time_print (FILE *fp)
72{
73 int ret;
74 char buf [TIME_BUF];
75 time_t clock;
76 struct tm *tm;
77
78 time (&clock);
79 tm = localtime (&clock);
80
81 ret = strftime (buf, TIME_BUF, "%Y/%m/%d %H:%M:%S", tm);
82 if (ret == 0) {
83 zlog_warn ("strftime error");
84 }
85
86 fprintf (fp, "%s ", buf);
87}
88
89/* va_list version of zlog. */
ajsd246bd92004-11-23 17:35:08 +000090static void
91vzlog (struct zlog *zl, int priority, const char *format, va_list args)
paul718e3742002-12-13 20:15:29 +000092{
93 /* If zlog is not specified, use default one. */
94 if (zl == NULL)
95 zl = zlog_default;
96
97 /* When zlog_default is also NULL, use stderr for logging. */
98 if (zl == NULL)
99 {
100 time_print (stderr);
101 fprintf (stderr, "%s: ", "unknown");
ajsd246bd92004-11-23 17:35:08 +0000102 vfprintf (stderr, format, args);
paul718e3742002-12-13 20:15:29 +0000103 fprintf (stderr, "\n");
104 fflush (stderr);
105
106 /* In this case we return at here. */
107 return;
108 }
109
paul718e3742002-12-13 20:15:29 +0000110 /* Syslog output */
ajs274a4a42004-12-07 15:39:31 +0000111 if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
ajsd246bd92004-11-23 17:35:08 +0000112 {
113 va_list ac;
114 va_copy(ac, args);
115 vsyslog (priority|zlog_default->facility, format, ac);
116 va_end(ac);
117 }
paul718e3742002-12-13 20:15:29 +0000118
119 /* File output. */
ajs274a4a42004-12-07 15:39:31 +0000120 if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
paul718e3742002-12-13 20:15:29 +0000121 {
ajsd246bd92004-11-23 17:35:08 +0000122 va_list ac;
paul718e3742002-12-13 20:15:29 +0000123 time_print (zl->fp);
hassob04c6992004-10-04 19:10:31 +0000124 if (zl->record_priority)
125 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000126 fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000127 va_copy(ac, args);
128 vfprintf (zl->fp, format, ac);
129 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000130 fprintf (zl->fp, "\n");
131 fflush (zl->fp);
132 }
133
134 /* stdout output. */
ajs274a4a42004-12-07 15:39:31 +0000135 if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
paul718e3742002-12-13 20:15:29 +0000136 {
ajsd246bd92004-11-23 17:35:08 +0000137 va_list ac;
paul718e3742002-12-13 20:15:29 +0000138 time_print (stdout);
hassob04c6992004-10-04 19:10:31 +0000139 if (zl->record_priority)
140 fprintf (stdout, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000141 fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000142 va_copy(ac, args);
143 vfprintf (stdout, format, ac);
144 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000145 fprintf (stdout, "\n");
146 fflush (stdout);
147 }
148
paul718e3742002-12-13 20:15:29 +0000149 /* Terminal monitor. */
ajs274a4a42004-12-07 15:39:31 +0000150 if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
151 vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
152 zlog_proto_names[zl->protocol], format, args);
paul718e3742002-12-13 20:15:29 +0000153}
154
ajs59a06a92004-11-23 18:19:14 +0000155static char *
156str_append(char *dst, int len, const char *src)
157{
158 while ((len-- > 0) && *src)
159 *dst++ = *src++;
160 return dst;
161}
162
163static char *
164num_append(char *s, int len, u_long x)
165{
166 char buf[30];
ajs7d149b82004-11-28 23:00:01 +0000167 char *t;
ajs59a06a92004-11-23 18:19:14 +0000168
ajs7d149b82004-11-28 23:00:01 +0000169 if (!x)
170 return str_append(s,len,"0");
171 *(t = &buf[sizeof(buf)-1]) = '\0';
ajs59a06a92004-11-23 18:19:14 +0000172 while (x && (t > buf))
173 {
174 *--t = '0'+(x % 10);
175 x /= 10;
176 }
177 return str_append(s,len,t);
178}
179
ajs31364272005-01-18 22:18:59 +0000180#if defined(SA_SIGINFO) || defined(HAVE_GLIBC_BACKTRACE)
ajs7d149b82004-11-28 23:00:01 +0000181static char *
182hex_append(char *s, int len, u_long x)
183{
184 char buf[30];
185 char *t;
186
187 if (!x)
188 return str_append(s,len,"0");
189 *(t = &buf[sizeof(buf)-1]) = '\0';
190 while (x && (t > buf))
191 {
192 u_int cc = (x % 16);
193 *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10));
194 x /= 16;
195 }
196 return str_append(s,len,t);
197}
ajs31364272005-01-18 22:18:59 +0000198#endif
ajs7d149b82004-11-28 23:00:01 +0000199
200static int syslog_fd = -1;
201
202/* Needs to be enhanced to support Solaris. */
203static int
204syslog_connect(void)
205{
206#ifdef SUNOS_5
207 return -1;
208#else
209 int fd;
210 char *s;
211 struct sockaddr_un addr;
212
213 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
214 return -1;
215 addr.sun_family = AF_UNIX;
216#ifdef _PATH_LOG
217#define SYSLOG_SOCKET_PATH _PATH_LOG
218#else
219#define SYSLOG_SOCKET_PATH "/dev/log"
220#endif
221 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
222#undef SYSLOG_SOCKET_PATH
223 *s = '\0';
224 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
225 {
226 close(fd);
227 return -1;
228 }
229 return fd;
230#endif
231}
232
233static void
234syslog_sigsafe(int priority, const char *msg, size_t msglen)
235{
236 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
237 char *s;
238
239 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
240 return;
241
242#define LOC s,buf+sizeof(buf)-s
243 s = buf;
244 s = str_append(LOC,"<");
245 s = num_append(LOC,priority);
246 s = str_append(LOC,">");
247 /* forget about the timestamp, too difficult in a signal handler */
248 s = str_append(LOC,zlog_default->ident);
249 if (zlog_default->syslog_options & LOG_PID)
250 {
251 s = str_append(LOC,"[");
252 s = num_append(LOC,getpid());
253 s = str_append(LOC,"]");
254 }
255 s = str_append(LOC,": ");
256 s = str_append(LOC,msg);
257 write(syslog_fd,buf,s-buf);
258#undef LOC
259}
260
261/* Note: the goal here is to use only async-signal-safe functions. */
ajs59a06a92004-11-23 18:19:14 +0000262void
ajs31364272005-01-18 22:18:59 +0000263zlog_signal(int signo, const char *action
264#ifdef SA_SIGINFO
265 , siginfo_t *siginfo, void *program_counter
266#endif
267 )
ajs59a06a92004-11-23 18:19:14 +0000268{
269 time_t now;
ajs40abf232005-01-12 17:27:27 +0000270 char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
ajs59a06a92004-11-23 18:19:14 +0000271 char *s = buf;
ajs7d149b82004-11-28 23:00:01 +0000272 char *msgstart = buf;
ajs59a06a92004-11-23 18:19:14 +0000273#define LOC s,buf+sizeof(buf)-s
274
275 time(&now);
276 if (zlog_default)
277 {
278 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
279 *s++ = ':';
280 *s++ = ' ';
ajs7d149b82004-11-28 23:00:01 +0000281 msgstart = s;
ajs59a06a92004-11-23 18:19:14 +0000282 }
283 s = str_append(LOC,"Received signal ");
284 s = num_append(LOC,signo);
285 s = str_append(LOC," at ");
286 s = num_append(LOC,now);
ajs31364272005-01-18 22:18:59 +0000287#ifdef SA_SIGINFO
ajs40abf232005-01-12 17:27:27 +0000288 s = str_append(LOC," (si_addr 0x");
289 s = hex_append(LOC,(u_long)(siginfo->si_addr));
290 if (program_counter)
291 {
292 s = str_append(LOC,", PC 0x");
293 s = hex_append(LOC,(u_long)program_counter);
294 }
295 s = str_append(LOC,"); ");
ajs31364272005-01-18 22:18:59 +0000296#else /* SA_SIGINFO */
297 s = str_append(LOC,"; ");
298#endif /* SA_SIGINFO */
ajs59a06a92004-11-23 18:19:14 +0000299 s = str_append(LOC,action);
ajs7d149b82004-11-28 23:00:01 +0000300 if (s < buf+sizeof(buf))
301 *s++ = '\n';
ajs59a06a92004-11-23 18:19:14 +0000302
ajs274a4a42004-12-07 15:39:31 +0000303 /* N.B. implicit priority is most severe */
ajs3378d202004-12-10 22:43:17 +0000304#define PRI LOG_ERR
ajs274a4a42004-12-07 15:39:31 +0000305
ajs59a06a92004-11-23 18:19:14 +0000306#define DUMP(FP) write(fileno(FP),buf,s-buf);
307 if (!zlog_default)
308 DUMP(stderr)
309 else
310 {
ajs274a4a42004-12-07 15:39:31 +0000311 if ((PRI <= zlog_default->maxlvl[ZLOG_DEST_FILE]) && zlog_default->fp)
ajs59a06a92004-11-23 18:19:14 +0000312 DUMP(zlog_default->fp)
ajs274a4a42004-12-07 15:39:31 +0000313 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajs59a06a92004-11-23 18:19:14 +0000314 DUMP(stdout)
ajs274a4a42004-12-07 15:39:31 +0000315 /* Remove trailing '\n' for monitor and syslog */
316 *--s = '\0';
317 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
318 vty_log_fixed(buf,s-buf);
319 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
320 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
ajs59a06a92004-11-23 18:19:14 +0000321 }
322#undef DUMP
323
ajs31364272005-01-18 22:18:59 +0000324 zlog_backtrace_sigsafe(PRI,
325#ifdef SA_SIGINFO
326 program_counter
327#else
328 NULL
329#endif
330 );
ajs274a4a42004-12-07 15:39:31 +0000331#undef PRI
ajs063ee522004-11-26 18:11:14 +0000332#undef LOC
333}
ajs59a06a92004-11-23 18:19:14 +0000334
ajs063ee522004-11-26 18:11:14 +0000335/* Log a backtrace using only async-signal-safe functions.
336 Needs to be enhanced to support syslog logging. */
337void
ajs239c26f2005-01-17 15:22:28 +0000338zlog_backtrace_sigsafe(int priority, void *program_counter)
ajs063ee522004-11-26 18:11:14 +0000339{
340#ifdef HAVE_GLIBC_BACKTRACE
ajs239c26f2005-01-17 15:22:28 +0000341 static const char pclabel[] = "Program counter: ";
ajs063ee522004-11-26 18:11:14 +0000342 void *array[20];
343 int size;
344 char buf[100];
345 char *s;
346#define LOC s,buf+sizeof(buf)-s
347
ajs063ee522004-11-26 18:11:14 +0000348 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
349 ((size_t)size > sizeof(array)/sizeof(array[0])))
350 return;
351 s = buf;
352 s = str_append(LOC,"Backtrace for ");
353 s = num_append(LOC,size);
354 s = str_append(LOC," stack frames:\n");
ajs59a06a92004-11-23 18:19:14 +0000355
356#define DUMP(FP) { \
ajs239c26f2005-01-17 15:22:28 +0000357 if (program_counter) \
358 { \
359 write(fileno(FP),pclabel,sizeof(pclabel)-1); \
360 backtrace_symbols_fd(&program_counter, 1, fileno(FP)); \
361 } \
ajs59a06a92004-11-23 18:19:14 +0000362 write(fileno(FP),buf,s-buf); \
363 backtrace_symbols_fd(array, size, fileno(FP)); \
364}
365
366 if (!zlog_default)
367 DUMP(stderr)
368 else
369 {
ajs274a4a42004-12-07 15:39:31 +0000370 if ((priority <= zlog_default->maxlvl[ZLOG_DEST_FILE]) &&
371 zlog_default->fp)
ajs063ee522004-11-26 18:11:14 +0000372 DUMP(zlog_default->fp)
ajs274a4a42004-12-07 15:39:31 +0000373 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajs063ee522004-11-26 18:11:14 +0000374 DUMP(stdout)
ajs274a4a42004-12-07 15:39:31 +0000375 /* Remove trailing '\n' for monitor and syslog */
376 *--s = '\0';
377 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
378 vty_log_fixed(buf,s-buf);
379 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
380 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
381 {
382 int i;
383 /* Just print the function addresses. */
384 for (i = 0; i < size; i++)
385 {
386 s = buf;
387 s = str_append(LOC,"[bt ");
388 s = num_append(LOC,i);
389 s = str_append(LOC,"] 0x");
390 s = hex_append(LOC,(u_long)(array[i]));
391 *s = '\0';
392 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
393 vty_log_fixed(buf,s-buf);
394 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
ajs7d149b82004-11-28 23:00:01 +0000395 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
ajs274a4a42004-12-07 15:39:31 +0000396 }
397 }
ajs59a06a92004-11-23 18:19:14 +0000398 }
399#undef DUMP
ajs59a06a92004-11-23 18:19:14 +0000400#undef LOC
ajs063ee522004-11-26 18:11:14 +0000401#endif /* HAVE_GLIBC_BACKTRACE */
402}
403
404void
405zlog_backtrace(int priority)
406{
407#ifndef HAVE_GLIBC_BACKTRACE
408 zlog(NULL, priority, "No backtrace available on this platform.");
409#else
410 void *array[20];
411 int size, i;
412 char **strings;
413
414 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
415 ((size_t)size > sizeof(array)/sizeof(array[0])))
416 {
417 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
418 "(valid range is between 1 and %u)",
419 size, sizeof(array)/sizeof(array[0]));
420 return;
421 }
422 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
423 if (!(strings = backtrace_symbols(array, size)))
424 {
425 zlog_err("Cannot get backtrace symbols (out of memory?)");
426 for (i = 0; i < size; i++)
427 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
428 }
429 else
430 {
431 for (i = 0; i < size; i++)
432 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
433 free(strings);
434 }
435#endif /* HAVE_GLIBC_BACKTRACE */
ajs59a06a92004-11-23 18:19:14 +0000436}
437
paul718e3742002-12-13 20:15:29 +0000438void
439zlog (struct zlog *zl, int priority, const char *format, ...)
440{
ajsd246bd92004-11-23 17:35:08 +0000441 va_list args;
paul718e3742002-12-13 20:15:29 +0000442
ajsd246bd92004-11-23 17:35:08 +0000443 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000444 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000445 va_end (args);
paul718e3742002-12-13 20:15:29 +0000446}
447
ajsd246bd92004-11-23 17:35:08 +0000448#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
449void \
450FUNCNAME(const char *format, ...) \
451{ \
452 va_list args; \
453 va_start(args, format); \
454 vzlog (NULL, PRIORITY, format, args); \
455 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000456}
457
ajsd246bd92004-11-23 17:35:08 +0000458ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000459
ajsd246bd92004-11-23 17:35:08 +0000460ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000461
ajsd246bd92004-11-23 17:35:08 +0000462ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000463
ajsd246bd92004-11-23 17:35:08 +0000464ZLOG_FUNC(zlog_notice, LOG_NOTICE)
465
466ZLOG_FUNC(zlog_debug, LOG_DEBUG)
467
468#undef ZLOG_FUNC
469
470#define PLOG_FUNC(FUNCNAME,PRIORITY) \
471void \
472FUNCNAME(struct zlog *zl, const char *format, ...) \
473{ \
474 va_list args; \
475 va_start(args, format); \
476 vzlog (zl, PRIORITY, format, args); \
477 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000478}
479
ajsd246bd92004-11-23 17:35:08 +0000480PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000481
ajsd246bd92004-11-23 17:35:08 +0000482PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000483
ajsd246bd92004-11-23 17:35:08 +0000484PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000485
ajsd246bd92004-11-23 17:35:08 +0000486PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000487
ajsd246bd92004-11-23 17:35:08 +0000488PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000489
ajsd246bd92004-11-23 17:35:08 +0000490#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000491
ajscee3df12004-11-24 17:14:49 +0000492void
493_zlog_assert_failed (const char *assertion, const char *file,
494 unsigned int line, const char *function)
495{
496 zlog_err("Assertion `%s' failed in file %s, line %u, function %s",
497 assertion,file,line,(function ? function : "?"));
ajs3378d202004-12-10 22:43:17 +0000498 zlog_backtrace(LOG_ERR);
ajscee3df12004-11-24 17:14:49 +0000499 abort();
500}
501
paul718e3742002-12-13 20:15:29 +0000502
503/* Open log stream */
504struct zlog *
ajs274a4a42004-12-07 15:39:31 +0000505openzlog (const char *progname, zlog_proto_t protocol,
paul718e3742002-12-13 20:15:29 +0000506 int syslog_flags, int syslog_facility)
507{
508 struct zlog *zl;
ajs274a4a42004-12-07 15:39:31 +0000509 u_int i;
paul718e3742002-12-13 20:15:29 +0000510
ajs274a4a42004-12-07 15:39:31 +0000511 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
paul718e3742002-12-13 20:15:29 +0000512
513 zl->ident = progname;
paul718e3742002-12-13 20:15:29 +0000514 zl->protocol = protocol;
515 zl->facility = syslog_facility;
ajs7d149b82004-11-28 23:00:01 +0000516 zl->syslog_options = syslog_flags;
paul718e3742002-12-13 20:15:29 +0000517
ajs274a4a42004-12-07 15:39:31 +0000518 /* Set default logging levels. */
519 for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++)
520 zl->maxlvl[i] = ZLOG_DISABLED;
521 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
522 zl->default_lvl = LOG_DEBUG;
523
paul718e3742002-12-13 20:15:29 +0000524 openlog (progname, syslog_flags, zl->facility);
525
526 return zl;
527}
528
529void
530closezlog (struct zlog *zl)
531{
532 closelog();
533 fclose (zl->fp);
534
535 XFREE (MTYPE_ZLOG, zl);
536}
537
538/* Called from command.c. */
539void
ajs274a4a42004-12-07 15:39:31 +0000540zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
paul718e3742002-12-13 20:15:29 +0000541{
542 if (zl == NULL)
543 zl = zlog_default;
544
ajs274a4a42004-12-07 15:39:31 +0000545 zl->maxlvl[dest] = log_level;
paul718e3742002-12-13 20:15:29 +0000546}
547
548int
ajs274a4a42004-12-07 15:39:31 +0000549zlog_set_file (struct zlog *zl, const char *filename, int log_level)
paul718e3742002-12-13 20:15:29 +0000550{
551 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000552 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000553
554 /* There is opend file. */
555 zlog_reset_file (zl);
556
557 /* Set default zl. */
558 if (zl == NULL)
559 zl = zlog_default;
560
561 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000562 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000563 fp = fopen (filename, "a");
gdtaa593d52003-12-22 20:15:53 +0000564 umask(oldumask);
ajs274a4a42004-12-07 15:39:31 +0000565 if (fp == NULL)
566 return 0;
paul718e3742002-12-13 20:15:29 +0000567
568 /* Set flags. */
569 zl->filename = strdup (filename);
ajs274a4a42004-12-07 15:39:31 +0000570 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
paul718e3742002-12-13 20:15:29 +0000571 zl->fp = fp;
572
573 return 1;
574}
575
576/* Reset opend file. */
577int
578zlog_reset_file (struct zlog *zl)
579{
580 if (zl == NULL)
581 zl = zlog_default;
582
paul718e3742002-12-13 20:15:29 +0000583 if (zl->fp)
584 fclose (zl->fp);
585 zl->fp = NULL;
ajs274a4a42004-12-07 15:39:31 +0000586 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000587
588 if (zl->filename)
589 free (zl->filename);
590 zl->filename = NULL;
591
592 return 1;
593}
594
595/* Reopen log file. */
596int
597zlog_rotate (struct zlog *zl)
598{
ajs274a4a42004-12-07 15:39:31 +0000599 int level;
paul718e3742002-12-13 20:15:29 +0000600
601 if (zl == NULL)
602 zl = zlog_default;
603
604 if (zl->fp)
605 fclose (zl->fp);
606 zl->fp = NULL;
ajs274a4a42004-12-07 15:39:31 +0000607 level = zl->maxlvl[ZLOG_DEST_FILE];
608 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000609
610 if (zl->filename)
611 {
gdtaa593d52003-12-22 20:15:53 +0000612 mode_t oldumask;
ajs274a4a42004-12-07 15:39:31 +0000613 int save_errno;
gdtaa593d52003-12-22 20:15:53 +0000614
615 oldumask = umask (0777 & ~LOGFILE_MASK);
ajs274a4a42004-12-07 15:39:31 +0000616 zl->fp = fopen (zl->filename, "a");
617 save_errno = errno;
618 umask(oldumask);
619 if (zl->fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000620 {
ajs274a4a42004-12-07 15:39:31 +0000621 zlog_err("Log rotate failed: cannot open file %s for append: %s",
622 zl->filename, safe_strerror(save_errno));
gdtaa593d52003-12-22 20:15:53 +0000623 return -1;
624 }
ajs274a4a42004-12-07 15:39:31 +0000625 zl->maxlvl[ZLOG_DEST_FILE] = level;
paul718e3742002-12-13 20:15:29 +0000626 }
627
628 return 1;
629}
630
paul718e3742002-12-13 20:15:29 +0000631/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000632const char *
paul718e3742002-12-13 20:15:29 +0000633lookup (struct message *mes, int key)
634{
635 struct message *pnt;
636
637 for (pnt = mes; pnt->key != 0; pnt++)
638 if (pnt->key == key)
639 return pnt->str;
640
641 return "";
642}
643
644/* Very old hacky version of message lookup function. Still partly
hassob04c6992004-10-04 19:10:31 +0000645 used in bgpd and ospfd. FIXME Seems that it's not used any more. */
hasso8c328f12004-10-05 21:01:23 +0000646const char *
paul718e3742002-12-13 20:15:29 +0000647mes_lookup (struct message *meslist, int max, int index)
648{
649 if (index < 0 || index >= max)
650 {
651 zlog_err ("message index out of bound: %d", max);
652 return NULL;
653 }
654 return meslist[index].str;
655}
ajsca359762004-11-19 23:40:16 +0000656
657/* Wrapper around strerror to handle case where it returns NULL. */
658const char *
659safe_strerror(int errnum)
660{
661 const char *s = strerror(errnum);
662 return (s != NULL) ? s : "Unknown error";
663}