Debian Codegraph @

Xpjproject/pjsip-apps/src/pygui/log.py

raw symbols SCIP
1 #
2 # pjsua Python GUI Demo
3 #
4 # Copyright (C)2013 Teluu Inc. (http://www.teluu.com)
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #
20 import sys
21 if sys.version_info[0] >= 3: # Python 3
22 import tkinter as tk
23 from tkinter import ttk
24 from tkinter import messagebox as msgbox
25 else:
26 import Tkinter as tk
27 import tkMessageBox as msgbox
28 import ttk
29
30 import pjsua2 as pj
31 import application
32
33 write=sys.stdout.write
34
35 class LogWindow(tk.Toplevel):
36 """
37 Log window
38 """
39 instance = None
40 def __init__(self, app):
41 tk.Toplevel.__init__(self, name='logwnd', width=640, height=480)
42 LogWindow.instance = self
43 self.app = app
44 self.state('withdrawn')
45 self.title('Log')
46 self._createWidgets()
47 self.protocol("WM_DELETE_WINDOW", self._onHide)
48
49 def addLog(self, entry):
50 """entry fields:
51 int level;
52 string msg;
53 long threadId;
54 string threadName;
55 """
56 self.addLog2(entry.level, entry.msg)
57
58 def addLog2(self, level, msg):
59 if level==5:
60 tags = ('trace',)
61 elif level==3:
62 tags = ('info',)
63 elif level==2:
64 tags = ('warning',)
65 elif level<=1:
66 tags = ('error',)
67 else:
68 tags = None
69 self.text.insert(tk.END, msg, tags)
70 self.text.see(tk.END)
71
72 def _createWidgets(self):
73 self.rowconfigure(0, weight=1)
74 self.rowconfigure(1, weight=0)
75 self.columnconfigure(0, weight=1)
76 self.columnconfigure(1, weight=0)
77
78 self.text = tk.Text(self, font=('Courier New', '8'), wrap=tk.NONE, undo=False, padx=4, pady=5)
79 self.text.grid(row=0, column=0, sticky='nswe', padx=5, pady=5)
80
81 scrl = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self.text.yview)
82 self.text.config(yscrollcommand=scrl.set)
83 scrl.grid(row=0, column=1, sticky='nsw', padx=5, pady=5)
84
85 scrl = ttk.Scrollbar(self, orient=tk.HORIZONTAL, command=self.text.xview)
86 self.text.config(xscrollcommand=scrl.set)
87 scrl.grid(row=1, column=0, sticky='we', padx=5, pady=5)
88
89 self.text.bind("<Key>", self._onKey)
90
91 self.text.tag_configure('normal', font=('Courier New', '8'), foreground='black')
92 self.text.tag_configure('trace', font=('Courier New', '8'), foreground='#777777')
93 self.text.tag_configure('info', font=('Courier New', '8', 'bold'), foreground='black')
94 self.text.tag_configure('warning', font=('Courier New', '8', 'bold'), foreground='cyan')
95 self.text.tag_configure('error', font=('Courier New', '8', 'bold'), foreground='red')
96
97 def _onKey(self, event):
98 # Ignore key event to make text widget read-only
99 return "break"
100
101 def _onHide(self):
102 # Hide when close ('x') button is clicked
103 self.withdraw()
104 self.app.showLogWindow.set(0)
105
106
107 def writeLog2(level, msg):
108 if LogWindow.instance:
109 LogWindow.instance.addLog2(level, msg)
110
111 def writeLog(entry):
112 if LogWindow.instance:
113 LogWindow.instance.addLog(entry)
114
115 class Logger(pj.LogWriter):
116 """
117 Logger to receive log messages from pjsua2
118 """
119 def __init__(self):
120 pj.LogWriter.__init__(self)
121
122 def write(self, entry):
123 write(entry.msg + "\r\n")
124 writeLog(entry)
125
126 if __name__ == '__main__':
127 application.main()