1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
5 Logging window and thread for log reading
7 __author__
= 'Michal Čihař'
8 __email__
= 'michal@cihar.com'
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
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
33 class Logger(threading
.Thread
):
35 Thread which reads defined files and posts events on change.
37 def __init__(self
, win
, filename
):
39 Initializes reader on filename, events will be sent to win.
41 threading
.Thread
.__init
__(self
)
43 self
.file_descriptor
= open(filename
, 'r')
44 self
.filename
= filename
49 This is basically tail -f reimplementation
51 while not self
.canceled
:
52 where
= self
.file_descriptor
.tell()
53 txt
= self
.file_descriptor
.readlines()
55 fd_results
= os
.fstat(self
.file_descriptor
.fileno())
57 st_results
= os
.stat(self
.filename
)
59 st_results
= fd_results
61 if st_results
[1] == fd_results
[1] or sys
.platform
== 'win32':
63 self
.file_descriptor
.seek(where
)
65 self
.file_descriptor
= open(self
.filename
, 'r')
67 evt
= Wammu
.Events
.LogEvent(txt
= ''.join(txt
))
68 wx
.PostEvent(self
.win
, evt
)
69 self
.file_descriptor
.close()
71 class LogFrame(wx
.Frame
):
73 Window with debug log.
76 def __init__(self
, parent
, cfg
):
78 Creates window and initializes event handlers.
81 if cfg
.HasEntry('/Debug/X') and cfg
.HasEntry('/Debug/Y'):
83 cfg
.ReadInt('/Debug/X'),
84 cfg
.ReadInt('/Debug/Y'))
86 pos
= wx
.DefaultPosition
88 cfg
.ReadInt('/Debug/Width'),
89 cfg
.ReadInt('/Debug/Height'))
97 wx
.DEFAULT_FRAME_STYLE | wx
.RESIZE_BORDER
)
98 self
.txt
= wx
.TextCtrl(
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
)
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
)