xref: /freebsd/libexec/bootpd/report.c (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 /* $FreeBSD$ */
2 
3 /*
4  * report() - calls syslog
5  */
6 
7 #include <stdarg.h>
8 
9 #include <stdio.h>
10 #include <syslog.h>
11 #include <string.h>
12 #include <errno.h>
13 
14 #include "report.h"
15 
16 #ifndef LOG_NDELAY
17 #define LOG_NDELAY	0
18 #endif
19 #ifndef LOG_DAEMON
20 #define LOG_DAEMON	0
21 #endif
22 #ifndef	LOG_BOOTP
23 #define LOG_BOOTP	LOG_DAEMON
24 #endif
25 
26 extern int debug;
27 extern char *progname;
28 
29 /*
30  * This is initialized so you get stderr until you call
31  *	report_init()
32  */
33 static int stderr_only = 1;
34 
35 void
36 report_init(int nolog)
37 {
38 	stderr_only = nolog;
39 #ifdef SYSLOG
40 	if (!stderr_only) {
41 		openlog(progname, LOG_PID | LOG_NDELAY, LOG_BOOTP);
42 	}
43 #endif
44 }
45 
46 /*
47  * This routine reports errors and such via stderr and syslog() if
48  * appopriate.  It just helps avoid a lot of "#ifdef SYSLOG" constructs
49  * from being scattered throughout the code.
50  *
51  * The syntax is identical to syslog(3), but %m is not considered special
52  * for output to stderr (i.e. you'll see "%m" in the output. . .).  Also,
53  * control strings should normally end with \n since newlines aren't
54  * automatically generated for stderr output (whereas syslog strips out all
55  * newlines and adds its own at the end).
56  */
57 
58 static char *levelnames[] = {
59 #ifdef LOG_SALERT
60 	"level(0): ",
61 	"alert(1): ",
62 	"alert(2): ",
63 	"emerg(3): ",
64 	"error(4): ",
65 	"crit(5):  ",
66 	"warn(6):  ",
67 	"note(7):  ",
68 	"info(8):  ",
69 	"debug(9): ",
70 	"level(?): "
71 #else
72 	"emerg(0): ",
73 	"alert(1): ",
74 	"crit(2):  ",
75 	"error(3): ",
76 	"warn(4):  ",
77 	"note(5):  ",
78 	"info(6):  ",
79 	"debug(7): ",
80 	"level(?): "
81 #endif
82 };
83 static int numlevels = sizeof(levelnames) / sizeof(levelnames[0]);
84 
85 
86 /*
87  * Print a log message using syslog(3) and/or stderr.
88  * The message passed in should not include a newline.
89  */
90 void
91 report(int priority, const char *fmt,...)
92 {
93 	va_list ap;
94 	static char buf[128];
95 
96 	if ((priority < 0) || (priority >= numlevels)) {
97 		priority = numlevels - 1;
98 	}
99 	va_start(ap, fmt);
100 	vsnprintf(buf, sizeof(buf), fmt, ap);
101 	va_end(ap);
102 
103 	/*
104 	 * Print the message
105 	 */
106 	if (stderr_only || (debug > 2)) {
107 		fprintf(stderr, "%s: %s %s\n",
108 				progname, levelnames[priority], buf);
109 	}
110 #ifdef SYSLOG
111 	if (!stderr_only)
112 		syslog((priority | LOG_BOOTP), "%s", buf);
113 #endif
114 }
115 
116 
117 
118 /*
119  * Return pointer to static string which gives full filesystem error message.
120  */
121 const char *
122 get_errmsg(void)
123 {
124 	return strerror(errno);
125 }
126 
127 /*
128  * Local Variables:
129  * tab-width: 4
130  * c-indent-level: 4
131  * c-argdecl-indent: 4
132  * c-continued-statement-offset: 4
133  * c-continued-brace-offset: -4
134  * c-label-offset: -4
135  * c-brace-offset: 0
136  * End:
137  */
138