-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogview.py
More file actions
347 lines (282 loc) · 10.2 KB
/
logview.py
File metadata and controls
347 lines (282 loc) · 10.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"""
Pyliner LogView
Enables the fast and easy browsing of Pyliner logs.
Usage:
python logview.py [filename]
"""
import argparse
import curses
import glob
import os
import re
from abc import abstractmethod
from future.moves import itertools
from pyliner.util import query_yes_no
print_errors = []
class LogLine(object):
"""A line in a log file."""
def __init__(self, log_line, text):
self.log_line = log_line
self.text = text
@property
def level(self):
"""Return the curses color-pair number for the level of the line."""
try:
level = self.text.split(' - ')[2]
except IndexError:
level = 'CRITICAL'
table = {
'NOTSET': curses.COLOR_WHITE,
'DEBUG': curses.COLOR_WHITE,
'INFO': curses.COLOR_WHITE,
'WARNING': curses.COLOR_YELLOW,
'ERROR': curses.COLOR_RED,
'CRITICAL': curses.COLOR_RED
}
return table[level]
class FilterLine(LogLine):
"""A line that has been filtered."""
def __init__(self, filter_line, log_line):
super(FilterLine, self).__init__(log_line.log_line, log_line.text)
self.filter_line = filter_line
class ViewLine(FilterLine):
"""A line that is in view."""
def __init__(self, view_line, filter_line):
super(ViewLine, self).__init__(filter_line.filter_line, filter_line)
self.view_line = view_line
class LogView(object):
FG_SHIFT = 8
def __init__(self, stdscr, log_name):
# Curses Initialization
# Start colors in curses
curses.start_color()
colors = [curses.COLOR_BLACK, curses.COLOR_RED, curses.COLOR_GREEN,
curses.COLOR_YELLOW, curses.COLOR_BLUE, curses.COLOR_MAGENTA,
curses.COLOR_CYAN, curses.COLOR_WHITE]
for fg, bg in itertools.permutations(colors, 2):
curses.init_pair(fg*LogView.FG_SHIFT + bg, fg, bg)
self.colors = colors
# Application State
self.exit = False
self.log_lines = None
self.log_name = log_name
self.state = None
self.state_map = None
self.stdscr = stdscr
with open(self.log_name) as fp:
self.log_lines = [LogLine(*x) for x in enumerate(fp.readlines())]
self.state_map = {
'NORMAL': NormalState,
'GOTO': GotoState
}
# Useful for User Interaction
self.filter = ''
self.filter_lines = None
""":type: list[FilterLine]"""
self.apply_filter(lambda line: True)
self.view_max = 1
self.view_min = 0
self.cursor_column = 0
self.cursor_line = 0
def apply_filter(self, func):
"""Set filter_lines to the lines filtered by func, and global_cursor
to the closest previous filtered line."""
if self.filter is func:
return
self.filter = func
if isinstance(func, str):
func = lambda line: line.text.contains(func)
self.filter_lines = [FilterLine(*x) for x in
enumerate(filter(func, self.log_lines))]
def center_view(self, filter_index):
"""Set view to contain lines centered around filter_index as much
as possible."""
h = self.height
h2 = h / 2
num_filter = len(self.filter_lines)
min_index = max(0, filter_index - h2)
max_index = min(num_filter, min_index + h)
# if max_index == num_filter:
# min_index = num_filter - h
# if min_index < 0:
# max_index = h - 1
# self.view_lines = [ViewLine(*x) for x in
# enumerate(self.filter_lines[min_index: max_index])]\
self.view_max = max_index
self.view_min = min_index
def cursor_move(self, lines):
self.cursor_line += lines
self.cursor_line = max(0, self.cursor_line)
self.cursor_line = min(len(self.filter_lines)-1, self.cursor_line)
self.put_line_in_view(self.cursor_line)
@staticmethod
def get_color(fg, bg):
return curses.color_pair(fg*LogView.FG_SHIFT + bg)
@property
def height(self):
return self.stdscr.getmaxyx()[0]
def put_line_in_view(self, filter_index):
"""Puts filter_index in view."""
cur_min = self.view_min
cur_max = self.view_max
if cur_min < filter_index < cur_max:
return
h = self.height
if filter_index < cur_min:
self.view_min = filter_index
self.view_max = self.view_min + h - 1
elif filter_index > cur_max:
self.view_max = filter_index + 1
self.view_min = self.view_max - h
def key_press(self, key):
if self.state.handle_key(key):
return
# Key Bindings
x_vel = 1
y_vel = 2
handled = True
if key == curses.KEY_DOWN:
self.cursor_move(x_vel)
elif key == curses.KEY_UP:
self.cursor_move(-x_vel)
elif key == curses.KEY_NPAGE:
self.cursor_move(10 * x_vel)
elif key == curses.KEY_PPAGE:
self.cursor_move(-10 * x_vel)
elif key == curses.KEY_LEFT:
self.cursor_column -= y_vel
elif key == curses.KEY_RIGHT:
self.cursor_column += y_vel
elif key == curses.KEY_HOME:
self.cursor_column -= 10 * y_vel
elif key == curses.KEY_END:
self.cursor_column += 10 * y_vel
elif key == curses.KEY_RESIZE:
self.resize()
else:
handled = False
if handled:
self.redraw()
return handled
def state_change(self, state):
if self.state is not None:
self.state.exit()
self.state = state
state.enter()
def redraw(self):
# Initialization
self.stdscr.clear()
height, width = self.stdscr.getmaxyx()
# Determine lines to display
self.put_line_in_view(self.cursor_line)
display_lines = [ViewLine(*x) for x in enumerate(
self.filter_lines[self.view_min:self.view_max])]
# Left Header
max_line = display_lines[-1].log_line
left_reserve = len(str(max_line))
# Bound column
longest_display = max(len(line.text) for line in display_lines)
self.cursor_column = max(0, self.cursor_column)
self.cursor_column = min(longest_display - 2, self.cursor_column)
# Display Log file
for line in display_lines:
bg = curses.COLOR_MAGENTA if line.filter_line == self.cursor_line\
else curses.COLOR_BLACK
color = self.get_color(line.level, bg)
left = '{:>{x}}'.format(line.log_line, x=left_reserve)
end_column = self.cursor_column + width - (len(left) + 2)
visible_line = line.text[self.cursor_column:end_column]
display = '{} {}'.format(left, visible_line)
try:
self.stdscr.addstr(line.view_line, 0, display, color)
except Exception as e:
# print_errors.append('Cannot display: {} {}'.format(line.filter_line, display))
self.stdscr.addstr(0, 0, str(e)[:width], color)
self.state.status_bar_render()
# Refresh the screen
self.stdscr.refresh()
def resize(self):
self.view_min = 0
self.view_max = self.height - 1
def run(self):
self.state_change(self.state_map['NORMAL'](self))
self.resize()
self.redraw()
while not self.exit:
# Wait for next input
self.key_press(self.stdscr.getch())
class BaseState(object):
def __init__(self, log_view):
self.log_view = log_view
def enter(self):
pass
def exit(self):
pass
@abstractmethod
def handle_key(self, key):
raise NotImplementedError()
@property
def stdscr(self):
return self.log_view.stdscr
class NormalState(BaseState):
def enter(self):
self.redraw()
def status_bar_render(self):
len_filter = len(self.log_view.filter_lines)
line_status = 'TOP' if self.log_view.cursor_line == 0 else\
'BOT' if self.log_view.cursor_line == len_filter-1 else\
str(self.log_view.cursor_line)
statusbarstr = "Press 'q' to exit | STATUS BAR | Pos: {} Col: {} " \
"View: {}:{} Height: {} FL: {}" \
.format(line_status, self.log_view.cursor_column,
self.log_view.view_min, self.log_view.view_max,
self.log_view.height, len(self.log_view.filter_lines))
height, width = self.stdscr.getmaxyx()
self.stdscr.attron(self.log_view.get_color(
curses.COLOR_BLACK, curses.COLOR_CYAN))
self.stdscr.addstr(height - 1, 0, statusbarstr)
self.stdscr.addstr(height - 1, len(statusbarstr),
" " * (width - len(statusbarstr) - 1))
self.stdscr.attroff(self.log_view.get_color(
curses.COLOR_BLACK, curses.COLOR_CYAN))
def handle_key(self, key):
handled = True
if key == ord('q'):
self.log_view.exit = True
else:
handled = False
return handled
def redraw(self):
pass
class GotoState(BaseState):
@property
def status_bar_height(self):
return 1
def status_bar_render(self):
pass
def handle_key(self, key):
pass
def log_view(stdscr, filename):
"""Main function. Displays a log viewer with Python curses."""
view = LogView(stdscr, filename)
view.run()
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Log Viewer for Pyliner'
)
parser.add_argument('filename', nargs='?',
help='Filename of log to view.')
args = parser.parse_args()
filename = args.filename
if filename is None:
log_files = glob.glob('logs/*')
filename = max(log_files, key=os.path.getctime)
if not os.path.isfile(filename):
print('filename must exist.')
exit(1)
if query_yes_no('NOTE: The Pyliner Log Viewer is still in the experimental '
'phase.\nBeware of bugs. Continue? '):
curses.wrapper(log_view, filename)
print('\n'.join(print_errors))
else:
exit()