1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 Dpkg::ErrorHandling - handle error conditions
22 This module provides functions to handle all reporting and error handling.
24 B<Note>: This is a private module, its API can change at any time.
28 package Dpkg
::ErrorHandling
0.02;
32 use feature
qw(state);
61 use Exporter
qw(import);
66 my $quiet_warnings = 0;
68 my $info_fh = \
*STDOUT
;
72 my $mode = $ENV{'DPKG_COLORS'} // 'auto';
75 if ($mode eq 'auto') {
76 ## no critic (InputOutput::ProhibitInteractiveTest)
77 $use_color = 1 if -t
*STDOUT
or -t
*STDERR
;
78 } elsif ($mode eq 'always') {
84 require Term
::ANSIColor
if $use_color;
99 REPORT_PROGNAME
() => {
102 REPORT_COMMAND
() => {
103 color
=> 'bold magenta',
107 # We do not translate this name because the untranslated output is
108 # part of the interface.
113 # We do not translate this name because it is a developer interface
114 # and all debug messages are untranslated anyway.
123 name
=> g_
('notice'),
126 color
=> 'bold yellow',
127 name
=> g_
('warning'),
139 if (exists $options{quiet_warnings
}) {
140 $quiet_warnings = $options{quiet_warnings
};
142 if (exists $options{debug_level
}) {
143 $debug_level = $options{debug_level
};
145 if (exists $options{info_fh
}) {
146 $info_fh = $options{info_fh
};
154 return $report_mode{$type}{name
} // '';
161 return $report_mode{$type}{color
} // 'clear';
166 my ($msg, $color) = @_;
168 state $use_color = setup_color
();
171 return Term
::ANSIColor
::colored
($msg, $color);
179 return report_pretty
("$Dpkg::PROGNAME: ", report_color
(REPORT_PROGNAME
));
186 return report_pretty
(report_name
($type), report_color
($type));
191 my ($type, $msg, @args) = @_;
193 $msg = sprintf $msg, @args if @args;
195 my $progname = _progname_prefix
();
196 my $typename = _typename_prefix
($type);
198 return "$progname$typename: $msg\n";
203 my ($level, @args) = @_;
205 print report
(REPORT_DEBUG
, @args) if $level <= $debug_level;
212 print { $info_fh } report
(REPORT_INFO
, @args) if not $quiet_warnings;
219 warn report
(REPORT_NOTICE
, @args) if not $quiet_warnings;
226 warn report
(REPORT_WARN
, @args) if not $quiet_warnings;
231 my ($msg, @args) = @_;
233 die report
(REPORT_ERROR
, "$msg: $!", @args);
240 die report
(REPORT_ERROR
, @args);
247 print { *STDERR
} report
(REPORT_ERROR
, @args);
254 print { *STDERR
} report_pretty
(" @cmd\n", report_color
(REPORT_COMMAND
));
261 $p = sprintf $p, @args if @args;
265 if (POSIX
::WIFEXITED
($?
)) {
266 my $ret = POSIX
::WEXITSTATUS
($?
);
267 error
(g_
('%s subprocess returned exit status %d'), $p, $ret);
268 } elsif (POSIX
::WIFSIGNALED
($?
)) {
269 my $sig = POSIX
::WTERMSIG
($?
);
270 error
(g_
('%s subprocess was killed by signal %d'), $p, $sig);
272 error
(g_
('%s subprocess failed with unknown status code %d'), $p, $?
);
278 my ($msg, @args) = @_;
280 state $printforhelp = g_
('Use --help for program usage information.');
282 $msg = sprintf $msg, @args if @args;
283 warn report
(REPORT_ERROR
, $msg);
284 warn "\n$printforhelp\n";
292 This is a private module.