| mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 1 | import sys, re, os, time |
| 2 | |
| 3 | |
| 4 | logfile = open(sys.argv[1], 'a', 0) |
| 5 | warnfile = 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> |
| 10 | def 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 | |
| 18 | pattern_file = os.path.join(os.path.dirname(__file__), 'warning_patterns') |
| 19 | pattern_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()) |
| 27 | patterns = zip(pattern_lines[0::3], pattern_lines[1::3]) |
| 28 | |
| 29 | # assert that the patterns are separated by empty lines |
| 30 | if sum(len(line.strip()) for line in pattern_lines[2::3]) > 0: |
| 31 | raise ValueError('warning patterns are not separated by blank lines') |
| 32 | |
| 33 | hooks = [(re.compile(regex.rstrip('\n')), make_alert(alert.rstrip('\n'))) |
| 34 | for regex, alert in patterns] |
| 35 | |
| 36 | while 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()) |