2
# LogBase.pm: logging library (base functionality) for sbuild
3
# Copyright © 2005 Ryan Murray <rmurray@debian.org>
4
# Copyright © 2005-2009 Roger Leigh <rleigh@debian.org>
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 2 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
# General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see
18
# <http://www.gnu.org/licenses/>.
20
#######################################################################
22
package Sbuild::LogBase;
31
our $saved_stdout = undef;
32
our $saved_stderr = undef;
36
our (@ISA, @EXPORT_OK);
40
@EXPORT_OK = qw(open_log close_log $log $saved_stdout $saved_stderr);
45
my $log_file = shift; # File to log to
46
my $logfunc = shift; # Function to handle logging
48
if (!defined($logfunc)) {
53
print $log_file $message;
57
$log_file->autoflush(1) if defined($log_file);
60
($pid = open($log, "|-"));
62
warn "Cannot open pipe to log: $!\n";
65
# We ignore SIG(INT|QUIT|TERM) because they will be caught in
66
# the parent which will subsequently close the logging stream
67
# resulting in our termination. This is needed to ensure the
68
# final log messages are sent and the parent doesn't die with
70
$SIG{'INT'} = 'IGNORE';
71
$SIG{'QUIT'} = 'IGNORE';
72
$SIG{'TERM'} = 'IGNORE';
74
$logfunc->($log_file, $_)
75
if (!$conf->get('NOLOG') && defined($log_file));
76
$logfunc->(\*STDOUT, $_)
77
if ($conf->get('VERBOSE'));
83
undef $log_file; # Close in parent
84
$log->autoflush(1); # Automatically flush
85
select($log); # It's the default stream
87
open($saved_stdout, ">&STDOUT") or warn "Can't redirect stdout\n";
88
open($saved_stderr, ">&STDERR") or warn "Can't redirect stderr\n";
89
open(STDOUT, '>&', $log) or warn "Can't redirect stdout\n";
90
open(STDERR, '>&', $log) or warn "Can't redirect stderr\n";
98
# Note: It's imperative to close and reopen in the exact order in
99
# which we originally opened and reopened, or else we can deadlock
100
# in wait4 when closing the log stream due to waiting on the child
102
open(STDERR, '>&', $saved_stderr) or warn "Can't redirect stderr\n"
103
if defined($saved_stderr);
104
open(STDOUT, '>&', $saved_stdout) or warn "Can't redirect stdout\n"
105
if defined($saved_stdout);
106
$saved_stderr->close();
108
$saved_stdout->close();