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 gboolean prefs_common_enable_log_standard(void);
58 gboolean prefs_common_enable_log_warning(void);
59 gboolean prefs_common_enable_log_error(void);
60 gboolean prefs_common_enable_log_status(void);
62 static gboolean invoke_hook_cb (gpointer data)
64 LogText *logtext = (LogText *)data;
65 hooks_invoke(get_log_hook(logtext->instance), logtext);
66 g_free(logtext->text);
71 void set_log_file(LogInstance instance, const gchar *filename)
76 /* backup old logfile if existing */
77 if (is_file_exist(filename)) {
80 backupname = g_strconcat(filename, ".bak", NULL);
81 if (rename(filename, backupname) < 0)
82 FILE_OP_ERROR(filename, "rename");
86 log_fp[instance] = g_fopen(filename, "wb");
87 if (!log_fp[instance])
88 FILE_OP_ERROR(filename, "fopen");
91 void close_log_file(LogInstance instance)
93 if (log_fp[instance]) {
94 fclose(log_fp[instance]);
95 log_fp[instance] = NULL;
99 const char *get_log_hook(LogInstance instance)
101 return log_instances[instance].hook;
104 void set_log_title(LogInstance instance, gchar *title)
106 log_instances[instance].title = title;
109 gchar *get_log_title(LogInstance instance)
111 return log_instances[instance].title;
114 void set_log_prefs(LogInstance instance, int* logwin_width, int* logwin_height)
116 log_instances[instance].prefs_logwin_width = logwin_width;
117 log_instances[instance].prefs_logwin_height = logwin_height;
120 void get_log_prefs(LogInstance instance, int** logwin_width, int** logwin_height)
123 *logwin_width = log_instances[instance].prefs_logwin_width;
125 *logwin_height = log_instances[instance].prefs_logwin_height;
128 void log_print(LogInstance instance, const gchar *format, ...)
131 gchar buf[BUFFSIZE + LOG_TIME_LEN];
133 LogText *logtext = g_new0(LogText, 1);
137 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
139 va_start(args, format);
140 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
143 if (debug_get_mode()) fputs(buf, stdout);
145 logtext->instance = instance;
146 logtext->text = g_strdup(buf);
147 logtext->type = LOG_NORMAL;
149 g_timeout_add(0, invoke_hook_cb, logtext);
151 if (log_fp[instance] && prefs_common_enable_log_standard()) {
152 fputs(buf, log_fp[instance]);
153 fflush(log_fp[instance]);
157 void log_message(LogInstance instance, const gchar *format, ...)
160 gchar buf[BUFFSIZE + LOG_TIME_LEN];
162 LogText *logtext = g_new0(LogText, 1);
166 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
168 va_start(args, format);
169 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
172 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
174 logtext->instance = instance;
175 logtext->text = g_strdup(buf + LOG_TIME_LEN);
176 logtext->type = LOG_MSG;
178 g_timeout_add(0, invoke_hook_cb, logtext);
180 if (log_fp[instance] && prefs_common_enable_log_standard()) {
181 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
182 fputs("* message: ", log_fp[instance]);
183 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
184 fflush(log_fp[instance]);
188 void log_warning(LogInstance instance, const gchar *format, ...)
191 gchar buf[BUFFSIZE + LOG_TIME_LEN];
193 LogText *logtext = g_new0(LogText, 1);
197 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
199 va_start(args, format);
200 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
203 g_warning("%s", buf);
205 logtext->instance = instance;
206 logtext->text = g_strdup(buf + LOG_TIME_LEN);
207 logtext->type = LOG_WARN;
209 g_timeout_add(0, invoke_hook_cb, logtext);
211 if (log_fp[instance] && prefs_common_enable_log_warning()) {
212 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
213 fputs("** warning: ", log_fp[instance]);
214 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
215 fflush(log_fp[instance]);
219 void log_error(LogInstance instance, const gchar *format, ...)
222 gchar buf[BUFFSIZE + LOG_TIME_LEN];
224 LogText *logtext = g_new0(LogText, 1);
228 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
230 va_start(args, format);
231 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
234 g_warning("%s", buf);
236 logtext->instance = instance;
237 logtext->text = g_strdup(buf + LOG_TIME_LEN);
238 logtext->type = LOG_ERROR;
240 g_timeout_add(0, invoke_hook_cb, logtext);
242 if (log_fp[instance] && prefs_common_enable_log_error()) {
243 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
244 fputs("*** error: ", log_fp[instance]);
245 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
246 fflush(log_fp[instance]);
250 void log_status_ok(LogInstance instance, const gchar *format, ...)
253 gchar buf[BUFFSIZE + LOG_TIME_LEN];
255 LogText *logtext = g_new0(LogText, 1);
259 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
261 va_start(args, format);
262 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
265 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
267 logtext->instance = instance;
268 logtext->text = g_strdup(buf + LOG_TIME_LEN);
269 logtext->type = LOG_STATUS_OK;
271 g_timeout_add(0, invoke_hook_cb, logtext);
273 if (log_fp[instance] && prefs_common_enable_log_status()) {
274 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
275 fputs("* OK: ", log_fp[instance]);
276 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
277 fflush(log_fp[instance]);
281 void log_status_nok(LogInstance instance, const gchar *format, ...)
284 gchar buf[BUFFSIZE + LOG_TIME_LEN];
286 LogText *logtext = g_new0(LogText, 1);
290 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
292 va_start(args, format);
293 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
296 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
298 logtext->instance = instance;
299 logtext->text = g_strdup(buf + LOG_TIME_LEN);
300 logtext->type = LOG_STATUS_NOK;
302 g_timeout_add(0, invoke_hook_cb, logtext);
304 if (log_fp[instance] && prefs_common_enable_log_status()) {
305 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
306 fputs("* NOT OK: ", log_fp[instance]);
307 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
308 fflush(log_fp[instance]);
312 void log_status_skip(LogInstance instance, const gchar *format, ...)
315 gchar buf[BUFFSIZE + LOG_TIME_LEN];
317 LogText *logtext = g_new0(LogText, 1);
321 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
323 va_start(args, format);
324 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
327 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
329 logtext->instance = instance;
330 logtext->text = g_strdup(buf + LOG_TIME_LEN);
331 logtext->type = LOG_STATUS_SKIP;
333 g_timeout_add(0, invoke_hook_cb, logtext);
335 if (log_fp[instance] && prefs_common_enable_log_status()) {
336 fwrite(buf, 1, LOG_TIME_LEN, log_fp[instance]);
337 fputs("* SKIPPED: ", log_fp[instance]);
338 fputs(buf + LOG_TIME_LEN, log_fp[instance]);
339 fflush(log_fp[instance]);