Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
XP is just a number
 
PerlMonks  

Logfile Viewer in Perl Tk

by hackdaddy (Hermit)
on Apr 16, 2002 at 22:28 UTC ( [id://159703]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

hackdaddy has asked for the wisdom of the Perl Monks concerning the following question:

I would like to create a logfile viewer in Perl Tk that can use a pipe to get input from another program's STDOUT. For instance:
$> tail -f logfile | logfileviewer
- or -
$> ls -l | logfileviewer
I have a while loop to get <STDIN> as seen below:
#!/usr/local/bin/perl -w use Tk; use Tk::HList; use Tk::ItemStyle; $|=1; ######################## # Text Window my $mw2 = MainWindow->new; $mw2->title("LogViewer"); my $tex = $mw2->Scrolled("Text", -background => 'grey', -foreground => 'white' )->pack( -fill => 'both', -expand => 1, ); #create text tag $tex->tagConfigure('red', -foreground => 'red' ); Tk::MainLoop; sub DisplayItem { print "Called DisplayItem\n"; $tex->insert("end", "$_[0]", 'red' ); } while( <STDIN> ) { print "While called with: $_\n"; chomp; &DisplayItem( $_ ); } # end while
However, I only get the following error when closing the MainWindow:
While called with: random text in file Called DisplayItem Failed to AUTOLOAD 'Tk::Frame::insert' at logviewer line 37
Any assistance would be appreciated. Thanks.

Replies are listed 'Best First'.
Re: Logfile Viewer in Perl Tk
by the_slycer (Chaplain) on Apr 17, 2002 at 00:27 UTC
    Tk has a function built in called fileevent that is made for stuff like this.

    Here's an example pretty much right out of "Learning Perl/Tk" from O'Reilly.
    use Tk; my $mw = MainWindow->new(); my $text = $mw->Scrolled("Text", -width => 80, -height => 25)->pack(-expand => 1); $mw->fileevent(STDIN, 'readable', [\&insert_text]); MainLoop; sub insert_text { my $curline=<STDIN>; $text->insert('end', $curline); }
    That seems to work very nicely with a tail command piping output to the script. The other option of course, would be to open tail as a filehandle, like:
    open(FH,"tail -f -n 1 logifle") || die "Could not tail: $!";
    Then use <FH> instead of <STDIN> in the above.

    P.S.
    My wife now hates you, I've often wondered how to do similar things it Tk, but never had a driving need. Now that I looked up an answer to your question, I might just start messing around with Tk again (I can see lots of possibilites!)
      Thanks, slycer. I found the example in "Learning Perl/Tk" that you referenced.

      The fileevent method will come in handy.

      By the way, this can be a nice tool using the HList widget because you can format list items with different styles.

      One of the things that comes to mind is showing errors in bright red or allowing different levels of debug logging to be shown. Or you could have an email if a certain string is found in a log file.

      P.S.
      My wife hates you now, too.

      Please add any ideas you might have about this tool. Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://159703]
Approved by VSarkiss
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.