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 |
|
|
#include "log.h" |
15 |
|
|
|
16 |
|
|
|
17 |
lefcha |
1.5 |
extern char logfile[PATH_MAX]; |
18 |
lefcha |
1.8 |
extern unsigned int options; |
19 |
lefcha |
1.6 |
|
20 |
lefcha |
1.8 |
static FILE *logfp = NULL; /* Points to logfile. */ |
21 |
lefcha |
1.1 |
|
22 |
|
|
|
23 |
|
|
/* |
24 |
lefcha |
1.7 |
* Prints message if not in OPT_DETAILS_QUITE mode. |
25 |
|
|
*/ |
26 |
|
|
void info(const char *info, ...) |
27 |
|
|
{ |
28 |
|
|
va_list args; |
29 |
lefcha |
1.8 |
|
30 |
lefcha |
1.7 |
if (!(options & OPT_DETAILS_QUITE)) { |
31 |
|
|
va_start(args, info); |
32 |
|
|
vprintf(info, args); |
33 |
|
|
va_end(args); |
34 |
|
|
} |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
|
38 |
|
|
/* |
39 |
lefcha |
1.6 |
* Prints message if in OPT_DETAILS_VERBOSE mode. |
40 |
lefcha |
1.1 |
*/ |
41 |
|
|
void verbose(const char *info, ...) |
42 |
|
|
{ |
43 |
|
|
va_list args; |
44 |
|
|
|
45 |
lefcha |
1.6 |
if (options & OPT_DETAILS_VERBOSE) { |
46 |
lefcha |
1.1 |
va_start(args, info); |
47 |
|
|
vprintf(info, args); |
48 |
|
|
va_end(args); |
49 |
|
|
} |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
/* |
53 |
|
|
* Prints error message and writes it into logfile. |
54 |
|
|
*/ |
55 |
|
|
void error(const char *errmsg, ...) |
56 |
|
|
{ |
57 |
|
|
va_list args; |
58 |
|
|
|
59 |
|
|
va_start(args, errmsg); |
60 |
|
|
|
61 |
lefcha |
1.8 |
if (logfp) { |
62 |
lefcha |
1.1 |
vfprintf(logfp, errmsg, args); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
vfprintf(stderr, errmsg, args); |
66 |
|
|
va_end(args); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
|
70 |
|
|
/* |
71 |
|
|
* Opens the file to save logging information. |
72 |
|
|
*/ |
73 |
|
|
int open_logfile(void) |
74 |
|
|
{ |
75 |
lefcha |
1.5 |
if (!(logfile[0])) |
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 |
|
|
* |
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 |
lefcha |
1.6 |
return 1; |
114 |
lefcha |
1.1 |
} |
115 |
|
|
|
116 |
lefcha |
1.6 |
return 0; |
117 |
lefcha |
1.1 |
} |
118 |
|
|
|
119 |
|
|
|
120 |
|
|
/* |
121 |
|
|
* Closes the logfile. |
122 |
|
|
*/ |
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 |
|
|
* Writes information to logfile. |
134 |
|
|
*/ |
135 |
lefcha |
1.1 |
void log_info(const char *info, ...) |
136 |
|
|
{ |
137 |
|
|
va_list args; |
138 |
|
|
|
139 |
lefcha |
1.8 |
if (logfp) { |
140 |
lefcha |
1.1 |
va_start(args, info); |
141 |
|
|
vfprintf(logfp, info, args); |
142 |
|
|
va_end(args); |
143 |
|
|
} |
144 |
|
|
} |