Tailing a file into x text widget 
Author Message
 Tailing a file into x text widget

Hi -

I have searched all over the internet for help on this, so please don't
just give me RTFM :P

I have a chunk of code that opens a filehandle on a tail of a logfile,
then prints the tail to a text widget in my main window.  I want this
widget to print certain lines in different colours if they have either
a) the word CRITICAL in them, or b) the word MAJOR in them.  Critical
should print in red, Major in something else.

Here is my code -

 1  #!/sbcimp/run/pkgs/gsbl/bin/perl -w
     2  #!/sbclocal/bin/perl -w
     3
     4
     5  use Tk;
     6
     7
     8
     9   #open (LOG, "tail -f commandpost.log|") || die "Nope : $!";
    10
    11
    12  my $mw = MainWindow->new();
    13
    14  my $text = $mw->Scrolled("Text",
    15       -relief => "sunken",
    16       -background => "gray60",
    17       -width => 80,
    18       -height=> 30);
    19
    20
    21     $text->pack(-side => "left", -fill => "both", -expand
=> "yes");
    22
    23     $text->tagConfigure("CRITICAL", -foreground => "red1");
    24     $text->tagConfigure("MAJOR", -foreground => "red3");
    25
    26  open (LOG, "tail -f commandpost.log|") || "Nope : $!\n";
    27
    28  $mw->fileevent(LOG, 'readable', [\&insert_text]);
    29
    30  MainLoop;
    31
    32
    33
    34  sub insert_text {
    35
    36  my $line;
    37
    38  if (defined($line = <LOG>)) {
    39
    40  if ($line =~ /CRITICAL(MAJOR)/) {
    41
    42    $text->insert('end',$line,$1);
    43    } else {
    44    $text->insert('end',$line);
    45    }
    46    }else{
    47    $mw->fileevent(\*LOG, 'readable', "");
    48    }
    49    }
    50
    51
    52
- Any ideas why this won't work?

HELP!

thanks

james

Sent via Deja.com
http://www.*-*-*.com/



Sat, 28 Jun 2003 16:51:16 GMT  
 Tailing a file into x text widget

Quote:

> Hi -

> I have searched all over the internet for help on this, so please
don't
> just give me RTFM :P

We don't use the F word here :-)

Quote:

> I have a chunk of code that opens a filehandle on a tail of a logfile,
> then prints the tail to a text widget in my main window.  I want this
> widget to print certain lines in different colours if they have either
> a) the word CRITICAL in them, or b) the word MAJOR in them.  Critical
> should print in red, Major in something else.

>     40  if ($line =~ /CRITICAL(MAJOR)/) {

Why not explicitly state it like this:

if ($line=~/(CRITICAL)/){
$text->insert('end',$line,$1);

Quote:
}

elsif($line=~/(MAJOR)/){
$text->insert('end',$line,$1);
Quote:
}

else{
$text->insert('end',$line);

Quote:
}

This works on UNIX.

Jack

Sent via Deja.com
http://www.deja.com/



Sun, 29 Jun 2003 08:31:37 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Large File edition in Text Widget

2. Loading a HUGE file in a Perl/Tk text widget

3. text widget and file handling - efficient way

4. Widget to display text file?

5. Load a file to text widget

6. Trailing file input to Tk text widget

7. Q: Changing Text-Color in Text-Widget?

8. text formatting in a Text widget

9. How update text in text widget when executing some other callback

10. Color Coding Text in a Tk::Text Widget

11. Checking for marked or selected text in a Text widget

12. Grabbing Text from Text widget

 

 
Powered by phpBB® Forum Software