summaryrefslogtreecommitdiffstats
path: root/src/windows/build/tee.pl
blob: 2c33370af0aa39120ca7871e7197331f536e8b4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Usage 'tee filename'
# Make sure that when using this as a perl pipe you
# print a EOF char!
#  (This may be a bug in perl 4 for NT)
#
# Use it like:
#	open(PIPE, "|$^X tee.pl foo.log") || die "Can't pipe";
#	open(STDOUT, ">&PIPE") || die "Can't dup pipe to stdout";
#	open(STDERR, ">&PIPE") || die "Can't dup pipe to stderr";

use IO::File;

#$SIG{'INT'} = \&handler;
#$SIG{'QUIT'} = \&handler;

$SIG{'INT'} = 'IGNORE';
$SIG{'QUIT'} = \&handler;

my $fh = new IO::File;

my $arg = shift;
my $file;
my $access = ">";

while ($arg) {
    if ($arg =~ /-a/) {
	$access = ">>";
    } elsif ($arg =~ /-i/) {
	$SIG{'INT'} = 'IGNORE';
	$SIG{'QUIT'} = 'IGNORE';
    } else {
	$file = $arg;
	last;
    }
    $arg = shift;
}

STDOUT->autoflush(1);

if ($file) {
    $fh->open($access.$file) || die "Could not open $file\n";
    $fh->autoflush(1);
}

while (<>) {
    $_ = &logtime.$_;
    print $_;
    print $fh $_ if $file;
}

sub logtime {
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    $mon = $mon + 1;
    $year %= 100;
    sprintf ("[%02d/%02d/%02d %02d:%02d:%02d] ",
	     $year, $mon, $mday,
	     $hour, $min, $sec);
}

sub handler {
    my $sig = shift;
    my $bailmsg = &logtime."Bailing out due to SIG$sig!\n";
    my $warnmsg = <<EOH;
*********************************
* FUTURE BUILDS MAY FAIL UNLESS *
* BUILD DIRECTORIES ARE CLEANED *
*********************************
EOH
    print $bailmsg, $warnmsg;
    print $fh $bailmsg, $warnmsg;
    print "Closing log...";
    undef $fh if $fh;
    print "closed!\n";
    exit(2);
}

END {
    undef $fh if $fh;
}