Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I wanted to learn a little more about CGIs and Cookies. This is the result. Any comments are welcome.

Error lines are wrapped with Text::Wrap. Each time the script is loaded, it tries to set a cookie containing the last line read in the error log. Each load of the script accesses the cookie and only displays unread lines.

# usage: # http://host/path/velog.cgi will show the lines in the error log # you haven't seen yet. # http://host/path/velog.cgi?all=234 will show all the lines in the
Update: I've taken some suggestions and made some changes. I'm much happier with the script now. Thanks all. I've posted the updated version as a reply for anyone who might be interested.
#!perl -wT #config variables my $logfile = q(D:\httpd\Apache\logs\error.log); my $initial = ""; #first line of each error my $subseq = ""; #additional lines for each error my $domain = ""; my $path = ""; my $expires = ""; my $cookiename = "veloglastline"; use strict; use CGI; use Text::Wrap; #returns a CGI::cookie that looks something like veloglastline=#### #where #### is the last line of the error log viewed. sub makecookie { my $q = shift; my $line = shift; my $cookie = $q->cookie( { -name => $cookiename, -value => $line, -domain => $domain, -expires => $expires, -path => $path } ); return $cookie; } #checkes for a CGI::cookie that looks something like veloglastline=### +# #where #### is the last line of the error log viewed. #defaults to 0 (ie, show the entire error log) sub getlastlineviewed { my $q = shift; my $cookie = $q->cookie($cookiename) || 0; my $line; if($cookie =~ m/^(\d*)$/) { $line = $1; }else{ $line = 0; } return $line; } #gets the last $lastline lines from the errorlog, and returns a REFERE +NCE #to an array of the last lines. sub getlogtext { my $q = shift; my $lastline = shift; my @lines; open (ELOG, "<$logfile") or die "unexpected error opening error log for reading: +$!\n"; #intentionally toss away these lines. <ELOG> while((!eof(ELOG)) && ($. < $lastline)); if(eof) { @lines = ("No unread lines found"); }else { @lines = <ELOG>; #slurp in the rest of the li +nes. } close ELOG; return \@lines; #return a REFERENCE to the a +rray! } #takes a reference to a CGI object, the total number of lines in the e +rrorlog #and a reference to an array of text. Sets a cookie with the number of + lines #read along, and prints the text inside <pre></pre> tags. #this may change to plain text in the near future. sub printerrorlog { my $q = shift; my $lastline = shift; my $text = shift; my $cookie = makecookie($q, $lastline); print $q->header(-type => "text/plain", -cookie => $cookie), wrap($initial, $subseq, @$text); } my $q = CGI->new(); my $startline; my $lastline; my $logtext; $startline = getlastlineviewed($q); $startline=0 if(defined($q->param("all"))); $logtext = getlogtext($q, $startline); $lastline = $startline + $#$logtext; printerrorlog($q, $lastline, $logtext);

In reply to Simple Apache Error Log Viewer by coolmichael

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-16 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found