aboutsummaryrefslogtreecommitdiff
path: root/scripts/trace_filter.py
blob: 3ad871515c7397d7cb746eab4d8897362e9c7d34 (plain)
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
#!/usr/bin/env python3

import argparse
import re
from pathlib import Path

MARK_LIST = ['tracing_mark_write']


def get_arg():
    parser = argparse.ArgumentParser(description='Filter a log file to a trace file.')
    parser.add_argument('log_file', metavar='log_file', type=str,
                        help='The input log file to process.')
    parser.add_argument('trace_file', metavar='trace_file', type=str, nargs='?',
                        help='The output trace file. If not provided, defaults to \'<log_file>.systrace\'.')

    args = parser.parse_args()
    return args


if __name__ == '__main__':
    args = get_arg()

    if not args.trace_file:
        log_file = Path(args.log_file)
        args.trace_file = log_file.with_suffix('.systrace').as_posix()

    print('log_file  :', args.log_file)
    print('trace_file:', args.trace_file)

    with open(args.log_file, 'r') as f:
        content = f.read()

    # compile regex pattern
    pattern = re.compile(r'(^.+-[0-9]+\s\[[0-9]]\s[0-9]+\.[0-9]+:\s('
                         + "|".join(MARK_LIST)
                         + r'):\s[B|E]\|[0-9]+\|.+$)', re.M)

    matches = pattern.findall(content)

    # write to args.trace_file
    with open(args.trace_file, 'w') as f:
        f.write('# tracer: nop\n#\n')
        for match in matches:
            f.write(match[0] + '\n')