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

Annotation of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.28 - (hide annotations)
Sat Feb 1 19:42:09 2003 UTC (21 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.27: +1 -0 lines
File MIME type: text/plain
Upgrade logfile each time data are appended.

1 lefcha 1.1 #include <stdio.h>
2     #include <stdlib.h>
3 lefcha 1.9 #include <unistd.h>
4 lefcha 1.1 #include <errno.h>
5     #include <string.h>
6     #include <stdarg.h>
7 lefcha 1.18 #include <signal.h>
8 lefcha 1.1 #include <limits.h>
9 lefcha 1.9 #include <sys/types.h>
10     #include <sys/stat.h>
11     #include <fcntl.h>
12 lefcha 1.12 #include <time.h>
13 lefcha 1.1
14     #include "config.h"
15     #include "imapfilter.h"
16    
17    
18 lefcha 1.25 extern int sockpri, sockaux;
19 lefcha 1.5 extern char logfile[PATH_MAX];
20 lefcha 1.8 extern unsigned int options;
21 lefcha 1.6
22 lefcha 1.10 static FILE *logfp = NULL; /* Pointer to logfile. */
23 lefcha 1.1
24    
25     /*
26 lefcha 1.17 * Print message if not in OPTION_DETAILS_QUIET mode.
27 lefcha 1.7 */
28 lefcha 1.14 void info(const char *info,...)
29 lefcha 1.7 {
30     va_list args;
31 lefcha 1.14
32 lefcha 1.16 if (options & OPTION_DETAILS_QUIET)
33 lefcha 1.13 return;
34 lefcha 1.14
35 lefcha 1.12 va_start(args, info);
36 lefcha 1.13 vprintf(info, args);
37 lefcha 1.12 va_end(args);
38     }
39    
40    
41     /*
42 lefcha 1.10 * Print message if in OPTION_DETAILS_VERBOSE mode.
43 lefcha 1.1 */
44 lefcha 1.14 void verbose(const char *info,...)
45 lefcha 1.1 {
46     va_list args;
47    
48 lefcha 1.10 if (options & OPTION_DETAILS_VERBOSE) {
49 lefcha 1.1 va_start(args, info);
50     vprintf(info, args);
51     va_end(args);
52     }
53     }
54    
55 lefcha 1.17
56 lefcha 1.1 /*
57 lefcha 1.10 * Print error message and write it into logfile.
58 lefcha 1.1 */
59 lefcha 1.14 void error(const char *errmsg,...)
60 lefcha 1.1 {
61     va_list args;
62    
63     va_start(args, errmsg);
64    
65 lefcha 1.14 vfprintf(stderr, errmsg, args);
66    
67 lefcha 1.27 if ((options & OPTION_ERRORS) && logfp) {
68 lefcha 1.1 vfprintf(logfp, errmsg, args);
69     }
70     va_end(args);
71     }
72    
73    
74     /*
75 lefcha 1.12 * Print error message and exit program.
76     */
77 lefcha 1.14 void fatal(unsigned int errnum, const char *fatal,...)
78 lefcha 1.12 {
79     va_list args;
80 lefcha 1.14
81 lefcha 1.12 va_start(args, fatal);
82     vfprintf(stderr, fatal, args);
83     va_end(args);
84 lefcha 1.14
85 lefcha 1.25 close_connection(&sockpri);
86     close_connection(&sockaux);
87 lefcha 1.22 secmem_clear();
88 lefcha 1.21 tty_restore();
89 lefcha 1.12 close_logfile();
90 lefcha 1.24 lockfile_remove();
91 lefcha 1.14
92 lefcha 1.12 exit(errnum);
93     }
94 lefcha 1.14
95 lefcha 1.18
96     /*
97     * Catch signals that cause program's termination.
98     */
99     void catch_signals(void)
100     {
101     signal(SIGINT, signal_handler);
102     signal(SIGQUIT, signal_handler);
103     signal(SIGTERM, signal_handler);
104     }
105    
106    
107     /*
108     * Signal handler for signals that cause termination of program.
109     */
110     void signal_handler(int sig)
111     {
112 lefcha 1.19 fatal(ERROR_SIGNAL, "imapfilter: killed by signal %d\n", sig);
113 lefcha 1.18 }
114 lefcha 1.12
115    
116     /*
117 lefcha 1.17 * Open the file for saving of logging information.
118 lefcha 1.1 */
119     int open_logfile(void)
120     {
121 lefcha 1.10 if (!*logfile)
122 lefcha 1.6 return 0; /* Logging not enabled. */
123 lefcha 1.4
124 lefcha 1.1 #ifdef DEBUG
125 lefcha 1.8 printf("debug: logfile: '%s'\n", logfile);
126 lefcha 1.1 #endif
127    
128 lefcha 1.21 if (create_file(logfile, S_IRUSR | S_IWUSR))
129 lefcha 1.12 return ERROR_TRIVIAL;
130 lefcha 1.9
131 lefcha 1.5 logfp = fopen(logfile, "a");
132 lefcha 1.1
133     if (!logfp) {
134 lefcha 1.12 error("imapfilter: opening logfile %s: %s\n",
135 lefcha 1.14 logfile, strerror(errno));
136 lefcha 1.12 return ERROR_TRIVIAL;
137 lefcha 1.1 }
138 lefcha 1.6 return 0;
139 lefcha 1.1 }
140    
141    
142     /*
143 lefcha 1.10 * Close the logfile.
144 lefcha 1.1 */
145     int close_logfile(void)
146     {
147 lefcha 1.8 if (!logfp)
148 lefcha 1.6 return 0;
149 lefcha 1.1 else
150     return fclose(logfp);
151     }
152    
153 lefcha 1.13
154     /*
155     * Prepares the log entry to be saved through continues calls, and writes it
156     * to logfile.
157     */
158     void log_info(int flag, void *ptr)
159     {
160     static struct {
161     char *server;
162     char *username;
163     char *mbox;
164     char *filter;
165     unsigned int *action;
166     char *destmbox;
167     char *hdrs;
168     } inf = {
169     NULL, NULL, NULL, NULL, NULL, NULL, NULL
170     };
171 lefcha 1.14
172 lefcha 1.13 if (!logfp)
173     return;
174 lefcha 1.14
175     switch (flag) {
176 lefcha 1.13 case LOG_WRITE:
177     fprintf(logfp, "%s %s %s %s %s %s%s\n", get_time(),
178     inf.server, inf.username, inf.mbox, inf.filter,
179     (*inf.action == FILTER_ACTION_DELETE ? "delete" :
180     *inf.action == FILTER_ACTION_COPY ? "copy " :
181     *inf.action == FILTER_ACTION_MOVE ? "move " : "list"),
182 lefcha 1.15 (!*inf.destmbox ? "" : inf.destmbox));
183 lefcha 1.14
184 lefcha 1.13 if (ptr) {
185 lefcha 1.26 inf.hdrs = (char *)ptr;
186     fprintf(logfp, "%c", '\t');
187 lefcha 1.13 while (*inf.hdrs) {
188     if (*inf.hdrs == '\n') {
189 lefcha 1.26 fprintf(logfp, "%c", '\n');
190 lefcha 1.13 if (*(inf.hdrs + 1))
191 lefcha 1.26 fprintf(logfp, "%c", '\t');
192 lefcha 1.13 inf.hdrs++;
193     } else
194 lefcha 1.26 fprintf(logfp, "%c", *(inf.hdrs++));
195 lefcha 1.13 }
196     }
197 lefcha 1.28 fflush(logfp);
198 lefcha 1.13 break;
199     case LOG_SERVER:
200 lefcha 1.26 inf.server = (char *)ptr;
201 lefcha 1.13 break;
202     case LOG_USERNAME:
203 lefcha 1.26 inf.username = (char *)ptr;
204 lefcha 1.14 break;
205 lefcha 1.13 case LOG_MAILBOX:
206 lefcha 1.26 inf.mbox = (char *)ptr;
207 lefcha 1.13 break;
208     case LOG_FILTER:
209 lefcha 1.26 inf.filter = (char *)ptr;
210 lefcha 1.14 break;
211 lefcha 1.13 case LOG_ACTION:
212 lefcha 1.26 inf.action = (unsigned int *)ptr;
213 lefcha 1.13 break;
214     case LOG_DESTINATION_MAILBOX:
215 lefcha 1.26 inf.destmbox = (char *)ptr;
216 lefcha 1.13 break;
217     }
218     }
219 lefcha 1.14
220 lefcha 1.1
221 lefcha 1.2 /*
222 lefcha 1.12 * Return current local time and date.
223 lefcha 1.2 */
224 lefcha 1.12 char *get_time(void)
225 lefcha 1.1 {
226 lefcha 1.12 char *ct;
227     time_t t;
228 lefcha 1.14
229 lefcha 1.12 t = time(NULL);
230 lefcha 1.14
231 lefcha 1.12 ct = ctime(&t);
232     *(strchr(ct, '\n')) = 0;
233 lefcha 1.1
234 lefcha 1.12 return ct;
235 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26