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

Annotation of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.42 - (hide annotations)
Tue Feb 10 22:21:09 2004 UTC (20 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.41: +13 -14 lines
File MIME type: text/plain
Replaced integer options and bitwise OPTION_* with an options struct.

1 lefcha 1.1 #include <stdio.h>
2 lefcha 1.35 #include <stdlib.h>
3 lefcha 1.34 #include <string.h>
4 lefcha 1.1 #include <errno.h>
5 lefcha 1.35 #include <stdarg.h>
6 lefcha 1.34 #include <limits.h>
7 lefcha 1.38 #include <sys/types.h>
8 lefcha 1.9 #include <sys/stat.h>
9 lefcha 1.12 #include <time.h>
10 lefcha 1.38 #include <signal.h>
11 lefcha 1.1
12     #include "config.h"
13     #include "imapfilter.h"
14 lefcha 1.40 #include "pathnames.h"
15 lefcha 1.1
16    
17 lefcha 1.33 extern conn_t connpri, connaux;
18 lefcha 1.42 extern opts_t opts;
19 lefcha 1.32 extern unsigned int flags;
20 lefcha 1.6
21 lefcha 1.40 static FILE *debugfp = NULL; /* Pointer to debug file. */
22 lefcha 1.10 static FILE *logfp = NULL; /* Pointer to logfile. */
23 lefcha 1.34
24    
25     void signal_handler(int sig);
26     char *get_time(void);
27 lefcha 1.1
28    
29     /*
30 lefcha 1.41 * Print message if not in the lowest verbosity level.
31 lefcha 1.7 */
32 lefcha 1.30 void
33     info(const char *info,...)
34 lefcha 1.7 {
35 lefcha 1.30 va_list args;
36 lefcha 1.14
37 lefcha 1.42 if (opts.verbosity == -2)
38 lefcha 1.30 return;
39 lefcha 1.14
40 lefcha 1.30 va_start(args, info);
41     vprintf(info, args);
42     va_end(args);
43 lefcha 1.12 }
44    
45    
46     /*
47 lefcha 1.41 * Print message if in the highest verbosity level.
48 lefcha 1.1 */
49 lefcha 1.30 void
50     verbose(const char *info,...)
51 lefcha 1.1 {
52 lefcha 1.30 va_list args;
53 lefcha 1.1
54 lefcha 1.42 if (opts.verbosity == 2) {
55 lefcha 1.30 va_start(args, info);
56     vprintf(info, args);
57     va_end(args);
58     }
59 lefcha 1.1 }
60    
61 lefcha 1.17
62 lefcha 1.1 /*
63 lefcha 1.40 * Write message to debug file.
64     */
65     void
66     debug(const char *debug,...)
67     {
68     va_list args;
69    
70 lefcha 1.42 if (opts.debug && debugfp) {
71 lefcha 1.40 va_start(args, debug);
72     vfprintf(debugfp, debug, args);
73     fflush(debugfp);
74     va_end(args);
75     }
76     }
77    
78    
79     /*
80 lefcha 1.10 * Print error message and write it into logfile.
81 lefcha 1.1 */
82 lefcha 1.30 void
83     error(const char *errmsg,...)
84 lefcha 1.1 {
85 lefcha 1.30 va_list args;
86 lefcha 1.1
87 lefcha 1.30 va_start(args, errmsg);
88 lefcha 1.1
89 lefcha 1.30 fprintf(stderr, "imapfilter: ");
90     vfprintf(stderr, errmsg, args);
91 lefcha 1.14
92 lefcha 1.42 if (opts.errors && logfp) {
93 lefcha 1.30 vfprintf(logfp, errmsg, args);
94     fflush(logfp);
95     }
96     va_end(args);
97 lefcha 1.1 }
98    
99    
100     /*
101 lefcha 1.12 * Print error message and exit program.
102     */
103 lefcha 1.30 void
104     fatal(unsigned int errnum, const char *fatal,...)
105 lefcha 1.12 {
106 lefcha 1.30 va_list args;
107    
108     va_start(args, fatal);
109     fprintf(stderr, "imapfilter: ");
110     vfprintf(stderr, fatal, args);
111     va_end(args);
112 lefcha 1.14
113 lefcha 1.33 close_connection(&connpri);
114     close_connection(&connaux);
115 lefcha 1.30 secmem_clear();
116     tty_restore();
117 lefcha 1.40 log_stop();
118 lefcha 1.30 lockfile_remove();
119 lefcha 1.40 debug_stop();
120 lefcha 1.14
121 lefcha 1.30 exit(errnum);
122 lefcha 1.12 }
123 lefcha 1.14
124 lefcha 1.18
125     /*
126 lefcha 1.40 * Catch signals that cause rereading of the configuration file or program's
127     * termination.
128 lefcha 1.18 */
129 lefcha 1.30 void
130     catch_signals(void)
131 lefcha 1.18 {
132 lefcha 1.36 signal(SIGUSR1, signal_handler);
133 lefcha 1.30 signal(SIGINT, signal_handler);
134     signal(SIGQUIT, signal_handler);
135     signal(SIGTERM, signal_handler);
136 lefcha 1.18 }
137    
138    
139     /*
140 lefcha 1.40 * Signal handler for signals that cause rereading of the configuration file or
141     * termination of program.
142 lefcha 1.18 */
143 lefcha 1.30 void
144     signal_handler(int sig)
145 lefcha 1.18 {
146 lefcha 1.39 if (sig == SIGUSR1)
147 lefcha 1.36 flags |= FLAG_SIGUSR1_RECEIVED;
148 lefcha 1.39 else
149 lefcha 1.32 fatal(ERROR_SIGNAL, "killed by signal %d\n", sig);
150 lefcha 1.18 }
151 lefcha 1.12
152    
153     /*
154 lefcha 1.40 * Open temporary debug file and associate a stream with the returned file
155     * descriptor.
156     */
157     int
158     debug_start(void)
159     {
160     static char dt[] = PATHNAME_DEBUG_FILE;
161     int fd;
162    
163 lefcha 1.42 if (!opts.debug)
164 lefcha 1.40 return 0;
165    
166     fd = mkstemp(dt);
167    
168     if (fd != -1) {
169     debugfp = fdopen(fd, "w");
170     if (debugfp == NULL) {
171 lefcha 1.42 error("opening debug file %s: %s\n", opts.logfile,
172 lefcha 1.40 strerror(errno));
173     return ERROR_TRIVIAL;
174     }
175     }
176     return 0;
177     }
178    
179    
180     /*
181     * Close temporary debug file.
182     */
183     int
184     debug_stop(void)
185     {
186     if (debugfp == NULL)
187     return 0;
188     else
189     return fclose(debugfp);
190     }
191    
192    
193     /*
194 lefcha 1.17 * Open the file for saving of logging information.
195 lefcha 1.1 */
196 lefcha 1.30 int
197 lefcha 1.40 log_start(void)
198 lefcha 1.1 {
199 lefcha 1.42 if (*opts.logfile == '\0')
200 lefcha 1.30 return 0; /* Logging not enabled. */
201 lefcha 1.4
202 lefcha 1.42 debug("log file: '%s'\n", opts.logfile);
203 lefcha 1.1
204 lefcha 1.42 if (create_file(opts.logfile, S_IRUSR | S_IWUSR))
205 lefcha 1.30 return ERROR_TRIVIAL;
206 lefcha 1.9
207 lefcha 1.42 logfp = fopen(opts.logfile, "a");
208 lefcha 1.30 if (logfp == NULL) {
209 lefcha 1.42 error("opening log file %s: %s\n", opts.logfile,
210     strerror(errno));
211 lefcha 1.30 return ERROR_TRIVIAL;
212     }
213     return 0;
214 lefcha 1.1 }
215    
216    
217     /*
218 lefcha 1.40 * Close the log file.
219 lefcha 1.1 */
220 lefcha 1.30 int
221 lefcha 1.40 log_stop(void)
222 lefcha 1.1 {
223 lefcha 1.30 if (logfp == NULL)
224     return 0;
225     else
226     return fclose(logfp);
227 lefcha 1.1 }
228    
229 lefcha 1.13
230     /*
231     * Prepares the log entry to be saved through continues calls, and writes it
232     * to logfile.
233     */
234 lefcha 1.30 void
235     log_info(int flag, void *ptr)
236 lefcha 1.13 {
237 lefcha 1.30 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 lefcha 1.31 case LOG_PREAMBLE:
254 lefcha 1.30 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 lefcha 1.31 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 lefcha 1.30 break;
307 lefcha 1.13 }
308     }
309 lefcha 1.14
310 lefcha 1.1
311 lefcha 1.2 /*
312 lefcha 1.12 * Return current local time and date.
313 lefcha 1.2 */
314 lefcha 1.30 char *
315     get_time(void)
316 lefcha 1.1 {
317 lefcha 1.30 char *ct;
318     time_t t;
319 lefcha 1.14
320 lefcha 1.30 t = time(NULL);
321 lefcha 1.14
322 lefcha 1.30 ct = ctime(&t);
323     *(strchr(ct, '\n')) = '\0';
324 lefcha 1.1
325 lefcha 1.30 return ct;
326 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26