Translated using Weblate.
[wammu.git] / Wammu / Logger.py
blob03beaf422604ce5224aa72ec7c8f8819156cb6a4
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 © 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
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 LoggerDebug(threading.Thread):
34 '''
35 Thread which reads defined files and prints it to stderr.
36 '''
37 def __init__(self, filename):
38 '''
39 Initializes reader on filename, text will be printed to stderr.
40 '''
41 threading.Thread.__init__(self)
42 self.file_descriptor = open(filename, 'r')
43 self.filename = filename
44 self.canceled = False
46 def run(self):
47 """
48 This is basically tail -f reimplementation
49 """
50 while not self.canceled:
51 where = self.file_descriptor.tell()
52 txt = self.file_descriptor.readlines()
53 if len(txt) == 0:
54 fd_results = os.fstat(self.file_descriptor.fileno())
55 try:
56 st_results = os.stat(self.filename)
57 except OSError:
58 st_results = fd_results
60 if st_results[1] == fd_results[1] or sys.platform == 'win32':
61 time.sleep(1)
62 self.file_descriptor.seek(where)
63 else:
64 self.file_descriptor = open(self.filename, 'r')
65 else:
66 sys.stderr.write(''.join(txt))
67 self.file_descriptor.close()
69 class Logger(threading.Thread):
70 '''
71 Thread which reads defined files and posts events on change.
72 '''
73 def __init__(self, win, filename):
74 '''
75 Initializes reader on filename, events will be sent to win.
76 '''
77 threading.Thread.__init__(self)
78 self.win = win
79 self.file_descriptor = open(filename, 'r')
80 self.filename = filename
81 self.canceled = False
83 def run(self):
84 """
85 This is basically tail -f reimplementation
86 """
87 while not self.canceled:
88 where = self.file_descriptor.tell()
89 txt = self.file_descriptor.readlines()
90 if len(txt) == 0:
91 fd_results = os.fstat(self.file_descriptor.fileno())
92 try:
93 st_results = os.stat(self.filename)
94 except OSError:
95 st_results = fd_results
97 if st_results[1] == fd_results[1] or sys.platform == 'win32':
98 time.sleep(1)
99 self.file_descriptor.seek(where)
100 else:
101 self.file_descriptor = open(self.filename, 'r')
102 else:
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.
116 self.cfg = cfg
117 if cfg.HasEntry('/Debug/X') and cfg.HasEntry('/Debug/Y'):
118 pos = wx.Point(
119 cfg.ReadInt('/Debug/X'),
120 cfg.ReadInt('/Debug/Y'))
121 else:
122 pos = wx.DefaultPosition
123 size = wx.Size(
124 cfg.ReadInt('/Debug/Width'),
125 cfg.ReadInt('/Debug/Height'))
126 wx.Frame.__init__(
127 self,
128 parent,
130 _('Wammu debug log'),
131 pos,
132 size,
133 wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER)
134 self.txt = wx.TextCtrl(
135 self,
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)
142 self.OnSize(None)
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)