1998-05-04 00:42:08 +08:00
|
|
|
/*
|
|
|
|
* BIRD Library -- Logging Functions
|
|
|
|
*
|
2000-06-05 20:49:04 +08:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1998-05-04 00:42:08 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-06-05 20:49:04 +08:00
|
|
|
/**
|
|
|
|
* DOC: Logging
|
|
|
|
*
|
|
|
|
* The Logging module offers a simple set of functions for writing
|
2000-06-08 20:37:21 +08:00
|
|
|
* messages to system logs and to the debug output. Message classes
|
|
|
|
* used by this module are described in |birdlib.h| and also in the
|
|
|
|
* user's manual.
|
2000-06-05 20:49:04 +08:00
|
|
|
*/
|
|
|
|
|
1998-05-04 00:42:08 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
2001-03-06 21:40:39 +08:00
|
|
|
#include <time.h>
|
1998-05-04 00:42:08 +08:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
1999-12-06 20:34:45 +08:00
|
|
|
#include "nest/cli.h"
|
2010-01-03 19:17:52 +08:00
|
|
|
#include "nest/mrtdump.h"
|
1998-06-17 22:33:29 +08:00
|
|
|
#include "lib/string.h"
|
1999-12-06 21:45:56 +08:00
|
|
|
#include "lib/lists.h"
|
|
|
|
#include "lib/unix.h"
|
1998-05-04 00:42:08 +08:00
|
|
|
|
|
|
|
static FILE *dbgf = NULL;
|
1999-12-06 21:45:56 +08:00
|
|
|
static list *current_log_list;
|
|
|
|
static list init_log_list;
|
1998-05-04 00:42:08 +08:00
|
|
|
|
2009-02-26 21:23:54 +08:00
|
|
|
bird_clock_t rate_limit_time = 5;
|
|
|
|
int rate_limit_count = 5;
|
|
|
|
|
1998-05-04 00:42:08 +08:00
|
|
|
#ifdef HAVE_SYSLOG
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
|
|
|
|
static int syslog_priorities[] = {
|
1999-12-06 21:45:56 +08:00
|
|
|
LOG_DEBUG,
|
|
|
|
LOG_DEBUG,
|
1998-05-04 00:42:08 +08:00
|
|
|
LOG_DEBUG,
|
|
|
|
LOG_INFO,
|
1999-12-06 21:45:56 +08:00
|
|
|
LOG_ERR,
|
1998-05-04 00:42:08 +08:00
|
|
|
LOG_WARNING,
|
|
|
|
LOG_ERR,
|
1999-12-06 21:45:56 +08:00
|
|
|
LOG_ERR,
|
|
|
|
LOG_CRIT,
|
1998-05-04 00:42:08 +08:00
|
|
|
LOG_CRIT
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char *class_names[] = {
|
|
|
|
"???",
|
|
|
|
"DBG",
|
1999-12-06 21:45:56 +08:00
|
|
|
"TRACE",
|
1998-05-04 00:42:08 +08:00
|
|
|
"INFO",
|
1999-12-06 21:45:56 +08:00
|
|
|
"RMT",
|
1998-05-04 00:42:08 +08:00
|
|
|
"WARN",
|
|
|
|
"ERR",
|
|
|
|
"AUTH",
|
1999-12-06 21:45:56 +08:00
|
|
|
"FATAL",
|
|
|
|
"BUG"
|
1998-05-04 00:42:08 +08:00
|
|
|
};
|
|
|
|
|
1998-06-17 22:33:29 +08:00
|
|
|
static void
|
1999-12-06 20:34:45 +08:00
|
|
|
vlog(int class, char *msg, va_list args)
|
1998-06-17 22:33:29 +08:00
|
|
|
{
|
1999-12-06 20:34:45 +08:00
|
|
|
char buf[1024];
|
1999-12-06 21:45:56 +08:00
|
|
|
struct log_config *l;
|
1998-06-17 22:33:29 +08:00
|
|
|
|
1999-12-06 20:34:45 +08:00
|
|
|
if (bvsnprintf(buf, sizeof(buf)-1, msg, args) < 0)
|
|
|
|
bsprintf(buf + sizeof(buf) - 100, " ... <too long>");
|
1998-06-17 22:33:29 +08:00
|
|
|
|
1999-12-06 21:45:56 +08:00
|
|
|
WALK_LIST(l, *current_log_list)
|
1998-05-04 00:42:08 +08:00
|
|
|
{
|
1999-12-06 21:45:56 +08:00
|
|
|
if (!(l->mask & (1 << class)))
|
|
|
|
continue;
|
|
|
|
if (l->fh)
|
|
|
|
{
|
|
|
|
time_t now = time(NULL);
|
|
|
|
struct tm *tm = localtime(&now);
|
|
|
|
|
|
|
|
if (l->terminal_flag)
|
|
|
|
fputs("bird: ", l->fh);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(l->fh, "%02d-%02d-%04d %02d:%02d:%02d <%s> ",
|
|
|
|
tm->tm_mday,
|
|
|
|
tm->tm_mon+1,
|
|
|
|
tm->tm_year+1900,
|
|
|
|
tm->tm_hour,
|
|
|
|
tm->tm_min,
|
|
|
|
tm->tm_sec,
|
|
|
|
class_names[class]);
|
|
|
|
}
|
|
|
|
fputs(buf, l->fh);
|
|
|
|
fputc('\n', l->fh);
|
|
|
|
fflush(l->fh);
|
|
|
|
}
|
1998-05-04 00:42:08 +08:00
|
|
|
#ifdef HAVE_SYSLOG
|
1999-12-06 21:45:56 +08:00
|
|
|
else
|
|
|
|
syslog(syslog_priorities[class], "%s", buf);
|
1998-05-04 00:42:08 +08:00
|
|
|
#endif
|
|
|
|
}
|
1999-12-06 20:34:45 +08:00
|
|
|
cli_echo(class, buf);
|
1998-05-04 00:42:08 +08:00
|
|
|
}
|
|
|
|
|
2000-06-05 20:49:04 +08:00
|
|
|
/**
|
|
|
|
* log - log a message
|
|
|
|
* @msg: printf-like formatting string with message class information
|
|
|
|
* prepended (%L_DEBUG to %L_BUG, see |lib/birdlib.h|)
|
|
|
|
*
|
|
|
|
* This function formats a message according to the format string @msg
|
2000-06-07 20:29:08 +08:00
|
|
|
* and writes it to the corresponding log file (as specified in the
|
2000-06-05 20:49:04 +08:00
|
|
|
* configuration). Please note that the message is automatically
|
|
|
|
* formatted as a full line, no need to include |\n| inside.
|
|
|
|
*/
|
1998-05-04 00:42:08 +08:00
|
|
|
void
|
2004-06-05 17:05:12 +08:00
|
|
|
log_msg(char *msg, ...)
|
1998-05-04 00:42:08 +08:00
|
|
|
{
|
|
|
|
int class = 1;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, msg);
|
1998-12-20 22:24:35 +08:00
|
|
|
if (*msg >= 1 && *msg <= 8)
|
1998-05-04 00:42:08 +08:00
|
|
|
class = *msg++;
|
|
|
|
vlog(class, msg, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2009-02-26 21:23:54 +08:00
|
|
|
void
|
|
|
|
log_rl(struct rate_limit *rl, char *msg, ...)
|
|
|
|
{
|
|
|
|
int class = 1;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
bird_clock_t delta = now - rl->timestamp;
|
|
|
|
if ((0 <= delta) && (delta < rate_limit_time))
|
|
|
|
{
|
|
|
|
rl->count++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rl->timestamp = now;
|
|
|
|
rl->count = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rl->count > rate_limit_count)
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(args, msg);
|
|
|
|
if (*msg >= 1 && *msg <= 8)
|
|
|
|
class = *msg++;
|
|
|
|
vlog(class, msg, args);
|
|
|
|
if (rl->count == rate_limit_count)
|
|
|
|
vlog(class, "...", args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2000-06-05 20:49:04 +08:00
|
|
|
/**
|
|
|
|
* bug - report an internal error
|
|
|
|
* @msg: a printf-like error message
|
|
|
|
*
|
|
|
|
* This function logs an internal error and aborts execution
|
|
|
|
* of the program.
|
|
|
|
*/
|
1998-12-20 22:24:35 +08:00
|
|
|
void
|
|
|
|
bug(char *msg, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, msg);
|
|
|
|
vlog(L_BUG[0], msg, args);
|
2000-05-08 20:38:00 +08:00
|
|
|
abort();
|
1998-12-20 22:24:35 +08:00
|
|
|
}
|
|
|
|
|
2000-06-05 20:49:04 +08:00
|
|
|
/**
|
|
|
|
* bug - report a fatal error
|
|
|
|
* @msg: a printf-like error message
|
|
|
|
*
|
|
|
|
* This function logs a fatal error and aborts execution
|
|
|
|
* of the program.
|
|
|
|
*/
|
1998-05-04 00:42:08 +08:00
|
|
|
void
|
|
|
|
die(char *msg, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, msg);
|
1998-12-20 22:24:35 +08:00
|
|
|
vlog(L_FATAL[0], msg, args);
|
1998-05-04 00:42:08 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2000-06-05 20:49:04 +08:00
|
|
|
/**
|
|
|
|
* debug - write to debug output
|
|
|
|
* @msg: a printf-like message
|
|
|
|
*
|
|
|
|
* This function formats the message @msg and prints it out
|
|
|
|
* to the debugging output. No newline character is appended.
|
|
|
|
*/
|
1998-05-04 00:42:08 +08:00
|
|
|
void
|
|
|
|
debug(char *msg, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
1999-12-06 20:34:45 +08:00
|
|
|
char buf[1024];
|
1998-05-04 00:42:08 +08:00
|
|
|
|
|
|
|
va_start(args, msg);
|
|
|
|
if (dbgf)
|
1999-12-06 21:45:56 +08:00
|
|
|
{
|
|
|
|
if (bvsnprintf(buf, sizeof(buf), msg, args) < 0)
|
|
|
|
bsprintf(buf + sizeof(buf) - 100, " ... <too long>\n");
|
|
|
|
fputs(buf, dbgf);
|
|
|
|
}
|
1998-05-04 00:42:08 +08:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-06-09 15:32:57 +08:00
|
|
|
log_init(int debug, int init)
|
1998-05-04 00:42:08 +08:00
|
|
|
{
|
1999-12-06 21:45:56 +08:00
|
|
|
static struct log_config lc_stderr = { mask: ~0, terminal_flag: 1 };
|
|
|
|
|
|
|
|
init_list(&init_log_list);
|
|
|
|
current_log_list = &init_log_list;
|
1998-05-04 00:42:08 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_SYSLOG
|
1999-12-06 21:45:56 +08:00
|
|
|
if (!debug)
|
1998-05-04 00:42:08 +08:00
|
|
|
{
|
1999-12-06 21:45:56 +08:00
|
|
|
static struct log_config lc_syslog = { mask: ~0 };
|
|
|
|
openlog("bird", LOG_CONS | LOG_NDELAY, LOG_DAEMON);
|
|
|
|
add_tail(current_log_list, &lc_syslog.n);
|
2000-06-09 15:32:57 +08:00
|
|
|
if (!init)
|
|
|
|
return;
|
1998-05-04 00:42:08 +08:00
|
|
|
}
|
1999-12-06 21:45:56 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
lc_stderr.fh = stderr;
|
|
|
|
add_tail(current_log_list, &lc_stderr.n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-06-20 07:50:02 +08:00
|
|
|
log_switch(int debug, list *l)
|
1999-12-06 21:45:56 +08:00
|
|
|
{
|
1999-12-06 21:51:04 +08:00
|
|
|
if (EMPTY_LIST(*l))
|
2000-06-20 07:50:02 +08:00
|
|
|
log_init(debug, 0);
|
1999-12-06 21:51:04 +08:00
|
|
|
else
|
|
|
|
current_log_list = l;
|
1998-05-04 00:42:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
log_init_debug(char *f)
|
|
|
|
{
|
|
|
|
if (dbgf && dbgf != stderr)
|
|
|
|
fclose(dbgf);
|
|
|
|
if (!f)
|
|
|
|
dbgf = NULL;
|
1999-12-06 21:45:56 +08:00
|
|
|
else if (!*f)
|
|
|
|
dbgf = stderr;
|
1998-05-04 00:42:08 +08:00
|
|
|
else if (!(dbgf = fopen(f, "a")))
|
|
|
|
log(L_ERR "Error opening debug file `%s': %m", f);
|
2000-03-13 06:53:05 +08:00
|
|
|
if (dbgf)
|
|
|
|
setvbuf(dbgf, NULL, _IONBF, 0);
|
1998-05-04 00:42:08 +08:00
|
|
|
}
|
2010-01-03 19:17:52 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
mrt_dump_message(struct proto *p, u16 type, u16 subtype, byte *buf, u32 len)
|
|
|
|
{
|
|
|
|
/* Prepare header */
|
|
|
|
put_u32(buf+0, now_real);
|
|
|
|
put_u16(buf+4, type);
|
|
|
|
put_u16(buf+6, subtype);
|
|
|
|
put_u32(buf+8, len - MRTDUMP_HDR_LENGTH);
|
|
|
|
|
|
|
|
if (p->cf->global->mrtdump_file != -1)
|
|
|
|
write(p->cf->global->mrtdump_file, buf, len);
|
|
|
|
}
|