#include <stdarg.h>
#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#include "report.h"
#ifndef LOG_NDELAY
#define LOG_NDELAY 0
#endif
#ifndef LOG_DAEMON
#define LOG_DAEMON 0
#endif
#ifndef LOG_BOOTP
#define LOG_BOOTP LOG_DAEMON
#endif
extern int debug;
extern char *progname;
static int stderr_only = 1;
void
report_init(int nolog)
{
stderr_only = nolog;
#ifdef SYSLOG
if (!stderr_only) {
openlog(progname, LOG_PID | LOG_NDELAY, LOG_BOOTP);
}
#endif
}
static char *levelnames[] = {
#ifdef LOG_SALERT
"level(0): ",
"alert(1): ",
"alert(2): ",
"emerg(3): ",
"error(4): ",
"crit(5): ",
"warn(6): ",
"note(7): ",
"info(8): ",
"debug(9): ",
"level(?): "
#else
"emerg(0): ",
"alert(1): ",
"crit(2): ",
"error(3): ",
"warn(4): ",
"note(5): ",
"info(6): ",
"debug(7): ",
"level(?): "
#endif
};
static int numlevels = sizeof(levelnames) / sizeof(levelnames[0]);
void
report(int priority, const char *fmt,...)
{
va_list ap;
static char buf[128];
if ((priority < 0) || (priority >= numlevels)) {
priority = numlevels - 1;
}
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (stderr_only || (debug > 2)) {
fprintf(stderr, "%s: %s %s\n",
progname, levelnames[priority], buf);
}
#ifdef SYSLOG
if (!stderr_only)
syslog((priority | LOG_BOOTP), "%s", buf);
#endif
}
const char *
get_errmsg(void)
{
return strerror(errno);
}