xref: /freebsd/libexec/bootpd/report.c (revision 9bc34f7ce14c4401e6ee449f9eeb2c9222fbe756)
144099b7bSPaul Traina /*
244099b7bSPaul Traina  * report() - calls syslog
344099b7bSPaul Traina  */
444099b7bSPaul Traina 
544099b7bSPaul Traina #ifdef	__STDC__
644099b7bSPaul Traina #include <stdarg.h>
744099b7bSPaul Traina #else
844099b7bSPaul Traina #include <varargs.h>
944099b7bSPaul Traina #endif
1044099b7bSPaul Traina 
1144099b7bSPaul Traina #include <stdio.h>
1244099b7bSPaul Traina #include <syslog.h>
1344099b7bSPaul Traina 
1444099b7bSPaul Traina #include "report.h"
1544099b7bSPaul Traina 
1644099b7bSPaul Traina #ifndef LOG_NDELAY
1744099b7bSPaul Traina #define LOG_NDELAY	0
1844099b7bSPaul Traina #endif
1944099b7bSPaul Traina #ifndef LOG_DAEMON
2044099b7bSPaul Traina #define LOG_DAEMON	0
2144099b7bSPaul Traina #endif
2244099b7bSPaul Traina #ifndef	LOG_BOOTP
2344099b7bSPaul Traina #define LOG_BOOTP	LOG_DAEMON
2444099b7bSPaul Traina #endif
2544099b7bSPaul Traina 
2644099b7bSPaul Traina extern int debug;
2744099b7bSPaul Traina extern char *progname;
2844099b7bSPaul Traina 
2944099b7bSPaul Traina /*
3044099b7bSPaul Traina  * This is initialized so you get stderr until you call
3144099b7bSPaul Traina  *	report_init()
3244099b7bSPaul Traina  */
3344099b7bSPaul Traina static int stderr_only = 1;
3444099b7bSPaul Traina 
3544099b7bSPaul Traina void
3644099b7bSPaul Traina report_init(nolog)
3744099b7bSPaul Traina 	int nolog;
3844099b7bSPaul Traina {
3944099b7bSPaul Traina 	stderr_only = nolog;
4044099b7bSPaul Traina #ifdef SYSLOG
4144099b7bSPaul Traina 	if (!stderr_only) {
4244099b7bSPaul Traina 		openlog(progname, LOG_PID | LOG_NDELAY, LOG_BOOTP);
4344099b7bSPaul Traina 	}
4444099b7bSPaul Traina #endif
4544099b7bSPaul Traina }
4644099b7bSPaul Traina 
4744099b7bSPaul Traina /*
4844099b7bSPaul Traina  * This routine reports errors and such via stderr and syslog() if
4944099b7bSPaul Traina  * appopriate.  It just helps avoid a lot of "#ifdef SYSLOG" constructs
5044099b7bSPaul Traina  * from being scattered throughout the code.
5144099b7bSPaul Traina  *
5244099b7bSPaul Traina  * The syntax is identical to syslog(3), but %m is not considered special
5344099b7bSPaul Traina  * for output to stderr (i.e. you'll see "%m" in the output. . .).  Also,
5444099b7bSPaul Traina  * control strings should normally end with \n since newlines aren't
5544099b7bSPaul Traina  * automatically generated for stderr output (whereas syslog strips out all
5644099b7bSPaul Traina  * newlines and adds its own at the end).
5744099b7bSPaul Traina  */
5844099b7bSPaul Traina 
5944099b7bSPaul Traina static char *levelnames[] = {
6044099b7bSPaul Traina #ifdef LOG_SALERT
6144099b7bSPaul Traina 	"level(0): ",
6244099b7bSPaul Traina 	"alert(1): ",
6344099b7bSPaul Traina 	"alert(2): ",
6444099b7bSPaul Traina 	"emerg(3): ",
6544099b7bSPaul Traina 	"error(4): ",
6644099b7bSPaul Traina 	"crit(5):  ",
6744099b7bSPaul Traina 	"warn(6):  ",
6844099b7bSPaul Traina 	"note(7):  ",
6944099b7bSPaul Traina 	"info(8):  ",
7044099b7bSPaul Traina 	"debug(9): ",
7144099b7bSPaul Traina 	"level(?): "
7244099b7bSPaul Traina #else
7344099b7bSPaul Traina 	"emerg(0): ",
7444099b7bSPaul Traina 	"alert(1): ",
7544099b7bSPaul Traina 	"crit(2):  ",
7644099b7bSPaul Traina 	"error(3): ",
7744099b7bSPaul Traina 	"warn(4):  ",
7844099b7bSPaul Traina 	"note(5):  ",
7944099b7bSPaul Traina 	"info(6):  ",
8044099b7bSPaul Traina 	"debug(7): ",
8144099b7bSPaul Traina 	"level(?): "
8244099b7bSPaul Traina #endif
8344099b7bSPaul Traina };
8444099b7bSPaul Traina static int numlevels = sizeof(levelnames) / sizeof(levelnames[0]);
8544099b7bSPaul Traina 
8644099b7bSPaul Traina 
8744099b7bSPaul Traina /*
8844099b7bSPaul Traina  * Print a log message using syslog(3) and/or stderr.
8944099b7bSPaul Traina  * The message passed in should not include a newline.
9044099b7bSPaul Traina  */
9144099b7bSPaul Traina #ifdef	__STDC__
9244099b7bSPaul Traina void
9344099b7bSPaul Traina report(int priority, char *fmt,...)
9444099b7bSPaul Traina #else
9544099b7bSPaul Traina /*VARARGS2*/
9644099b7bSPaul Traina void
9744099b7bSPaul Traina report(priority, fmt, va_alist)
9844099b7bSPaul Traina 	int priority;
9944099b7bSPaul Traina 	char *fmt;
10044099b7bSPaul Traina 	va_dcl
10144099b7bSPaul Traina #endif
10244099b7bSPaul Traina {
10344099b7bSPaul Traina 	va_list ap;
10444099b7bSPaul Traina 	static char buf[128];
10544099b7bSPaul Traina 
10644099b7bSPaul Traina 	if ((priority < 0) || (priority >= numlevels)) {
10744099b7bSPaul Traina 		priority = numlevels - 1;
10844099b7bSPaul Traina 	}
10944099b7bSPaul Traina #ifdef	__STDC__
11044099b7bSPaul Traina 	va_start(ap, fmt);
11144099b7bSPaul Traina #else
11244099b7bSPaul Traina 	va_start(ap);
11344099b7bSPaul Traina #endif
1149bc34f7cSWarner Losh 	vsnprintf(buf, sizeof(buf), fmt, ap);
11544099b7bSPaul Traina 	va_end(ap);
11644099b7bSPaul Traina 
11744099b7bSPaul Traina 	/*
11844099b7bSPaul Traina 	 * Print the message
11944099b7bSPaul Traina 	 */
12044099b7bSPaul Traina 	if (stderr_only || (debug > 2)) {
12144099b7bSPaul Traina 		fprintf(stderr, "%s: %s %s\n",
12244099b7bSPaul Traina 				progname, levelnames[priority], buf);
12344099b7bSPaul Traina 	}
12444099b7bSPaul Traina #ifdef SYSLOG
12544099b7bSPaul Traina 	if (!stderr_only)
12644099b7bSPaul Traina 		syslog((priority | LOG_BOOTP), "%s", buf);
12744099b7bSPaul Traina #endif
12844099b7bSPaul Traina }
12944099b7bSPaul Traina 
13044099b7bSPaul Traina 
13144099b7bSPaul Traina 
13244099b7bSPaul Traina /*
13344099b7bSPaul Traina  * Return pointer to static string which gives full filesystem error message.
13444099b7bSPaul Traina  */
13544099b7bSPaul Traina char *
13644099b7bSPaul Traina get_errmsg()
13744099b7bSPaul Traina {
13844099b7bSPaul Traina 	extern int errno;
13944099b7bSPaul Traina 	extern char *strerror();
14044099b7bSPaul Traina 
14144099b7bSPaul Traina 	return strerror(errno);
14244099b7bSPaul Traina }
14344099b7bSPaul Traina 
14444099b7bSPaul Traina /*
14544099b7bSPaul Traina  * Local Variables:
14644099b7bSPaul Traina  * tab-width: 4
14744099b7bSPaul Traina  * c-indent-level: 4
14844099b7bSPaul Traina  * c-argdecl-indent: 4
14944099b7bSPaul Traina  * c-continued-statement-offset: 4
15044099b7bSPaul Traina  * c-continued-brace-offset: -4
15144099b7bSPaul Traina  * c-label-offset: -4
15244099b7bSPaul Traina  * c-brace-offset: 0
15344099b7bSPaul Traina  * End:
15444099b7bSPaul Traina  */
155