1#!/usr/bin/env python3
2
3import argparse
4import re
5from pathlib import Path
6
7MARK_LIST = ['tracing_mark_write']
8
9
10def get_arg():
11    parser = argparse.ArgumentParser(description='Filter a log file to a trace file.')
12    parser.add_argument('log_file', metavar='log_file', type=str,
13                        help='The input log file to process.')
14    parser.add_argument('trace_file', metavar='trace_file', type=str, nargs='?',
15                        help='The output trace file. If not provided, defaults to \'<log_file>.systrace\'.')
16
17    args = parser.parse_args()
18    return args
19
20
21if __name__ == '__main__':
22    args = get_arg()
23
24    if not args.trace_file:
25        log_file = Path(args.log_file)
26        args.trace_file = log_file.with_suffix('.systrace').as_posix()
27
28    print('log_file  :', args.log_file)
29    print('trace_file:', args.trace_file)
30
31    with open(args.log_file, 'r') as f:
32        content = f.read()
33
34    # compile regex pattern
35    pattern = re.compile(r'(^.+-[0-9]+\s\[[0-9]]\s[0-9]+\.[0-9]+:\s('
36                         + "|".join(MARK_LIST)
37                         + r'):\s[B|E]\|[0-9]+\|.+$)', re.M)
38
39    matches = pattern.findall(content)
40
41    # write to args.trace_file
42    with open(args.trace_file, 'w') as f:
43        f.write('# tracer: nop\n#\n')
44        for match in matches:
45            f.write(match[0] + '\n')
46