|
1 # |
|
2 # Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of the License "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 |
|
17 Raptor log parsing utilities. |
|
18 |
|
19 Line-by-line based log reporting. |
|
20 """ |
|
21 |
|
22 import re |
|
23 |
|
24 |
|
25 class LogItem(object): |
|
26 keep = False |
|
27 def __init__(self, name, pattern, keep=False, subpattern=None): |
|
28 self.name = name |
|
29 self.matcher = re.compile(pattern, re.I) |
|
30 self.count = 0 |
|
31 |
|
32 if subpattern: |
|
33 self.subpattern = re.compile(subpattern,re.I) |
|
34 else: |
|
35 self.subpattern = None |
|
36 |
|
37 if keep and LogItem.keep: |
|
38 self.keep = {} |
|
39 else: |
|
40 self.keep = None |
|
41 |
|
42 self.subpatterncount = 0 |
|
43 |
|
44 def xml(self): |
|
45 xml = "<logitem name='%s' count='%i' subpatterncount='%i' " % ( self.name, self.count, self.subpatterncount) |
|
46 if self.keep == None: |
|
47 return xml + " />" |
|
48 |
|
49 xml += ">\n" |
|
50 |
|
51 index = self.keep.keys() |
|
52 index.sort(cmp=lambda y,x: self.keep[x] - self.keep[y]) |
|
53 for i in index: |
|
54 xml += "<match count='" + str(self.keep[i]) +"'><![CDATA[\n" + i + "]]></match>\n" |
|
55 |
|
56 return xml + "</logitem>" |
|
57 |
|
58 def match(self, line): |
|
59 result = self.matcher.search(line) |
|
60 if result != None: |
|
61 if self.keep != None: |
|
62 try: |
|
63 self.keep[result.group()] += 1 |
|
64 except: |
|
65 self.keep[result.group()] = 1 |
|
66 if self.subpattern != None: |
|
67 self.subpatterncount += len(self.subpattern.findall(line)) |
|
68 for i in self.subpattern.findall(line): |
|
69 print i |
|
70 self.count += 1 |
|
71 |