in reply to Re: How to log all output from a program?
in thread How to log all output from a program?

you can do he same from perl:
my $pid = open STDOUT, '|-'; unless ($pid) { defined $pid or die "unable to fork new process: $!"; open my $tee, '>>', $stdout_log_fn or die "unable to open '$stdout_log_fn'"; select $tee; $| = 1; select STDOUT; $| = 1; while (<>) { print $tee "OUT $_"; print $_; } exit(0); } # and repeat for STDERR

Replies are listed 'Best First'.
Re^3: How to log all output from a program?
by shmem (Chancellor) on Oct 15, 2007 at 08:50 UTC
    Ah, of course. But you also can do that from the outside :-)

    BTW, if you open/close the filehandle inside the loop (and perhaps include $$ in the output) it is less probable that multiple processes mess up the logfiles.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      BTW, if you open/close the filehandle inside the loop (and perhaps include $$ in the output) it is less probable that multiple processes mess up the logfiles

      I don't think so. Usually, the OS provides atomic write operations up to a certain length. If your write operations go over that limit, the way to ensure that data from different processes does not interleave is to lock the file.

      Reopening the file on every write allows to rotate the logs from the outside without the current script knowing.

Re^3: How to log all output from a program?
by salva (Canon) on Oct 16, 2007 at 21:42 UTC
    BTW, I have just released to CPAN File-Tee that's an augmented version of the code above.