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

Annotation of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.41 - (hide annotations)
Mon Feb 9 19:56:42 2004 UTC (20 years, 1 month 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 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.5 extern char logfile[PATH_MAX];
19 lefcha 1.8 extern unsigned int options;
20 lefcha 1.41 extern int verbosity;
21 lefcha 1.32 extern unsigned int flags;
22 lefcha 1.6
23 lefcha 1.40 static FILE *debugfp = NULL; /* Pointer to debug file. */
24 lefcha 1.10 static FILE *logfp = NULL; /* Pointer to logfile. */
25 lefcha 1.34
26    
27     void signal_handler(int sig);
28     char *get_time(void);
29 lefcha 1.1
30    
31     /*
32 lefcha 1.41 * Print message if not in the lowest verbosity level.
33 lefcha 1.7 */
34 lefcha 1.30 void
35     info(const char *info,...)
36 lefcha 1.7 {
37 lefcha 1.30 va_list args;
38 lefcha 1.14
39 lefcha 1.41 if (verbosity == -2)
40 lefcha 1.30 return;
41 lefcha 1.14
42 lefcha 1.30 va_start(args, info);
43     vprintf(info, args);
44     va_end(args);
45 lefcha 1.12 }
46    
47    
48     /*
49 lefcha 1.41 * Print message if in the highest verbosity level.
50 lefcha 1.1 */
51 lefcha 1.30 void
52     verbose(const char *info,...)
53 lefcha 1.1 {
54 lefcha 1.30 va_list args;
55 lefcha 1.1
56 lefcha 1.41 if (verbosity == 2) {
57 lefcha 1.30 va_start(args, info);
58     vprintf(info, args);
59     va_end(args);
60     }
61 lefcha 1.1 }
62    
63 lefcha 1.17
64 lefcha 1.1 /*
65 lefcha 1.40 * 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 lefcha 1.10 * Print error message and write it into logfile.
83 lefcha 1.1 */
84 lefcha 1.30 void
85     error(const char *errmsg,...)
86 lefcha 1.1 {
87 lefcha 1.30 va_list args;
88 lefcha 1.1
89 lefcha 1.30 va_start(args, errmsg);
90 lefcha 1.1
91 lefcha 1.30 fprintf(stderr, "imapfilter: ");
92     vfprintf(stderr, errmsg, args);
93 lefcha 1.14
94 lefcha 1.30 if ((options & OPTION_ERRORS) && logfp) {
95     vfprintf(logfp, errmsg, args);
96     fflush(logfp);
97     }
98     va_end(args);
99 lefcha 1.1 }
100    
101    
102     /*
103 lefcha 1.12 * Print error message and exit program.
104     */
105 lefcha 1.30 void
106     fatal(unsigned int errnum, const char *fatal,...)
107 lefcha 1.12 {
108 lefcha 1.30 va_list args;
109    
110     va_start(args, fatal);
111     fprintf(stderr, "imapfilter: ");
112     vfprintf(stderr, fatal, args);
113     va_end(args);
114 lefcha 1.14
115 lefcha 1.33 close_connection(&connpri);
116     close_connection(&connaux);
117 lefcha 1.30 secmem_clear();
118     tty_restore();
119 lefcha 1.40 log_stop();
120 lefcha 1.30 lockfile_remove();
121 lefcha 1.40 debug_stop();
122 lefcha 1.14
123 lefcha 1.30 exit(errnum);
124 lefcha 1.12 }
125 lefcha 1.14
126 lefcha 1.18
127     /*
128 lefcha 1.40 * Catch signals that cause rereading of the configuration file or program's
129     * termination.
130 lefcha 1.18 */
131 lefcha 1.30 void
132     catch_signals(void)
133 lefcha 1.18 {
134 lefcha 1.36 signal(SIGUSR1, signal_handler);
135 lefcha 1.30 signal(SIGINT, signal_handler);
136     signal(SIGQUIT, signal_handler);
137     signal(SIGTERM, signal_handler);
138 lefcha 1.18 }
139    
140    
141     /*
142 lefcha 1.40 * Signal handler for signals that cause rereading of the configuration file or
143     * termination of program.
144 lefcha 1.18 */
145 lefcha 1.30 void
146     signal_handler(int sig)
147 lefcha 1.18 {
148 lefcha 1.39 if (sig == SIGUSR1)
149 lefcha 1.36 flags |= FLAG_SIGUSR1_RECEIVED;
150 lefcha 1.39 else
151 lefcha 1.32 fatal(ERROR_SIGNAL, "killed by signal %d\n", sig);
152 lefcha 1.18 }
153 lefcha 1.12
154    
155     /*
156 lefcha 1.40 * 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 lefcha 1.17 * Open the file for saving of logging information.
197 lefcha 1.1 */
198 lefcha 1.30 int
199 lefcha 1.40 log_start(void)
200 lefcha 1.1 {
201 lefcha 1.30 if (*logfile == '\0')
202     return 0; /* Logging not enabled. */
203 lefcha 1.4
204 lefcha 1.40 debug("log file: '%s'\n", logfile);
205 lefcha 1.1
206 lefcha 1.30 if (create_file(logfile, S_IRUSR | S_IWUSR))
207     return ERROR_TRIVIAL;
208 lefcha 1.9
209 lefcha 1.30 logfp = fopen(logfile, "a");
210     if (logfp == NULL) {
211 lefcha 1.40 error("opening log file %s: %s\n", logfile, strerror(errno));
212 lefcha 1.30 return ERROR_TRIVIAL;
213     }
214     return 0;
215 lefcha 1.1 }
216    
217    
218     /*
219 lefcha 1.40 * Close the log file.
220 lefcha 1.1 */
221 lefcha 1.30 int
222 lefcha 1.40 log_stop(void)
223 lefcha 1.1 {
224 lefcha 1.30 if (logfp == NULL)
225     return 0;
226     else
227     return fclose(logfp);
228 lefcha 1.1 }
229    
230 lefcha 1.13
231     /*
232     * Prepares the log entry to be saved through continues calls, and writes it
233     * to logfile.
234     */
235 lefcha 1.30 void
236     log_info(int flag, void *ptr)
237 lefcha 1.13 {
238 lefcha 1.30 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 lefcha 1.31 case LOG_PREAMBLE:
255 lefcha 1.30 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 lefcha 1.31 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 lefcha 1.30 break;
308 lefcha 1.13 }
309     }
310 lefcha 1.14
311 lefcha 1.1
312 lefcha 1.2 /*
313 lefcha 1.12 * Return current local time and date.
314 lefcha 1.2 */
315 lefcha 1.30 char *
316     get_time(void)
317 lefcha 1.1 {
318 lefcha 1.30 char *ct;
319     time_t t;
320 lefcha 1.14
321 lefcha 1.30 t = time(NULL);
322 lefcha 1.14
323 lefcha 1.30 ct = ctime(&t);
324     *(strchr(ct, '\n')) = '\0';
325 lefcha 1.1
326 lefcha 1.30 return ct;
327 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26