note
codeacrobat
What I want:
<li>read unbuffered from a child process ( long running process ).
</li>
<li>capture childprocess stderr & stdout separately
</li>
<li>write the childprocess stdout and stderr stream to a logfile and tag lines with a <timestamp> and a tag <INFO> or <ERROR> depending on the source stream.
</li>
IPC::Run3 almost does what I need.
<code>
#!/usr/bin/perl
# timestamp.pl
$|++;
while(time - $^T<10){
select undef,undef,undef, 0.1;
if ( rand()<0.5) {
print STDERR time(), " warn\n";
} else {
print time(), " info\n";
}
}
__END__
</code>
<code>
use IPC::Run3 qw(run3);
my @cmd = ('perl', 'timestamp.pl');
run3 \@cmd, undef, \&out, \&err;
sub out {
print time(), " out: $_\n";
}
sub err {
print time(), " err: $_\n";
}
__END__
# The problem is that run3 buffers the output.
1215162770 out: 1215162760 info
1215162770 out: 1215162760 info
1215162770 out: 1215162761 info
1215162770 out: 1215162761 info
1215162770 out: 1215162761 info
1215162770 out: 1215162761 info
1215162770 out: 1215162762 info
1215162770 out: 1215162762 info
1215162770 out: 1215162762 info
1215162770 out: 1215162763 info
</code>
<div class="pmsig"><div class="pmsig-507953">
<blockquote>
<hr/>
<code>print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});</code>
</blockquote>
</div></div>
695360
695512