Revision 2259 src/logwindow.c

logwindow.c (revision 2259)
1 1
/*
2 2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2005 Hiroyuki Yamamoto
3
 * Copyright (C) 1999-2009 Hiroyuki Yamamoto
4 4
 *
5 5
 * This program is free software; you can redistribute it and/or modify
6 6
 * it under the terms of the GNU General Public License as published by
......
41 41

  
42 42
static LogWindow *logwindow;
43 43

  
44
#if USE_THREADS
45
static GThread *main_thread;
46
#endif
47

  
44 48
static void log_window_print_func	(const gchar	*str);
45 49
static void log_window_message_func	(const gchar	*str);
46 50
static void log_window_warning_func	(const gchar	*str);
......
99 103
	logwin->text = text;
100 104
	logwin->lines = 1;
101 105

  
106
#if USE_THREADS
107
	logwin->aqueue = g_async_queue_new();
108

  
109
	main_thread = g_thread_self();
110
	debug_print("main_thread = %p\n", main_thread);
111
#endif
112

  
102 113
	logwindow = logwin;
103 114

  
104 115
	return logwin;
......
160 171
	gtk_window_present(GTK_WINDOW(logwin->window));
161 172
}
162 173

  
163
void log_window_append(const gchar *str, LogType type)
174
static void log_window_append_real(const gchar *str, LogType type)
164 175
{
165 176
	GtkTextView *text;
166 177
	GtkTextBuffer *buffer;
......
172 183

  
173 184
	g_return_if_fail(logwindow != NULL);
174 185

  
186
#if USE_THREADS
187
	if (g_thread_self() != main_thread) {
188
		return;
189
	}
190
#endif
191

  
192
	gdk_threads_enter();
193

  
175 194
	text = GTK_TEXT_VIEW(logwindow->text);
176 195
	buffer = gtk_text_view_get_buffer(text);
177 196

  
......
233 252
	}
234 253

  
235 254
	logwindow->lines++;
255

  
256
	gdk_threads_leave();
236 257
}
237 258

  
259
void log_window_append(const gchar *str, LogType type)
260
{
261
#if USE_THREADS
262
	if (g_thread_self() != main_thread) {
263
		fprintf(stderr, "log_window_append called from non-main thread (%p)\n", g_thread_self());
264
		log_window_append_queue(str, type);
265
		return;
266
	}
267

  
268
	log_window_flush();
269
#endif
270
	log_window_append_real(str, type);
271
}
272

  
273
typedef struct _LogData
274
{
275
	gchar *str;
276
	LogType type;
277
} LogData;
278

  
279
void log_window_append_queue(const gchar *str, LogType type)
280
{
281
#if USE_THREADS
282
	LogData *logdata;
283

  
284
	logdata = g_new(LogData, 1);
285
	logdata->str = g_strdup(str);
286
	logdata->type = type;
287

  
288
	g_print("append_queue: (%d) %s\n", type, str);
289
	g_async_queue_push(logwindow->aqueue, logdata);
290
#endif
291
}
292

  
293
void log_window_flush(void)
294
{
295
#if USE_THREADS
296
	LogData *logdata;
297

  
298
	if (g_thread_self() != main_thread) {
299
		fprintf(stderr, "log_window_flush called from non-main thread (%p)\n", g_thread_self());
300
		return;
301
	}
302

  
303
	while ((logdata = g_async_queue_try_pop(logwindow->aqueue))) {
304
		g_print("flush_queue: (%d) %s\n", logdata->type, logdata->str);
305
		log_window_append_real(logdata->str, logdata->type);
306
		g_free(logdata->str);
307
		g_free(logdata);
308
	}
309
#endif
310
}
311

  
238 312
static void log_window_print_func(const gchar *str)
239 313
{
240 314
	log_window_append(str, LOG_NORMAL);

Also available in: Unified diff