2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include <glib/gi18n.h>
38 static FILE *log_fp[LOG_INSTANCE_MAX] = {
43 typedef struct _LogInstanceData LogInstanceData;
45 struct _LogInstanceData {
48 int *prefs_logwin_width;
49 int *prefs_logwin_height;
52 static LogInstanceData log_instances[LOG_INSTANCE_MAX] = {
53 { LOG_APPEND_TEXT_HOOKLIST, NULL, NULL, NULL },
54 { DEBUG_FILTERING_APPEND_TEXT_HOOKLIST, NULL, NULL, NULL }
57 static gboolean invoke_hook_cb (gpointer data)
59 LogText *logtext = (LogText *)data;
60 hooks_invoke(get_log_hook(logtext->instance), logtext);
61 g_free(logtext->text);
66 void set_log_file(LogInstance instance, const gchar *filename)
71 /* backup old logfile if existing */
72 if (is_file_exist(filename)) {
75 backupname = g_strconcat(filename, ".bak", NULL);
76 if (rename(filename, backupname) < 0)
77 FILE_OP_ERROR(filename, "rename");
81 log_fp[instance] = g_fopen(filename, "wb");
82 if (!log_fp[instance])
83 FILE_OP_ERROR(filename, "fopen");
86 void close_log_file(LogInstance instance)
88 if (log_fp[instance]) {
89 fclose(log_fp[instance]);
90 log_fp[instance] = NULL;
94 const char *get_log_hook(LogInstance instance)
96 return log_instances[instance].hook;
99 void set_log_title(LogInstance instance, gchar *title)
101 log_instances[instance].title = title;
104 gchar *get_log_title(LogInstance instance)
106 return log_instances[instance].title;
109 void set_log_prefs(LogInstance instance, int* logwin_width, int* logwin_height)
111 log_instances[instance].prefs_logwin_width = logwin_width;
112 log_instances[instance].prefs_logwin_height = logwin_height;
115 void get_log_prefs(LogInstance instance, int** logwin_width, int** logwin_height)
118 *logwin_width = log_instances[instance].prefs_logwin_width;
120 *logwin_height = log_instances[instance].prefs_logwin_height;
123 void log_print(LogInstance instance, const gchar *format, ...)
126 gchar buf[BUFFSIZE + LOG_TIME_LEN];
128 LogText *logtext = g_new0(LogText, 1);
132 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
134 va_start(args, format);
135 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
138 if (debug_get_mode()) fputs(buf, stdout);
140 logtext->instance = instance;
141 logtext->text = g_strdup(buf);
142 logtext->type = LOG_NORMAL;
144 g_timeout_add(0, invoke_hook_cb, logtext);
146 if (log_fp[instance]) {
147 fputs(buf, log_fp[instance]);
148 fflush(log_fp[instance]);
152 void log_message(LogInstance instance, const gchar *format, ...)
155 gchar buf[BUFFSIZE + LOG_TIME_LEN];
157 LogText *logtext = g_new0(LogText, 1);
161 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
163 va_start(args, format);
164 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
167 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
169 logtext->instance = instance;
170 logtext->text = g_strdup(buf + LOG_TIME_LEN);
171 logtext->type = LOG_MSG;
173 g_timeout_add(0, invoke_hook_cb, logtext);
175 if (log_fp[instance]) {
176 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
177 fputs("* message: ", log_fp[instance]);
178 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
179 fflush(log_fp[instance]);
183 void log_warning(LogInstance instance, const gchar *format, ...)
186 gchar buf[BUFFSIZE + LOG_TIME_LEN];
188 LogText *logtext = g_new0(LogText, 1);
192 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
194 va_start(args, format);
195 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
198 g_warning("%s", buf);
200 logtext->instance = instance;
201 logtext->text = g_strdup(buf + LOG_TIME_LEN);
202 logtext->type = LOG_WARN;
204 g_timeout_add(0, invoke_hook_cb, logtext);
206 if (log_fp[instance]) {
207 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
208 fputs("** warning: ", log_fp[instance]);
209 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
210 fflush(log_fp[instance]);
214 void log_error(LogInstance instance, const gchar *format, ...)
217 gchar buf[BUFFSIZE + LOG_TIME_LEN];
219 LogText *logtext = g_new0(LogText, 1);
223 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
225 va_start(args, format);
226 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
229 g_warning("%s", buf);
231 logtext->instance = instance;
232 logtext->text = g_strdup(buf + LOG_TIME_LEN);
233 logtext->type = LOG_ERROR;
235 g_timeout_add(0, invoke_hook_cb, logtext);
237 if (log_fp[instance]) {
238 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
239 fputs("*** error: ", log_fp[instance]);
240 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
241 fflush(log_fp[instance]);
245 void log_status_ok(LogInstance instance, const gchar *format, ...)
248 gchar buf[BUFFSIZE + LOG_TIME_LEN];
250 LogText *logtext = g_new0(LogText, 1);
254 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
256 va_start(args, format);
257 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
260 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
262 logtext->instance = instance;
263 logtext->text = g_strdup(buf + LOG_TIME_LEN);
264 logtext->type = LOG_STATUS_OK;
266 g_timeout_add(0, invoke_hook_cb, logtext);
268 if (log_fp[instance]) {
269 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
270 fputs("* OK: ", log_fp[instance]);
271 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
272 fflush(log_fp[instance]);
276 void log_status_nok(LogInstance instance, const gchar *format, ...)
279 gchar buf[BUFFSIZE + LOG_TIME_LEN];
281 LogText *logtext = g_new0(LogText, 1);
285 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
287 va_start(args, format);
288 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
291 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
293 logtext->instance = instance;
294 logtext->text = g_strdup(buf + LOG_TIME_LEN);
295 logtext->type = LOG_STATUS_NOK;
297 g_timeout_add(0, invoke_hook_cb, logtext);
299 if (log_fp[instance]) {
300 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
301 fputs("* NOT OK: ", log_fp[instance]);
302 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
303 fflush(log_fp[instance]);
307 void log_status_skip(LogInstance instance, const gchar *format, ...)
310 gchar buf[BUFFSIZE + LOG_TIME_LEN];
312 LogText *logtext = g_new0(LogText, 1);
316 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
318 va_start(args, format);
319 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
322 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
324 logtext->instance = instance;
325 logtext->text = g_strdup(buf + LOG_TIME_LEN);
326 logtext->type = LOG_STATUS_SKIP;
328 g_timeout_add(0, invoke_hook_cb, logtext);
330 if (log_fp[instance]) {
331 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
332 fputs("* SKIPPED: ", log_fp[instance]);
333 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
334 fflush(log_fp[instance]);