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

Diff of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1.1.1 by lefcha, Sat Aug 11 15:52:52 2001 UTC revision 1.46 by lefcha, Sat Feb 14 19:14:43 2004 UTC
# Line 1  Line 1 
1  #include <stdio.h>  #include <stdio.h>
2  #include <stdlib.h>  #include <stdlib.h>
 #include <errno.h>  
3  #include <string.h>  #include <string.h>
4    #include <errno.h>
5  #include <stdarg.h>  #include <stdarg.h>
6  #include <limits.h>  #include <limits.h>
7    #include <sys/types.h>
8    #include <sys/stat.h>
9    #include <time.h>
10    #include <signal.h>
11    
12  #include "config.h"  #include "config.h"
13  #include "imapfilter.h"  #include "imapfilter.h"
14  #include "log.h"  #include "pathnames.h"
15    
16    
17    extern connection_t connpri, connaux;
18    extern options_t opts;
19    extern unsigned int flags;
20    
21    static FILE *debugfp = NULL;    /* Pointer to debug file. */
22    static FILE *logfp = NULL;      /* Pointer to logfile. */
23    
24    
25  extern int options;  void signal_handler(int sig);
26  extern account_data account;  char *log_time(void);
 static FILE *logfp;             /* Points to logfile. */  
27    
28    
29  /*  /*
30   * Print message if in OPTION_DETAILS_VERBOSE mode.   * Print message if not in the lowest verbosity level.
31   */   */
32  void verbose(const char *info, ...)  void
33    info(const char *info,...)
34  {  {
35      va_list args;          va_list args;
36    
37            if (opts.verbosity == -2)
38                    return;
39    
     if (options & OPTION_DETAILS_VERBOSE) {  
40          va_start(args, info);          va_start(args, info);
41          vprintf(info, args);          vprintf(info, args);
42          va_end(args);          va_end(args);
     }  
43  }  }
44    
45    
46  /*  /*
47   * Prints error message and writes it into logfile.   * Print message if in the highest verbosity level.
48   */   */
49  void error(const char *errmsg, ...)  void
50    verbose(const char *info,...)
51  {  {
52      va_list args;          va_list args;
53    
54      va_start(args, errmsg);          if (opts.verbosity == 2) {
55                    va_start(args, info);
56                    vprintf(info, args);
57                    va_end(args);
58            }
59    }
60    
     if (!(options & OPTION_LOGGING_DISABLED)) {  
         vfprintf(logfp, errmsg, args);  
     }  
61    
62      vfprintf(stderr, errmsg, args);  /*
63      va_end(args);   * Write message to debug file.
64     */
65    void
66    debug(const char *debug,...)
67    {
68            va_list args;
69    
70            if (opts.debug && debugfp) {
71                    va_start(args, debug);
72                    vfprintf(debugfp, debug, args);
73                    fflush(debugfp);
74                    va_end(args);
75            }
76  }  }
77    
78    
79  /*  /*
80   * Opens the file to save logging information.   * Print error message and write it into logfile.
81   */   */
82  int open_logfile(void)  void
83    error(const char *errmsg,...)
84  {  {
85      char *home;          va_list args;
86    
87      if (options & OPTION_LOGGING_DISABLED)          va_start(args, errmsg);
         return SUCCESS;  
88    
89      if (!account.logfile[0]) {          fprintf(stderr, "imapfilter: ");
90          home = getenv("HOME");          vfprintf(stderr, errmsg, args);
91    
92          snprintf(account.logfile, PATH_MAX, "%s/%s", home,          if (opts.errors && logfp) {
93                   ".imapfilter_log");                  vfprintf(logfp, errmsg, args);
94      }                  fflush(logfp);
95  #ifdef DEBUG          }
96      printf("debug: logfile: %s\n", account.logfile);          va_end(args);
97  #endif  }
98    
     logfp = fopen(account.logfile, "a");  
99    
100      if (!logfp) {  /*
101          fprintf(stderr, "imapfilter: opening logfile %s: %s\n",   * Print error message and exit program.
102                  account.logfile, strerror(errno));   */
103          return FAILURE;  void
104      }  fatal(unsigned int errnum, const char *fatal,...)
105    {
106            va_list args;
107    
108            va_start(args, fatal);
109            fprintf(stderr, "imapfilter: ");
110            vfprintf(stderr, fatal, args);
111            va_end(args);
112    
113      return SUCCESS;          close_connection(&connpri);
114            close_connection(&connaux);
115            secmem_clear();
116            tty_restore();
117            log_stop();
118            lockfile_remove();
119            debug_stop();
120    
121            exit(errnum);
122  }  }
123    
124    
125  /*  /*
126   * Closes the logfile.   * Catch signals that cause rereading of the configuration file or program's
127     * termination.
128   */   */
129  int close_logfile(void)  void
130    catch_signals(void)
131  {  {
132      if (options & OPTION_LOGGING_DISABLED)  
133          return SUCCESS;          signal(SIGUSR1, signal_handler);
134      else          signal(SIGINT, signal_handler);
135          return fclose(logfp);          signal(SIGQUIT, signal_handler);
136            signal(SIGTERM, signal_handler);
137  }  }
138    
139    
140  /* Writes information to logfile. */  /*
141  void log_info(const char *info, ...)   * Signal handler for signals that cause rereading of the configuration file or
142     * termination of program.
143     */
144    void
145    signal_handler(int sig)
146  {  {
     va_list args;  
147    
148      if (!(options & OPTION_LOGGING_DISABLED)) {          if (sig == SIGUSR1)
149          va_start(args, info);                  flags |= FLAG_SIGUSR1;
150          vfprintf(logfp, info, args);          else
151          va_end(args);                  fatal(ERROR_SIGNAL, "killed by signal %d\n", sig);
152      }  }
153    
154    
155    /*
156     * 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;
163            int fd;
164    
165            if (!opts.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", opts.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    
189            if (debugfp == NULL)
190                    return 0;
191            else
192                    return fclose(debugfp);
193    }
194    
195    
196    /*
197     * Open the file for saving of logging information.
198     */
199    int
200    log_start(void)
201    {
202    
203            if (*opts.logfile == '\0')
204                    return 0;       /* Logging not enabled. */
205    
206            debug("log file: '%s'\n", opts.logfile);
207    
208            if (create_file(opts.logfile, S_IRUSR | S_IWUSR))
209                    return ERROR_TRIVIAL;
210    
211            logfp = fopen(opts.logfile, "a");
212            if (logfp == NULL) {
213                    error("opening log file %s: %s\n", opts.logfile,
214                        strerror(errno));
215                    return ERROR_TRIVIAL;
216            }
217            return 0;
218    }
219    
220    
221    /*
222     * Close the log file.
223     */
224    int
225    log_stop(void)
226    {
227    
228            if (logfp == NULL)
229                    return 0;
230            else
231                    return fclose(logfp);
232    }
233    
234    
235    /*
236     * Prepares the log entry to be saved through continues calls, and writes it to
237     * logfile.
238     */
239    void
240    log_info(int flag, void *ptr)
241    {
242            static struct {
243                    char *account;
244                    char *mbox;
245                    char *filter;
246                    unsigned int *action;
247                    char *destaccount;
248                    char *destmbox;
249                    char *hdrs;
250            } inf = {
251                    NULL, NULL, NULL, NULL, NULL, NULL, NULL
252            };
253    
254            if (logfp == NULL)
255                    return;
256    
257            switch (flag) {
258            case LOG_PREAMBLE:
259                    fprintf(logfp, "%s %s %s %s %s%s %s\n", log_time(),
260                        inf.account, inf.mbox, inf.filter,
261                        (*inf.action == ACTION_DELETE ? "delete" :
262                        *inf.action == ACTION_COPY ? "copy" :
263                        *inf.action == ACTION_MOVE ? "move" :
264                        *inf.action == ACTION_RCOPY ? "rcopy " :
265                        *inf.action == ACTION_RMOVE ? "rmove " :
266                        *inf.action == ACTION_FLAG_ADD ||
267                        *inf.action == ACTION_FLAG_REMOVE ||
268                        *inf.action == ACTION_FLAG_REPLACE ? "flag" :
269                        *inf.action == ACTION_LIST ? "list" :
270                        "unknown "),
271                        (inf.destaccount == NULL ? "" : inf.destaccount),
272                        (*inf.destmbox == '\0' ? "" : inf.destmbox));
273                    fflush(logfp);
274                    break;
275            case LOG_ACCOUNT:
276                    inf.account = (char *)ptr;
277                    break;
278            case LOG_MBOX:
279                    inf.mbox = (char *)ptr;
280                    break;
281            case LOG_FILTER:
282                    inf.filter = (char *)ptr;
283                    break;
284            case LOG_ACTION:
285                    inf.action = (unsigned int *)ptr;
286                    break;
287            case LOG_DEST_ACCOUNT:
288                    if (ptr == NULL)
289                            inf.destaccount = NULL;
290                    else
291                            inf.destaccount = ((account_t *) ptr)->key;
292                    break;
293            case LOG_DEST_MBOX:
294                    inf.destmbox = (char *)ptr;
295                    break;
296            case LOG_HEADER:
297                    if (ptr) {
298                            inf.hdrs = (char *)ptr;
299                            fputc('\t', logfp);
300                            while (*inf.hdrs != '\0') {
301                                    if (*inf.hdrs == '\r') {
302                                            fputc('\n', logfp);
303                                            if (*(inf.hdrs + 2) != '\0')
304                                                    fputc('\t', logfp);
305                                            inf.hdrs += 2;
306                                    } else
307                                            fputc(*(inf.hdrs++), logfp);
308                            }
309                    }
310                    fflush(logfp);
311                    break;
312            }
313    }
314    
315    
316    /*
317     * Return current local time and date.
318     */
319    char *
320    log_time(void)
321    {
322            char *ct;
323            time_t t;
324    
325            t = time(NULL);
326    
327            ct = ctime(&t);
328            *(strchr(ct, '\n')) = '\0';
329    
330            return ct;
331  }  }

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.46

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26