]> git.donarmstrong.com Git - infobot.git/blob - scripts/parse_warn.pl
Initial revision
[infobot.git] / scripts / parse_warn.pl
1 #!/usr/bin/perl -w
2
3 # leading and trailing context lines.
4 my $contextspread       = 2;
5
6 use strict;
7
8 $| = 1;
9
10 if (!scalar @ARGV) {
11     print "Usage: parse_warn.pl <files>\n";
12     print "Example: parse_warn.pl log/*\n";
13     exit 0;
14 }
15
16 my %done;
17 my $file;
18
19 foreach $file (@ARGV) {
20     if (! -f $file) {
21         print "warning: $file does not exist.\n";
22         next;
23     }
24     my $str = ' value at .* line ';
25
26     print "Opening $file... ";
27     if ($file =~ /bz2$/) {      # bz2
28         open(FILE, "bzcat $file | egrep '$str' |");
29     } elsif ($file =~ /gz$/) {  # gz
30         open(FILE, "zegrep '$str' $file |");
31     } else {                    # raw
32         open(FILE, "egrep '$str' $file |");
33     }
34
35     print "Parsing... ";
36     while (<FILE>) {
37         if (/ at (\S+) line (\d+)/) {
38             my ($file,$lineno) = ($1,$2+1);
39             $done{$file}{$lineno}++;
40         }
41     }
42     close FILE;
43
44     print "Done.\n";
45 }
46
47 foreach $file (keys %done) {
48     my $count = scalar(keys %{$done{$file}});
49     print "warn $file: $count unique warnings.\n";
50
51     if (! -f $file) {
52         print "=> error: does not exist.\n\n";
53         next;
54     }
55
56     if (open(IN,$file)) {
57         my @lines = <IN>;
58         close IN;
59
60         my $total = scalar @lines;
61         my $spread = 0;
62         my $done = 0;
63         for(my $i=0; $i<=$total; $i++) {
64             next unless (exists $done{$file}{$i+$contextspread} or $spread);
65
66             if (exists $done{$file}{$i+$contextspread}) {
67                 print "@@ $i @@\n" unless ($spread);
68                 # max lines between offending lines should be 2*context-1.
69                 # coincidence that it is!
70                 $spread = 2*$contextspread;
71             } else {
72                 $spread--;
73             }
74
75             if (exists $done{$file}{$i}) {
76                 print "*** ";
77             } else {
78                 print "--- ";
79             }
80
81             if ($i >= $total) {
82                 print "EOF\n";
83             } else {
84                 print $lines[$i];
85             }
86         }
87         print "\n";
88     } else {
89         print "=> error: could not open file.\n";
90     }
91 }