1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24 
25 #include "defs.h"
26 
27 #ifdef G_OS_WIN32
28 #  include <w32lib.h>
29 #endif
30 
31 #include <glib.h>
32 #include <glib/gi18n.h>
33 
34 #include "utils.h"
35 #include "log.h"
36 #include "hooks.h"
37 #include "file-utils.h"
38 
39 #define FWRITE(_b,_s,_n,_f)	if (fwrite(_b,_s,_n,_f) != _n) { \
40 					g_message("log fwrite failed!\n"); \
41 					return; \
42 				}
43 #define FPUTS(_b,_f)		if (fputs(_b,_f) == EOF) { \
44 					g_message("log fputs failed!\n"); \
45 					return; \
46 				}
47 #define FFLUSH(_f)		if (fflush(_f) != 0) { \
48 					g_message("log fflush failed!\n"); \
49 					return; \
50 				}
51 
52 static FILE *log_fp[LOG_INSTANCE_MAX] = {
53 	NULL,
54 	NULL
55 };
56 
57 static size_t log_size[LOG_INSTANCE_MAX] = {
58 	0,
59 	0
60 };
61 
62 static gchar *log_filename[LOG_INSTANCE_MAX] = {
63 	NULL,
64 	NULL
65 };
66 
67 /* read-only */
68 static gboolean log_error_capability[LOG_INSTANCE_MAX] = {
69 	TRUE,
70 	FALSE
71 };
72 
73 typedef struct _LogInstanceData LogInstanceData;
74 
75 struct _LogInstanceData {
76 	const char *hook;
77 	gchar *title;
78 	int *prefs_logwin_width;
79 	int *prefs_logwin_height;
80 };
81 
82 static LogInstanceData log_instances[LOG_INSTANCE_MAX] = {
83 	{ LOG_APPEND_TEXT_HOOKLIST, NULL, NULL, NULL },
84 	{ DEBUG_FILTERING_APPEND_TEXT_HOOKLIST, NULL, NULL, NULL }
85 };
86 
87 gboolean prefs_common_enable_log_standard(void);
88 gboolean prefs_common_enable_log_warning(void);
89 gboolean prefs_common_enable_log_error(void);
90 gboolean prefs_common_enable_log_status(void);
91 
invoke_hook_cb(gpointer data)92 static gboolean invoke_hook_cb (gpointer data)
93 {
94 	LogText *logtext = (LogText *)data;
95 	hooks_invoke(get_log_hook(logtext->instance), logtext);
96 	g_free(logtext->text);
97 	g_free(logtext);
98 	return FALSE;
99 }
100 
set_log_file(LogInstance instance,const gchar * filename)101 void set_log_file(LogInstance instance, const gchar *filename)
102 {
103 	gchar *fullname = NULL;
104 	if (log_fp[instance])
105 		return;
106 
107 	if (!g_path_is_absolute(filename)) {
108 		fullname = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
109 					filename, NULL);
110 	} else {
111 		fullname = g_strdup(filename);
112 	}
113 	/* backup old logfile if existing */
114 	if (is_file_exist(fullname)) {
115 		gchar *backupname;
116 
117 		backupname = g_strconcat(fullname, ".bak", NULL);
118 		claws_unlink(backupname);
119 		if (g_rename(fullname, backupname) < 0)
120 			FILE_OP_ERROR(fullname, "rename");
121 		g_free(backupname);
122 	}
123 
124 	log_fp[instance] = fopen(fullname, "wb");
125 	if (!log_fp[instance]) {
126 		FILE_OP_ERROR(fullname, "fopen");
127 		log_filename[instance] = NULL;
128 		g_free(fullname);
129 		return;
130 	}
131 	log_filename[instance] = g_strdup(fullname);
132 	log_size[instance] = 0;
133 	g_free(fullname);
134 }
135 
close_log_file(LogInstance instance)136 void close_log_file(LogInstance instance)
137 {
138 	if (log_fp[instance]) {
139 		fclose(log_fp[instance]);
140 		log_fp[instance] = NULL;
141 		log_size[instance] = 0;
142 		g_free(log_filename[instance]);
143 		log_filename[instance] = NULL;
144 	}
145 }
146 
rotate_log(LogInstance instance)147 static void rotate_log(LogInstance instance)
148 {
149 	if (log_size[instance] > 10 * 1024* 1024) {
150 		gchar *filename = g_strdup(log_filename[instance]);
151 		debug_print("rotating %s\n", filename);
152 		close_log_file(instance);
153 		set_log_file(instance, filename);
154 		g_free(filename);
155 	}
156 }
157 
get_log_hook(LogInstance instance)158 const char *get_log_hook(LogInstance instance)
159 {
160 	return log_instances[instance].hook;
161 }
162 
set_log_title(LogInstance instance,gchar * title)163 void set_log_title(LogInstance instance, gchar *title)
164 {
165 	log_instances[instance].title = title;
166 }
167 
get_log_title(LogInstance instance)168 gchar *get_log_title(LogInstance instance)
169 {
170 	return log_instances[instance].title;
171 }
172 
set_log_prefs(LogInstance instance,int * logwin_width,int * logwin_height)173 void set_log_prefs(LogInstance instance, int* logwin_width, int* logwin_height)
174 {
175 	log_instances[instance].prefs_logwin_width = logwin_width;
176 	log_instances[instance].prefs_logwin_height = logwin_height;
177 }
178 
get_log_prefs(LogInstance instance,int ** logwin_width,int ** logwin_height)179 void get_log_prefs(LogInstance instance, int** logwin_width, int** logwin_height)
180 {
181 	if (logwin_width)
182 		*logwin_width = log_instances[instance].prefs_logwin_width;
183 	if (logwin_height)
184 		*logwin_height = log_instances[instance].prefs_logwin_height;
185 }
186 
get_log_error_capability(LogInstance instance)187 gboolean get_log_error_capability(LogInstance instance)
188 {
189 	return log_error_capability[instance];
190 
191 }
192 
log_print(LogInstance instance,const gchar * format,...)193 void log_print(LogInstance instance, const gchar *format, ...)
194 {
195 	va_list args;
196 	gchar buf[BUFFSIZE + LOG_TIME_LEN];
197 	time_t t;
198 	LogText *logtext = g_new0(LogText, 1);
199 	struct tm buft;
200 
201 	time(&t);
202 	strftime(buf, LOG_TIME_LEN + 1, LOG_TIME_FORMAT, localtime_r(&t, &buft));
203 
204 	va_start(args, format);
205 	g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
206 	va_end(args);
207 
208 	if (debug_get_mode()) g_print("%s", buf);
209 
210 	logtext->instance = instance;
211 	logtext->text = g_strdup(buf);
212 	logtext->type = LOG_NORMAL;
213 
214 	g_timeout_add(0, invoke_hook_cb, logtext);
215 
216 	if (log_fp[instance] && prefs_common_enable_log_standard()) {
217 		FPUTS(buf, log_fp[instance])
218 		log_size[instance] += strlen(buf);
219 		FFLUSH(log_fp[instance])
220 		rotate_log(instance);
221 	}
222 }
223 
log_message(LogInstance instance,const gchar * format,...)224 void log_message(LogInstance instance, const gchar *format, ...)
225 {
226 	va_list args;
227 	gchar buf[BUFFSIZE + LOG_TIME_LEN];
228 	time_t t;
229 	LogText *logtext = g_new0(LogText, 1);
230 	struct tm buft;
231 
232 	time(&t);
233 	strftime(buf, LOG_TIME_LEN + 1, LOG_TIME_FORMAT, localtime_r(&t, &buft));
234 
235 	va_start(args, format);
236 	g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
237 	va_end(args);
238 
239 	if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
240 
241 	logtext->instance = instance;
242 	logtext->text = g_strdup(buf + LOG_TIME_LEN);
243 	logtext->type = LOG_MSG;
244 
245 	g_timeout_add(0, invoke_hook_cb, logtext);
246 
247 	if (log_fp[instance] && prefs_common_enable_log_standard()) {
248 		FWRITE(buf, 1, LOG_TIME_LEN, log_fp[instance])
249 		FPUTS("* message: ", log_fp[instance])
250 		log_size[instance] += strlen("* message: ");
251 		FPUTS(buf + LOG_TIME_LEN, log_fp[instance])
252 		log_size[instance] += strlen(buf);
253 		FFLUSH(log_fp[instance])
254 		rotate_log(instance);
255 	}
256 }
257 
log_warning(LogInstance instance,const gchar * format,...)258 void log_warning(LogInstance instance, const gchar *format, ...)
259 {
260 	va_list args;
261 	gchar buf[BUFFSIZE + LOG_TIME_LEN];
262 	time_t t;
263 	LogText *logtext = g_new0(LogText, 1);
264 	struct tm buft;
265 
266 	time(&t);
267 	strftime(buf, LOG_TIME_LEN + 1, LOG_TIME_FORMAT, localtime_r(&t, &buft));
268 
269 	va_start(args, format);
270 	g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
271 	va_end(args);
272 
273 	g_warning("%s", buf);
274 
275 	logtext->instance = instance;
276 	logtext->text = g_strdup(buf + LOG_TIME_LEN);
277 	logtext->type = LOG_WARN;
278 
279 	g_timeout_add(0, invoke_hook_cb, logtext);
280 
281 	if (log_fp[instance] && prefs_common_enable_log_warning()) {
282 		FWRITE(buf, 1, LOG_TIME_LEN, log_fp[instance])
283 		FPUTS("** warning: ", log_fp[instance])
284 		log_size[instance] += strlen("** warning: ");
285 		FPUTS(buf + LOG_TIME_LEN, log_fp[instance])
286 		log_size[instance] += strlen(buf);
287 		FFLUSH(log_fp[instance])
288 		rotate_log(instance);
289 	}
290 }
291 
log_error(LogInstance instance,const gchar * format,...)292 void log_error(LogInstance instance, const gchar *format, ...)
293 {
294 	va_list args;
295 	gchar buf[BUFFSIZE + LOG_TIME_LEN];
296 	time_t t;
297 	LogText *logtext = g_new0(LogText, 1);
298 	struct tm buft;
299 
300 	time(&t);
301 	strftime(buf, LOG_TIME_LEN + 1, LOG_TIME_FORMAT, localtime_r(&t, &buft));
302 
303 	va_start(args, format);
304 	g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
305 	va_end(args);
306 
307 	g_warning("%s", buf);
308 
309 	logtext->instance = instance;
310 	logtext->text = g_strdup(buf + LOG_TIME_LEN);
311 	logtext->type = LOG_ERROR;
312 
313 	g_timeout_add(0, invoke_hook_cb, logtext);
314 
315 	if (log_fp[instance] && prefs_common_enable_log_error()) {
316 		FWRITE(buf, 1, LOG_TIME_LEN, log_fp[instance])
317 		FPUTS("*** error: ", log_fp[instance])
318 		log_size[instance] += strlen("*** error: ");
319 		FPUTS(buf + LOG_TIME_LEN, log_fp[instance])
320 		log_size[instance] += strlen(buf);
321 		FFLUSH(log_fp[instance])
322 		rotate_log(instance);
323 	}
324 }
325 
log_status_ok(LogInstance instance,const gchar * format,...)326 void log_status_ok(LogInstance instance, const gchar *format, ...)
327 {
328 	va_list args;
329 	gchar buf[BUFFSIZE + LOG_TIME_LEN];
330 	time_t t;
331 	LogText *logtext = g_new0(LogText, 1);
332 	struct tm buft;
333 
334 	time(&t);
335 	strftime(buf, LOG_TIME_LEN + 1, LOG_TIME_FORMAT, localtime_r(&t, &buft));
336 
337 	va_start(args, format);
338 	g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
339 	va_end(args);
340 
341 	if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
342 
343 	logtext->instance = instance;
344 	logtext->text = g_strdup(buf + LOG_TIME_LEN);
345 	logtext->type = LOG_STATUS_OK;
346 
347 	g_timeout_add(0, invoke_hook_cb, logtext);
348 
349 	if (log_fp[instance] && prefs_common_enable_log_status()) {
350 		FWRITE(buf, 1, LOG_TIME_LEN, log_fp[instance])
351 		FPUTS("* OK: ", log_fp[instance])
352 		log_size[instance] += strlen("* OK: ");
353 		FPUTS(buf + LOG_TIME_LEN, log_fp[instance])
354 		log_size[instance] += strlen(buf);
355 		FFLUSH(log_fp[instance])
356 		rotate_log(instance);
357 	}
358 }
359 
log_status_nok(LogInstance instance,const gchar * format,...)360 void log_status_nok(LogInstance instance, const gchar *format, ...)
361 {
362 	va_list args;
363 	gchar buf[BUFFSIZE + LOG_TIME_LEN];
364 	time_t t;
365 	LogText *logtext = g_new0(LogText, 1);
366 	struct tm buft;
367 
368 	time(&t);
369 	strftime(buf, LOG_TIME_LEN + 1, LOG_TIME_FORMAT, localtime_r(&t, &buft));
370 
371 	va_start(args, format);
372 	g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
373 	va_end(args);
374 
375 	if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
376 
377 	logtext->instance = instance;
378 	logtext->text = g_strdup(buf + LOG_TIME_LEN);
379 	logtext->type = LOG_STATUS_NOK;
380 
381 	g_timeout_add(0, invoke_hook_cb, logtext);
382 
383 	if (log_fp[instance] && prefs_common_enable_log_status()) {
384 		FWRITE(buf, 1, LOG_TIME_LEN, log_fp[instance])
385 		FPUTS("* NOT OK: ", log_fp[instance])
386 		log_size[instance] += strlen("* NOT OK: ");
387 		FPUTS(buf + LOG_TIME_LEN, log_fp[instance])
388 		log_size[instance] += strlen(buf);
389 		FFLUSH(log_fp[instance])
390 		rotate_log(instance);
391 	}
392 }
393 
log_status_skip(LogInstance instance,const gchar * format,...)394 void log_status_skip(LogInstance instance, const gchar *format, ...)
395 {
396 	va_list args;
397 	gchar buf[BUFFSIZE + LOG_TIME_LEN];
398 	time_t t;
399 	LogText *logtext = g_new0(LogText, 1);
400 	struct tm buft;
401 
402 	time(&t);
403 	strftime(buf, LOG_TIME_LEN + 1, LOG_TIME_FORMAT, localtime_r(&t, &buft));
404 
405 	va_start(args, format);
406 	g_vsnprintf(buf + LOG_TIME_LEN, BUFFSIZE, format, args);
407 	va_end(args);
408 
409 	if (debug_get_mode()) g_message("%s", buf + LOG_TIME_LEN);
410 
411 	logtext->instance = instance;
412 	logtext->text = g_strdup(buf + LOG_TIME_LEN);
413 	logtext->type = LOG_STATUS_SKIP;
414 
415 	g_timeout_add(0, invoke_hook_cb, logtext);
416 
417 	if (log_fp[instance] && prefs_common_enable_log_status()) {
418 		FWRITE(buf, 1, LOG_TIME_LEN, log_fp[instance])
419 		FPUTS("* SKIPPED: ", log_fp[instance])
420 		log_size[instance] += strlen("* SKIPPED: ");
421 		FPUTS(buf + LOG_TIME_LEN, log_fp[instance])
422 		log_size[instance] += strlen(buf);
423 		FFLUSH(log_fp[instance])
424 		rotate_log(instance);
425 	}
426 }
427 
428 #undef FWRITE
429 #undef FPUTS
430 #undef FFLUSH
431 
432