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