# HG changeset patch # User Heiko Schlittermann # Date 1233172573 -3600 # Node ID 066e61c3785b5be93960dfff213c9b62a564cd36 # Parent b43a7dc491c27f30184d746bb7168d6b2309fe8d Added 'tag'-feature. diff -r b43a7dc491c2 -r 066e61c3785b hlog --- a/hlog Wed Jan 28 20:29:46 2009 +0100 +++ b/hlog Wed Jan 28 20:56:13 2009 +0100 @@ -29,7 +29,7 @@ my $opt_port = 8080; my $opt_lines = 10; my $logfile = "hlog.log"; -my $FILE; +my %FILE; sub handle_request($); sub date1123(;$); @@ -49,7 +49,15 @@ open(LOG, ">>$logfile"); print LOG localtime() . " started\n"; - $FILE = shift or pod2usage(); + pod2usage() if not @ARGV; + + foreach (@ARGV) { + $_ = "default=$_" if not /=/ or /^\//; + my ($tag, $file) = split /=/, $_, 2; + die "tag $tag already exists with file $FILE{$tag}\n" + if exists $FILE{$tag}; + $FILE{$tag} = $file; + } my $listener = new IO::Socket::INET( LocalAddr => $opt_addr, @@ -60,16 +68,16 @@ ) or die "Can't create listener socket: $!\n"; $SIG{CHLD} = sub { - while ((my $pid = waitpid(-1, WNOHANG)) > 0) { - print LOG localtime() . " child $pid died\n"; - } + while ((my $pid = waitpid(-1, WNOHANG)) > 0) { + print LOG localtime() . " child $pid died\n"; + } }; warn "listener $$ $opt_addr:$opt_port\n"; while (1) { - my $client = $listener->accept; - next if not defined $client; # may be because of signal + my $client = $listener->accept; + next if not defined $client; # may be because of signal print LOG $_ = localtime() @@ -82,7 +90,7 @@ my $pid = fork(); die "Can't fork: $!\n" if not defined $pid; if ($pid == 0) { - $SIG{CHLD} = "DEFAULT"; + $SIG{CHLD} = "DEFAULT"; $listener->close; handle_request($client); exit 0; @@ -107,16 +115,23 @@ $client->print(http "400 Bad Request" => bad_request); } - my $lines = /(\d+)$/ ? $1 : $opt_lines; + # number of lines and tag to show + my $lines = (s/(\d+)$// ? $1 : $opt_lines); + my $tag = (s/^\/*(\w+)// ? $1 : "default"); # read the header(s) and discard while (<$client>) { last if /^\s*$/ } - # number of lines to show + if (not exists $FILE{$tag}) { + $client->print(http "500 unknown file tag", + "Sorry, unknown file tag \"$tag\""); + print LOG " unknown tag $tag"; + return; + } - my %file = analyze($FILE); + my %file = analyze($FILE{$tag}); if (!%file) { - $client->print(http "500 internal error"); + $client->print(http "500 internal error", "internal error"); print LOG $@; return; } @@ -214,6 +229,7 @@ =head1 SYNOPSIS hlog [-p|--port port] [-a|--address address] [--lines n] file + hlog [-p|--port port] [-a|--address address] [--lines n] tag=file ... hlog [-h|--help] [-m|--man] =head1 DESCRIPTION @@ -239,4 +255,19 @@ =back +=head1 EXAMPLES + +Using tags makes it possible to access more then one log file +via the same running instance by specifying the tag in the URL. + +Once started as: + + hlog error=/var/log/apache/error.log access=/var/log/apache/access.log + +The following URLs are valid: + + http://:8080/error + http://:8080/access?10 + + =cut