This source file includes following definitions.
- xopenlog
- xcloselog
- make_msg
- slog
- log_e
- explain
- explain_e
- complain
- complain_e
- die
- die_e
- xdebug
- xdebug_e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 #include <unistd.h>
39 #include <syslog.h>
40 #include <stdio.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <signal.h>
44 #include <sys/types.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include "global.h"
48
49 static char truncated[] = " (truncated)";
50 static char msg[MAX_MSG + 1];
51 static int log_open = 0;
52
53
54 int complaints = 0;
55
56 static void
57 xopenlog(void)
58 {
59 if (!log_open)
60 {
61 openlog(program_name, LOG_PID, SYSLOG_FACILITY);
62 log_open = 1;
63 }
64 }
65
66 void
67 xcloselog(void)
68 {
69 if (log_open) closelog();
70 log_open = 0;
71 }
72
73 static void
74 make_msg(const char *fmt, va_list args)
75
76 {
77 int len;
78
79
80
81 len = vsnprintf(msg, sizeof(msg), fmt, args);
82 if (len >= sizeof(msg) - 1)
83 strcpy(msg + sizeof(msg) - sizeof(truncated), truncated);
84 }
85
86 static void
87 slog(int priority, const char *fmt, va_list args)
88
89
90 {
91 make_msg(fmt, args);
92 xopenlog();
93 syslog(priority, "%s", msg);
94 if (!in_background)
95 {
96 if (priority == EXPLAIN_LEVEL && !quiet)
97 fprintf(stderr, "%s\n", msg);
98 else if (priority == COMPLAIN_LEVEL)
99 fprintf(stderr, "%s: %s\n", program_name, msg);
100 }
101 }
102
103 static void
104 log_e(int priority, const char *fmt, va_list args)
105
106
107 {
108 int saved_errno;
109
110 saved_errno = errno;
111 make_msg(fmt, args);
112 xopenlog();
113 syslog(priority, "%s: %s", msg, strerror(saved_errno));
114 if (!in_background)
115 {
116 if (priority == EXPLAIN_LEVEL && !quiet)
117 fprintf(stderr, "%s: %s\n", msg, strerror(saved_errno));
118 else if (priority == COMPLAIN_LEVEL)
119 fprintf(stderr, "%s: %s: %s\n",
120 program_name, msg, strerror(saved_errno));
121 }
122 }
123
124 void
125 explain(const char *fmt, ...)
126
127 {
128 va_list args;
129
130 va_start(args, fmt);
131 slog(EXPLAIN_LEVEL, fmt, args);
132 va_end(args);
133 }
134
135 void
136 explain_e(const char *fmt, ...)
137
138 {
139 va_list args;
140
141 va_start(args, fmt);
142 log_e(EXPLAIN_LEVEL, fmt, args);
143 va_end(args);
144 }
145
146 void
147 complain(const char *fmt, ...)
148
149 {
150 va_list args;
151
152 va_start(args, fmt);
153 slog(COMPLAIN_LEVEL, fmt, args);
154 va_end(args);
155
156 complaints += 1;
157 }
158
159 void
160 complain_e(const char *fmt, ...)
161
162 {
163 va_list args;
164
165 va_start(args, fmt);
166 log_e(COMPLAIN_LEVEL, fmt, args);
167 va_end(args);
168
169 complaints += 1;
170 }
171
172 void
173 die(const char *fmt, ...)
174
175 {
176 va_list args;
177
178 va_start(args, fmt);
179 slog(COMPLAIN_LEVEL, fmt, args);
180 va_end(args);
181 if (getpid() == primary_pid) complain("Aborted");
182
183 exit(FAILURE_EXIT);
184 }
185
186 void
187 die_e(const char *fmt, ...)
188
189 {
190 va_list args;
191
192 va_start(args, fmt);
193 log_e(COMPLAIN_LEVEL, fmt, args);
194 va_end(args);
195 if (getpid() == primary_pid) complain("Aborted");
196
197 exit(FAILURE_EXIT);
198 }
199
200 #ifdef DEBUG
201
202
203
204
205 void
206 xdebug(const char *fmt, ...)
207 {
208 va_list args;
209
210 va_start(args, fmt);
211 slog(DEBUG_LEVEL, fmt, args);
212 va_end(args);
213 }
214
215 void
216 xdebug_e(const char *fmt, ...)
217 {
218 va_list args;
219
220 va_start(args, fmt);
221 log_e(DEBUG_LEVEL, fmt, args);
222 va_end(args);
223 }
224
225 #endif