Alexandre Lision | 0e14301 | 2014-01-22 11:02:46 -0500 | [diff] [blame] | 1 | # $Id: log.py 4704 2014-01-16 05:30:46Z ming $ |
| 2 | # |
| 3 | # pjsua Python GUI Demo |
| 4 | # |
| 5 | # Copyright (C)2013 Teluu Inc. (http://www.teluu.com) |
| 6 | # |
| 7 | # This program is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; either version 2 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program; if not, write to the Free Software |
| 19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | # |
| 21 | import sys |
| 22 | if sys.version_info[0] >= 3: # Python 3 |
| 23 | import tkinter as tk |
| 24 | from tkinter import ttk |
| 25 | from tkinter import messagebox as msgbox |
| 26 | else: |
| 27 | import Tkinter as tk |
| 28 | import tkMessageBox as msgbox |
| 29 | import ttk |
| 30 | |
| 31 | import pjsua2 as pj |
| 32 | import application |
| 33 | |
| 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 | print entry.msg, |
| 124 | writeLog(entry) |
| 125 | |
| 126 | if __name__ == '__main__': |
| 127 | application.main() |