1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <errno.h> |
4 |
#include <string.h> |
5 |
#include <stdarg.h> |
6 |
#include <limits.h> |
7 |
|
8 |
#include "config.h" |
9 |
#include "imapfilter.h" |
10 |
#include "log.h" |
11 |
|
12 |
|
13 |
extern int options; |
14 |
extern char logfile[PATH_MAX]; |
15 |
|
16 |
static FILE *logfp; /* Points to logfile. */ |
17 |
|
18 |
|
19 |
/* |
20 |
* Prints message if not in OPT_DETAILS_QUITE mode. |
21 |
*/ |
22 |
void info(const char *info, ...) |
23 |
{ |
24 |
va_list args; |
25 |
|
26 |
if (!(options & OPT_DETAILS_QUITE)) { |
27 |
va_start(args, info); |
28 |
vprintf(info, args); |
29 |
va_end(args); |
30 |
} |
31 |
} |
32 |
|
33 |
|
34 |
/* |
35 |
* Prints message if in OPT_DETAILS_VERBOSE mode. |
36 |
*/ |
37 |
void verbose(const char *info, ...) |
38 |
{ |
39 |
va_list args; |
40 |
|
41 |
if (options & OPT_DETAILS_VERBOSE) { |
42 |
va_start(args, info); |
43 |
vprintf(info, args); |
44 |
va_end(args); |
45 |
} |
46 |
} |
47 |
|
48 |
/* |
49 |
* Prints error message and writes it into logfile. |
50 |
*/ |
51 |
void error(const char *errmsg, ...) |
52 |
{ |
53 |
va_list args; |
54 |
|
55 |
va_start(args, errmsg); |
56 |
|
57 |
if (logfile[0]) { |
58 |
vfprintf(logfp, errmsg, args); |
59 |
} |
60 |
|
61 |
vfprintf(stderr, errmsg, args); |
62 |
va_end(args); |
63 |
} |
64 |
|
65 |
|
66 |
/* |
67 |
* Opens the file to save logging information. |
68 |
*/ |
69 |
int open_logfile(void) |
70 |
{ |
71 |
if (!(logfile[0])) |
72 |
return 0; /* Logging not enabled. */ |
73 |
|
74 |
#ifdef DEBUG |
75 |
printf("debug: logfile: %s\n", logfile); |
76 |
#endif |
77 |
|
78 |
logfp = fopen(logfile, "a"); |
79 |
|
80 |
if (!logfp) { |
81 |
fprintf(stderr, "imapfilter: opening logfile %s: %s\n", |
82 |
logfile, strerror(errno)); |
83 |
return 1; |
84 |
} |
85 |
|
86 |
return 0; |
87 |
} |
88 |
|
89 |
|
90 |
/* |
91 |
* Closes the logfile. |
92 |
*/ |
93 |
int close_logfile(void) |
94 |
{ |
95 |
if (!(logfile[0])) |
96 |
return 0; |
97 |
else |
98 |
return fclose(logfp); |
99 |
} |
100 |
|
101 |
|
102 |
/* |
103 |
* Writes information to logfile. |
104 |
*/ |
105 |
void log_info(const char *info, ...) |
106 |
{ |
107 |
va_list args; |
108 |
|
109 |
if (logfile[0]) { |
110 |
va_start(args, info); |
111 |
vfprintf(logfp, info, args); |
112 |
va_end(args); |
113 |
} |
114 |
} |