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

Diff of /imapfilter/log.c

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

revision 1.29 by lefcha, Fri Feb 21 18:31:29 2003 UTC revision 1.30 by lefcha, Sat Feb 22 16:06:41 2003 UTC
# Line 25  static FILE *logfp = NULL;     /* Pointer to Line 25  static FILE *logfp = NULL;     /* Pointer to
25  /*  /*
26   * Print message if not in OPTION_DETAILS_QUIET mode.   * Print message if not in OPTION_DETAILS_QUIET mode.
27   */   */
28  void info(const char *info,...)  void
29    info(const char *info,...)
30  {  {
31      va_list args;          va_list args;
32    
33      if (options & OPTION_DETAILS_QUIET)          if (options & OPTION_DETAILS_QUIET)
34          return;                  return;
35    
36      va_start(args, info);          va_start(args, info);
37      vprintf(info, args);          vprintf(info, args);
38      va_end(args);          va_end(args);
39  }  }
40    
41    
42  /*  /*
43   * Print message if in OPTION_DETAILS_VERBOSE mode.   * Print message if in OPTION_DETAILS_VERBOSE mode.
44   */   */
45  void verbose(const char *info,...)  void
46    verbose(const char *info,...)
47  {  {
48      va_list args;          va_list args;
49    
50      if (options & OPTION_DETAILS_VERBOSE) {          if (options & OPTION_DETAILS_VERBOSE) {
51          va_start(args, info);                  va_start(args, info);
52          vprintf(info, args);                  vprintf(info, args);
53          va_end(args);                  va_end(args);
54      }          }
55  }  }
56    
57    
58  /*  /*
59   * Print error message and write it into logfile.   * Print error message and write it into logfile.
60   */   */
61  void error(const char *errmsg,...)  void
62    error(const char *errmsg,...)
63  {  {
64      va_list args;          va_list args;
65    
66      va_start(args, errmsg);          va_start(args, errmsg);
67    
68      vfprintf(stderr, errmsg, args);          fprintf(stderr, "imapfilter: ");
69            vfprintf(stderr, errmsg, args);
70    
71      if ((options & OPTION_ERRORS) && logfp) {          if ((options & OPTION_ERRORS) && logfp) {
72          vfprintf(logfp, errmsg, args);                  vfprintf(logfp, errmsg, args);
73      }                  fflush(logfp);
74      va_end(args);          }
75            va_end(args);
76  }  }
77    
78    
79  /*  /*
80   * Print error message and exit program.   * Print error message and exit program.
81   */   */
82  void fatal(unsigned int errnum, const char *fatal,...)  void
83    fatal(unsigned int errnum, const char *fatal,...)
84  {  {
85      va_list args;          va_list args;
86    
87            va_start(args, fatal);
88            fprintf(stderr, "imapfilter: ");
89            vfprintf(stderr, fatal, args);
90            va_end(args);
91    
92      va_start(args, fatal);          close_connection(&sockpri);
93      vfprintf(stderr, fatal, args);          close_connection(&sockaux);
94      va_end(args);          secmem_clear();
95            tty_restore();
96      close_connection(&sockpri);          close_logfile();
97      close_connection(&sockaux);          lockfile_remove();
     secmem_clear();  
     tty_restore();  
     close_logfile();  
     lockfile_remove();  
98    
99      exit(errnum);          exit(errnum);
100  }  }
101    
102    
103  /*  /*
104   * Catch signals that cause program's termination.   * Catch signals that cause program's termination.
105   */   */
106  void catch_signals(void)  void
107    catch_signals(void)
108  {  {
109      signal(SIGINT, signal_handler);          signal(SIGINT, signal_handler);
110      signal(SIGQUIT, signal_handler);          signal(SIGQUIT, signal_handler);
111      signal(SIGTERM, signal_handler);          signal(SIGTERM, signal_handler);
112  }  }
113    
114    
115  /*  /*
116   * Signal handler for signals that cause termination of program.   * Signal handler for signals that cause termination of program.
117   */   */
118  void signal_handler(int sig)  void
119    signal_handler(int sig)
120  {  {
121      fatal(ERROR_SIGNAL, "imapfilter: killed by signal %d\n", sig);          fatal(ERROR_SIGNAL, "killed by signal %d\n", sig);
122  }  }
123    
124    
125  /*  /*
126   * Open the file for saving of logging information.   * Open the file for saving of logging information.
127   */   */
128  int open_logfile(void)  int
129    open_logfile(void)
130  {  {
131      if (!*logfile)          if (*logfile == '\0')
132          return 0;               /* Logging not enabled. */                  return 0;       /* Logging not enabled. */
133    
134  #ifdef DEBUG  #ifdef DEBUG
135      fprintf(stderr, "debug: logfile: '%s'\n", logfile);          fprintf(stderr, "debug: logfile: '%s'\n", logfile);
136  #endif  #endif
137    
138      if (create_file(logfile, S_IRUSR | S_IWUSR))          if (create_file(logfile, S_IRUSR | S_IWUSR))
139          return ERROR_TRIVIAL;                  return ERROR_TRIVIAL;
   
     logfp = fopen(logfile, "a");  
140    
141      if (!logfp) {          logfp = fopen(logfile, "a");
142          error("imapfilter: opening logfile %s: %s\n",          if (logfp == NULL) {
143                logfile, strerror(errno));                  error("opening logfile %s: %s\n", logfile, strerror(errno));
144          return ERROR_TRIVIAL;                  return ERROR_TRIVIAL;
145      }          }
146      return 0;          return 0;
147  }  }
148    
149    
150  /*  /*
151   * Close the logfile.   * Close the logfile.
152   */   */
153  int close_logfile(void)  int
154    close_logfile(void)
155  {  {
156      if (!logfp)          if (logfp == NULL)
157          return 0;                  return 0;
158      else          else
159          return fclose(logfp);                  return fclose(logfp);
160  }  }
161    
162    
# Line 155  int close_logfile(void) Line 164  int close_logfile(void)
164   * 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
165   * to logfile.   * to logfile.
166   */   */
167  void log_info(int flag, void *ptr)  void
168    log_info(int flag, void *ptr)
169  {  {
170      static struct {          static struct {
171          char *server;                  char *account;
172          char *username;                  char *mbox;
173          char *mbox;                  char *filter;
174          char *filter;                  unsigned int *action;
175          unsigned int *action;                  char *destaccount;
176          char *destmbox;                  char *destmbox;
177          char *hdrs;                  char *hdrs;
178      } inf = {          } inf = {
179          NULL, NULL, NULL, NULL, NULL, NULL, NULL                  NULL, NULL, NULL, NULL, NULL, NULL, NULL
180      };          };
181    
182      if (!logfp)          if (logfp == NULL)
183          return;                  return;
184    
185      switch (flag) {          switch (flag) {
186      case LOG_WRITE:          case LOG_WRITE:
187          fprintf(logfp, "%s %s %s %s %s %s%s\n", get_time(),                  fprintf(logfp, "%s %s %s %s %s%s %s\n", get_time(),
188                  inf.server, inf.username, inf.mbox, inf.filter,                      inf.account, inf.mbox, inf.filter,
189                  (*inf.action == FILTER_ACTION_DELETE ? "delete" :                      (*inf.action == FILTER_ACTION_DELETE ? "delete" :
190                   *inf.action == FILTER_ACTION_COPY ? "copy " :                          *inf.action == FILTER_ACTION_COPY ? "copy" :
191                   *inf.action == FILTER_ACTION_MOVE ? "move " : "list"),                          *inf.action == FILTER_ACTION_MOVE ? "move" :
192                  (!*inf.destmbox ? "" : inf.destmbox));                          *inf.action == FILTER_ACTION_RCOPY ? "rcopy " :
193                            *inf.action == FILTER_ACTION_RMOVE ? "rmove " :
194          if (ptr) {                          *inf.action == FILTER_ACTION_FLAG_ADD ||
195              inf.hdrs = (char *)ptr;                          *inf.action == FILTER_ACTION_FLAG_REMOVE ||
196              fprintf(logfp, "%c", '\t');                          *inf.action == FILTER_ACTION_FLAG_REPLACE ? "flag" :
197              while (*inf.hdrs) {                          *inf.action == FILTER_ACTION_LIST ? "list" :
198                  if (*inf.hdrs == '\n') {                          "unknown "),
199                      fprintf(logfp, "%c", '\n');                      (inf.destaccount == NULL ? "" : inf.destaccount),
200                      if (*(inf.hdrs + 1))                      (*inf.destmbox == '\0' ? "" : inf.destmbox));
201                          fprintf(logfp, "%c", '\t');  
202                      inf.hdrs++;                  if (ptr) {
203                  } else                          inf.hdrs = (char *)ptr;
204                      fprintf(logfp, "%c", *(inf.hdrs++));                          fputc('\t', logfp);
205              }                          while (*inf.hdrs != '\0') {
206                                    if (*inf.hdrs == '\n') {
207                                            fputc('\n', logfp);
208                                            if (*(inf.hdrs + 1) != '\0')
209                                                    fputc('\t', logfp);
210                                            inf.hdrs++;
211                                    } else
212                                            fputc(*(inf.hdrs++), logfp);
213                            }
214                    }
215                    fflush(logfp);
216                    break;
217            case LOG_ACCOUNT:
218                    inf.account = (char *)ptr;
219                    break;
220            case LOG_MAILBOX:
221                    inf.mbox = (char *)ptr;
222                    break;
223            case LOG_FILTER:
224                    inf.filter = (char *)ptr;
225                    break;
226            case LOG_ACTION:
227                    inf.action = (unsigned int *)ptr;
228                    break;
229            case LOG_DESTINATION_ACCOUNT:
230                    if (ptr == NULL)
231                            inf.destaccount = NULL;
232                    else
233                            inf.destaccount = ((account_t *) ptr)->key;
234                    break;
235            case LOG_DESTINATION_MAILBOX:
236                    inf.destmbox = (char *)ptr;
237                    break;
238          }          }
         fflush(logfp);  
         break;  
     case LOG_SERVER:  
         inf.server = (char *)ptr;  
         break;  
     case LOG_USERNAME:  
         inf.username = (char *)ptr;  
         break;  
     case LOG_MAILBOX:  
         inf.mbox = (char *)ptr;  
         break;  
     case LOG_FILTER:  
         inf.filter = (char *)ptr;  
         break;  
     case LOG_ACTION:  
         inf.action = (unsigned int *)ptr;  
         break;  
     case LOG_DESTINATION_MAILBOX:  
         inf.destmbox = (char *)ptr;  
         break;  
     }  
239  }  }
240    
241    
242  /*  /*
243   * Return current local time and date.   * Return current local time and date.
244   */   */
245  char *get_time(void)  char *
246    get_time(void)
247  {  {
248      char *ct;          char *ct;
249      time_t t;          time_t t;
250    
251      t = time(NULL);          t = time(NULL);
252    
253      ct = ctime(&t);          ct = ctime(&t);
254      *(strchr(ct, '\n')) = 0;          *(strchr(ct, '\n')) = '\0';
255    
256      return ct;          return ct;
257  }  }

Legend:
Removed from v.1.29  
changed lines
  Added in v.1.30

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26