raptorlog.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. Raptor log parsing utilities.
  17. Line-by-line based log reporting.
  18. """
  19. import re
  20. class LogItem(object):
  21. keep = False
  22. def __init__(self, name, pattern, keep=False, subpattern=None):
  23. self.name = name
  24. self.matcher = re.compile(pattern, re.I)
  25. self.count = 0
  26. if subpattern:
  27. self.subpattern = re.compile(subpattern,re.I)
  28. else:
  29. self.subpattern = None
  30. if keep and LogItem.keep:
  31. self.keep = {}
  32. else:
  33. self.keep = None
  34. self.subpatterncount = 0
  35. def xml(self):
  36. xml = "<logitem name='%s' count='%i' subpatterncount='%i' " % ( self.name, self.count, self.subpatterncount)
  37. if self.keep == None:
  38. return xml + " />"
  39. xml += ">\n"
  40. index = self.keep.keys()
  41. index.sort(cmp=lambda y,x: self.keep[x] - self.keep[y])
  42. for i in index:
  43. xml += "<match count='" + str(self.keep[i]) +"'><![CDATA[\n" + i + "]]></match>\n"
  44. return xml + "</logitem>"
  45. def match(self, line):
  46. result = self.matcher.search(line)
  47. if result != None:
  48. if self.keep != None:
  49. try:
  50. self.keep[result.group()] += 1
  51. except:
  52. self.keep[result.group()] = 1
  53. if self.subpattern != None:
  54. self.subpatterncount += len(self.subpattern.findall(line))
  55. for i in self.subpattern.findall(line):
  56. print i
  57. self.count += 1