]> git.donarmstrong.com Git - infobot.git/blob - scripts/parse_warn.pl
dunno
[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 = ' at .* line ';
25
26     print "Opening $file... ";
27     if ( $file =~ /bz2$/ ) {    # bz2
28         open( FILE, "bzcat $file | egrep '$str' |" );
29     }
30     elsif ( $file =~ /gz$/ ) {    # gz
31         open( FILE, "zegrep '$str' $file |" );
32     }
33     else {                        # raw
34         open( FILE, "egrep '$str' $file |" );
35     }
36
37     print "Parsing... ";
38     while (<FILE>) {
39         if (/ at (\S+) line (\d+)/) {
40             my ( $file, $lineno ) = ( $1, $2 + 1 );
41             $done{$file}{$lineno}++;
42         }
43     }
44     close FILE;
45
46     print "Done.\n";
47 }
48
49 foreach $file ( keys %done ) {
50     my $count = scalar( keys %{ $done{$file} } );
51     print "warn $file: $count unique warnings.\n";
52
53     if ( !-f $file ) {
54         print "=> error: does not exist.\n\n";
55         next;
56     }
57
58     if ( open( IN, $file ) ) {
59         my @lines = <IN>;
60         close IN;
61
62         my $total  = scalar @lines;
63         my $spread = 0;
64         my $done   = 0;
65         for ( my $i = 0 ; $i <= $total ; $i++ ) {
66             next
67               unless ( exists $done{$file}{ $i + $contextspread } or $spread );
68
69             if ( exists $done{$file}{ $i + $contextspread } ) {
70                 print "@@ $i @@\n" unless ($spread);
71
72                 # max lines between offending lines should be 2*context-1.
73                 # coincidence that it is!
74                 $spread = 2 * $contextspread;
75             }
76             else {
77                 $spread--;
78             }
79
80             if ( exists $done{$file}{$i} ) {
81                 print "*** ";
82             }
83             else {
84                 print "--- ";
85             }
86
87             if ( $i >= $total ) {
88                 print "EOF\n";
89             }
90             else {
91                 print $lines[$i];
92             }
93         }
94         print "\n";
95     }
96     else {
97         print "=> error: could not open file.\n";
98     }
99 }
100
101 # vim:ts=4:sw=4:expandtab:tw=80