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

Diff of /imapfilter/log.c

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

revision 1.36.2.3 by lefcha, Sat Nov 8 17:00:12 2003 UTC revision 1.46 by lefcha, Sat Feb 14 19:14:43 2004 UTC
# Line 11  Line 11 
11    
12  #include "config.h"  #include "config.h"
13  #include "imapfilter.h"  #include "imapfilter.h"
14    #include "pathnames.h"
15    
16    
17  extern conn_t connpri, connaux;  extern connection_t connpri, connaux;
18  extern char logfile[PATH_MAX];  extern options_t opts;
 extern unsigned int options;  
19  extern unsigned int flags;  extern unsigned int flags;
20    
21    static FILE *debugfp = NULL;    /* Pointer to debug file. */
22  static FILE *logfp = NULL;      /* Pointer to logfile. */  static FILE *logfp = NULL;      /* Pointer to logfile. */
23    
24    
25  void signal_handler(int sig);  void signal_handler(int sig);
26  char *get_time(void);  char *log_time(void);
27    
28    
29  /*  /*
30   * Print message if not in OPTION_DETAILS_QUIET mode.   * Print message if not in the lowest verbosity level.
31   */   */
32  void  void
33  info(const char *info,...)  info(const char *info,...)
34  {  {
35          va_list args;          va_list args;
36    
37          if (options & OPTION_DETAILS_QUIET)          if (opts.verbosity == -2)
38                  return;                  return;
39    
40          va_start(args, info);          va_start(args, info);
# Line 43  info(const char *info,...) Line 44  info(const char *info,...)
44    
45    
46  /*  /*
47   * Print message if in OPTION_DETAILS_VERBOSE mode.   * Print message if in the highest verbosity level.
48   */   */
49  void  void
50  verbose(const char *info,...)  verbose(const char *info,...)
51  {  {
52          va_list args;          va_list args;
53    
54          if (options & OPTION_DETAILS_VERBOSE) {          if (opts.verbosity == 2) {
55                  va_start(args, info);                  va_start(args, info);
56                  vprintf(info, args);                  vprintf(info, args);
57                  va_end(args);                  va_end(args);
# Line 59  verbose(const char *info,...) Line 60  verbose(const char *info,...)
60    
61    
62  /*  /*
63     * 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   * Print error message and write it into logfile.   * Print error message and write it into logfile.
81   */   */
82  void  void
# Line 71  error(const char *errmsg,...) Line 89  error(const char *errmsg,...)
89          fprintf(stderr, "imapfilter: ");          fprintf(stderr, "imapfilter: ");
90          vfprintf(stderr, errmsg, args);          vfprintf(stderr, errmsg, args);
91    
92          if ((options & OPTION_ERRORS) && logfp) {          if (opts.errors && logfp) {
93                  vfprintf(logfp, errmsg, args);                  vfprintf(logfp, errmsg, args);
94                  fflush(logfp);                  fflush(logfp);
95          }          }
# Line 96  fatal(unsigned int errnum, const char *f Line 114  fatal(unsigned int errnum, const char *f
114          close_connection(&connaux);          close_connection(&connaux);
115          secmem_clear();          secmem_clear();
116          tty_restore();          tty_restore();
117          close_logfile();          log_stop();
118          lockfile_remove();          lockfile_remove();
119            debug_stop();
120    
121          exit(errnum);          exit(errnum);
122  }  }
123    
124    
125  /*  /*
126   * Catch signals that cause program's termination.   * Catch signals that cause rereading of the configuration file or program's
127     * termination.
128   */   */
129  void  void
130  catch_signals(void)  catch_signals(void)
131  {  {
132    
133          signal(SIGUSR1, signal_handler);          signal(SIGUSR1, signal_handler);
134          signal(SIGINT, signal_handler);          signal(SIGINT, signal_handler);
135          signal(SIGQUIT, signal_handler);          signal(SIGQUIT, signal_handler);
# Line 117  catch_signals(void) Line 138  catch_signals(void)
138    
139    
140  /*  /*
141   * Signal handler for signals that cause termination of program.   * Signal handler for signals that cause rereading of the configuration file or
142     * termination of program.
143   */   */
144  void  void
145  signal_handler(int sig)  signal_handler(int sig)
146  {  {
147    
148          if (sig == SIGUSR1)          if (sig == SIGUSR1)
149                  flags |= FLAG_SIGUSR1_RECEIVED;                  flags |= FLAG_SIGUSR1;
150          else          else
151                  fatal(ERROR_SIGNAL, "killed by signal %d\n", sig);                  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.   * Open the file for saving of logging information.
198   */   */
199  int  int
200  open_logfile(void)  log_start(void)
201  {  {
202          if (*logfile == '\0')  
203            if (*opts.logfile == '\0')
204                  return 0;       /* Logging not enabled. */                  return 0;       /* Logging not enabled. */
205    
206  #ifdef DEBUG          debug("log file: '%s'\n", opts.logfile);
         fprintf(stderr, "debug: logfile: '%s'\n", logfile);  
 #endif  
207    
208          if (create_file(logfile, S_IRUSR | S_IWUSR))          if (create_file(opts.logfile, S_IRUSR | S_IWUSR))
209                  return ERROR_TRIVIAL;                  return ERROR_TRIVIAL;
210    
211          logfp = fopen(logfile, "a");          logfp = fopen(opts.logfile, "a");
212          if (logfp == NULL) {          if (logfp == NULL) {
213                  error("opening logfile %s: %s\n", logfile, strerror(errno));                  error("opening log file %s: %s\n", opts.logfile,
214                        strerror(errno));
215                  return ERROR_TRIVIAL;                  return ERROR_TRIVIAL;
216          }          }
217          return 0;          return 0;
# Line 155  open_logfile(void) Line 219  open_logfile(void)
219    
220    
221  /*  /*
222   * Close the logfile.   * Close the log file.
223   */   */
224  int  int
225  close_logfile(void)  log_stop(void)
226  {  {
227    
228          if (logfp == NULL)          if (logfp == NULL)
229                  return 0;                  return 0;
230          else          else
# Line 168  close_logfile(void) Line 233  close_logfile(void)
233    
234    
235  /*  /*
236   * Prepares the log entry to be saved through continues calls, and writes it   * Prepares the log entry to be saved through continues calls, and writes it to
237   * to logfile.   * logfile.
238   */   */
239  void  void
240  log_info(int flag, void *ptr)  log_info(int flag, void *ptr)
# Line 191  log_info(int flag, void *ptr) Line 256  log_info(int flag, void *ptr)
256    
257          switch (flag) {          switch (flag) {
258          case LOG_PREAMBLE:          case LOG_PREAMBLE:
259                  fprintf(logfp, "%s %s %s %s %s%s %s\n", get_time(),                  fprintf(logfp, "%s %s %s %s %s%s %s\n", log_time(),
260                      inf.account, inf.mbox, inf.filter,                      inf.account, inf.mbox, inf.filter,
261                      (*inf.action == FILTER_ACTION_DELETE ? "delete" :                      (*inf.action == ACTION_DELETE ? "delete" :
262                          *inf.action == FILTER_ACTION_COPY ? "copy" :                      *inf.action == ACTION_COPY ? "copy" :
263                          *inf.action == FILTER_ACTION_MOVE ? "move" :                      *inf.action == ACTION_MOVE ? "move" :
264                          *inf.action == FILTER_ACTION_RCOPY ? "rcopy " :                      *inf.action == ACTION_RCOPY ? "rcopy " :
265                          *inf.action == FILTER_ACTION_RMOVE ? "rmove " :                      *inf.action == ACTION_RMOVE ? "rmove " :
266                          *inf.action == FILTER_ACTION_FLAG_ADD ||                      *inf.action == ACTION_FLAG_ADD ||
267                          *inf.action == FILTER_ACTION_FLAG_REMOVE ||                      *inf.action == ACTION_FLAG_REMOVE ||
268                          *inf.action == FILTER_ACTION_FLAG_REPLACE ? "flag" :                      *inf.action == ACTION_FLAG_REPLACE ? "flag" :
269                          *inf.action == FILTER_ACTION_LIST ? "list" :                      *inf.action == ACTION_LIST ? "list" :
270                          "unknown "),                      "unknown "),
271                      (inf.destaccount == NULL ? "" : inf.destaccount),                      (inf.destaccount == NULL ? "" : inf.destaccount),
272                      (*inf.destmbox == '\0' ? "" : inf.destmbox));                      (*inf.destmbox == '\0' ? "" : inf.destmbox));
273                  fflush(logfp);                  fflush(logfp);
# Line 210  log_info(int flag, void *ptr) Line 275  log_info(int flag, void *ptr)
275          case LOG_ACCOUNT:          case LOG_ACCOUNT:
276                  inf.account = (char *)ptr;                  inf.account = (char *)ptr;
277                  break;                  break;
278          case LOG_MAILBOX:          case LOG_MBOX:
279                  inf.mbox = (char *)ptr;                  inf.mbox = (char *)ptr;
280                  break;                  break;
281          case LOG_FILTER:          case LOG_FILTER:
# Line 219  log_info(int flag, void *ptr) Line 284  log_info(int flag, void *ptr)
284          case LOG_ACTION:          case LOG_ACTION:
285                  inf.action = (unsigned int *)ptr;                  inf.action = (unsigned int *)ptr;
286                  break;                  break;
287          case LOG_DESTINATION_ACCOUNT:          case LOG_DEST_ACCOUNT:
288                  if (ptr == NULL)                  if (ptr == NULL)
289                          inf.destaccount = NULL;                          inf.destaccount = NULL;
290                  else                  else
291                          inf.destaccount = ((account_t *) ptr)->key;                          inf.destaccount = ((account_t *) ptr)->key;
292                  break;                  break;
293          case LOG_DESTINATION_MAILBOX:          case LOG_DEST_MBOX:
294                  inf.destmbox = (char *)ptr;                  inf.destmbox = (char *)ptr;
295                  break;                  break;
296          case LOG_HEADER:          case LOG_HEADER:
# Line 252  log_info(int flag, void *ptr) Line 317  log_info(int flag, void *ptr)
317   * Return current local time and date.   * Return current local time and date.
318   */   */
319  char *  char *
320  get_time(void)  log_time(void)
321  {  {
322          char *ct;          char *ct;
323          time_t t;          time_t t;

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26