File: LogBase.pm

package info (click to toggle)
sbuild 0.91.5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,832 kB
  • sloc: perl: 18,718; sh: 1,210; python: 823; sql: 797; lisp: 304; makefile: 293
file content (117 lines) | stat: -rw-r--r-- 3,300 bytes parent folder | download | duplicates (5)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#
# LogBase.pm: logging library (base functionality) for sbuild
# Copyright © 2005      Ryan Murray <rmurray@debian.org>
# Copyright © 2005-2009 Roger Leigh <rleigh@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
#######################################################################

package Sbuild::LogBase;

use strict;
use warnings;
use English;

sub open_log ($$$);
sub close_log ($);

our $log          = undef;
our $saved_stdout = undef;
our $saved_stderr = undef;

BEGIN {
	use Exporter ();
	our (@ISA, @EXPORT_OK);

	@ISA = qw(Exporter);

	@EXPORT_OK = qw(open_log close_log $log $saved_stdout $saved_stderr);
}

sub open_log ($$$) {
	my $conf     = shift;
	my $log_file = shift;    # File to log to
	my $logfunc  = shift;    # Function to handle logging

	if (!defined($logfunc)) {
		$logfunc = sub {
			my $log_file = shift;
			my $message  = shift;

			print $log_file $message;
		}
	}

	$log_file->autoflush(1) if defined($log_file);

	my $pid;
	($pid = open($log, "|-"));
	if (!defined $pid) {
		warn "Cannot open pipe to log: $!\n";
	} elsif ($pid == 0) {
		# We ignore SIG(INT|QUIT|TERM) because they will be caught in
		# the parent which will subsequently close the logging stream
		# resulting in our termination.  This is needed to ensure the
		# final log messages are sent and the parent doesn't die with
		# SIGPIPE.
		$SIG{'INT'}   = 'IGNORE';
		$SIG{'QUIT'}  = 'IGNORE';
		$SIG{'TERM'}  = 'IGNORE';
		$PROGRAM_NAME = 'main log for ' . $PROGRAM_NAME;
		while (<STDIN>) {
			$logfunc->($log_file, $_)
			  if (!$conf->get('NOLOG') && defined($log_file));
			$logfunc->(\*STDOUT, $_)
			  if ($conf->get('VERBOSE'));
		}
		undef $log_file;
		exit 0;
	}

	undef $log_file;       # Close in parent
	$log->autoflush(1);    # Automatically flush
	select($log);          # It's the default stream

	open($saved_stdout, ">&STDOUT") or warn "Can't redirect stdout\n";
	open($saved_stderr, ">&STDERR") or warn "Can't redirect stderr\n";
	open(STDOUT, '>&', $log)        or warn "Can't redirect stdout\n";
	open(STDERR, '>&', $log)        or warn "Can't redirect stderr\n";

	return $log;
}

sub close_log ($) {
	my $conf = shift;

	# Note: It's imperative to close and reopen in the exact order in
	# which we originally opened and reopened, or else we can deadlock
	# in wait4 when closing the log stream due to waiting on the child
	# forever.
	open(STDERR, '>&', $saved_stderr)
	  or warn "Can't redirect stderr\n"
	  if defined($saved_stderr);
	open(STDOUT, '>&', $saved_stdout)
	  or warn "Can't redirect stdout\n"
	  if defined($saved_stdout);
	$saved_stderr->close();
	undef $saved_stderr;
	$saved_stdout->close();
	undef $saved_stdout;
	$log->close();
	undef $log;
}

1;