blob: d14080c62d47857122c792dea0c3a442d497ec85 [file] [log] [blame]
Jordan Haltermanc9ce09a2017-03-21 11:27:25 -07001#!/usr/bin/env python
2# Splits ONOS DEBUG logs by partition/session for debugging consistent primitives.
3# When logs are split, this script will create a separate log file for each partition
4# and each Atomix session, e.g. karaf.log.partition-1, karaf.log.session-10, etc.
5#
6# Usage: onos-split-logs [directory|file]
7import sys, os, re
8from os.path import isfile, isdir, dirname, basename
9
10path = sys.argv[1]
11
12INDENT = 25
13SEP = ' | '
14
15DOMAINS = ('com', 'org', 'net', 'io', 'us')
16
17def is_exception(line):
18 for domain in DOMAINS:
19 if line.startswith(domain + '.'):
20 return True
21 return line.strip().startswith('at ')
22
23def find_pattern(f, pattern):
24 values = set()
25 prog = re.compile(pattern)
26 for line in f:
27 match = prog.search(line)
28 if match:
29 values.add(match.group(1))
30 f.seek(0)
31 return values
32
33def split_line(line):
34 if is_exception(line):
35 return line
36 split = line.split('|')
37 if len(split) > 5:
38 ts = split[0].strip()
39 cls = split[3].strip()
40 log = split[5].strip(' /')
41 tsbuf = ''.join([' ' for i in range(INDENT - len(ts))])
42 clsbuf = ''.join([' ' for i in range(INDENT - len(cls))])
43 return ts + tsbuf + SEP + cls + clsbuf + SEP + log
44 return ''
45
46def split_log(infile, matcher, filemaker, matchmaker):
47 for match in matcher(infile):
48 matchers = matchmaker(match)
49 with open(filemaker(match), 'w+') as outfile:
50 for line in infile:
51 for m in matchers:
52 if m(line):
53 outfile.write(split_line(line))
54 break
55 infile.seek(0)
56
57def split_partitions(filepath, f):
58 split_log(
59 f,
60 lambda f: find_pattern(f, '9876-partition-([0-9]+)'),
61 lambda partition: filepath + '.partition-' + partition,
62 lambda partition: (
63 lambda line: 'partition-' + partition in line,
64 is_exception))
65
66def split_sessions(filepath, f):
67 split_log(
68 f,
69 lambda f: find_pattern(f, 'session=([0-9]+)'),
70 lambda session: filepath + '.session-' + session,
71 lambda session: (
72 lambda line: ' ' + session + ' - ' in line,
73 lambda line: 'session=' + session in line,
74 is_exception))
75
76def split_file(dirpath, filename):
77 filepath = dirpath + os.sep + filename
78 with open(filepath, 'r') as f:
79 split_partitions(filepath, f)
80 split_sessions(filepath, f)
81
82def split_files(path):
83 for (dirpath, dirnames, filenames) in os.walk(path):
84 for filename in filenames:
85 if '.log' in filename:
86 split_file(dirpath, filename)
87
88if __name__ == '__main__':
89 path = sys.argv[1]
90 if isfile(path):
91 split_file(dirname(path), basename(path))
92 else:
93 split_files(path)