CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
[SOLVED] Showing logs in forms input textarea live like tail -f

 



terrykhatri531
User

Aug 18, 2014, 11:59 AM

Post #1 of 7 (913 views)
[SOLVED] Showing logs in forms input textarea live like tail -f Can't Post

Hi Chris,

I have a script which generates reports and writes errors and execution progress to a logfile, what I want to do is to show the contents of the logs live in the forms textarea ( see the picture attached ) like tail -f style.

Is it doable ? I saw the perl module on cpan which is File::Tail :


Code
  
use File::Tail;
$file=File::Tail->new("/some/log/file");
while (defined($line=$file->read)) {
print "$line";
}


But how to print it to the textarea.

Here is form's snippet :


Code
  
print qq!
<div class="container">
<form action="" method="post">
<table cellpadding=5 cellspacing=0 border=5 bordercolor=#989898 >
<tr><td style="background-color:#C0C0C0;">
Reports Execution Logs :<textarea readonly name="logs" rows="10" cols="62"></textarea>
</td></tr></table>
<input type="submit" class="tsc_c3b_large tsc_button tsc_c3b_grey tsc_c3b_input" name="go" value="Exit" />
</form></div>
!;




Or is there some other way of doing it ?

Many many thanks !!

Terry


(This post was edited by terrykhatri531 on Aug 19, 2014, 1:57 PM)
Attachments: logs.png (7.95 KB)


Zhris
Enthusiast

Aug 18, 2014, 1:26 PM

Post #2 of 7 (908 views)
Re: [terrykhatri531] Showing logs in forms input textarea live like tail -f [In reply to] Can't Post

I don't think File::Tail will be suitable in a CGI environment as you want to wrap output with a textarea, it wouldn't reach the closing textarea tag etc until the loop has finished, perhaps some kind of fork could solve this. A similar question was asked here.

I would write some ajax that regularly calls a Perl script that uses File::Backwards to fetch the last n lines and update the textarea. Don't forget to HTML escape the contents of the textarea i.e. $cgi->escapeHTML( $textarea_contents );.

Chris


(This post was edited by Zhris on Aug 18, 2014, 1:50 PM)


terrykhatri531
User

Aug 18, 2014, 2:24 PM

Post #3 of 7 (898 views)
Re: [Zhris] Showing logs in forms input textarea live like tail -f [In reply to] Can't Post

Thanks Chris, will look into that later but for now can you please show me how to cat and print contents of an existing logfile to the textarea.

Many thanks

Terry


Zhris
Enthusiast

Aug 18, 2014, 2:28 PM

Post #4 of 7 (894 views)
Re: [terrykhatri531] Showing logs in forms input textarea live like tail -f [In reply to] Can't Post

Something like this:


Code
#!/usr/bin/perl 
use strict;
use warnings;
use CGI;

open my $fh, '<', '/some/log/file' or die "cannot open: $!";
my $string = do { local $/ = undef; <$fh> };
close $fh;

my $cgi = CGI->new;
$string = $cgi->escapeHTML($string);

print "<textarea>$string</textarea>";


Chris


(This post was edited by Zhris on Aug 18, 2014, 2:29 PM)


terrykhatri531
User

Aug 18, 2014, 3:38 PM

Post #5 of 7 (885 views)
Re: [Zhris] Showing logs in forms input textarea live like tail -f [In reply to] Can't Post


In Reply To
Something like this:


Code
#!/usr/bin/perl  
use strict;
use warnings;
use CGI;

open my $fh, '<', '/some/log/file' or die "cannot open: $!";
my $string = do { local $/ = undef; <$fh> };
close $fh;

my $cgi = CGI->new;
$string = $cgi->escapeHTML($string);

print "<textarea>$string</textarea>";


Thanks Chris, keeping in view my first comment what I want to do is to print the $string variable to an existing testarea in the form like :


Code
print qq!  
<div class="container">
<form action="" method="post">
<table cellpadding=5 cellspacing=0 border=5 bordercolor=#989898 >
<tr><td style="background-color:#C0C0C0;">
Reports Execution Logs :<textarea readonly name="logs" rows="10" cols="62">$string</textarea>
</td></tr></table>
<input type="submit" class="tsc_c3b_large tsc_button tsc_c3b_grey tsc_c3b_input" name="go" value="Exit" />
</form></div>
!;


How do I do that ?

Many thanks!!
Terry


Zhris
Enthusiast

Aug 18, 2014, 3:41 PM

Post #6 of 7 (882 views)
Re: [terrykhatri531] Showing logs in forms input textarea live like tail -f [In reply to] Can't Post

Just replace the final print in my version with your print.

By the way you can also pipe tail through your file handle:


Code
open my $fh, "tail -100 /some/log/file |" or die "cannot open: $!";


Chris


(This post was edited by Zhris on Aug 19, 2014, 5:50 AM)


terrykhatri531
User

Aug 19, 2014, 1:40 PM

Post #7 of 7 (818 views)
Re: [Zhris] Showing logs in forms input textarea live like tail -f [In reply to] Can't Post

Thanks Chris !!

I found a java plugin at http://webtailx.sourceforge.net/?id=demo that works fine.

Rgds.

Terry

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives