]> git.pld-linux.org Git - packages/rpm-build-tools.git/blob - teeboth
always append; allow empty logfile (it will just colorise stderr then)
[packages/rpm-build-tools.git] / teeboth
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Fcntl;
6 use POSIX ":sys_wait_h";
7 use IPC::Open3;
8 use IO::Handle;
9 use IO::Select;
10
11 my $out = shift @ARGV;
12 die unless @ARGV;
13
14 open my $fout, ">>", $out or die if $out;
15
16 my $select = IO::Select->new();
17 my $alive = 1;
18 my $pid;
19
20 my $code;
21 sub sigchld
22 {
23         my $kid;
24         do {
25                 $kid = waitpid( -1, WNOHANG );
26                 if ( $kid == $pid ) {
27                         $code = $? >> 8;
28                         $alive = 0
29                 }
30         } while ( $kid > 0 );
31 }
32 $SIG{CHLD} = \&sigchld;
33
34 $pid = open3( \*child_in, \*child_out, \*child_err, @ARGV );
35 close child_in;
36
37 sub sethandle
38 {
39         my $h = shift;
40         my $flags = 0;
41
42         fcntl ( $h, F_GETFL, $flags )
43                 or die "Couldn't get flags for HANDLE : $!\n";
44         $flags |= O_NONBLOCK;
45         fcntl ( $h, F_SETFL, $flags )
46                 or die "Couldn't set flags for HANDLE: $!\n";
47
48         $select->add( $h );
49 }
50
51 sethandle( \*child_out );
52 sethandle( \*child_err );
53
54 while ( $alive ) {
55         foreach my $h ( $select->can_read() ) {
56                 sysread $h, $_, 1024;
57                 print $fout $_ if $fout;
58                 if ( $h == \*child_err ) {
59                         print "\033[31m$_\033[0m";
60                 } else {
61                         print $_;
62                 }
63                 STDOUT->flush();
64         }
65 }
66
67 exit $code;
This page took 0.227497 seconds and 4 git commands to generate.