~ubuntu-branches/ubuntu/karmic/sbuild/karmic-proposed

« back to all changes in this revision

Viewing changes to lib/Sbuild/LogBase.pm

  • Committer: Bazaar Package Importer
  • Author(s): Roger Leigh, Roger Leigh
  • Date: 2009-05-17 15:52:53 UTC
  • mfrom: (8.1.7 upstream) (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090517155253-fbxadfsyaf940ete
Tags: 0.58.3-1
[ Roger Leigh ]
* New release.
* debian/control:
  - Update to Standards Version 3.8.1.
  - Add buildd package.
  - Add libsbuild-perl package.
  - All packages depend upon libsbuild-perl.
* Add support for appending a tag to version numbers (Closes: #475777).
  Thanks to Timothy G Abbott for this patch.
* When using the --help or --version options, don't abort if not
  in the sbuild group (Closes: #523670).  Group membership is now
  only performed after options parsing, and only if required.
* Allow config files to use $HOME (Closes: #524564).  Thanks to
  James Vega for this patch.
* Restore buildd package.
* Split common library functions into new libsbuild-perl package.
* debian/sbuild.(preinst|postinst|postrm):
  - Remove special cases for versions older than oldstable.  Update
    addition and removal of sbuild group to use return value of getent
    rather than parsing getent output.
  - Use addgroup/delgroup in place of adduser/deluser.
  - Use --system when adding and deleting group, to ensure creation
    of a system group.  Migrate existing non-system group and group
    members if the system group is not present.
  - Handle removal of 50sbuild setup script.
* debian/buildd.(preinst|postinst|postrm): Add maintainer scripts for
  buildd package.  Move configuration file from /etc/buildd.conf to
  /etc/buildd/buildd.conf if present.  Also create buildd user and
  group for running the buildd daemon.
* Sbuild::Conf: Don't default MAINTAINER_NAME to $DEBEMAIL if unset
  in the configuration file (Closes: #520158).
* /etc/schroot/setup.d/50sbuild: Remove.  The setup tasks performed by
  this script are now handled internally by sbuild.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
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>
 
5
#
 
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.
 
10
#
 
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.
 
15
#
 
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/>.
 
19
#
 
20
#######################################################################
 
21
 
 
22
package Sbuild::LogBase;
 
23
 
 
24
use strict;
 
25
use warnings;
 
26
 
 
27
sub open_log ($$$);
 
28
sub close_log ($);
 
29
 
 
30
our $log = undef;
 
31
our $saved_stdout = undef;
 
32
our $saved_stderr = undef;
 
33
 
 
34
BEGIN {
 
35
    use Exporter ();
 
36
    our (@ISA, @EXPORT_OK);
 
37
 
 
38
    @ISA = qw(Exporter);
 
39
 
 
40
    @EXPORT_OK = qw(open_log close_log $log $saved_stdout $saved_stderr);
 
41
}
 
42
 
 
43
sub open_log ($$$) {
 
44
    my $conf = shift;
 
45
    my $log_file = shift; # File to log to
 
46
    my $logfunc = shift; # Function to handle logging
 
47
 
 
48
    if (!defined($logfunc)) {
 
49
        $logfunc = sub {
 
50
            my $log_file = shift;
 
51
            my $message = shift;
 
52
 
 
53
            print $log_file $message;
 
54
        }
 
55
    }
 
56
 
 
57
    $log_file->autoflush(1) if defined($log_file);
 
58
 
 
59
    my $pid;
 
60
    ($pid = open($log, "|-"));
 
61
    if (!defined $pid) {
 
62
        warn "Cannot open pipe to log: $!\n";
 
63
    }
 
64
    elsif ($pid == 0) {
 
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
 
69
        # SIGPIPE.
 
70
        $SIG{'INT'} = 'IGNORE';
 
71
        $SIG{'QUIT'} = 'IGNORE';
 
72
        $SIG{'TERM'} = 'IGNORE';
 
73
        while (<STDIN>) {
 
74
            $logfunc->($log_file, $_)
 
75
                if (!$conf->get('NOLOG') && defined($log_file));
 
76
            $logfunc->(\*STDOUT, $_)
 
77
                if ($conf->get('VERBOSE'));
 
78
        }
 
79
        undef $log_file;
 
80
        exit 0;
 
81
    }
 
82
 
 
83
    undef $log_file; # Close in parent
 
84
    $log->autoflush(1); # Automatically flush
 
85
    select($log); # It's the default stream
 
86
 
 
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";
 
91
 
 
92
    return $log;
 
93
}
 
94
 
 
95
sub close_log ($) {
 
96
    my $conf = shift;
 
97
 
 
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
 
101
    # forever.
 
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();
 
107
    undef $saved_stderr;
 
108
    $saved_stdout->close();
 
109
    undef $saved_stdout;
 
110
    $log->close();
 
111
    undef $log;
 
112
}
 
113
 
 
114
1;