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