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/>.
14 package Dpkg::ErrorHandling;
19 our $VERSION = '0.02';
47 use Exporter qw(import);
53 my $quiet_warnings = 0;
55 my $info_fh = \*STDOUT;
60 my $mode = $ENV{'DPKG_COLORS'} // 'auto';
62 if ($mode eq 'auto') {
63 ## no critic (InputOutput::ProhibitInteractiveTest)
64 $use_color = 1 if -t *STDOUT or -t *STDERR;
65 } elsif ($mode eq 'always') {
86 REPORT_PROGNAME() => {
90 color => 'bold magenta',
94 # We do not translate this name because the untranslated output is
95 # part of the interface.
100 # We do not translate this name because it is a developer interface
101 # and all debug messages are untranslated anyway.
110 name => g_('notice'),
113 color => 'bold yellow',
114 name => g_('warning'),
126 if (exists $options{quiet_warnings}) {
127 $quiet_warnings = $options{quiet_warnings};
129 if (exists $options{debug_level}) {
130 $debug_level = $options{debug_level};
132 if (exists $options{info_fh}) {
133 $info_fh = $options{info_fh};
141 return $report_mode{$type}{name} // '';
148 return $report_mode{$type}{color} // 'clear';
153 my ($msg, $color) = @_;
156 return colored($msg, $color);
164 return report_pretty("$Dpkg::PROGNAME: ", report_color(REPORT_PROGNAME));
171 return report_pretty(report_name($type), report_color($type));
176 my ($type, $msg) = (shift, shift);
178 $msg = sprintf($msg, @_) if (@_);
180 my $progname = _progname_prefix();
181 my $typename = _typename_prefix($type);
183 return "$progname$typename: $msg\n";
189 print report(REPORT_DEBUG, @_) if $level <= $debug_level;
194 print { $info_fh } report(REPORT_INFO, @_) if not $quiet_warnings;
199 warn report(REPORT_NOTICE, @_) if not $quiet_warnings;
204 warn report(REPORT_WARN, @_) if not $quiet_warnings;
210 die report(REPORT_ERROR, "$msg: $!", @_);
215 die report(REPORT_ERROR, @_);
220 print { *STDERR } report(REPORT_ERROR, @_);
227 print { *STDERR } report_pretty(" @cmd\n", report_color(REPORT_COMMAND));
234 $p = sprintf($p, @_) if (@_);
238 if (POSIX::WIFEXITED($?)) {
239 error(g_('%s gave error exit status %s'), $p, POSIX::WEXITSTATUS($?));
240 } elsif (POSIX::WIFSIGNALED($?)) {
241 error(g_('%s died from signal %s'), $p, POSIX::WTERMSIG($?));
243 error(g_('%s failed with unknown exit code %d'), $p, $?);
247 my $printforhelp = g_('Use --help for program usage information.');
253 $msg = sprintf($msg, @_) if (@_);
254 warn report(REPORT_ERROR, $msg);
255 warn "\n$printforhelp\n";