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