2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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.
33 static FILE *log_fp = NULL;
35 gboolean invoke_hook_cb (gpointer data)
37 LogText *logtext = (LogText *)data;
38 hooks_invoke(LOG_APPEND_TEXT_HOOKLIST, logtext);
39 g_free(logtext->text);
44 void set_log_file(const gchar *filename)
49 /* backup old logfile if existing */
50 if (is_file_exist(filename)) {
53 backupname = g_strconcat(filename, ".bak", NULL);
54 if (rename(filename, backupname) < 0)
55 FILE_OP_ERROR(filename, "rename");
59 log_fp = g_fopen(filename, "wb");
61 FILE_OP_ERROR(filename, "fopen");
64 void close_log_file(void)
72 void log_print(const gchar *format, ...)
75 gchar buf[BUFFSIZE + LOG_TIME_LEN];
77 LogText *logtext = g_new0(LogText, 1);
80 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
82 va_start(args, format);
83 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
86 if (debug_get_mode()) fputs(buf, stdout);
88 logtext->text = g_strdup(buf);
89 logtext->type = LOG_NORMAL;
91 g_timeout_add(0, invoke_hook_cb, logtext);
99 void log_message(const gchar *format, ...)
102 gchar buf[BUFFSIZE + LOG_TIME_LEN];
104 LogText *logtext = g_new0(LogText, 1);
107 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
109 va_start(args, format);
110 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
113 if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
114 logtext->text = g_strdup(buf + LOG_TIME_LEN);
115 logtext->type = LOG_MSG;
117 g_timeout_add(0, invoke_hook_cb, logtext);
120 fwrite(buf, 1, LOG_TIME_LEN, log_fp);
121 fputs("* message: ", log_fp);
122 fputs(buf + LOG_TIME_LEN, log_fp);
127 void log_warning(const gchar *format, ...)
130 gchar buf[BUFFSIZE + LOG_TIME_LEN];
132 LogText *logtext = g_new0(LogText, 1);
135 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
137 va_start(args, format);
138 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
141 g_warning("%s", buf);
142 logtext->text = g_strdup(buf + LOG_TIME_LEN);
143 logtext->type = LOG_WARN;
145 g_timeout_add(0, invoke_hook_cb, logtext);
148 fwrite(buf, 1, LOG_TIME_LEN, log_fp);
149 fputs("** warning: ", log_fp);
150 fputs(buf + LOG_TIME_LEN, log_fp);
155 void log_error(const gchar *format, ...)
158 gchar buf[BUFFSIZE + LOG_TIME_LEN];
160 LogText *logtext = g_new0(LogText, 1);
163 strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
165 va_start(args, format);
166 g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
169 g_warning("%s", buf);
170 logtext->text = g_strdup(buf + LOG_TIME_LEN);
171 logtext->type = LOG_ERROR;
173 g_timeout_add(0, invoke_hook_cb, logtext);
176 fwrite(buf, 1, LOG_TIME_LEN, log_fp);
177 fputs("*** error: ", log_fp);
178 fputs(buf + LOG_TIME_LEN, log_fp);