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