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 © 2003 - 2010 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 LoggerDebug(threading
.Thread
):
35 Thread which reads defined files and prints it to stderr.
37 def __init__(self
, filename
):
39 Initializes reader on filename, text will be printed to stderr.
41 threading
.Thread
.__init
__(self
)
42 self
.file_descriptor
= open(filename
, 'r')
43 self
.filename
= filename
48 This is basically tail -f reimplementation
50 while not self
.canceled
:
51 where
= self
.file_descriptor
.tell()
52 txt
= self
.file_descriptor
.readlines()
54 fd_results
= os
.fstat(self
.file_descriptor
.fileno())
56 st_results
= os
.stat(self
.filename
)
58 st_results
= fd_results
60 if st_results
[1] == fd_results
[1] or sys
.platform
== 'win32':
62 self
.file_descriptor
.seek(where
)
64 self
.file_descriptor
= open(self
.filename
, 'r')
66 sys
.stderr
.write(''.join(txt
))
67 self
.file_descriptor
.close()
69 class Logger(threading
.Thread
):
71 Thread which reads defined files and posts events on change.
73 def __init__(self
, win
, filename
):
75 Initializes reader on filename, events will be sent to win.
77 threading
.Thread
.__init
__(self
)
79 self
.file_descriptor
= open(filename
, 'r')
80 self
.filename
= filename
85 This is basically tail -f reimplementation
87 while not self
.canceled
:
88 where
= self
.file_descriptor
.tell()
89 txt
= self
.file_descriptor
.readlines()
91 fd_results
= os
.fstat(self
.file_descriptor
.fileno())
93 st_results
= os
.stat(self
.filename
)
95 st_results
= fd_results
97 if st_results
[1] == fd_results
[1] or sys
.platform
== 'win32':
99 self
.file_descriptor
.seek(where
)
101 self
.file_descriptor
= open(self
.filename
, 'r')
103 evt
= Wammu
.Events
.LogEvent(txt
= ''.join(txt
))
104 wx
.PostEvent(self
.win
, evt
)
105 self
.file_descriptor
.close()
107 class LogFrame(wx
.Frame
):
109 Window with debug log.
112 def __init__(self
, parent
, cfg
):
114 Creates window and initializes event handlers.
117 if cfg
.HasEntry('/Debug/X') and cfg
.HasEntry('/Debug/Y'):
119 cfg
.ReadInt('/Debug/X'),
120 cfg
.ReadInt('/Debug/Y'))
122 pos
= wx
.DefaultPosition
124 cfg
.ReadInt('/Debug/Width'),
125 cfg
.ReadInt('/Debug/Height'))
130 _('Wammu debug log'),
133 wx
.DEFAULT_FRAME_STYLE | wx
.RESIZE_BORDER
)
134 self
.txt
= wx
.TextCtrl(
137 _('Here will appear debug messages from Gammu...\n'),
138 style
= wx
.TE_MULTILINE | wx
.TE_READONLY
)
139 self
.txt
.SetFont(wx
.Font(9, wx
.MODERN
, wx
.NORMAL
, wx
.NORMAL
))
140 Wammu
.Events
.EVT_LOG(self
, self
.OnLog
)
141 wx
.EVT_SIZE(self
, self
.OnSize
)
144 def OnLog(self
, evt
):
146 Event handler for text events from Logger.
148 self
.txt
.AppendText(evt
.txt
)
150 def OnSize(self
, evt
):
152 Resize handler to correctly resize text area.
154 width
, height
= self
.GetClientSizeTuple()
155 self
.txt
.SetDimensions(0, 0, width
, height
)