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 account_data account; |
15 |
static FILE *logfp; /* Points to logfile. */ |
16 |
|
17 |
|
18 |
/* |
19 |
* Print message if in OPTION_DETAILS_VERBOSE mode. |
20 |
*/ |
21 |
void verbose(const char *info, ...) |
22 |
{ |
23 |
va_list args; |
24 |
|
25 |
if (options & OPTION_DETAILS_VERBOSE) { |
26 |
va_start(args, info); |
27 |
vprintf(info, args); |
28 |
va_end(args); |
29 |
} |
30 |
} |
31 |
|
32 |
/* |
33 |
* Prints error message and writes it into logfile. |
34 |
*/ |
35 |
void error(const char *errmsg, ...) |
36 |
{ |
37 |
va_list args; |
38 |
|
39 |
va_start(args, errmsg); |
40 |
|
41 |
if (!(options & OPTION_LOGGING_DISABLED)) { |
42 |
vfprintf(logfp, errmsg, args); |
43 |
} |
44 |
|
45 |
vfprintf(stderr, errmsg, args); |
46 |
va_end(args); |
47 |
} |
48 |
|
49 |
|
50 |
/* |
51 |
* Opens the file to save logging information. |
52 |
*/ |
53 |
int open_logfile(void) |
54 |
{ |
55 |
char *home; |
56 |
|
57 |
if (options & OPTION_LOGGING_DISABLED) |
58 |
return SUCCESS; |
59 |
|
60 |
if (!account.logfile[0]) { |
61 |
home = getenv("HOME"); |
62 |
|
63 |
snprintf(account.logfile, PATH_MAX, "%s/%s", home, |
64 |
".imapfilter_log"); |
65 |
} |
66 |
#ifdef DEBUG |
67 |
printf("debug: logfile: %s\n", account.logfile); |
68 |
#endif |
69 |
|
70 |
logfp = fopen(account.logfile, "a"); |
71 |
|
72 |
if (!logfp) { |
73 |
fprintf(stderr, "imapfilter: opening logfile %s: %s\n", |
74 |
account.logfile, strerror(errno)); |
75 |
return FAILURE; |
76 |
} |
77 |
|
78 |
return SUCCESS; |
79 |
} |
80 |
|
81 |
|
82 |
/* |
83 |
* Closes the logfile. |
84 |
*/ |
85 |
int close_logfile(void) |
86 |
{ |
87 |
if (options & OPTION_LOGGING_DISABLED) |
88 |
return SUCCESS; |
89 |
else |
90 |
return fclose(logfp); |
91 |
} |
92 |
|
93 |
|
94 |
/* Writes information to logfile. */ |
95 |
void log_info(const char *info, ...) |
96 |
{ |
97 |
va_list args; |
98 |
|
99 |
if (!(options & OPTION_LOGGING_DISABLED)) { |
100 |
va_start(args, info); |
101 |
vfprintf(logfp, info, args); |
102 |
va_end(args); |
103 |
} |
104 |
} |