From fa12939c2d1af69872b5f404ea9168a789ef77af Mon Sep 17 00:00:00 2001 From: fukachan Date: Wed, 22 Feb 2006 12:05:59 +0000 Subject: option -O date=PATTERN simulates "grep for the log file". especially, "-O date=yesterday" shows yesterday's log. --- fml/lib/FML/Command/Admin/log.pm | 101 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 2 deletions(-) diff --git a/fml/lib/FML/Command/Admin/log.pm b/fml/lib/FML/Command/Admin/log.pm index 27ffae47..78f56aab 100644 --- a/fml/lib/FML/Command/Admin/log.pm +++ b/fml/lib/FML/Command/Admin/log.pm @@ -4,7 +4,7 @@ # All rights reserved. This program is free software; you can # redistribute it and/or modify it under the same terms as Perl itself. # -# $FML: log.pm,v 1.29 2005/08/17 12:08:43 fukachan Exp $ +# $FML: log.pm,v 1.30 2005/08/19 11:20:23 fukachan Exp $ # package FML::Command::Admin::log; @@ -92,12 +92,94 @@ sub cgi_menu # Side Effects: none # Return Value: none sub _show_log +{ + my ($self, $curproc, $log_file, $sl_args) = @_; + my $c_opts = $curproc->command_line_cui_specific_options() || {}; + + if (defined $c_opts->{ date }) { + if ($c_opts->{ date } eq 'yesterday') { + my $date = time - 24*3600; + $self->_show_log_grep($curproc, $log_file, $sl_args, $date); + } + else { + my $date = $c_opts->{ date } || ''; + $self->_show_log_grep($curproc, $log_file, $sl_args, $date); + } + } + else { + $self->_show_log_tail($curproc, $log_file, $sl_args); + } +} + + +# Descriptions: grep log file by date. +# Arguments: OBJ($self) +# OBJ($curproc) STR($log_file) HASH_REF($sl_args) NUM($when) +# Side Effects: none +# Return Value: none +sub _show_log_grep +{ + my ($self, $curproc, $log_file, $sl_args, $when) = @_; + my $regexp = ''; + my $is_cgi = 1 if $sl_args->{ printing_style } eq 'html'; + my $charset = $curproc->langinfo_get_charset($is_cgi ? "cgi" : "log_file"); + + if ($when =~ /^\d+$/) { + $regexp = $self->_log_date_string($when) || ''; + } + elsif ($when =~ /^[\d\/]+$/) { + $regexp = $when; + } + + unless ($regexp) { + croak("no regexp"); + } + + use Mail::Message::Encode; + my $encode = new Mail::Message::Encode; + + use FileHandle; + my $fh = new FileHandle $log_file; + my $wh = \*STDOUT; + + if (defined $fh) { + if ($is_cgi) { print $wh "
\n";}
+
+	# show the last $last_n_lines lines by default.
+	my ($buf, $s);
+      LINE:
+	while ($buf = <$fh>) {
+	    $s = $encode->convert( $buf, $charset );
+
+	    if ($s =~ /^\S*$regexp/) {
+		if ($is_cgi) {
+		    print $wh (_html_to_text($s));
+		    print $wh "\n";
+		}
+		else {
+		    print $wh $s;
+		}
+	    }
+	}
+	$fh->close;
+
+	if ($is_cgi) { print $wh "
\n";} + } +} + + +# Descriptions: tail log. +# Arguments: OBJ($self) OBJ($curproc) STR($log_file) HASH_REF($sl_args) +# Side Effects: none +# Return Value: none +sub _show_log_tail { my ($self, $curproc, $log_file, $sl_args) = @_; my $is_cgi = 1 if $sl_args->{ printing_style } eq 'html'; my $line_count = 0; my $line_max = 0; - my $charset = $curproc->langinfo_get_charset($is_cgi ? "cgi" : "log_file"); + my $charset = + $curproc->langinfo_get_charset($is_cgi ? "cgi" : "log_file"); # run "tail -100 log" by default. my $config = $curproc->config(); @@ -163,6 +245,21 @@ sub _html_to_text } +# Descriptions: return date string YY/MM/DD. +# Arguments: OBJ($self) NUM($when) +# Side Effects: none +# Return Value: STR +sub _log_date_string +{ + my ($self, $when) = @_; + + use Mail::Message::Date; + my $date = new Mail::Message::Date; + my $log_date = $date->log_file_style($when); + return (split(/\s+/, $log_date))[0]; +} + + =head1 CODING STYLE See C on fml coding style guide. -- cgit v1.2.1