report.c revision 65426
1275698Sdelphij/* $FreeBSD: head/libexec/bootpd/report.c 65426 2000-09-04 05:48:09Z imp $ */
2275698Sdelphij
3275698Sdelphij/*
4275698Sdelphij * report() - calls syslog
5275698Sdelphij */
6275698Sdelphij
7275698Sdelphij#ifdef	__STDC__
8275698Sdelphij#include <stdarg.h>
9275698Sdelphij#else
10275698Sdelphij#include <varargs.h>
11275698Sdelphij#endif
12275698Sdelphij
13275698Sdelphij#include <stdio.h>
14275698Sdelphij#include <syslog.h>
15275698Sdelphij#include <string.h>
16275698Sdelphij#include <errno.h>
17275698Sdelphij
18275698Sdelphij#include "report.h"
19275698Sdelphij
20275698Sdelphij#ifndef LOG_NDELAY
21275698Sdelphij#define LOG_NDELAY	0
22275698Sdelphij#endif
23275698Sdelphij#ifndef LOG_DAEMON
24275698Sdelphij#define LOG_DAEMON	0
25275698Sdelphij#endif
26275698Sdelphij#ifndef	LOG_BOOTP
27275698Sdelphij#define LOG_BOOTP	LOG_DAEMON
28275698Sdelphij#endif
29275698Sdelphij
30275698Sdelphijextern int debug;
31275698Sdelphijextern char *progname;
32275698Sdelphij
33275698Sdelphij/*
34275698Sdelphij * This is initialized so you get stderr until you call
35275698Sdelphij *	report_init()
36275698Sdelphij */
37275698Sdelphijstatic int stderr_only = 1;
38275698Sdelphij
39275698Sdelphijvoid
40275698Sdelphijreport_init(nolog)
41275698Sdelphij	int nolog;
42275698Sdelphij{
43275698Sdelphij	stderr_only = nolog;
44275698Sdelphij#ifdef SYSLOG
45275698Sdelphij	if (!stderr_only) {
46275698Sdelphij		openlog(progname, LOG_PID | LOG_NDELAY, LOG_BOOTP);
47275698Sdelphij	}
48275698Sdelphij#endif
49275698Sdelphij}
50275698Sdelphij
51275698Sdelphij/*
52275698Sdelphij * This routine reports errors and such via stderr and syslog() if
53275698Sdelphij * appopriate.  It just helps avoid a lot of "#ifdef SYSLOG" constructs
54275698Sdelphij * from being scattered throughout the code.
55275698Sdelphij *
56275698Sdelphij * The syntax is identical to syslog(3), but %m is not considered special
57275698Sdelphij * for output to stderr (i.e. you'll see "%m" in the output. . .).  Also,
58275698Sdelphij * control strings should normally end with \n since newlines aren't
59275698Sdelphij * automatically generated for stderr output (whereas syslog strips out all
60275698Sdelphij * newlines and adds its own at the end).
61275698Sdelphij */
62275698Sdelphij
63275698Sdelphijstatic char *levelnames[] = {
64275698Sdelphij#ifdef LOG_SALERT
65275698Sdelphij	"level(0): ",
66275698Sdelphij	"alert(1): ",
67267897Sdelphij	"alert(2): ",
68267897Sdelphij	"emerg(3): ",
69267897Sdelphij	"error(4): ",
70267897Sdelphij	"crit(5):  ",
71267897Sdelphij	"warn(6):  ",
72267897Sdelphij	"note(7):  ",
73267897Sdelphij	"info(8):  ",
74267897Sdelphij	"debug(9): ",
75267897Sdelphij	"level(?): "
76267897Sdelphij#else
77267897Sdelphij	"emerg(0): ",
78267897Sdelphij	"alert(1): ",
79267897Sdelphij	"crit(2):  ",
80267897Sdelphij	"error(3): ",
81267897Sdelphij	"warn(4):  ",
82267897Sdelphij	"note(5):  ",
83267897Sdelphij	"info(6):  ",
84267897Sdelphij	"debug(7): ",
85267897Sdelphij	"level(?): "
86267897Sdelphij#endif
87267897Sdelphij};
88267897Sdelphijstatic int numlevels = sizeof(levelnames) / sizeof(levelnames[0]);
89267897Sdelphij
90267897Sdelphij
91267897Sdelphij/*
92267897Sdelphij * Print a log message using syslog(3) and/or stderr.
93267897Sdelphij * The message passed in should not include a newline.
94267897Sdelphij */
95267897Sdelphij#ifdef	__STDC__
96267897Sdelphijvoid
97267897Sdelphijreport(int priority, char *fmt,...)
98267897Sdelphij#else
99267897Sdelphij/*VARARGS2*/
100267897Sdelphijvoid
101267897Sdelphijreport(priority, fmt, va_alist)
102267897Sdelphij	int priority;
103267897Sdelphij	char *fmt;
104267897Sdelphij	va_dcl
105267897Sdelphij#endif
106267897Sdelphij{
107267897Sdelphij	va_list ap;
108267897Sdelphij	static char buf[128];
109267897Sdelphij
110267897Sdelphij	if ((priority < 0) || (priority >= numlevels)) {
111267897Sdelphij		priority = numlevels - 1;
112267897Sdelphij	}
113267897Sdelphij#ifdef	__STDC__
114267897Sdelphij	va_start(ap, fmt);
115267897Sdelphij#else
116267897Sdelphij	va_start(ap);
117267897Sdelphij#endif
118267897Sdelphij	vsnprintf(buf, sizeof(buf), fmt, ap);
119267897Sdelphij	va_end(ap);
120267897Sdelphij
121267897Sdelphij	/*
122267897Sdelphij	 * Print the message
123267897Sdelphij	 */
124267897Sdelphij	if (stderr_only || (debug > 2)) {
125267897Sdelphij		fprintf(stderr, "%s: %s %s\n",
126267897Sdelphij				progname, levelnames[priority], buf);
127267897Sdelphij	}
128267897Sdelphij#ifdef SYSLOG
129267897Sdelphij	if (!stderr_only)
130267897Sdelphij		syslog((priority | LOG_BOOTP), "%s", buf);
131267897Sdelphij#endif
132267897Sdelphij}
133267897Sdelphij
134267897Sdelphij
135267897Sdelphij
136267897Sdelphij/*
137267897Sdelphij * Return pointer to static string which gives full filesystem error message.
138267897Sdelphij */
139267897Sdelphijchar *
140267897Sdelphijget_errmsg()
141267897Sdelphij{
142267897Sdelphij	return strerror(errno);
143267897Sdelphij}
144267897Sdelphij
145267897Sdelphij/*
146267897Sdelphij * Local Variables:
147267897Sdelphij * tab-width: 4
148267897Sdelphij * c-indent-level: 4
149267897Sdelphij * c-argdecl-indent: 4
150267897Sdelphij * c-continued-statement-offset: 4
151267897Sdelphij * c-continued-brace-offset: -4
152267897Sdelphij * c-label-offset: -4
153267897Sdelphij * c-brace-offset: 0
154267897Sdelphij * End:
155267897Sdelphij */
156267897Sdelphij