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