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

Contents of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26