2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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/>.
22 #include "claws-features.h"
33 #include <glib/gi18n.h>
40 #define FWRITE(_b,_s,_n,_f) if (fwrite(_b,_s,_n,_f) != _n) { \
41 g_message("log fwrite failed!\n"); \
44 #define FPUTS(_b,_f) if (fputs(_b,_f) == EOF) { \
45 g_message("log fputs failed!\n"); \
48 #define FFLUSH(_f) if (fflush(_f) != 0) { \
49 g_message("log fflush failed!\n"); \
53 static FILE *log_fp[LOG_INSTANCE_MAX] = {
58 static size_t log_size[LOG_INSTANCE_MAX] = {
63 static gchar *log_filename[LOG_INSTANCE_MAX] = {
69 static gboolean log_error_capability[LOG_INSTANCE_MAX] = {
74 typedef struct _LogInstanceData LogInstanceData;
76 struct _LogInstanceData {
79 int *prefs_logwin_width;
80 int *prefs_logwin_height;
83 static LogInstanceData log_instances[LOG_INSTANCE_MAX] = {
84 { LOG_APPEND_TEXT_HOOKLIST, NULL, NULL, NULL },
85 { DEBUG_FILTERING_APPEND_TEXT_HOOKLIST, NULL, NULL, NULL }
88 gboolean prefs_common_enable_log_standard(void);
89 gboolean prefs_common_enable_log_warning(void);
90 gboolean prefs_common_enable_log_error(void);
91 gboolean prefs_common_enable_log_status(void);
93 static gboolean invoke_hook_cb (gpointer data)
95 LogText *logtext = (LogText *)data;
96 hooks_invoke(get_log_hook(logtext->instance), logtext);
97 g_free(logtext->text);
102 void set_log_file(LogInstance instance, const gchar *filename)
104 gchar *fullname = NULL;
105 if (log_fp[instance])
108 if (!g_path_is_absolute(filename)) {
109 fullname = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
112 fullname = g_strdup(filename);
114 /* backup old logfile if existing */
115 if (is_file_exist(fullname)) {
118 backupname = g_strconcat(fullname, ".bak", NULL);
119 claws_unlink(backupname);
120 if (g_rename(fullname, backupname) < 0)
121 FILE_OP_ERROR(fullname, "rename");
125 log_fp[instance] = g_fopen(fullname, "wb");
126 if (!log_fp[instance]) {
127 FILE_OP_ERROR(fullname, "fopen");
128 log_filename[instance] = NULL;
132 log_filename[instance] = g_strdup(fullname);
133 log_size[instance] = 0;
137 void close_log_file(LogInstance instance)
139 if (log_fp[instance]) {
140 safe_fclose(log_fp[instance]);
141 log_fp[instance] = NULL;
142 log_size[instance] = 0;
143 g_free(log_filename[instance]);
144 log_filename[instance] = NULL;
148 static void rotate_log(LogInstance instance)
150 if (log_size[instance] > 10 * 1024* 1024) {
151 gchar *filename = g_strdup(log_filename[instance]);
152 debug_print("rotating %s\n", filename);
153 close_log_file(instance);
154 set_log_file(instance, filename);
159 const char *get_log_hook(LogInstance instance)
161 return log_instances[instance].hook;
164 void set_log_title(LogInstance instance, gchar *title)
166 log_instances[instance].title = title;
169 gchar *get_log_title(LogInstance instance)
171 return log_instances[instance].title;
174 void set_log_prefs(LogInstance instance, int* logwin_width, int* logwin_height)
176 log_instances[instance].prefs_logwin_width = logwin_width;
177 log_instances[instance].prefs_logwin_height = logwin_height;
180 void get_log_prefs(LogInstance instance, int** logwin_width, int** logwin_height)
183 *logwin_width = log_instances[instance].prefs_logwin_width;
185 *logwin_height = log_instances[instance].prefs_logwin_height;
188 gboolean get_log_error_capability(LogInstance instance)
190 return log_error_capability[instance];
194 void log_print(LogInstance instance, const gchar *format, ...)
197 gchar buf[BUFFSIZE + LOG_TIME_LEN];
199 LogText *logtext = g_new0(LogText, 1);
203 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
205 va_start(args, format);
206 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
209 if (debug_get_mode()) g_print("%s", buf);
211 logtext->instance = instance;
212 logtext->text = g_strdup(buf);
213 logtext->type = LOG_NORMAL;
215 g_timeout_add(0, invoke_hook_cb, logtext);
217 if (log_fp[instance] && prefs_common_enable_log_standard()) {
218 FPUTS(buf, log_fp[instance])
219 log_size[instance] += strlen(buf);
220 FFLUSH(log_fp[instance])
221 rotate_log(instance);
225 void log_message(LogInstance instance, const gchar *format, ...)
228 gchar buf[BUFFSIZE + LOG_TIME_LEN];
230 LogText *logtext = g_new0(LogText, 1);
234 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
236 va_start(args, format);
237 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
240 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
242 logtext->instance = instance;
243 logtext->text = g_strdup(buf + LOG_TIME_LEN);
244 logtext->type = LOG_MSG;
246 g_timeout_add(0, invoke_hook_cb, logtext);
248 if (log_fp[instance] && prefs_common_enable_log_standard()) {
249 FWRITE(buf, 1, LOG_TIME_LEN, log_fp[instance])
250 FPUTS("* message: ", log_fp[instance])
251 log_size[instance] += strlen("* message: ");
252 FPUTS(buf + LOG_TIME_LEN, log_fp[instance])
253 log_size[instance] += strlen(buf);
254 FFLUSH(log_fp[instance])
255 rotate_log(instance);
259 void log_warning(LogInstance instance, const gchar *format, ...)
262 gchar buf[BUFFSIZE + LOG_TIME_LEN];
264 LogText *logtext = g_new0(LogText, 1);
268 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
270 va_start(args, format);
271 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
274 g_warning("%s", buf);
276 logtext->instance = instance;
277 logtext->text = g_strdup(buf + LOG_TIME_LEN);
278 logtext->type = LOG_WARN;
280 g_timeout_add(0, invoke_hook_cb, logtext);
282 if (log_fp[instance] && prefs_common_enable_log_warning()) {
283 FWRITE(buf, 1, LOG_TIME_LEN, log_fp[instance])
284 FPUTS("** warning: ", log_fp[instance])
285 log_size[instance] += strlen("** warning: ");
286 FPUTS(buf + LOG_TIME_LEN, log_fp[instance])
287 log_size[instance] += strlen(buf);
288 FFLUSH(log_fp[instance])
289 rotate_log(instance);
293 void log_error(LogInstance instance, const gchar *format, ...)
296 gchar buf[BUFFSIZE + LOG_TIME_LEN];
298 LogText *logtext = g_new0(LogText, 1);
302 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
304 va_start(args, format);
305 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
308 g_warning("%s", buf);
310 logtext->instance = instance;
311 logtext->text = g_strdup(buf + LOG_TIME_LEN);
312 logtext->type = LOG_ERROR;
314 g_timeout_add(0, invoke_hook_cb, logtext);
316 if (log_fp[instance] && prefs_common_enable_log_error()) {
317 FWRITE(buf, 1, LOG_TIME_LEN, log_fp[instance])
318 FPUTS("*** error: ", log_fp[instance])
319 log_size[instance] += strlen("*** error: ");
320 FPUTS(buf + LOG_TIME_LEN, log_fp[instance])
321 log_size[instance] += strlen(buf);
322 FFLUSH(log_fp[instance])
323 rotate_log(instance);
327 void log_status_ok(LogInstance instance, const gchar *format, ...)
330 gchar buf[BUFFSIZE + LOG_TIME_LEN];
332 LogText *logtext = g_new0(LogText, 1);
336 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
338 va_start(args, format);
339 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
342 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
344 logtext->instance = instance;
345 logtext->text = g_strdup(buf + LOG_TIME_LEN);
346 logtext->type = LOG_STATUS_OK;
348 g_timeout_add(0, invoke_hook_cb, logtext);
350 if (log_fp[instance] && prefs_common_enable_log_status()) {
351 FWRITE(buf, 1, LOG_TIME_LEN, log_fp[instance])
352 FPUTS("* OK: ", log_fp[instance])
353 log_size[instance] += strlen("* OK: ");
354 FPUTS(buf + LOG_TIME_LEN, log_fp[instance])
355 log_size[instance] += strlen(buf);
356 FFLUSH(log_fp[instance])
357 rotate_log(instance);
361 void log_status_nok(LogInstance instance, const gchar *format, ...)
364 gchar buf[BUFFSIZE + LOG_TIME_LEN];
366 LogText *logtext = g_new0(LogText, 1);
370 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
372 va_start(args, format);
373 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
376 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
378 logtext->instance = instance;
379 logtext->text = g_strdup(buf + LOG_TIME_LEN);
380 logtext->type = LOG_STATUS_NOK;
382 g_timeout_add(0, invoke_hook_cb, logtext);
384 if (log_fp[instance] && prefs_common_enable_log_status()) {
385 FWRITE(buf, 1, LOG_TIME_LEN, log_fp[instance])
386 FPUTS("* NOT OK: ", log_fp[instance])
387 log_size[instance] += strlen("* NOT OK: ");
388 FPUTS(buf + LOG_TIME_LEN, log_fp[instance])
389 log_size[instance] += strlen(buf);
390 FFLUSH(log_fp[instance])
391 rotate_log(instance);
395 void log_status_skip(LogInstance instance, const gchar *format, ...)
398 gchar buf[BUFFSIZE + LOG_TIME_LEN];
400 LogText *logtext = g_new0(LogText, 1);
404 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime_r(&t, &buft));
406 va_start(args, format);
407 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
410 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
412 logtext->instance = instance;
413 logtext->text = g_strdup(buf + LOG_TIME_LEN);
414 logtext->type = LOG_STATUS_SKIP;
416 g_timeout_add(0, invoke_hook_cb, logtext);
418 if (log_fp[instance] && prefs_common_enable_log_status()) {
419 FWRITE(buf, 1, LOG_TIME_LEN, log_fp[instance])
420 FPUTS("* SKIPPED: ", log_fp[instance])
421 log_size[instance] += strlen("* SKIPPED: ");
422 FPUTS(buf + LOG_TIME_LEN, log_fp[instance])
423 log_size[instance] += strlen(buf);
424 FFLUSH(log_fp[instance])
425 rotate_log(instance);