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

Contents of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations)
Sun Sep 30 20:21:57 2001 UTC (22 years, 6 months ago) by lefcha
Branch: MAIN
Changes since 1.11: +104 -24 lines
File MIME type: text/plain
New file logging format and error codes

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26