Commit e103487a authored by venaas's avatar venaas Committed by venaas

added logging to file and syslog (need to add support for specifying facility)

git-svn-id: https://svn.testnett.uninett.no/radsecproxy/trunk@72 e88ac4ed-0b26-0410-9574-a7f39faa03bf
parent f32f6b2e
......@@ -10,22 +10,101 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <sys/time.h>
#include <syslog.h>
#include <errno.h>
#include "debug.h"
static char *debug_ident = NULL;
static uint8_t debug_level = DBG_WARN;
static FILE *debug_file = NULL;
static int debug_syslogfacility = 0;
void debug_init(char *ident) {
debug_file = stderr;
setvbuf(debug_file, NULL, _IONBF, 0);
debug_ident = ident;
}
void debug_set_level(uint8_t level) {
debug_level = level;
}
uint8_t debug_get_level() {
return debug_level;
}
int debug_set_destination(char *dest) {
extern int errno;
if (!strncasecmp(dest, "file:///", 8)) {
debug_file = fopen(dest + 7, "a");
if (!debug_file)
debugx(1, DBG_ERR, "Failed to open logfile %s\n%s",
dest + 7, strerror(errno));
setvbuf(debug_file, NULL, _IONBF, 0);
return 1;
}
if (!strcasecmp(dest, "x-syslog://")) {
debug_syslogfacility = LOG_DAEMON;
openlog(debug_ident, LOG_PID, debug_syslogfacility);
return 1;
}
return 0;
}
void debug_logit(uint8_t level, const char *format, va_list ap) {
struct timeval now;
char *timebuf;
int priority;
if (debug_syslogfacility) {
switch (level) {
case DBG_INFO:
priority = LOG_INFO;
break;
case DBG_WARN:
priority = LOG_WARNING;
break;
case DBG_ERR:
priority = LOG_ERR;
break;
default:
priority = LOG_DEBUG;
}
vsyslog(priority, format, ap);
} else {
timebuf = malloc(256);
if (timebuf) {
gettimeofday(&now, NULL);
ctime_r(&now.tv_sec, timebuf);
timebuf[strlen(timebuf) - 1] = '\0';
fprintf(debug_file, "%s: ", timebuf);
free(timebuf);
}
vfprintf(debug_file, format, ap);
fprintf(debug_file, "\n");
}
}
void debug(uint8_t level, char *format, ...) {
va_list ap;
if (level < debug_level)
return;
va_start(ap, format);
debug_logit(level, format, ap);
va_end(ap);
}
void debugx(int status, uint8_t level, char *format, ...) {
if (level >= debug_level) {
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
debug_logit(level, format, ap);
va_end(ap);
fprintf(stderr, "\n");
}
if (level >= DBG_ERR)
exit(1);
exit(status);
}
......@@ -10,5 +10,9 @@
#define DBG_WARN 32
#define DBG_ERR 64
void debug_init(char *ident);
void debug_set_level(uint8_t level);
uint8_t debug_get_level();
void debug(uint8_t level, char *format, ...);
void debugx(int status, uint8_t level, char *format, ...);
int debug_set_destination(char *dest);
This diff is collapsed.
......@@ -18,3 +18,8 @@ TLSCertificateKeyPassword follow the white rabbit
#listenUDP localhost
#listenTCP 10.10.10.10:2084
#ListenTCP [2001:700:1:7:215:f2ff:fe35:307d]:2084
# Optional log level. 2 is default, 1 is less, 3 is more
#LogLevel 2
#Optional LogDestinatinon, else stderr used for logging
#LogDestination x-syslog://
#LogDestination file:///tmp/rp.log
......@@ -12,7 +12,7 @@
sizeof(struct sockaddr_in) : \
sizeof(struct sockaddr_in6))
#define DEBUG_LEVEL DBG_INFO
#define DEBUG_LEVEL DBG_WARN
#define CONFIG_MAIN "/etc/radsecproxy/radsecproxy.conf"
#define CONFIG_SERVERS "/etc/radsecproxy/servers.conf"
......@@ -57,6 +57,8 @@ struct options {
char *tlscertificatekeypassword;
char *listenudp;
char *listentcp;
char *logdestination;
uint8_t loglevel;
uint8_t statusserver;
};
......
......@@ -13,10 +13,11 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
#include "debug.h"
#if 0
#include <errno.h>
void errx(char *format, ...) {
extern int errno;
......@@ -47,6 +48,7 @@ void err(char *format, ...) {
} else
fprintf(stderr, "\n");
}
#endif
char *stringcopy(char *s, int len) {
char *r;
......@@ -78,7 +80,7 @@ char *addr2string(struct sockaddr *addr, socklen_t len) {
}
if (getnameinfo(addr, len, addr_buf[i], sizeof(addr_buf[i]),
NULL, 0, NI_NUMERICHOST)) {
err("getnameinfo");
debug(DBG_WARN, "getnameinfo failed");
return NULL;
}
return addr_buf[i];
......@@ -93,19 +95,19 @@ int connectport(int type, char *host, char *port) {
hints.ai_family = AF_UNSPEC;
if (getaddrinfo(host, port, &hints, &res0) != 0) {
err("connectport: can't resolve host %s port %s", host, port);
debug(DBG_ERR, "connectport: can't resolve host %s port %s", host, port);
return -1;
}
for (res = res0; res; res = res->ai_next) {
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s < 0) {
err("connectport: socket failed");
debug(DBG_WARN, "connectport: socket failed");
continue;
}
if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
break;
err("connectport: connect failed");
debug(DBG_WARN, "connectport: connect failed");
close(s);
s = -1;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment