]> sjero.net Git - linphone/blob - gtk-glade/logging.c
purge out p2pproxy
[linphone] / gtk-glade / logging.c
1 /*
2 linphone, gtk-glade interface.
3 Copyright (C) 2008  Simon MORLAT (simon.morlat@linphone.org)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20 #include "linphone.h"
21
22 static GtkWidget *log_window=NULL;
23 static GStaticMutex log_mutex=G_STATIC_MUTEX_INIT;
24 static GList *log_queue=NULL;
25
26 typedef struct _LinphoneGtkLog{
27         OrtpLogLevel lev;
28         gchar *msg;
29 }LinphoneGtkLog;
30
31 void linphone_gtk_create_log_window(void){
32         GtkTextBuffer *b;
33         log_window=linphone_gtk_create_window("log");
34         b=gtk_text_view_get_buffer(GTK_TEXT_VIEW(linphone_gtk_get_widget(log_window,"textview")));
35         gtk_text_buffer_create_tag(b,"red","foreground","red",NULL);
36         gtk_text_buffer_create_tag(b,"orange","foreground","orange",NULL);
37 }
38
39 void linphone_gtk_destroy_log_window(void){
40         GtkWidget *w=log_window;
41         g_static_mutex_lock(&log_mutex);
42         log_window=NULL;
43         gtk_widget_destroy(w);
44         g_static_mutex_unlock(&log_mutex);
45 }
46
47 void linphone_gtk_log_show(void){
48         gtk_widget_show(log_window);
49         gtk_window_present(GTK_WINDOW(log_window));
50 }
51
52 static void linphone_gtk_display_log(OrtpLogLevel lev, const char *msg){
53         GtkTextIter iter,begin;
54         int off;
55         static GtkTextView *v=NULL;
56         GtkTextBuffer *b;
57         const char *lname="undef";
58
59         if (log_window==NULL) {
60                 return;
61         }
62
63         if (v==NULL) v=GTK_TEXT_VIEW(linphone_gtk_get_widget(log_window,"textview"));
64         b=gtk_text_view_get_buffer(v);
65         switch(lev){
66                 case ORTP_DEBUG:
67                         lname="debug";
68                         break;
69                 case ORTP_MESSAGE:
70                         lname="message";
71                         break;
72                 case ORTP_WARNING:
73                         lname="warning";
74                         break;
75                 case ORTP_ERROR:
76                         lname="error";
77                         break;
78                 case ORTP_FATAL:
79                         lname="fatal";
80                         break;
81                 default:
82                         g_error("Bad level !");
83         }
84         gtk_text_buffer_get_end_iter(b,&iter);
85         off=gtk_text_iter_get_offset(&iter);
86         gtk_text_buffer_insert(b,&iter,lname,-1);
87         gtk_text_buffer_get_end_iter(b,&iter);
88         gtk_text_buffer_insert(b,&iter,": ",-1);
89         gtk_text_buffer_get_end_iter(b,&iter);
90         gtk_text_buffer_insert(b,&iter,msg,-1);
91         gtk_text_buffer_get_end_iter(b,&iter);
92         gtk_text_buffer_insert(b,&iter,"\n",-1);
93         gtk_text_buffer_get_end_iter(b,&iter);
94         gtk_text_buffer_get_iter_at_offset(b,&begin,off);
95         if (lev==ORTP_ERROR || lev==ORTP_FATAL) gtk_text_buffer_apply_tag_by_name(b,"red",&begin,&iter);
96         else if (lev==ORTP_WARNING) gtk_text_buffer_apply_tag_by_name(b,"orange",&begin,&iter);
97         gtk_text_buffer_get_end_iter(b,&iter);
98         //gtk_text_view_scroll_to_iter(v,&iter,0,FALSE,0,0);
99 }
100
101 gboolean linphone_gtk_check_logs(){
102         GList *elem;
103         g_static_mutex_lock(&log_mutex);
104         for(elem=log_queue;elem!=NULL;elem=elem->next){
105                 LinphoneGtkLog *lgl=(LinphoneGtkLog*)elem->data;
106                 linphone_gtk_display_log(lgl->lev,lgl->msg);
107                 g_free(lgl->msg);
108                 g_free(lgl);
109         }
110         if (log_queue) g_list_free(log_queue);
111         log_queue=NULL;
112         g_static_mutex_unlock(&log_mutex);
113         return TRUE;
114 }
115
116 void linphone_gtk_log_push(OrtpLogLevel lev, const char *fmt, va_list args){
117         gchar *msg=g_strdup_vprintf(fmt,args);
118         LinphoneGtkLog *lgl=g_new(LinphoneGtkLog,1);
119         lgl->lev=lev;
120         lgl->msg=msg;
121         g_static_mutex_lock(&log_mutex);
122         log_queue=g_list_append(log_queue,lgl);
123         g_static_mutex_unlock(&log_mutex);
124 }
125