Re: Glib::IO->add_watch and log files

On Mar 19, 2007, at 10:50 PM, Daniel Kasak wrote:

open LOGFILE, "<", "$self->{globals}->{appdir}/app.log";
$| = 1;

Glib::IO->add_watch ( fileno(LOGFILE), ['in', 'hup'], sub {
    my ($fileno, $condition) = @_;
    if ($condition eq 'hup') {

You should check for $condition >= 'hup', not simply eq 'hup', and should always do this check *after* reading any available input. Reason: you can get 'hup' and 'in' on the same callback, and if you do the hup check first in that situation, you'll silently drop the last chunk of input.

        warn "done\n";
        close LOGFILE;
        return 0;  # uninstall
    my $line;
    sysread LOGFILE, $line, 1024;

A very quick experiment with your code shows that sysread is simply returning 0 here for all reads after reaching the end of the file. It appears that you simply don't get a 'hup' at the end of input when reading a local disk file.

I think that you want to add this line here:

       my $nread = sysread LOGFILE, $line, 1024;
return 0 unless $nread; # quit if we've reached the end of the file

    $buffer->insert($buffer->get_end_iter, $line);
    return 1;

Now, if what you're wanting to do is show new input when it arrives, a la "tail" in follow mode... well, from a quick glance at the source to GNU tail, that appears to be done by polling the file system for size changes.

If the monkey could type one keystroke every nanosecond, the expected waiting time until the monkey types out Hamlet is so long that the estimated age of the universe is insignificant by comparison ... this is not a practical method for writing plays.
  -- Gian-Carlo Rota

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]