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