forked from dodola/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flog
executable file
·368 lines (271 loc) · 8.25 KB
/
flog
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python
# Copyright 2017 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""flog is a developer friendly log listener with automatic crash symbol decoder.
The crash is archived to /tmp/fuchsia-crash or a specified directory.
Configurable keywords or lines with them can be color coded.
"""
import argparse
import os
import re
import subprocess
import sys
from time import gmtime
from time import strftime
CASE_SENSITIVE = False
BEGINS = []
ENDS = []
SUPPRESSES = []
CRASH_DIR = '/tmp/fuchsia_crash'
# TODO(porce): Support regular expression
# TODO(porce): Support compatibility with REGEX_ regex expressions
COLOR_LINES = {'error': 'red'}
COLOR_WORDS = {
'warn': 'white-on-red',
'error': 'white-on-red',
'fail': 'white-on-red',
'exception': 'white-on-red',
'address': 'green',
'did not add device in bind': 'green',
}
RESET = '\033[1;0m'
COLORS = {
'WHITE-ON-RED': '\033[41;37m',
'BLACK': '\033[30;1m',
'RED': '\033[31;1m',
'GREEN': '\033[32;1m',
'YELLOW': '\033[33m',
'BLUE': '\033[34;1m',
'MAGENTA': '\033[35;1m',
'CYAN': '\033[36m',
}
FIRST_LOG_AFTER_BOOTUP = '[00000.000] 00000.00000> bootdata:'
# REGEX_* are constructed when the command line arguments are parsed.
REGEX_BEGINS = ''
REGEX_ENDS = ''
REGEX_SUPPRESS = ''
REGEX_COLOR_LINES = ''
REGEX_COLOR_WORDS = ''
def static_vars(**kwargs):
def decorate(func):
for k in kwargs:
setattr(func, k, kwargs[k])
return func
return decorate
def now_str():
return strftime('%Y%m%d_%H%M%S', gmtime())
def get_log_listener(args):
# return 'cat /tmp/z' # Unit test
listener = 'fx log'
return '{} {}'.format(listener, ' '.join(x for x in args))
@static_vars(crash_dump=[])
@static_vars(is_crashing=False)
def monitor_crash(line):
if '<==' in line and 'exception' in line:
monitor_crash.is_crashing = True
if monitor_crash.is_crashing is True:
monitor_crash.crash_dump.append(line)
if ': end' in line and 'bt#' in line:
decode_backtrace(monitor_crash.crash_dump, CRASH_DIR)
monitor_crash.crash_dump = []
monitor_crash.is_crashing = False
def color(string, color_name):
if color_name.upper() not in COLORS:
return string
ends_in_newline = string.endswith('\n')
if ends_in_newline:
string = string[:-1]
result = '{}{}{}'.format(COLORS[color_name.upper()], string, RESET)
if ends_in_newline:
result += '\n'
return result
def anymatch(test_string, regexes):
global CASE_SENSITIVE
if CASE_SENSITIVE:
ans = re.search(regexes, test_string)
else:
ans = re.search(regexes, test_string, flags=re.IGNORECASE)
return ans.group(0) if ans else None
@static_vars(is_in_session=False)
def is_suppressed(line):
"""Test if the log may be printed or not.
Args:
line (str): A log line.
Returns:
True if the log line should be suppressed. False otherwise.
"""
if line.startswith(FIRST_LOG_AFTER_BOOTUP):
# A reboot occurs. Reset to default.
is_suppressed.is_in_session = False
if not BEGINS or anymatch(line, REGEX_BEGINS):
if not is_suppressed.is_in_session:
print '\n' * 3
is_suppressed.is_in_session = True
if anymatch(line, REGEX_ENDS):
is_suppressed.is_in_session = False
if not is_suppressed.is_in_session:
return True
if anymatch(line, REGEX_SUPPRESSES):
return True
return False
def colorize(line_incoming):
"""Color-code the log line.
Args:
line_incoming: log line.
Returns:
color-coded log line
"""
line = line_incoming
k = anymatch(line, REGEX_COLOR_LINES)
if k:
v = COLOR_LINES.get(k.lower(), '')
line = color(line, v)
return line
if anymatch(line, REGEX_COLOR_WORDS):
# TODO(porce): Maybe there are less costly ways.
for k, v in COLOR_WORDS.iteritems():
replacement = r'%s\1%s' % (COLORS[v.upper()], RESET)
search = '(?i)(' + '|'.join(map(re.escape, [k])) + ')'
line = re.sub(search, replacement, line)
return line
def print_log(line):
if is_suppressed(line):
return
print colorize(line),
def hijack_stdout(cmd):
proc = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
return iter(proc.stdout.readline, '')
def get_decode_cmd():
decode_cmd = 'fx symbolize'
return decode_cmd
def print_bt(line):
"""Print backtrace with some colors.
Args:
line (str): Backtrace line.
"""
tokens = line.split(' at ')
methods = tokens[:-1]
methods.append('')
methods_text = ' at '.join(x for x in methods)
path = tokens[-1]
path_tokens = path.split('/')
paths = path_tokens[:-1]
paths.append('')
path_text = '/'.join(x for x in paths)
path_colored = path_text + color(path_tokens[-1], 'red')
print methods_text, path_colored,
def decode_backtrace(crash_dump, dst_dir):
"""A wrapper to fsymbolize.
Args:
crash_dump (array): array of log lines.
dst_dir (str): directory to archive to.
"""
os.system('mkdir -p {}'.format(dst_dir))
crash_file_path = '{}/{}.crash'.format(dst_dir, now_str())
tmp_file = '{}/tmp.dump'.format(dst_dir)
f = open(tmp_file, 'w')
for line in crash_dump:
f.write(line)
f.close()
cmd = '{} < {} > {}'.format(get_decode_cmd(), tmp_file, crash_file_path)
os.system(cmd)
print '\n\n'
is_start = False
with open(crash_file_path, 'r') as f:
for line in f:
if 'start of symbolized stack:' in line:
is_start = True
if is_start:
print_bt(line)
print '\n\n'
def parse_color_map(string):
"""Converted comma separated text into a color code map.
Args:
string (str): a text line
Returns:
Dictonary whose key is a text pattern and value is the color name.
"""
m = {}
items = string.split(',')
for item in items:
sep = ':'
if sep not in item:
continue
idx = item.rfind(sep)
text = item[:idx]
color_name = item[idx + 1:]
if text.__len__() == 0:
continue
m[text] = color_name
return m
def proc_cmdline():
"""Argument parser.
Returns:
args.
"""
example_commands = """
Pro tip: Use comma seperated texts for multiple matches
Example:
$ flog --begin \'my module starts,rare event\'
--end \'my module ends\'
--suppress \'verbose,chatty\'
--lines \'error msg:red,warn:blue\'
--words \'register 0x00:green,exit:yellow\'
"""
p = argparse.ArgumentParser(
description='A friendly Fuchsia log listener',
epilog=example_commands,
formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument('--begin', type=str, help='trigger texts to start logging')
p.add_argument('--end', type=str, help='trigger texts to end logging')
p.add_argument(
'--case', type=bool, help='match case-sensitively', default=False)
p.add_argument('--suppress', type=str, help='text to suppress the line')
p.add_argument('--lines', type=str, help='colorize the line. {text:color}')
p.add_argument('--words', type=str, help='colorize the word. {text:color}')
p.add_argument('--crashdir', type=str, help='directory to store crash files.')
p.add_argument(
'remainders',
nargs=argparse.REMAINDER,
help='arguments passed to loglistener')
args = p.parse_args()
global CASE_SENSITIVE
global BEGINS
global ENDS
global SUPPRESSES
global COLOR_LINES
global COLOR_WORDS
global CRASH_DIR
CASE_SENSITIVE = args.case
if args.begin:
BEGINS.extend(args.begin.split(','))
if args.end:
ENDS.extend(args.end.split(','))
if args.suppress:
SUPPRESSES.extend(args.suppress.split(','))
if args.lines:
COLOR_LINES.update(parse_color_map(args.lines))
if args.words:
COLOR_WORDS.update(parse_color_map(args.words))
if args.crashdir:
CRASH_DIR = args.crashdir
global REGEX_BEGINS
global REGEX_ENDS
global REGEX_SUPPRESSES
global REGEX_COLOR_LINES
global REGEX_COLOR_WORDS
# TODO(porce): Support regex input
REGEX_BEGINS = '|'.join(BEGINS)
REGEX_ENDS = '|'.join(ENDS)
REGEX_SUPPRESSES = '|'.join(SUPPRESSES)
REGEX_COLOR_LINES = '|'.join(k for k, v in COLOR_LINES.iteritems())
REGEX_COLOR_WORDS = '|'.join(k for k, v in COLOR_WORDS.iteritems())
return args
def main():
args = proc_cmdline()
cmd = get_log_listener(args.remainders)
for line in hijack_stdout(cmd):
print_log(line)
monitor_crash(line)
main()