blob: 6cd15c7657ca2fe80722bb7392f93509262f5a16 [file] [log] [blame]
mblighf4e04152008-02-21 16:05:53 +00001import sys, re, os, time
2
3
4logfile = open(sys.argv[1], 'a', 0)
5warnfile = os.fdopen(int(sys.argv[2]), 'w', 0)
6
7
8# the format for a warning used here is:
9# <timestamp (integer)> <tab> <status (string)> <newline>
10def make_alert(msg):
11 def alert(*params):
12 formatted_msg = msg % params
13 timestamped_msg = "%d\t%s" % (time.time(), formatted_msg)
14 print >> warnfile, timestamped_msg
15 return alert
16
17
18pattern_file = os.path.join(os.path.dirname(__file__), 'warning_patterns')
19pattern_lines = open(pattern_file).readlines()
20
21# expected pattern format:
22# <regex> <newline> <alert> <newline> <newline>
23# regex = a python regular expression
24# alert = a string describing the alert message
25# if the regex matches the line, this displayed warning will
26# be the result of (alert % match.groups())
27patterns = zip(pattern_lines[0::3], pattern_lines[1::3])
28
29# assert that the patterns are separated by empty lines
30if sum(len(line.strip()) for line in pattern_lines[2::3]) > 0:
31 raise ValueError('warning patterns are not separated by blank lines')
32
33hooks = [(re.compile(regex.rstrip('\n')), make_alert(alert.rstrip('\n')))
34 for regex, alert in patterns]
35
36while True:
37 line = sys.stdin.readline()
38 logfile.write(line)
39 for regex, callback in hooks:
40 match = re.match(regex, line.strip())
41 if match:
42 callback(*match.groups())