-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreplog.py.cstringio
More file actions
53 lines (41 loc) · 1.46 KB
/
greplog.py.cstringio
File metadata and controls
53 lines (41 loc) · 1.46 KB
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
#!/usr/bin/python
from __future__ import print_function
import argparse
import fileinput
import re
import sys
import cStringIO
def handleMessage(message, filter):
if filter.search(message):
print(message, end='')
def main():
parser = argparse.ArgumentParser("Filter logs")
parser.add_argument('pattern', nargs=1,
help='Filter pattern')
parser.add_argument('files', metavar='file', nargs='*', default='-',
help='Input file')
args = parser.parse_args()
#print(args)
#pattern = sys.argv[1]
#files = sys.argv[2:]
pattern = args.pattern[0]
files = args.files
# 2011-06-22 17:49:44,732 DEBUG
# 2011-07-27 11:46:45,282 35195 TRACE
# 11:46:45,282 TRACE
possibleMessageStartFilters = [re.compile('^\d{4}-\d{2}-\d{2} '), re.compile('^\d{2}:\d{2}:\d{2},\d{3} ')]
messageFilter = re.compile(pattern, re.MULTILINE | re.DOTALL)
f = fileinput.input(files)
firstLine = next(f)
messageStartFilter = next(x for x in possibleMessageStartFilters if x.match(firstLine))
messageBuffer = cStringIO.StringIO()
messageBuffer.write(firstLine)
for line in f:
if messageStartFilter.match(line):
#print("Analyzing message " + messageBuffer.getvalue())
handleMessage(messageBuffer.getvalue(), messageFilter)
messageBuffer = cStringIO.StringIO()
messageBuffer.write(line)
handleMessage(messageBuffer.getvalue(), messageFilter)
messageBuffer.close()
main()