165426Simp/* $FreeBSD$ */
265426Simp
33229Spst/*
43229Spst * report() - calls syslog
53229Spst */
63229Spst
73229Spst#include <stdarg.h>
83229Spst
93229Spst#include <stdio.h>
103229Spst#include <syslog.h>
1165426Simp#include <string.h>
1265426Simp#include <errno.h>
133229Spst
143229Spst#include "report.h"
153229Spst
163229Spst#ifndef LOG_NDELAY
173229Spst#define LOG_NDELAY	0
183229Spst#endif
193229Spst#ifndef LOG_DAEMON
203229Spst#define LOG_DAEMON	0
213229Spst#endif
223229Spst#ifndef	LOG_BOOTP
233229Spst#define LOG_BOOTP	LOG_DAEMON
243229Spst#endif
253229Spst
263229Spstextern int debug;
273229Spstextern char *progname;
283229Spst
293229Spst/*
303229Spst * This is initialized so you get stderr until you call
313229Spst *	report_init()
323229Spst */
333229Spststatic int stderr_only = 1;
343229Spst
353229Spstvoid
363229Spstreport_init(nolog)
373229Spst	int nolog;
383229Spst{
393229Spst	stderr_only = nolog;
403229Spst#ifdef SYSLOG
413229Spst	if (!stderr_only) {
423229Spst		openlog(progname, LOG_PID | LOG_NDELAY, LOG_BOOTP);
433229Spst	}
443229Spst#endif
453229Spst}
463229Spst
473229Spst/*
483229Spst * This routine reports errors and such via stderr and syslog() if
493229Spst * appopriate.  It just helps avoid a lot of "#ifdef SYSLOG" constructs
503229Spst * from being scattered throughout the code.
513229Spst *
523229Spst * The syntax is identical to syslog(3), but %m is not considered special
533229Spst * for output to stderr (i.e. you'll see "%m" in the output. . .).  Also,
543229Spst * control strings should normally end with \n since newlines aren't
553229Spst * automatically generated for stderr output (whereas syslog strips out all
563229Spst * newlines and adds its own at the end).
573229Spst */
583229Spst
593229Spststatic char *levelnames[] = {
603229Spst#ifdef LOG_SALERT
613229Spst	"level(0): ",
623229Spst	"alert(1): ",
633229Spst	"alert(2): ",
643229Spst	"emerg(3): ",
653229Spst	"error(4): ",
663229Spst	"crit(5):  ",
673229Spst	"warn(6):  ",
683229Spst	"note(7):  ",
693229Spst	"info(8):  ",
703229Spst	"debug(9): ",
713229Spst	"level(?): "
723229Spst#else
733229Spst	"emerg(0): ",
743229Spst	"alert(1): ",
753229Spst	"crit(2):  ",
763229Spst	"error(3): ",
773229Spst	"warn(4):  ",
783229Spst	"note(5):  ",
793229Spst	"info(6):  ",
803229Spst	"debug(7): ",
813229Spst	"level(?): "
823229Spst#endif
833229Spst};
843229Spststatic int numlevels = sizeof(levelnames) / sizeof(levelnames[0]);
853229Spst
863229Spst
873229Spst/*
883229Spst * Print a log message using syslog(3) and/or stderr.
893229Spst * The message passed in should not include a newline.
903229Spst */
913229Spstvoid
9269200Skrisreport(int priority, const char *fmt,...)
933229Spst{
943229Spst	va_list ap;
953229Spst	static char buf[128];
963229Spst
973229Spst	if ((priority < 0) || (priority >= numlevels)) {
983229Spst		priority = numlevels - 1;
993229Spst	}
1003229Spst	va_start(ap, fmt);
10131971Simp	vsnprintf(buf, sizeof(buf), fmt, ap);
1023229Spst	va_end(ap);
1033229Spst
1043229Spst	/*
1053229Spst	 * Print the message
1063229Spst	 */
1073229Spst	if (stderr_only || (debug > 2)) {
1083229Spst		fprintf(stderr, "%s: %s %s\n",
1093229Spst				progname, levelnames[priority], buf);
1103229Spst	}
1113229Spst#ifdef SYSLOG
1123229Spst	if (!stderr_only)
1133229Spst		syslog((priority | LOG_BOOTP), "%s", buf);
1143229Spst#endif
1153229Spst}
1163229Spst
1173229Spst
1183229Spst
1193229Spst/*
1203229Spst * Return pointer to static string which gives full filesystem error message.
1213229Spst */
12269200Skrisconst char *
1233229Spstget_errmsg()
1243229Spst{
1253229Spst	return strerror(errno);
1263229Spst}
1273229Spst
1283229Spst/*
1293229Spst * Local Variables:
1303229Spst * tab-width: 4
1313229Spst * c-indent-level: 4
1323229Spst * c-argdecl-indent: 4
1333229Spst * c-continued-statement-offset: 4
1343229Spst * c-continued-brace-offset: -4
1353229Spst * c-label-offset: -4
1363229Spst * c-brace-offset: 0
1373229Spst * End:
1383229Spst */
139