# HG changeset patch # User Heiko Schlittermann # Date 1233172851 -3600 # Node ID ce482bda336e0640dbe964f6d0376014c07208b3 # Parent 9166982f8b19b2b57f58dc0b55956ec05384758e# Parent f79215d95c42638c76f8d0aa1c52997c2c2c5bf1 [merged] tag feature diff -r 9166982f8b19 -r ce482bda336e .hgtags --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgtags Wed Jan 28 21:00:51 2009 +0100 @@ -0,0 +1,4 @@ +b3cf6e84d2c6151e7e777d1aced93c0572ff04f3 stable +066e61c3785b5be93960dfff213c9b62a564cd36 stable +066e61c3785b5be93960dfff213c9b62a564cd36 [tagged] as stable +0000000000000000000000000000000000000000 [tagged] as stable diff -r 9166982f8b19 -r ce482bda336e Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Wed Jan 28 21:00:51 2009 +0100 @@ -0,0 +1,27 @@ + +DESTDIR = +prefix = /usr/local +bindir = ${prefix}/bin +man1dir = ${prefix}/share/man/man1 + +.PHONY: all clean install + +SCRIPT = hlog +MAN1 = $(SCRIPT:=.1.gz) + +CLEANFILES \ + = $(MAN1) + +all: $(MAN1) +install: all + # bin + install -m 0755 -d ${DESTDIR}${bindir} + install -m 0755 $(SCRIPT) ${DESTDIR}${bindir}/ + # man + install -m 0755 -d ${DESTDIR}${man1dir} + install -m 0644 $(MAN1) ${DESTDIR}${man1dir}/ + +clean: ; -rm -f $(CLEANFILES) + +%.1.gz: % ; pod2man $< | gzip >$@ + diff -r 9166982f8b19 -r ce482bda336e hlog --- a/hlog Wed Jan 28 18:57:56 2009 +0100 +++ b/hlog Wed Jan 28 21:00:51 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(;$); @@ -51,7 +51,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, @@ -62,16 +70,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() @@ -84,7 +92,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; @@ -109,16 +117,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; } @@ -243,11 +258,12 @@ =head1 NAME - hlog - simple http server providing access to some logfile +hlog - simple http server providing access to some logfile =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 @@ -273,4 +289,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