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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <glib/gi18n.h>
34 static FILE *log_fp[LOG_INSTANCE_MAX] = {
39 typedef struct _LogInstanceData LogInstanceData;
41 struct _LogInstanceData {
44 int *prefs_logwin_width;
45 int *prefs_logwin_height;
48 static LogInstanceData log_instances[LOG_INSTANCE_MAX] = {
49 { LOG_APPEND_TEXT_HOOKLIST, NULL, NULL, NULL },
50 { DEBUG_FILTERING_APPEND_TEXT_HOOKLIST, NULL, NULL, NULL }
53 static gboolean invoke_hook_cb (gpointer data)
55 LogText *logtext = (LogText *)data;
56 hooks_invoke(get_log_hook(logtext->instance), logtext);
57 g_free(logtext->text);
62 void set_log_file(LogInstance instance, const gchar *filename)
67 /* backup old logfile if existing */
68 if (is_file_exist(filename)) {
71 backupname = g_strconcat(filename, ".bak", NULL);
72 if (rename(filename, backupname) < 0)
73 FILE_OP_ERROR(filename, "rename");
77 log_fp[instance] = g_fopen(filename, "wb");
78 if (!log_fp[instance])
79 FILE_OP_ERROR(filename, "fopen");
82 void close_log_file(LogInstance instance)
84 if (log_fp[instance]) {
85 fclose(log_fp[instance]);
86 log_fp[instance] = NULL;
90 const char *get_log_hook(LogInstance instance)
92 return log_instances[instance].hook;
95 void set_log_title(LogInstance instance, gchar *title)
97 log_instances[instance].title = title;
100 gchar *get_log_title(LogInstance instance)
102 return log_instances[instance].title;
105 void set_log_prefs(LogInstance instance, int* logwin_width, int* logwin_height)
107 log_instances[instance].prefs_logwin_width = logwin_width;
108 log_instances[instance].prefs_logwin_height = logwin_height;
111 void get_log_prefs(LogInstance instance, int** logwin_width, int** logwin_height)
114 *logwin_width = log_instances[instance].prefs_logwin_width;
116 *logwin_height = log_instances[instance].prefs_logwin_height;
119 void log_print(LogInstance instance, const gchar *format, ...)
122 gchar buf[BUFFSIZE + LOG_TIME_LEN];
124 LogText *logtext = g_new0(LogText, 1);
128 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
130 va_start(args, format);
131 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
134 if (debug_get_mode()) fputs(buf, stdout);
136 logtext->instance = instance;
137 logtext->text = g_strdup(buf);
138 logtext->type = LOG_NORMAL;
140 g_timeout_add(0, invoke_hook_cb, logtext);
142 if (log_fp[instance]) {
143 fputs(buf, log_fp[instance]);
144 fflush(log_fp[instance]);
148 void log_message(LogInstance instance, const gchar *format, ...)
151 gchar buf[BUFFSIZE + LOG_TIME_LEN];
153 LogText *logtext = g_new0(LogText, 1);
157 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
159 va_start(args, format);
160 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
163 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
165 logtext->instance = instance;
166 logtext->text = g_strdup(buf + LOG_TIME_LEN);
167 logtext->type = LOG_MSG;
169 g_timeout_add(0, invoke_hook_cb, logtext);
171 if (log_fp[instance]) {
172 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
173 fputs("* message: ", log_fp[instance]);
174 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
175 fflush(log_fp[instance]);
179 void log_warning(LogInstance instance, const gchar *format, ...)
182 gchar buf[BUFFSIZE + LOG_TIME_LEN];
184 LogText *logtext = g_new0(LogText, 1);
188 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
190 va_start(args, format);
191 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
194 g_warning("%s", buf);
196 logtext->instance = instance;
197 logtext->text = g_strdup(buf + LOG_TIME_LEN);
198 logtext->type = LOG_WARN;
200 g_timeout_add(0, invoke_hook_cb, logtext);
202 if (log_fp[instance]) {
203 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
204 fputs("** warning: ", log_fp[instance]);
205 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
206 fflush(log_fp[instance]);
210 void log_error(LogInstance instance, const gchar *format, ...)
213 gchar buf[BUFFSIZE + LOG_TIME_LEN];
215 LogText *logtext = g_new0(LogText, 1);
219 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
221 va_start(args, format);
222 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
225 g_warning("%s", buf);
227 logtext->instance = instance;
228 logtext->text = g_strdup(buf + LOG_TIME_LEN);
229 logtext->type = LOG_ERROR;
231 g_timeout_add(0, invoke_hook_cb, logtext);
233 if (log_fp[instance]) {
234 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
235 fputs("*** error: ", log_fp[instance]);
236 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
237 fflush(log_fp[instance]);
241 void log_status_ok(LogInstance instance, const gchar *format, ...)
244 gchar buf[BUFFSIZE + LOG_TIME_LEN];
246 LogText *logtext = g_new0(LogText, 1);
250 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
252 va_start(args, format);
253 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
256 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
258 logtext->instance = instance;
259 logtext->text = g_strdup(buf + LOG_TIME_LEN);
260 logtext->type = LOG_STATUS_OK;
262 g_timeout_add(0, invoke_hook_cb, logtext);
264 if (log_fp[instance]) {
265 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
266 fputs("* OK: ", log_fp[instance]);
267 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
268 fflush(log_fp[instance]);
272 void log_status_nok(LogInstance instance, const gchar *format, ...)
275 gchar buf[BUFFSIZE + LOG_TIME_LEN];
277 LogText *logtext = g_new0(LogText, 1);
281 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
283 va_start(args, format);
284 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
287 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
289 logtext->instance = instance;
290 logtext->text = g_strdup(buf + LOG_TIME_LEN);
291 logtext->type = LOG_STATUS_NOK;
293 g_timeout_add(0, invoke_hook_cb, logtext);
295 if (log_fp[instance]) {
296 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
297 fputs("* NOT OK: ", log_fp[instance]);
298 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
299 fflush(log_fp[instance]);
303 void log_status_skip(LogInstance instance, const gchar *format, ...)
306 gchar buf[BUFFSIZE + LOG_TIME_LEN];
308 LogText *logtext = g_new0(LogText, 1);
312 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
314 va_start(args, format);
315 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
318 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
320 logtext->instance = instance;
321 logtext->text = g_strdup(buf + LOG_TIME_LEN);
322 logtext->type = LOG_STATUS_SKIP;
324 g_timeout_add(0, invoke_hook_cb, logtext);
326 if (log_fp[instance]) {
327 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
328 fputs("* SKIPPED: ", log_fp[instance]);
329 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
330 fflush(log_fp[instance]);