Commit | Line | Data |
---|---|---|
835813fd | 1 | #define IMAGER_NO_CONTEXT |
696cb85d | 2 | #include "imageri.h" |
e11d297f | 3 | #include "imconfig.h" |
02d1d628 | 4 | #include "log.h" |
35891892 | 5 | #include <stdlib.h> |
10ea52a3 | 6 | #include <errno.h> |
8d14daab | 7 | #include "imerror.h" |
10ea52a3 TC |
8 | |
9 | #ifdef IMAGER_LOG | |
02d1d628 AMH |
10 | |
11 | #define DTBUFF 50 | |
12 | #define DATABUFF DTBUFF+3+10+1+5+1+1 | |
13 | ||
696cb85d | 14 | #define LOG_DATE_FORMAT "%Y/%m/%d %H:%M:%S" |
a482206e | 15 | |
cd45c810 TC |
16 | static i_mutex_t log_mutex; |
17 | ||
44d86483 TC |
18 | static void |
19 | im_vloog(pIMCTX, int level, const char *fmt, va_list ap); | |
20 | ||
02d1d628 AMH |
21 | /* |
22 | * Logging is active | |
23 | */ | |
24 | ||
10ea52a3 | 25 | int |
696cb85d | 26 | im_init_log(pIMCTX, const char* name,int level) { |
10ea52a3 | 27 | i_clear_error(); |
6ee0c41f | 28 | |
cd45c810 TC |
29 | if (!log_mutex) { |
30 | log_mutex = i_mutex_new(); | |
31 | } | |
32 | ||
6ee0c41f TC |
33 | if (aIMCTX->lg_file) { |
34 | if (aIMCTX->own_log) | |
35 | fclose(aIMCTX->lg_file); | |
36 | aIMCTX->lg_file = NULL; | |
37 | } | |
38 | ||
696cb85d | 39 | aIMCTX->log_level = level; |
02d1d628 | 40 | if (level < 0) { |
696cb85d | 41 | aIMCTX->lg_file = NULL; |
02d1d628 AMH |
42 | } else { |
43 | if (name == NULL) { | |
696cb85d | 44 | aIMCTX->lg_file = stderr; |
6ee0c41f | 45 | aIMCTX->own_log = 0; |
02d1d628 | 46 | } else { |
696cb85d | 47 | if (NULL == (aIMCTX->lg_file = fopen(name, "w+")) ) { |
835813fd | 48 | im_push_errorf(aIMCTX, errno, "Cannot open file '%s': (%d)", name, errno); |
10ea52a3 | 49 | return 0; |
02d1d628 | 50 | } |
6ee0c41f TC |
51 | aIMCTX->own_log = 1; |
52 | setvbuf(aIMCTX->lg_file, NULL, _IONBF, BUFSIZ); | |
02d1d628 AMH |
53 | } |
54 | } | |
696cb85d | 55 | if (aIMCTX->lg_file) { |
835813fd | 56 | im_log((aIMCTX, 0,"Imager - log started (level = %d)\n", level)); |
10ea52a3 TC |
57 | } |
58 | ||
696cb85d | 59 | return aIMCTX->lg_file != NULL; |
02d1d628 AMH |
60 | } |
61 | ||
62 | void | |
b1e96952 | 63 | i_fatal(int exitcode,const char *fmt, ... ) { |
02d1d628 | 64 | va_list ap; |
835813fd | 65 | dIMCTX; |
44d86483 TC |
66 | |
67 | if (aIMCTX->lg_file != NULL) { | |
a482206e | 68 | va_start(ap,fmt); |
44d86483 | 69 | im_vloog(aIMCTX, 0, fmt, ap); |
a482206e TC |
70 | va_end(ap); |
71 | } | |
72 | exit(exitcode); | |
73 | } | |
02d1d628 | 74 | |
44d86483 TC |
75 | void |
76 | im_fatal(pIMCTX, int exitcode,const char *fmt, ... ) { | |
77 | va_list ap; | |
44d86483 TC |
78 | |
79 | if (aIMCTX->lg_file != NULL) { | |
80 | va_start(ap,fmt); | |
81 | im_vloog(aIMCTX, 0, fmt, ap); | |
82 | va_end(ap); | |
83 | } | |
84 | exit(exitcode); | |
85 | } | |
d03fd5a4 | 86 | |
bd8052a6 TC |
87 | /* |
88 | =item i_loog(level, format, ...) | |
89 | =category Logging | |
90 | ||
91 | This is an internal function called by the mm_log() macro. | |
92 | ||
93 | =cut | |
94 | */ | |
02d1d628 | 95 | |
696cb85d TC |
96 | static void |
97 | im_vloog(pIMCTX, int level, const char *fmt, va_list ap) { | |
98 | time_t timi; | |
99 | struct tm *str_tm; | |
100 | char date_buffer[DTBUFF]; | |
101 | ||
2e23caff | 102 | if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level) |
696cb85d | 103 | return; |
cd45c810 TC |
104 | |
105 | i_mutex_lock(log_mutex); | |
696cb85d TC |
106 | |
107 | timi = time(NULL); | |
108 | str_tm = localtime(&timi); | |
109 | strftime(date_buffer, DTBUFF, LOG_DATE_FORMAT, str_tm); | |
110 | fprintf(aIMCTX->lg_file, "[%s] %10s:%-5d %3d: ", date_buffer, | |
111 | aIMCTX->filename, aIMCTX->line, level); | |
112 | vfprintf(aIMCTX->lg_file, fmt, ap); | |
113 | fflush(aIMCTX->lg_file); | |
cd45c810 TC |
114 | |
115 | i_mutex_unlock(log_mutex); | |
696cb85d TC |
116 | } |
117 | ||
02d1d628 | 118 | void |
bf1573f9 | 119 | i_loog(int level,const char *fmt, ... ) { |
835813fd | 120 | dIMCTX; |
02d1d628 | 121 | va_list ap; |
696cb85d | 122 | |
2e23caff | 123 | if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level) |
696cb85d TC |
124 | return; |
125 | ||
126 | va_start(ap,fmt); | |
127 | im_vloog(aIMCTX, level, fmt, ap); | |
128 | va_end(ap); | |
129 | } | |
130 | ||
131 | void | |
132 | im_loog(pIMCTX, int level,const char *fmt, ... ) { | |
133 | va_list ap; | |
134 | ||
2e23caff | 135 | if (!aIMCTX || !aIMCTX->lg_file || level > aIMCTX->log_level) |
696cb85d TC |
136 | return; |
137 | ||
138 | va_start(ap,fmt); | |
139 | im_vloog(aIMCTX, level, fmt, ap); | |
140 | va_end(ap); | |
02d1d628 AMH |
141 | } |
142 | ||
6cfee9d1 TC |
143 | /* |
144 | =item i_lhead(file, line) | |
145 | =category Logging | |
146 | ||
147 | This is an internal function called by the mm_log() macro. | |
148 | ||
149 | =cut | |
150 | */ | |
02d1d628 AMH |
151 | |
152 | void | |
696cb85d | 153 | im_lhead(pIMCTX, const char *file, int line) { |
2e23caff | 154 | if (aIMCTX && aIMCTX->lg_file != NULL) { |
696cb85d TC |
155 | aIMCTX->filename = file; |
156 | aIMCTX->line = line; | |
02d1d628 AMH |
157 | } |
158 | } | |
10ea52a3 | 159 | |
696cb85d | 160 | void i_lhead(const char *file, int line) { |
835813fd | 161 | dIMCTX; |
696cb85d TC |
162 | |
163 | im_lhead(aIMCTX, file, line); | |
164 | } | |
165 | ||
10ea52a3 TC |
166 | #else |
167 | ||
168 | /* | |
169 | * Logging is inactive - insert dummy functions | |
170 | */ | |
171 | ||
835813fd | 172 | int im_init_log(pIMCTX, const char* name,int onoff) { |
10ea52a3 TC |
173 | i_clear_error(); |
174 | i_push_error(0, "Logging disabled"); | |
175 | return 0; | |
176 | } | |
177 | ||
178 | void i_fatal(int exitcode,const char *fmt, ... ) { exit(exitcode); } | |
44d86483 | 179 | void im_fatal(pIMCTX, int exitcode,const char *fmt, ... ) { exit(exitcode); } |
10ea52a3 TC |
180 | |
181 | void | |
182 | i_loog(int level,const char *fmt, ... ) { | |
183 | } | |
184 | ||
835813fd TC |
185 | void |
186 | im_loog(pIMCTX, int level,const char *fmt, ... ) { | |
187 | } | |
188 | ||
10ea52a3 TC |
189 | void |
190 | i_lhead(const char *file, int line) { } | |
191 | ||
835813fd TC |
192 | void |
193 | im_lhead(pIMCTX, const char *file, int line) { } | |
194 | ||
10ea52a3 | 195 | #endif |