Freeze upstream versions
[netvirt.git] / resources / commons / pplog.py
1 #!/usr/bin/python
2
3 import string
4 from sys import stdout,stdin,argv,exit
5
6 if len(argv) > 1 and argv[1] in ['-h', '-?', '--h', '--help']:
7     print 'pplog.py pretty prints Open Daylight log lines, useful for lines containing large nested objects'
8     print 'Usage: Simply pipe the lines you are interested through this script'
9     exit(0)
10
11 line_num = 0
12 def nl():
13     global line_num
14     line_num += 1
15     stdout.write('\n' + i)
16
17 for l in stdin:
18     if '|' not in l: continue
19
20     (t, level, component, cat, art, msg) = string.split(l, '|', maxsplit=5)
21
22     I = '  '
23     opener = '[{<'
24     closer = ']}>'
25     i = ''
26
27     in_ws = 1
28     is_empty_list = 0
29     last_char = ''
30     title = ''
31     title_stack = []
32
33     for c in msg:
34         if in_ws and c in ' \t': continue
35         in_ws = 0
36
37         if c in closer:
38             i = i[:-2]
39             if not is_empty_list and last_char not in closer: nl()
40             in_ws = 1
41             is_empty_list = 0
42             title = ''
43         elif is_empty_list:
44             is_empty_list = 0
45             nl()
46
47         if last_char in closer and c != ',': nl()
48
49         stdout.write(c)
50         if not c in opener: title += c
51         last_char = c
52
53         if c in closer:
54             if len(title_stack):
55                 (t,ln) = title_stack.pop()
56                 if (line_num - ln) > 5: stdout.write(' /* ' + t.strip() + ' */')
57
58         if c in opener:
59             i += I
60             in_ws = 1
61             is_empty_list = 1
62             if title:
63                 title_stack.append((title, line_num))
64                 title = ''
65
66         if c == ',':
67             nl()
68             in_ws = 1
69             title = ''