From 6032aa6ade49545d7c8b6025cf6e6373eb7c910c Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 3 May 1998 16:42:08 +0000 Subject: Added new subdir for UNIX-dependent files. Now contains only functions for logging, but it will change soon. --- sysdep/unix/Makefile | 3 ++ sysdep/unix/log.c | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 sysdep/unix/Makefile create mode 100644 sysdep/unix/log.c (limited to 'sysdep') diff --git a/sysdep/unix/Makefile b/sysdep/unix/Makefile new file mode 100644 index 00000000..60f80576 --- /dev/null +++ b/sysdep/unix/Makefile @@ -0,0 +1,3 @@ +OBJS=log.o + +include $(TOPDIR)/Rules diff --git a/sysdep/unix/log.c b/sysdep/unix/log.c new file mode 100644 index 00000000..8d9facfa --- /dev/null +++ b/sysdep/unix/log.c @@ -0,0 +1,147 @@ +/* + * BIRD Library -- Logging Functions + * + * (c) 1998 Martin Mares + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#include +#include +#include +#include + +#include "nest/bird.h" + +static int log_inited; +static FILE *logf = NULL; +static FILE *dbgf = NULL; + +#ifdef HAVE_SYSLOG +#include + +static int syslog_priorities[] = { + LOG_INFO, + LOG_DEBUG, + LOG_INFO, + LOG_WARNING, + LOG_ERR, + LOG_NOTICE, + LOG_CRIT +}; +#endif + +static char *class_names[] = { + "???", + "DBG", + "INFO", + "WARN", + "ERR", + "AUTH", + "FATAL" +}; + +static void +vlog(int class, char *msg, va_list args) +{ + if (logf) + { + time_t now = time(NULL); + struct tm *tm = localtime(&now); + + fprintf(logf, "%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]); + vfprintf(logf, msg, args); + fputc('\n', logf); + fflush(logf); + } +#ifdef HAVE_SYSLOG + else if (log_inited) + vsyslog(syslog_priorities[class], msg, args); +#endif + else + { + fputs("bird: ", stderr); + vfprintf(stderr, msg, args); + fputc('\n', stderr); + fflush(stderr); + } +} + +void +log(char *msg, ...) +{ + int class = 1; + va_list args; + + va_start(args, msg); + if (*msg >= 1 && *msg <= 6) + class = *msg++; + vlog(class, msg, args); + va_end(args); +} + +void +die(char *msg, ...) +{ + va_list args; + + va_start(args, msg); + vlog(6, msg, args); + exit(1); +} + +void +debug(char *msg, ...) +{ + va_list args; + + va_start(args, msg); + if (dbgf) + vfprintf(dbgf, msg, args); + va_end(args); +} + +void +log_init(char *f) +{ + FILE *new; + + if (!f) + new = stderr; + else if (!*f) + { + new = NULL; +#ifdef HAVE_SYSLOG + openlog("bird", LOG_CONS | LOG_NDELAY, LOG_DAEMON); +#endif + } + else if (!(new = fopen(f, "a"))) + { + log(L_ERR "Unable to open log file `%s': %m", f); + return; + } + if (logf && logf != stderr) + fclose(logf); + logf = new; + log_inited = 1; +} + +void +log_init_debug(char *f) +{ + if (dbgf && dbgf != stderr) + fclose(dbgf); + if (!f) + dbgf = stderr; + else if (!*f) + dbgf = NULL; + else if (!(dbgf = fopen(f, "a"))) + log(L_ERR "Error opening debug file `%s': %m", f); +} -- cgit v1.2.3