report.c revision 3229
13229Spst/*
23229Spst * report() - calls syslog
33229Spst */
43229Spst
53229Spst#ifdef	__STDC__
63229Spst#include <stdarg.h>
73229Spst#else
83229Spst#include <varargs.h>
93229Spst#endif
103229Spst
113229Spst#include <stdio.h>
123229Spst#include <syslog.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 */
913229Spst#ifdef	__STDC__
923229Spstvoid
933229Spstreport(int priority, char *fmt,...)
943229Spst#else
953229Spst/*VARARGS2*/
963229Spstvoid
973229Spstreport(priority, fmt, va_alist)
983229Spst	int priority;
993229Spst	char *fmt;
1003229Spst	va_dcl
1013229Spst#endif
1023229Spst{
1033229Spst	va_list ap;
1043229Spst	static char buf[128];
1053229Spst
1063229Spst	if ((priority < 0) || (priority >= numlevels)) {
1073229Spst		priority = numlevels - 1;
1083229Spst	}
1093229Spst#ifdef	__STDC__
1103229Spst	va_start(ap, fmt);
1113229Spst#else
1123229Spst	va_start(ap);
1133229Spst#endif
1143229Spst	vsprintf(buf, fmt, ap);
1153229Spst	va_end(ap);
1163229Spst
1173229Spst	/*
1183229Spst	 * Print the message
1193229Spst	 */
1203229Spst	if (stderr_only || (debug > 2)) {
1213229Spst		fprintf(stderr, "%s: %s %s\n",
1223229Spst				progname, levelnames[priority], buf);
1233229Spst	}
1243229Spst#ifdef SYSLOG
1253229Spst	if (!stderr_only)
1263229Spst		syslog((priority | LOG_BOOTP), "%s", buf);
1273229Spst#endif
1283229Spst}
1293229Spst
1303229Spst
1313229Spst
1323229Spst/*
1333229Spst * Return pointer to static string which gives full filesystem error message.
1343229Spst */
1353229Spstchar *
1363229Spstget_errmsg()
1373229Spst{
1383229Spst	extern int errno;
1393229Spst	extern char *strerror();
1403229Spst
1413229Spst	return strerror(errno);
1423229Spst}
1433229Spst
1443229Spst/*
1453229Spst * Local Variables:
1463229Spst * tab-width: 4
1473229Spst * c-indent-level: 4
1483229Spst * c-argdecl-indent: 4
1493229Spst * c-continued-statement-offset: 4
1503229Spst * c-continued-brace-offset: -4
1513229Spst * c-label-offset: -4
1523229Spst * c-brace-offset: 0
1533229Spst * End:
1543229Spst */
155