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