1/*2* report() - calls syslog3*/45#include <stdarg.h>67#include <stdio.h>8#include <syslog.h>9#include <string.h>10#include <errno.h>1112#include "report.h"1314#ifndef LOG_NDELAY15#define LOG_NDELAY 016#endif17#ifndef LOG_DAEMON18#define LOG_DAEMON 019#endif20#ifndef LOG_BOOTP21#define LOG_BOOTP LOG_DAEMON22#endif2324extern int debug;25extern char *progname;2627/*28* This is initialized so you get stderr until you call29* report_init()30*/31static int stderr_only = 1;3233void34report_init(int nolog)35{36stderr_only = nolog;37#ifdef SYSLOG38if (!stderr_only) {39openlog(progname, LOG_PID | LOG_NDELAY, LOG_BOOTP);40}41#endif42}4344/*45* This routine reports errors and such via stderr and syslog() if46* appopriate. It just helps avoid a lot of "#ifdef SYSLOG" constructs47* from being scattered throughout the code.48*49* The syntax is identical to syslog(3), but %m is not considered special50* for output to stderr (i.e. you'll see "%m" in the output. . .). Also,51* control strings should normally end with \n since newlines aren't52* automatically generated for stderr output (whereas syslog strips out all53* newlines and adds its own at the end).54*/5556static char *levelnames[] = {57#ifdef LOG_SALERT58"level(0): ",59"alert(1): ",60"alert(2): ",61"emerg(3): ",62"error(4): ",63"crit(5): ",64"warn(6): ",65"note(7): ",66"info(8): ",67"debug(9): ",68"level(?): "69#else70"emerg(0): ",71"alert(1): ",72"crit(2): ",73"error(3): ",74"warn(4): ",75"note(5): ",76"info(6): ",77"debug(7): ",78"level(?): "79#endif80};81static int numlevels = sizeof(levelnames) / sizeof(levelnames[0]);828384/*85* Print a log message using syslog(3) and/or stderr.86* The message passed in should not include a newline.87*/88void89report(int priority, const char *fmt,...)90{91va_list ap;92static char buf[128];9394if ((priority < 0) || (priority >= numlevels)) {95priority = numlevels - 1;96}97va_start(ap, fmt);98vsnprintf(buf, sizeof(buf), fmt, ap);99va_end(ap);100101/*102* Print the message103*/104if (stderr_only || (debug > 2)) {105fprintf(stderr, "%s: %s %s\n",106progname, levelnames[priority], buf);107}108#ifdef SYSLOG109if (!stderr_only)110syslog((priority | LOG_BOOTP), "%s", buf);111#endif112}113114115116/*117* Return pointer to static string which gives full filesystem error message.118*/119const char *120get_errmsg(void)121{122return strerror(errno);123}124125/*126* Local Variables:127* tab-width: 4128* c-indent-level: 4129* c-argdecl-indent: 4130* c-continued-statement-offset: 4131* c-continued-brace-offset: -4132* c-label-offset: -4133* c-brace-offset: 0134* End:135*/136137138