2003-07-02 22:33:18 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
# $Id$
|
|
|
|
# greplogs - grep logs for a string.
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
2004-08-27 22:31:02 +00:00
|
|
|
use bytes;
|
2003-07-02 22:33:18 +00:00
|
|
|
|
|
|
|
my $html = 0;
|
2003-07-22 20:52:25 +00:00
|
|
|
my $lines = 0;
|
2003-07-02 22:33:18 +00:00
|
|
|
die unless @ARGV;
|
|
|
|
if ($ARGV[0] eq '-html') {
|
|
|
|
shift;
|
|
|
|
$html = 1;
|
|
|
|
}
|
2003-07-22 20:52:25 +00:00
|
|
|
if ($ARGV[0] eq '-l') {
|
|
|
|
shift;
|
|
|
|
$lines = shift;
|
|
|
|
}
|
2003-07-02 22:33:18 +00:00
|
|
|
|
|
|
|
my $s = shift; die unless defined $s;
|
|
|
|
|
2003-07-02 22:52:53 +00:00
|
|
|
# if the string to grep for ends with a digit, add a zero-width negative
|
|
|
|
# look-ahead assertion against another digit. e.g., if we are searching for
|
|
|
|
# "wu_12" do not match "wu_125".
|
|
|
|
if ($s =~ /\d$/) {
|
|
|
|
$s .= "(?!\\d)";
|
|
|
|
}
|
|
|
|
|
2003-07-02 22:33:18 +00:00
|
|
|
my $prev_file = '';
|
|
|
|
|
2003-07-22 20:52:25 +00:00
|
|
|
my $nlines = 0;
|
|
|
|
my @slines = ();
|
|
|
|
|
|
|
|
sub printline($)
|
|
|
|
{
|
|
|
|
++$nlines;
|
|
|
|
if ($lines < 0) {
|
|
|
|
# tail - need to store all lines first
|
|
|
|
push(@slines, $_[0]);
|
|
|
|
} else {
|
|
|
|
return if ($lines > 0 && $nlines > $lines) ;
|
|
|
|
print $_[0];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
sub finish_printlines()
|
|
|
|
{
|
|
|
|
if ($lines < 0) {
|
|
|
|
if (scalar(@slines) > -$lines) {
|
|
|
|
@slines = @slines[$#slines+$lines+1..$#slines];
|
|
|
|
}
|
|
|
|
print @slines;
|
|
|
|
}
|
|
|
|
$nlines = 0;
|
|
|
|
@slines = ();
|
|
|
|
}
|
|
|
|
|
2003-07-02 22:33:18 +00:00
|
|
|
if ($html) { print "<pre>"; }
|
|
|
|
while (<ARGV>) {
|
|
|
|
chomp;
|
|
|
|
if (/$s/) {
|
|
|
|
if ($ARGV ne $prev_file) {
|
2003-07-22 20:52:25 +00:00
|
|
|
finish_printlines();
|
2003-07-02 22:33:18 +00:00
|
|
|
$prev_file = $ARGV;
|
|
|
|
if ($html) {
|
|
|
|
print "</pre><h2>$ARGV</h2><pre>";
|
|
|
|
} else {
|
|
|
|
print "\n\n$ARGV:\n";
|
|
|
|
}
|
2003-07-22 20:52:25 +00:00
|
|
|
$nlines = 0;
|
2003-07-22 21:17:26 +00:00
|
|
|
$. = 0;
|
2003-07-02 22:33:18 +00:00
|
|
|
}
|
2003-07-22 21:17:26 +00:00
|
|
|
my $n = sprintf("%05d", $.);
|
2003-07-22 20:52:25 +00:00
|
|
|
|
2003-07-02 22:33:18 +00:00
|
|
|
if ($html) {
|
|
|
|
s%.*debug.*%<font color=grey>$&</font>%i;
|
2003-07-02 22:52:53 +00:00
|
|
|
s%.*(critical|error).*%<font color=red>$&</font>%i;
|
2003-07-02 22:33:18 +00:00
|
|
|
s%$s%<b>$&</b>%g if $s;
|
2003-07-22 20:52:25 +00:00
|
|
|
printline "<font size=-1><a name=$. href=show_log.php?f=$ARGV#$.>$n</a></font> $_\n";
|
2003-07-02 22:33:18 +00:00
|
|
|
} else {
|
2003-07-22 20:52:25 +00:00
|
|
|
printline " $n: $_\n";
|
2003-07-02 22:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-07-22 20:52:25 +00:00
|
|
|
finish_printlines();
|
2003-07-02 22:33:18 +00:00
|
|
|
if ($html) { print "</pre>"; }
|