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