Glib::IO->add_watch

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Glib::IO->add_watch

Gtk+ - Perl mailing list
Hi all. I'm trying to adapt some code I have for tailing the output of
an app. When I fork using open() ... things work ( I have some issues
with tail never exiting, but that's to be expected ). When I open a
filehandle for reading these redirected log files however, my callback
I passed to add_watch() gets called in a busy loop, and nothing else
happens. I assume I'm just doing something simple wrong? What is that
thing? :) Thanks ...

Dan

---

# Redirect STDOUT / STDERR to log files
my $app_log_path = "$logdir/app_" . $timestamp . ".log";
my $err_log_path = "$logdir/err_" . $timestamp . ".log";

my ( $STDOUT_READER , $STDERR_READER );

say( "Redirecting:\nSTDOUT: $app_log_path\nSTDERR: $err_log_path" );
open STDOUT, '>', $app_log_path or die "Can't redirect STDOUT: $!";
open STDERR, '>', $err_log_path or die "Can't redirect STDERR: $!";

# open $STDOUT_READER , "<" , $app_log_path or die( "Can't open stdout
log file!: $!" );
open( $STDOUT_READER, "tail -f $app_log_path |" )
    || die( "Can't fork!\n" . $! );

# open $STDERR_READER , "<" , $err_log_path or die( "Can't open stderr
log file!: $!" );
open( $STDERR_READER, "tail -f $err_log_path |" )
    || die( "Can't fork!\n" . $! );

Glib::IO->add_watch( fileno( $STDOUT_READER ) , ['in'] , sub {
    my ( $fileno, $condition ) = @_;
    # parse logs and write to textview
}
_______________________________________________
gtk-perl-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-perl-list
Reply | Threaded
Open this post in threaded view
|

Re: Glib::IO->add_watch

Gtk+ - Perl mailing list
On 03/04/2019 00:35, Daniel Kasak via gtk-perl-list wrote:
> Hi all. I'm trying to adapt some code I have for tailing the output of
> an app. When I fork using open() ... things work ( I have some issues
> with tail never exiting, but that's to be expected ). When I open a
> filehandle for reading these redirected log files however, my callback
> I passed to add_watch() gets called in a busy loop, and nothing else
> happens. I assume I'm just doing something simple wrong? What is that
> thing? :) Thanks ...

The _watch_cmd() sub in gscan2pdf does what you want - uses
Glib::IO->add_watch() on stdout and stderr and triggers a callback on
every line read:

https://sourceforge.net/p/gscan2pdf/code/ci/master/tree/lib/Gscan2pdf/Frontend/CLI.pm#l504

Regards

Jeff


_______________________________________________
gtk-perl-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-perl-list

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Glib::IO->add_watch

Gtk+ - Perl mailing list
On Thu, Apr 4, 2019 at 6:45 AM Jeff via gtk-perl-list
<[hidden email]> wrote:

>
> On 03/04/2019 00:35, Daniel Kasak via gtk-perl-list wrote:
> > Hi all. I'm trying to adapt some code I have for tailing the output of
> > an app. When I fork using open() ... things work ( I have some issues
> > with tail never exiting, but that's to be expected ). When I open a
> > filehandle for reading these redirected log files however, my callback
> > I passed to add_watch() gets called in a busy loop, and nothing else
> > happens. I assume I'm just doing something simple wrong? What is that
> > thing? :) Thanks ...
>
> The _watch_cmd() sub in gscan2pdf does what you want - uses
> Glib::IO->add_watch() on stdout and stderr and triggers a callback on
> every line read:
>
> https://sourceforge.net/p/gscan2pdf/code/ci/master/tree/lib/Gscan2pdf/Frontend/CLI.pm#l504

Thanks for the response Jeff ... but the example you linked is very
similar to what I have ( in another codebase ) that works. ie I don't
have problems with this method when *forking* a process - it works
really well. I have problems with this method when I want to tail ( or
use Glib::IO->add_watch() on ) STDOUT & STDERR of the *current*
process.

Dan
_______________________________________________
gtk-perl-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-perl-list