-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracelog.py
139 lines (114 loc) · 4.08 KB
/
tracelog.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#
# A PyGtk-based Python Trace Collector window
#
# Copyright (C) 2007 TK Soh <teekaysoh@gmail.com>
#
import pygtk
pygtk.require("2.0")
import gtk
import gobject
import pango
import threading
import Queue
import win32trace
try:
from gitgtk.gitlib import toutf
except ImportError:
import locale
_encoding = locale.getpreferredencoding()
def toutf(s):
return s.decode(_encoding, 'replace').encode('utf-8')
class TraceLog():
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("Python Trace Collector")
# construct window
self.window.set_default_size(700, 400)
self.main_area = gtk.VBox()
self.window.add(self.main_area)
# mimic standard dialog widgets
self.action_area = gtk.HBox()
self.main_area.pack_end(self.action_area, False, False, 5)
sep = gtk.HSeparator()
self.main_area.pack_end(sep, False, False, 0)
self.vbox = gtk.VBox()
self.main_area.pack_end(self.vbox)
# add python trace ouput window
scrolledwindow = gtk.ScrolledWindow()
scrolledwindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self.textview = gtk.TextView(buffer=None)
self.textview.set_editable(False)
self.textview.modify_font(pango.FontDescription("Monospace"))
scrolledwindow.add(self.textview)
self.textview.set_editable(False)
self.textbuffer = self.textview.get_buffer()
self.vbox.pack_start(scrolledwindow, True, True)
self.vbox.show_all()
# add buttons
self._button_quit = gtk.Button("Quit")
self._button_quit.connect('clicked', self._on_ok_clicked)
self.action_area.pack_end(self._button_quit, False, False, 5)
self._button_clear = gtk.Button("Clear")
self._button_clear.connect('clicked', self._on_clear_clicked)
self.action_area.pack_end(self._button_clear, False, False, 5)
# add assorted window event handlers
self.window.connect('map_event', self._on_window_map_event)
self.window.connect('delete_event', self._on_window_close_clicked)
def _on_ok_clicked(self, button):
self._stop_read_thread()
gtk.main_quit()
def _on_clear_clicked(self, button):
self.write("", False)
def _on_window_close_clicked(self, event, param):
self._stop_read_thread()
gtk.main_quit()
def _on_window_map_event(self, event, param):
self._begin_trace()
def _begin_trace(self):
self.queue = Queue.Queue()
win32trace.InitRead()
self.write("Collecting Python Trace Output...\n")
gobject.timeout_add(10, self._process_queue)
self._start_read_thread()
def _start_read_thread(self):
self._read_trace = True
self.thread1 = threading.Thread(target=self._do_read_trace)
self.thread1.start()
def _stop_read_thread(self):
self._read_trace = False
# wait for worker thread to to fix Unhandled exception in thread
self.thread1.join()
def _process_queue(self):
"""
Handle all the messages currently in the queue (if any).
"""
while self.queue.qsize():
try:
msg = self.queue.get(0)
self.write(msg)
except Queue.Empty:
pass
return True
def _do_read_trace(self):
"""
print buffer collected in win32trace
"""
while self._read_trace:
msg = win32trace.read()
if msg:
self.queue.put(msg)
def write(self, msg, append=True):
msg = toutf(msg)
if append:
enditer = self.textbuffer.get_end_iter()
self.textbuffer.insert(enditer, msg)
else:
self.textbuffer.set_text(msg)
def main(self):
self.window.show_all()
gtk.main()
def run():
dlg = TraceLog()
dlg.main()
if __name__ == "__main__":
run()