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