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

Annotation of /imapfilter/log.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (hide annotations)
Wed Sep 12 15:40:35 2001 UTC (22 years, 7 months ago) by lefcha
Branch: MAIN
Changes since 1.10: +1 -1 lines
File MIME type: text/plain
Logfile option and freeing of data structure's memory enabled.

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.1
12     #include "config.h"
13     #include "imapfilter.h"
14    
15    
16 lefcha 1.5 extern char logfile[PATH_MAX];
17 lefcha 1.8 extern unsigned int options;
18 lefcha 1.6
19 lefcha 1.10 static FILE *logfp = NULL; /* Pointer to logfile. */
20 lefcha 1.1
21    
22     /*
23 lefcha 1.11 * Print message if not in OPTION_DETAILS_QUITE mode.
24 lefcha 1.7 */
25     void info(const char *info, ...)
26     {
27     va_list args;
28 lefcha 1.8
29 lefcha 1.10 if (!(options & OPTION_DETAILS_QUITE)) {
30 lefcha 1.7 va_start(args, info);
31     vprintf(info, args);
32     va_end(args);
33     }
34     }
35    
36    
37     /*
38 lefcha 1.10 * Print message if in OPTION_DETAILS_VERBOSE mode.
39 lefcha 1.1 */
40     void verbose(const char *info, ...)
41     {
42     va_list args;
43    
44 lefcha 1.10 if (options & OPTION_DETAILS_VERBOSE) {
45 lefcha 1.1 va_start(args, info);
46     vprintf(info, args);
47     va_end(args);
48     }
49     }
50    
51     /*
52 lefcha 1.10 * Print error message and write it into logfile.
53 lefcha 1.1 */
54     void error(const char *errmsg, ...)
55     {
56     va_list args;
57    
58     va_start(args, errmsg);
59    
60 lefcha 1.8 if (logfp) {
61 lefcha 1.1 vfprintf(logfp, errmsg, args);
62     }
63    
64     vfprintf(stderr, errmsg, args);
65 lefcha 1.10
66 lefcha 1.1 va_end(args);
67     }
68    
69    
70     /*
71 lefcha 1.10 * Open the file to save logging information.
72 lefcha 1.1 */
73     int open_logfile(void)
74     {
75 lefcha 1.10 if (!*logfile)
76 lefcha 1.6 return 0; /* Logging not enabled. */
77 lefcha 1.4
78 lefcha 1.1 #ifdef DEBUG
79 lefcha 1.8 printf("debug: logfile: '%s'\n", logfile);
80 lefcha 1.1 #endif
81    
82 lefcha 1.9 if (create_logfile())
83     return 1;
84    
85 lefcha 1.5 logfp = fopen(logfile, "a");
86 lefcha 1.1
87     if (!logfp) {
88     fprintf(stderr, "imapfilter: opening logfile %s: %s\n",
89 lefcha 1.5 logfile, strerror(errno));
90 lefcha 1.9 return 1;
91     }
92    
93     return 0;
94     }
95    
96    
97     /*
98 lefcha 1.10 * If file does not exist, create it with proper permissions.
99 lefcha 1.9 */
100     int create_logfile(void)
101     {
102     int fd;
103     struct stat fs;
104    
105     if (stat(logfile, &fs) && errno == ENOENT) {
106     fd = creat(logfile, S_IRUSR | S_IWUSR);
107     close(fd);
108     return 0;
109     } else if (!S_ISREG(fs.st_mode)) {
110     printf
111     ("imapfilter: file %s already exists and not a regular file\n",
112     logfile);
113 lefcha 1.6 return 1;
114 lefcha 1.1 }
115    
116 lefcha 1.6 return 0;
117 lefcha 1.1 }
118    
119    
120     /*
121 lefcha 1.10 * Close the logfile.
122 lefcha 1.1 */
123     int close_logfile(void)
124     {
125 lefcha 1.8 if (!logfp)
126 lefcha 1.6 return 0;
127 lefcha 1.1 else
128     return fclose(logfp);
129     }
130    
131    
132 lefcha 1.2 /*
133 lefcha 1.10 * Write information to logfile.
134 lefcha 1.2 */
135 lefcha 1.1 void log_info(const char *info, ...)
136     {
137     va_list args;
138    
139 lefcha 1.10 va_start(args, info);
140    
141 lefcha 1.8 if (logfp) {
142 lefcha 1.1 vfprintf(logfp, info, args);
143     }
144 lefcha 1.10
145     vfprintf(stdout, info, args);
146    
147     va_end(args);
148 lefcha 1.1 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26