Bluez support cleanup.
[wammu.git] / Wammu / Logger.py
blob13b184eb643485f3b49ea0ce5f6cf3b845cd4a5e
1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
3 '''
4 Wammu - Phone manager
5 Logging window and thread for log reading
6 '''
7 __author__ = 'Michal Čihař'
8 __email__ = 'michal@cihar.com'
9 __license__ = '''
10 Copyright (c) 2003 - 2007 Michal Čihař
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License version 2 as published by
14 the Free Software Foundation.
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 more details.
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 '''
26 import threading
27 import wx
28 import os
29 import sys
30 import time
31 import Wammu.Events
33 class Logger(threading.Thread):
34 '''
35 Thread which reads defined files and posts events on change.
36 '''
37 def __init__(self, win, filename):
38 '''
39 Initializes reader on filename, events will be sent to win.
40 '''
41 threading.Thread.__init__(self)
42 self.win = win
43 self.file_descriptor = open(filename, 'r')
44 self.filename = filename
45 self.canceled = False
47 def run(self):
48 """
49 This is basically tail -f reimplementation
50 """
51 while not self.canceled:
52 where = self.file_descriptor.tell()
53 txt = self.file_descriptor.readlines()
54 if len(txt) == 0:
55 fd_results = os.fstat(self.file_descriptor.fileno())
56 try:
57 st_results = os.stat(self.filename)
58 except OSError:
59 st_results = fd_results
61 if st_results[1] == fd_results[1] or sys.platform == 'win32':
62 time.sleep(1)
63 self.file_descriptor.seek(where)
64 else:
65 self.file_descriptor = open(self.filename, 'r')
66 else:
67 evt = Wammu.Events.LogEvent(txt = ''.join(txt))
68 wx.PostEvent(self.win, evt)
69 self.file_descriptor.close()
71 class LogFrame(wx.Frame):
72 '''
73 Window with debug log.
74 '''
76 def __init__(self, parent, cfg):
77 '''
78 Creates window and initializes event handlers.
79 '''
80 self.cfg = cfg
81 if cfg.HasEntry('/Debug/X') and cfg.HasEntry('/Debug/Y'):
82 pos = wx.Point(
83 cfg.ReadInt('/Debug/X'),
84 cfg.ReadInt('/Debug/Y'))
85 else:
86 pos = wx.DefaultPosition
87 size = wx.Size(
88 cfg.ReadInt('/Debug/Width'),
89 cfg.ReadInt('/Debug/Height'))
90 wx.Frame.__init__(
91 self,
92 parent,
93 -1,
94 _('Wammu debug log'),
95 pos,
96 size,
97 wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER)
98 self.txt = wx.TextCtrl(
99 self,
100 -1,
101 _('Here will appear debug messages from Gammu...\n'),
102 style = wx.TE_MULTILINE | wx.TE_READONLY)
103 self.txt.SetFont(wx.Font(9, wx.MODERN, wx.NORMAL, wx.NORMAL))
104 Wammu.Events.EVT_LOG(self, self.OnLog)
105 wx.EVT_SIZE(self, self.OnSize)
106 self.OnSize(None)
108 def OnLog(self, evt):
110 Event handler for text events from Logger.
112 self.txt.AppendText(evt.txt)
114 def OnSize(self, evt):
116 Resize handler to correctly resize text area.
118 width, height = self.GetClientSizeTuple()
119 self.txt.SetDimensions(0, 0, width, height)