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

Annotation of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (hide annotations)
Wed Oct 17 14:07:29 2001 UTC (22 years, 5 months ago) by lefcha
Branch: MAIN
Changes since 1.16: +3 -2 lines
File MIME type: text/plain
Typo error fixes.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26