I'd try the following:
use IPC::Run qw( run new_chunker ); sub new_prefixer { my ($prefix) = @_; return sub { my ($in_ref, $out_ref) = @_; return input_avail && do { $$out_ref = join('', $$out_ref, $prefix, $$in_ref); $$in_ref = ''; 1 }; } } sub new_printer { my ($fh) = @_; return sub { print $fh ($_[0]); } } { my @cmd = ('find', '/fst/home/', '-name', '*ooo*'); open(my $fh_log, '>>', 'h1.log') or die("Unable to open log file: $!\n"); my $printer = new_printer($fh_log); run \@cmd, '>', \$fh_log, '2>', new_chunker(), new_prefixer('STDERR: '), $printer; or die("Error executing child. Child returned $&\n"); # or: # # run \@cmd, # '>', new_chunker(), new_prefixer('STDOUT: '), $printer, # '2>', new_chunker(), new_prefixer('STDERR: '), $printer; # or die("Error executing child. Child returned $&\n"); }
Notes:
Untested.
The above won't work (loss of sync between STDOUT and STDERR) if your application buffers STDOUT. You could try to get around that by changing '>' to '>pty>'. Read these notes for details.
May not work on Windows due to inability to select file handles. Windows might also be unable to create PTTYs.
new_chunker() assures that the prefix is added to every line.
In reply to Re: Prepending a string to STDERR output, and logging STDOUT & STDERR synchronously to a file
by ikegami
in thread Prepending a string to STDERR output, and logging STDOUT & STDERR synchronously to a file
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |