[a37b2b]: plugins / logger.py Maximize Restore History

Download this file

logger.py    115 lines (98 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'''
This software is licensed under the GPL (GNU General Public License) version 2
as it appears here: http://www.gnu.org/copyleft/gpl.html
It is also included with this archive as `gpl.txt <gpl.txt>`_.
'''
import time
import sys
import os
import re
import wx
import cStringIO
import Queue
lerexp = re.compile('(\r\n)|(\r)|(\n)')
def fixle(st):
return lerexp.sub(os.linesep, st)
class pseudo_logger:
def __init__(self):
self.buffer = cStringIO.StringIO()
self.softspace = 0
self.lg = 1
self.lt = None
sys.stdout = sys.stderr = self
def write(self, data):
## print >>sys.__stdout__, repr(data), self.softspace
if self.lg and self.lt != time.asctime():
self.lt = time.asctime()
x = '[ %s ] '%self.lt
self.buffer.write(x)
sys.__stdout__.write(x)
self.lg = data[-1:] == '\n'
x = fixle(data)
self.buffer.write(x)
if isinstance(x, str):
x = x.decode('latin1')
sys.__stdout__.write(x.encode('utf8'))
def flush(self):
sys.__stdout__.flush()
l = pseudo_logger()
class logger(wx.TextCtrl):
def __init__(self, parent):
global l
wx.TextCtrl.__init__(self, parent, -1, style=wx.TE_READONLY|wx.TE_MULTILINE|wx.TE_RICH)
self.softspace = 0
self.lg = 1
self.lt = None
sys.stdout = sys.stderr = self
self.data = Queue.Queue()
l.buffer.seek(0)
self.data.put(l.buffer.read())
l = self
self.lastwrite = 0
wx.CallAfter(self.handle_writes)
def write(self, data):
## print >>sys.__stdout__, repr(data), self.softspace
if self.lg and self.lt != time.asctime():
self.lt = time.asctime()
x = '[ %s ] '%self.lt
self.data.put(x)
try:
sys.__stdout__.write(x)
except:
pass
self.lg = data[-1:] == '\n'
x = fixle(data)
self.data.put(x)
try:
if isinstance(x, str):
x = x.decode('latin1')
sys.__stdout__.write(x.encode('utf8'))
except:
pass
ct = time.time()
if ct - self.lastwrite > .5:
wx.CallAfter(self.handle_writes)
def flush(self):
try:
sys.__stdout__.flush()
except:
pass
def handle_writes(self):
while self.data.qsize():
self.SetInsertionPointEnd()
data = []
for i in xrange(self.data.qsize()):
data.append(self.data.get())
self.WriteText(''.join(data))
lc = linecount = self.GetNumberOfLines()
lp = lastpos = self.GetLastPosition()
for i in xrange(min(2, linecount)):
linecount -= 1
lastpos -= self.GetLineLength(linecount)
self.Refresh()
self.Update()
wx.CallAfter(self.ShowPosition,
lastpos + (lp != lastpos and
bool(self.GetLineLength(linecount))))
def AppendText(self, txt):
self.write(txt)