2006-08-22 [colin] 2.4.0cvs73
[claws.git] / src / common / log.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <stdio.h>
27 #include <glib.h>
28
29 #include "utils.h"
30 #include "log.h"
31 #include "hooks.h"
32
33 static FILE *log_fp = NULL;
34
35 gboolean invoke_hook_cb (gpointer data)
36 {
37         LogText *logtext = (LogText *)data;
38         hooks_invoke(LOG_APPEND_TEXT_HOOKLIST, logtext);
39         g_free(logtext->text);
40         g_free(logtext);
41         return FALSE;
42 }
43
44 void set_log_file(const gchar *filename)
45 {
46         if (log_fp)
47                 return;
48
49         /* backup old logfile if existing */
50         if (is_file_exist(filename)) {
51                 gchar *backupname;
52                 
53                 backupname = g_strconcat(filename, ".bak", NULL);
54                 if (rename(filename, backupname) < 0)
55                         FILE_OP_ERROR(filename, "rename");
56                 g_free(backupname);
57         }
58
59         log_fp = g_fopen(filename, "wb");
60         if (!log_fp)
61                 FILE_OP_ERROR(filename, "fopen");
62 }
63
64 void close_log_file(void)
65 {
66         if (log_fp) {
67                 fclose(log_fp);
68                 log_fp = NULL;
69         }
70 }
71
72 void log_print(const gchar *format, ...)
73 {
74         va_list args;
75         gchar buf[BUFFSIZE + LOG_TIME_LEN];
76         time_t t;
77         LogText *logtext = g_new0(LogText, 1);
78         
79         time(&t);
80         strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
81
82         va_start(args, format);
83         g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
84         va_end(args);
85
86         if (debug_get_mode()) fputs(buf, stdout);
87
88         logtext->text = g_strdup(buf);
89         logtext->type = LOG_NORMAL;
90         
91         g_timeout_add(0, invoke_hook_cb, logtext);
92         
93         if (log_fp) {
94                 fputs(buf, log_fp);
95                 fflush(log_fp);
96         }
97 }
98
99 void log_message(const gchar *format, ...)
100 {
101         va_list args;
102         gchar buf[BUFFSIZE + LOG_TIME_LEN];
103         time_t t;
104         LogText *logtext = g_new0(LogText, 1);
105
106         time(&t);
107         strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
108
109         va_start(args, format);
110         g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
111         va_end(args);
112
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;
116         
117         g_timeout_add(0, invoke_hook_cb, logtext);
118
119         if (log_fp) {
120                 fwrite(buf, 1, LOG_TIME_LEN, log_fp);
121                 fputs("* message: ", log_fp);
122                 fputs(buf + LOG_TIME_LEN, log_fp);
123                 fflush(log_fp);
124         }
125 }
126
127 void log_warning(const gchar *format, ...)
128 {
129         va_list args;
130         gchar buf[BUFFSIZE + LOG_TIME_LEN];
131         time_t t;
132         LogText *logtext = g_new0(LogText, 1);
133
134         time(&t);
135         strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
136
137         va_start(args, format);
138         g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
139         va_end(args);
140
141         g_warning("%s", buf);
142         logtext->text = g_strdup(buf + LOG_TIME_LEN);
143         logtext->type = LOG_WARN;
144         
145         g_timeout_add(0, invoke_hook_cb, logtext);
146
147         if (log_fp) {
148                 fwrite(buf, 1, LOG_TIME_LEN, log_fp);
149                 fputs("** warning: ", log_fp);
150                 fputs(buf + LOG_TIME_LEN, log_fp);
151                 fflush(log_fp);
152         }
153 }
154
155 void log_error(const gchar *format, ...)
156 {
157         va_list args;
158         gchar buf[BUFFSIZE + LOG_TIME_LEN];
159         time_t t;
160         LogText *logtext = g_new0(LogText, 1);
161
162         time(&t);
163         strftime(buf, LOG_TIME_LEN + 1, "[%H:%M:%S] ", localtime(&t));
164
165         va_start(args, format);
166         g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
167         va_end(args);
168
169         g_warning("%s", buf);
170         logtext->text = g_strdup(buf + LOG_TIME_LEN);
171         logtext->type = LOG_ERROR;
172         
173         g_timeout_add(0, invoke_hook_cb, logtext);
174
175         if (log_fp) {
176                 fwrite(buf, 1, LOG_TIME_LEN, log_fp);
177                 fputs("*** error: ", log_fp);
178                 fputs(buf + LOG_TIME_LEN, log_fp);
179                 fflush(log_fp);
180         }
181 }