/[imapfilter]/imapfilter/log.c
ViewVC logotype

Contents of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.41 - (show annotations)
Mon Feb 9 19:56:42 2004 UTC (20 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.40: +5 -4 lines
File MIME type: text/plain
Add verbosity levels and remove the no warning command line option.

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <stdarg.h>
6 #include <limits.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <time.h>
10 #include <signal.h>
11
12 #include "config.h"
13 #include "imapfilter.h"
14 #include "pathnames.h"
15
16
17 extern conn_t connpri, connaux;
18 extern char logfile[PATH_MAX];
19 extern unsigned int options;
20 extern int verbosity;
21 extern unsigned int flags;
22
23 static FILE *debugfp = NULL; /* Pointer to debug file. */
24 static FILE *logfp = NULL; /* Pointer to logfile. */
25
26
27 void signal_handler(int sig);
28 char *get_time(void);
29
30
31 /*
32 * Print message if not in the lowest verbosity level.
33 */
34 void
35 info(const char *info,...)
36 {
37 va_list args;
38
39 if (verbosity == -2)
40 return;
41
42 va_start(args, info);
43 vprintf(info, args);
44 va_end(args);
45 }
46
47
48 /*
49 * Print message if in the highest verbosity level.
50 */
51 void
52 verbose(const char *info,...)
53 {
54 va_list args;
55
56 if (verbosity == 2) {
57 va_start(args, info);
58 vprintf(info, args);
59 va_end(args);
60 }
61 }
62
63
64 /*
65 * Write message to debug file.
66 */
67 void
68 debug(const char *debug,...)
69 {
70 va_list args;
71
72 if ((options & OPTION_DEBUG) && debugfp) {
73 va_start(args, debug);
74 vfprintf(debugfp, debug, args);
75 fflush(debugfp);
76 va_end(args);
77 }
78 }
79
80
81 /*
82 * Print error message and write it into logfile.
83 */
84 void
85 error(const char *errmsg,...)
86 {
87 va_list args;
88
89 va_start(args, errmsg);
90
91 fprintf(stderr, "imapfilter: ");
92 vfprintf(stderr, errmsg, args);
93
94 if ((options & OPTION_ERRORS) && logfp) {
95 vfprintf(logfp, errmsg, args);
96 fflush(logfp);
97 }
98 va_end(args);
99 }
100
101
102 /*
103 * Print error message and exit program.
104 */
105 void
106 fatal(unsigned int errnum, const char *fatal,...)
107 {
108 va_list args;
109
110 va_start(args, fatal);
111 fprintf(stderr, "imapfilter: ");
112 vfprintf(stderr, fatal, args);
113 va_end(args);
114
115 close_connection(&connpri);
116 close_connection(&connaux);
117 secmem_clear();
118 tty_restore();
119 log_stop();
120 lockfile_remove();
121 debug_stop();
122
123 exit(errnum);
124 }
125
126
127 /*
128 * Catch signals that cause rereading of the configuration file or program's
129 * termination.
130 */
131 void
132 catch_signals(void)
133 {
134 signal(SIGUSR1, signal_handler);
135 signal(SIGINT, signal_handler);
136 signal(SIGQUIT, signal_handler);
137 signal(SIGTERM, signal_handler);
138 }
139
140
141 /*
142 * Signal handler for signals that cause rereading of the configuration file or
143 * termination of program.
144 */
145 void
146 signal_handler(int sig)
147 {
148 if (sig == SIGUSR1)
149 flags |= FLAG_SIGUSR1_RECEIVED;
150 else
151 fatal(ERROR_SIGNAL, "killed by signal %d\n", sig);
152 }
153
154
155 /*
156 * Open temporary debug file and associate a stream with the returned file
157 * descriptor.
158 */
159 int
160 debug_start(void)
161 {
162 static char dt[] = PATHNAME_DEBUG_FILE;
163 int fd;
164
165 if (!(options & OPTION_DEBUG))
166 return 0;
167
168 fd = mkstemp(dt);
169
170 if (fd != -1) {
171 debugfp = fdopen(fd, "w");
172 if (debugfp == NULL) {
173 error("opening debug file %s: %s\n", logfile,
174 strerror(errno));
175 return ERROR_TRIVIAL;
176 }
177 }
178 return 0;
179 }
180
181
182 /*
183 * Close temporary debug file.
184 */
185 int
186 debug_stop(void)
187 {
188 if (debugfp == NULL)
189 return 0;
190 else
191 return fclose(debugfp);
192 }
193
194
195 /*
196 * Open the file for saving of logging information.
197 */
198 int
199 log_start(void)
200 {
201 if (*logfile == '\0')
202 return 0; /* Logging not enabled. */
203
204 debug("log file: '%s'\n", logfile);
205
206 if (create_file(logfile, S_IRUSR | S_IWUSR))
207 return ERROR_TRIVIAL;
208
209 logfp = fopen(logfile, "a");
210 if (logfp == NULL) {
211 error("opening log file %s: %s\n", logfile, strerror(errno));
212 return ERROR_TRIVIAL;
213 }
214 return 0;
215 }
216
217
218 /*
219 * Close the log file.
220 */
221 int
222 log_stop(void)
223 {
224 if (logfp == NULL)
225 return 0;
226 else
227 return fclose(logfp);
228 }
229
230
231 /*
232 * Prepares the log entry to be saved through continues calls, and writes it
233 * to logfile.
234 */
235 void
236 log_info(int flag, void *ptr)
237 {
238 static struct {
239 char *account;
240 char *mbox;
241 char *filter;
242 unsigned int *action;
243 char *destaccount;
244 char *destmbox;
245 char *hdrs;
246 } inf = {
247 NULL, NULL, NULL, NULL, NULL, NULL, NULL
248 };
249
250 if (logfp == NULL)
251 return;
252
253 switch (flag) {
254 case LOG_PREAMBLE:
255 fprintf(logfp, "%s %s %s %s %s%s %s\n", get_time(),
256 inf.account, inf.mbox, inf.filter,
257 (*inf.action == FILTER_ACTION_DELETE ? "delete" :
258 *inf.action == FILTER_ACTION_COPY ? "copy" :
259 *inf.action == FILTER_ACTION_MOVE ? "move" :
260 *inf.action == FILTER_ACTION_RCOPY ? "rcopy " :
261 *inf.action == FILTER_ACTION_RMOVE ? "rmove " :
262 *inf.action == FILTER_ACTION_FLAG_ADD ||
263 *inf.action == FILTER_ACTION_FLAG_REMOVE ||
264 *inf.action == FILTER_ACTION_FLAG_REPLACE ? "flag" :
265 *inf.action == FILTER_ACTION_LIST ? "list" :
266 "unknown "),
267 (inf.destaccount == NULL ? "" : inf.destaccount),
268 (*inf.destmbox == '\0' ? "" : inf.destmbox));
269 fflush(logfp);
270 break;
271 case LOG_ACCOUNT:
272 inf.account = (char *)ptr;
273 break;
274 case LOG_MAILBOX:
275 inf.mbox = (char *)ptr;
276 break;
277 case LOG_FILTER:
278 inf.filter = (char *)ptr;
279 break;
280 case LOG_ACTION:
281 inf.action = (unsigned int *)ptr;
282 break;
283 case LOG_DESTINATION_ACCOUNT:
284 if (ptr == NULL)
285 inf.destaccount = NULL;
286 else
287 inf.destaccount = ((account_t *) ptr)->key;
288 break;
289 case LOG_DESTINATION_MAILBOX:
290 inf.destmbox = (char *)ptr;
291 break;
292 case LOG_HEADER:
293 if (ptr) {
294 inf.hdrs = (char *)ptr;
295 fputc('\t', logfp);
296 while (*inf.hdrs != '\0') {
297 if (*inf.hdrs == '\r') {
298 fputc('\n', logfp);
299 if (*(inf.hdrs + 2) != '\0')
300 fputc('\t', logfp);
301 inf.hdrs += 2;
302 } else
303 fputc(*(inf.hdrs++), logfp);
304 }
305 }
306 fflush(logfp);
307 break;
308 }
309 }
310
311
312 /*
313 * Return current local time and date.
314 */
315 char *
316 get_time(void)
317 {
318 char *ct;
319 time_t t;
320
321 t = time(NULL);
322
323 ct = ctime(&t);
324 *(strchr(ct, '\n')) = '\0';
325
326 return ct;
327 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26