]>
git.proxmox.com Git - mirror_frr.git/blob - lib/log.c
2 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
28 struct zlog
*zlog_default
= NULL
;
30 const char *zlog_proto_names
[] =
45 const char *zlog_priority
[] =
60 /* For time string format. */
63 /* Utility routine for current time printing. */
73 tm
= localtime (&clock
);
75 ret
= strftime (buf
, TIME_BUF
, "%Y/%m/%d %H:%M:%S", tm
);
77 zlog_warn ("strftime error");
80 fprintf (fp
, "%s ", buf
);
83 /* va_list version of zlog. */
85 vzlog (struct zlog
*zl
, int priority
, const char *format
, va_list args
)
87 /* If zlog is not specified, use default one. */
91 /* When zlog_default is also NULL, use stderr for logging. */
95 fprintf (stderr
, "%s: ", "unknown");
96 vfprintf (stderr
, format
, args
);
97 fprintf (stderr
, "\n");
100 /* In this case we return at here. */
104 /* only log this information if it has not been masked out */
105 if ( priority
> zl
->maskpri
)
109 if (zl
->flags
& ZLOG_SYSLOG
)
113 vsyslog (priority
|zlog_default
->facility
, format
, ac
);
118 if (zl
->flags
& ZLOG_FILE
)
122 if (zl
->record_priority
)
123 fprintf (zl
->fp
, "%s: ", zlog_priority
[priority
]);
124 fprintf (zl
->fp
, "%s: ", zlog_proto_names
[zl
->protocol
]);
126 vfprintf (zl
->fp
, format
, ac
);
128 fprintf (zl
->fp
, "\n");
133 if (zl
->flags
& ZLOG_STDOUT
)
137 if (zl
->record_priority
)
138 fprintf (stdout
, "%s: ", zlog_priority
[priority
]);
139 fprintf (stdout
, "%s: ", zlog_proto_names
[zl
->protocol
]);
141 vfprintf (stdout
, format
, ac
);
143 fprintf (stdout
, "\n");
148 if (zl
->flags
& ZLOG_STDERR
)
152 if (zl
->record_priority
)
153 fprintf (stderr
, "%s: ", zlog_priority
[priority
]);
154 fprintf (stderr
, "%s: ", zlog_proto_names
[zl
->protocol
]);
156 vfprintf (stderr
, format
, ac
);
158 fprintf (stderr
, "\n");
162 /* Terminal monitor. */
163 vty_log (zlog_proto_names
[zl
->protocol
], format
, args
);
167 str_append(char *dst
, int len
, const char *src
)
169 while ((len
-- > 0) && *src
)
175 num_append(char *s
, int len
, u_long x
)
181 while (x
&& (t
> buf
))
186 return str_append(s
,len
,t
);
189 /* Note: the goal here is to use only async-signal-safe functions.
190 Needs to be enhanced to support syslog logging. */
192 zlog_signal(int signo
, const char *action
)
195 char buf
[sizeof("DEFAULT: Received signal S at T; aborting...")+60];
197 #define LOC s,buf+sizeof(buf)-s
202 s
= str_append(LOC
,zlog_proto_names
[zlog_default
->protocol
]);
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
);
214 #define DUMP(FP) write(fileno(FP),buf,s-buf);
219 if ((zlog_default
->flags
& ZLOG_FILE
) && zlog_default
->fp
)
220 DUMP(zlog_default
->fp
)
221 if (zlog_default
->flags
& ZLOG_STDOUT
)
223 if (zlog_default
->flags
& ZLOG_STDERR
)
225 /* Is there a signal-safe way to send a syslog message? */
229 zlog_backtrace_sigsafe(LOG_ERR
);
233 /* Log a backtrace using only async-signal-safe functions.
234 Needs to be enhanced to support syslog logging. */
236 zlog_backtrace_sigsafe(int priority
)
238 #ifdef HAVE_GLIBC_BACKTRACE
243 #define LOC s,buf+sizeof(buf)-s
245 /* only log this information if it has not been masked out */
246 if (zlog_default
&& (priority
> zlog_default
->maskpri
))
249 if (((size
= backtrace(array
,sizeof(array
)/sizeof(array
[0]))) <= 0) ||
250 ((size_t)size
> sizeof(array
)/sizeof(array
[0])))
253 s
= str_append(LOC
,"Backtrace for ");
254 s
= num_append(LOC
,size
);
255 s
= str_append(LOC
," stack frames:\n");
258 write(fileno(FP),buf,s-buf); \
259 backtrace_symbols_fd(array, size, fileno(FP)); \
266 if ((zlog_default
->flags
& ZLOG_FILE
) && zlog_default
->fp
)
267 DUMP(zlog_default
->fp
)
268 if (zlog_default
->flags
& ZLOG_STDOUT
)
270 if (zlog_default
->flags
& ZLOG_STDERR
)
272 /* Is there a signal-safe way to send a syslog message? */
276 #endif /* HAVE_GLIBC_BACKTRACE */
280 zlog_backtrace(int priority
)
282 #ifndef HAVE_GLIBC_BACKTRACE
283 zlog(NULL
, priority
, "No backtrace available on this platform.");
289 if (((size
= backtrace(array
,sizeof(array
)/sizeof(array
[0]))) <= 0) ||
290 ((size_t)size
> sizeof(array
)/sizeof(array
[0])))
292 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
293 "(valid range is between 1 and %u)",
294 size
, sizeof(array
)/sizeof(array
[0]));
297 zlog(NULL
, priority
, "Backtrace for %d stack frames:", size
);
298 if (!(strings
= backtrace_symbols(array
, size
)))
300 zlog_err("Cannot get backtrace symbols (out of memory?)");
301 for (i
= 0; i
< size
; i
++)
302 zlog(NULL
, priority
, "[bt %d] %p",i
,array
[i
]);
306 for (i
= 0; i
< size
; i
++)
307 zlog(NULL
, priority
, "[bt %d] %s",i
,strings
[i
]);
310 #endif /* HAVE_GLIBC_BACKTRACE */
314 zlog (struct zlog
*zl
, int priority
, const char *format
, ...)
318 va_start(args
, format
);
319 vzlog (zl
, priority
, format
, args
);
323 #define ZLOG_FUNC(FUNCNAME,PRIORITY) \
325 FUNCNAME(const char *format, ...) \
328 va_start(args, format); \
329 vzlog (NULL, PRIORITY, format, args); \
333 ZLOG_FUNC(zlog_err
, LOG_ERR
)
335 ZLOG_FUNC(zlog_warn
, LOG_WARNING
)
337 ZLOG_FUNC(zlog_info
, LOG_INFO
)
339 ZLOG_FUNC(zlog_notice
, LOG_NOTICE
)
341 ZLOG_FUNC(zlog_debug
, LOG_DEBUG
)
345 #define PLOG_FUNC(FUNCNAME,PRIORITY) \
347 FUNCNAME(struct zlog *zl, const char *format, ...) \
350 va_start(args, format); \
351 vzlog (zl, PRIORITY, format, args); \
355 PLOG_FUNC(plog_err
, LOG_ERR
)
357 PLOG_FUNC(plog_warn
, LOG_WARNING
)
359 PLOG_FUNC(plog_info
, LOG_INFO
)
361 PLOG_FUNC(plog_notice
, LOG_NOTICE
)
363 PLOG_FUNC(plog_debug
, LOG_DEBUG
)
368 _zlog_assert_failed (const char *assertion
, const char *file
,
369 unsigned int line
, const char *function
)
371 zlog_err("Assertion `%s' failed in file %s, line %u, function %s",
372 assertion
,file
,line
,(function
? function
: "?"));
373 zlog_backtrace(LOG_ERR
);
378 /* Open log stream */
380 openzlog (const char *progname
, int flags
, zlog_proto_t protocol
,
381 int syslog_flags
, int syslog_facility
)
385 zl
= XMALLOC(MTYPE_ZLOG
, sizeof (struct zlog
));
386 memset (zl
, 0, sizeof (struct zlog
));
388 zl
->ident
= progname
;
390 zl
->protocol
= protocol
;
391 zl
->facility
= syslog_facility
;
392 zl
->maskpri
= LOG_DEBUG
;
393 zl
->record_priority
= 0;
395 openlog (progname
, syslog_flags
, zl
->facility
);
401 closezlog (struct zlog
*zl
)
406 XFREE (MTYPE_ZLOG
, zl
);
409 /* Called from command.c. */
411 zlog_set_flag (struct zlog
*zl
, int flags
)
420 zlog_reset_flag (struct zlog
*zl
, int flags
)
429 zlog_set_file (struct zlog
*zl
, const char *filename
)
434 /* There is opend file. */
435 zlog_reset_file (zl
);
437 /* Set default zl. */
442 oldumask
= umask (0777 & ~LOGFILE_MASK
);
443 fp
= fopen (filename
, "a");
452 zl
->filename
= strdup (filename
);
453 zl
->flags
|= ZLOG_FILE
;
459 /* Reset opend file. */
461 zlog_reset_file (struct zlog
*zl
)
466 zl
->flags
&= ~ZLOG_FILE
;
479 /* Reopen log file. */
481 zlog_rotate (struct zlog
*zl
)
496 oldumask
= umask (0777 & ~LOGFILE_MASK
);
497 fp
= fopen (zl
->filename
, "a");
510 /* Message lookup function. */
512 lookup (struct message
*mes
, int key
)
516 for (pnt
= mes
; pnt
->key
!= 0; pnt
++)
523 /* Very old hacky version of message lookup function. Still partly
524 used in bgpd and ospfd. FIXME Seems that it's not used any more. */
526 mes_lookup (struct message
*meslist
, int max
, int index
)
528 if (index
< 0 || index
>= max
)
530 zlog_err ("message index out of bound: %d", max
);
533 return meslist
[index
].str
;
536 /* Wrapper around strerror to handle case where it returns NULL. */
538 safe_strerror(int errnum
)
540 const char *s
= strerror(errnum
);
541 return (s
!= NULL
) ? s
: "Unknown error";