| 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. | ||||||||||||||
| 5 | # | ||||||||||||||
| 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. | ||||||||||||||
| 10 | # | ||||||||||||||
| 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/>. | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =encoding utf8 | ||||||||||||||
| 15 | |||||||||||||||
| 16 | =head1 NAME | ||||||||||||||
| 17 | |||||||||||||||
| 18 | Dpkg::ErrorHandling - handle error conditions | ||||||||||||||
| 19 | |||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||
| 21 | |||||||||||||||
| 22 | This module provides functions to handle all reporting and error handling. | ||||||||||||||
| 23 | |||||||||||||||
| 24 | B<Note>: This is a private module, its API can change at any time. | ||||||||||||||
| 25 | |||||||||||||||
| 26 | =cut | ||||||||||||||
| 27 | |||||||||||||||
| 28 | package Dpkg::ErrorHandling 0.02; | ||||||||||||||
| 29 | |||||||||||||||
| 30 | 575 | ▶ | use v5.36; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | |||||||||||||||
| 32 | 573 | our @EXPORT_OK = qw( | |||||||||||||
| 33 | REPORT_PROGNAME | ||||||||||||||
| 34 | REPORT_COMMAND | ||||||||||||||
| 35 | REPORT_STATUS | ||||||||||||||
| 36 | REPORT_DEBUG | ||||||||||||||
| 37 | REPORT_HINT | ||||||||||||||
| 38 | REPORT_INFO | ||||||||||||||
| 39 | REPORT_NOTICE | ||||||||||||||
| 40 | REPORT_WARN | ||||||||||||||
| 41 | REPORT_ERROR | ||||||||||||||
| 42 | report_pretty | ||||||||||||||
| 43 | report_color | ||||||||||||||
| 44 | report | ||||||||||||||
| 45 | ); | ||||||||||||||
| 46 | 573 | our @EXPORT = qw( | |||||||||||||
| 47 | report_options | ||||||||||||||
| 48 | debug | ||||||||||||||
| 49 | hint | ||||||||||||||
| 50 | info | ||||||||||||||
| 51 | notice | ||||||||||||||
| 52 | warning | ||||||||||||||
| 53 | error | ||||||||||||||
| 54 | errormsg | ||||||||||||||
| 55 | syserr | ||||||||||||||
| 56 | printcmd | ||||||||||||||
| 57 | subprocerr | ||||||||||||||
| 58 | usageerr | ||||||||||||||
| 59 | ); | ||||||||||||||
| 60 | |||||||||||||||
| 61 | 575 | ▶ | use Exporter qw(import); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 62 | |||||||||||||||
| 63 | 575 | ▶ | use Dpkg (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 64 | 575 | ▶ | use Dpkg::Gettext; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 65 | 575 | ▶ | use Dpkg::Color; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 66 | |||||||||||||||
| 67 | 573 | my $quiet_warnings = 0; | |||||||||||||
| 68 | 573 | my $show_hints = 1; | |||||||||||||
| 69 | 573 | my $debug_level = 0; | |||||||||||||
| 70 | 573 | my $info_fh = \*STDOUT; | |||||||||||||
| 71 | |||||||||||||||
| 72 | 575 | ▶ | use constant { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 73 | 575 | REPORT_PROGNAME => 1, | |||||||||||||
| 74 | REPORT_COMMAND => 2, | ||||||||||||||
| 75 | REPORT_STATUS => 3, | ||||||||||||||
| 76 | REPORT_INFO => 4, | ||||||||||||||
| 77 | REPORT_NOTICE => 5, | ||||||||||||||
| 78 | REPORT_WARN => 6, | ||||||||||||||
| 79 | REPORT_ERROR => 7, | ||||||||||||||
| 80 | REPORT_DEBUG => 8, | ||||||||||||||
| 81 | REPORT_HINT => 9, | ||||||||||||||
| 82 | 575 | }; | |||||||||||||
| 83 | |||||||||||||||
| 84 | 573 | my %report_mode = ( | |||||||||||||
| 85 | REPORT_PROGNAME() => { | ||||||||||||||
| 86 | color => 'bold', | ||||||||||||||
| 87 | }, | ||||||||||||||
| 88 | REPORT_COMMAND() => { | ||||||||||||||
| 89 | color => 'bold magenta', | ||||||||||||||
| 90 | }, | ||||||||||||||
| 91 | REPORT_STATUS() => { | ||||||||||||||
| 92 | color => 'clear', | ||||||||||||||
| 93 | # We do not translate this name because the untranslated output is | ||||||||||||||
| 94 | # part of the interface. | ||||||||||||||
| 95 | name => 'status', | ||||||||||||||
| 96 | }, | ||||||||||||||
| 97 | REPORT_DEBUG() => { | ||||||||||||||
| 98 | color => 'clear', | ||||||||||||||
| 99 | # We do not translate this name because it is a developer interface | ||||||||||||||
| 100 | # and all debug messages are untranslated anyway. | ||||||||||||||
| 101 | name => 'debug', | ||||||||||||||
| 102 | }, | ||||||||||||||
| 103 | REPORT_HINT() => { | ||||||||||||||
| 104 | color => 'bold blue', | ||||||||||||||
| 105 | name => g_('hint'), | ||||||||||||||
| 106 | }, | ||||||||||||||
| 107 | REPORT_INFO() => { | ||||||||||||||
| 108 | color => 'green', | ||||||||||||||
| 109 | name => g_('info'), | ||||||||||||||
| 110 | }, | ||||||||||||||
| 111 | REPORT_NOTICE() => { | ||||||||||||||
| 112 | color => 'yellow', | ||||||||||||||
| 113 | name => g_('notice'), | ||||||||||||||
| 114 | }, | ||||||||||||||
| 115 | REPORT_WARN() => { | ||||||||||||||
| 116 | color => 'bold yellow', | ||||||||||||||
| 117 | name => g_('warning'), | ||||||||||||||
| 118 | }, | ||||||||||||||
| 119 | REPORT_ERROR() => { | ||||||||||||||
| 120 | color => 'bold red', | ||||||||||||||
| 121 | name => g_('error'), | ||||||||||||||
| 122 | }, | ||||||||||||||
| 123 | ); | ||||||||||||||
| 124 | |||||||||||||||
| 125 | sub report_options | ||||||||||||||
| 126 | 530 | ▶ | { | ||||||||||||
|
Subroutine
report_options: called CC=5 SCAR=19.1
Pod
undocumented
| |||||||||||||||
| 127 | 530 | my (%opts) = @_; | |||||||||||||
| 128 | |||||||||||||||
| 129 | 530 | ▶ | if (exists $opts{quiet_warnings}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | 530 | $quiet_warnings = $opts{quiet_warnings}; | |||||||||||||
| 131 | } | ||||||||||||||
| 132 | 530 | ▶ | if (exists $opts{show_hints}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | 0 | $show_hints = $opts{show_hints}; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | 530 | ▶ | if (exists $opts{debug_level}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 136 | 0 | $debug_level = $opts{debug_level}; | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | 530 | ▶ | if (exists $opts{info_fh}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 139 | 0 | $info_fh = $opts{info_fh}; | |||||||||||||
| 140 | } | ||||||||||||||
| 141 | } | ||||||||||||||
| 142 | |||||||||||||||
| 143 | sub report_name | ||||||||||||||
| 144 | 105 | ▶ | { | ||||||||||||
|
Subroutine
report_name: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||
| 145 | 105 | my $type = shift; | |||||||||||||
| 146 | |||||||||||||||
| 147 | 105 | ▶ | return $report_mode{$type}{name} // ''; | ||||||||||||
|
Condition50%1/2
$report_mode{$type}{"name"} // ""
MC/DC0%0/1
$report_mode{$type}{"name"} // ""
$report_mode{$type}{"name"}
Truth table50%1/2
A
A= $report_mode{$type}{"name"}
| |||||||||||||||
| 148 | } | ||||||||||||||
| 149 | |||||||||||||||
| 150 | sub report_color | ||||||||||||||
| 151 | 210 | ▶ | { | ||||||||||||
|
Subroutine
report_color: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||
| 152 | 210 | my $type = shift; | |||||||||||||
| 153 | |||||||||||||||
| 154 | 210 | ▶ | return $report_mode{$type}{color} // 'clear'; | ||||||||||||
|
Condition50%1/2
$report_mode{$type}{"color"} // "clear"
MC/DC0%0/1
$report_mode{$type}{"color"} // "clear"
$report_mode{$type}{"color"}
Truth table50%1/2
A
A= $report_mode{$type}{"color"}
| |||||||||||||||
| 155 | } | ||||||||||||||
| 156 | |||||||||||||||
| 157 | sub report_pretty | ||||||||||||||
| 158 | 0 | ▶ | { | ||||||||||||
|
Subroutine
report_pretty: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 159 | 0 | my ($msg, $color) = @_; | |||||||||||||
| 160 | |||||||||||||||
| 161 | 0 | return colored_str($msg, $color); | |||||||||||||
| 162 | } | ||||||||||||||
| 163 | |||||||||||||||
| 164 | sub _progname_prefix | ||||||||||||||
| 165 | 105 | ▶ | { | ||||||||||||
|
Subroutine
_progname_prefix: called CC=1 SCAR=0.0
| |||||||||||||||
| 166 | 105 | return colored_str("$Dpkg::PROGNAME: ", report_color(REPORT_PROGNAME)); | |||||||||||||
| 167 | } | ||||||||||||||
| 168 | |||||||||||||||
| 169 | sub _typename_prefix | ||||||||||||||
| 170 | 105 | ▶ | { | ||||||||||||
|
Subroutine
_typename_prefix: called CC=1 SCAR=0.0
| |||||||||||||||
| 171 | 105 | my $type = shift; | |||||||||||||
| 172 | |||||||||||||||
| 173 | 105 | return colored_str(report_name($type), report_color($type)); | |||||||||||||
| 174 | } | ||||||||||||||
| 175 | |||||||||||||||
| 176 | sub report | ||||||||||||||
| 177 | 105 | ▶ | { | ||||||||||||
|
Subroutine
report: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 178 | 105 | my ($type, $msg, @args) = @_; | |||||||||||||
| 179 | |||||||||||||||
| 180 | 105 | ▶ | $msg = sprintf $msg, @args if @args; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 181 | |||||||||||||||
| 182 | 105 | my $progname = _progname_prefix(); | |||||||||||||
| 183 | 105 | my $typename = _typename_prefix($type); | |||||||||||||
| 184 | |||||||||||||||
| 185 | 105 | return "$progname$typename: $msg\n"; | |||||||||||||
| 186 | } | ||||||||||||||
| 187 | |||||||||||||||
| 188 | sub debug | ||||||||||||||
| 189 | 0 | ▶ | { | ||||||||||||
|
Subroutine
debug: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 190 | 0 | my ($level, @args) = @_; | |||||||||||||
| 191 | |||||||||||||||
| 192 | 0 | ▶ | print report(REPORT_DEBUG, @args) if $level <= $debug_level; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 193 | } | ||||||||||||||
| 194 | |||||||||||||||
| 195 | sub hint | ||||||||||||||
| 196 | 0 | ▶ | { | ||||||||||||
|
Subroutine
hint: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 197 | 0 | my @args = @_; | |||||||||||||
| 198 | |||||||||||||||
| 199 | 0 | ▶ | return if not $show_hints; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 200 | |||||||||||||||
| 201 | 0 | ▶ | print report(REPORT_HINT, @args) if not $quiet_warnings; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 202 | } | ||||||||||||||
| 203 | |||||||||||||||
| 204 | sub info | ||||||||||||||
| 205 | 0 | ▶ | { | ||||||||||||
|
Subroutine
info: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 206 | 0 | my @args = @_; | |||||||||||||
| 207 | |||||||||||||||
| 208 | 0 | ▶ | print { $info_fh } report(REPORT_INFO, @args) if not $quiet_warnings; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 209 | } | ||||||||||||||
| 210 | |||||||||||||||
| 211 | sub notice | ||||||||||||||
| 212 | 0 | ▶ | { | ||||||||||||
|
Subroutine
notice: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 213 | 0 | my @args = @_; | |||||||||||||
| 214 | |||||||||||||||
| 215 | 0 | ▶ | warn report(REPORT_NOTICE, @args) if not $quiet_warnings; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 216 | } | ||||||||||||||
| 217 | |||||||||||||||
| 218 | sub warning | ||||||||||||||
| 219 | 22426 | ▶ | { | ||||||||||||
|
Subroutine
warning: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 220 | 22426 | my @args = @_; | |||||||||||||
| 221 | |||||||||||||||
| 222 | 22426 | ▶ | warn report(REPORT_WARN, @args) if not $quiet_warnings; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 223 | } | ||||||||||||||
| 224 | |||||||||||||||
| 225 | sub syserr | ||||||||||||||
| 226 | 11 | ▶ | { | ||||||||||||
|
Subroutine
syserr: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 227 | 11 | my ($msg, @args) = @_; | |||||||||||||
| 228 | |||||||||||||||
| 229 | 11 | die report(REPORT_ERROR, "$msg: $!", @args); | |||||||||||||
| 230 | } | ||||||||||||||
| 231 | |||||||||||||||
| 232 | sub error | ||||||||||||||
| 233 | 77 | ▶ | { | ||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 234 | 77 | my @args = @_; | |||||||||||||
| 235 | |||||||||||||||
| 236 | 77 | die report(REPORT_ERROR, @args); | |||||||||||||
| 237 | } | ||||||||||||||
| 238 | |||||||||||||||
| 239 | sub errormsg | ||||||||||||||
| 240 | 0 | ▶ | { | ||||||||||||
|
Subroutine
errormsg: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 241 | 0 | my @args = @_; | |||||||||||||
| 242 | |||||||||||||||
| 243 | 0 | print { *STDERR } report(REPORT_ERROR, @args); | |||||||||||||
| 244 | } | ||||||||||||||
| 245 | |||||||||||||||
| 246 | sub printcmd | ||||||||||||||
| 247 | 0 | ▶ | { | ||||||||||||
|
Subroutine
printcmd: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 248 | 0 | my (@cmd) = @_; | |||||||||||||
| 249 | |||||||||||||||
| 250 | 0 | print { *STDERR } colored_str(" @cmd\n", report_color(REPORT_COMMAND)); | |||||||||||||
| 251 | } | ||||||||||||||
| 252 | |||||||||||||||
| 253 | sub subprocerr | ||||||||||||||
| 254 | 0 | ▶ | { | ||||||||||||
|
Subroutine
subprocerr: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 255 | 0 | my ($p, @args) = @_; | |||||||||||||
| 256 | |||||||||||||||
| 257 | 0 | ▶ | $p = sprintf $p, @args if @args; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 258 | |||||||||||||||
| 259 | 0 | require POSIX; | |||||||||||||
| 260 | |||||||||||||||
| 261 | 0 | ▶ | if (POSIX::WIFEXITED($?)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 262 | 0 | my $ret = POSIX::WEXITSTATUS($?); | |||||||||||||
| 263 | 0 | error(g_('%s subprocess failed with exit status %d'), $p, $ret); | |||||||||||||
| 264 | } elsif (POSIX::WIFSIGNALED($?)) { | ||||||||||||||
| 265 | 0 | my $sig = POSIX::WTERMSIG($?); | |||||||||||||
| 266 | 0 | error(g_('%s subprocess was killed by signal %d'), $p, $sig); | |||||||||||||
| 267 | } else { | ||||||||||||||
| 268 | 0 | error(g_('%s subprocess failed with unknown status code %d'), $p, $?); | |||||||||||||
| 269 | } | ||||||||||||||
| 270 | } | ||||||||||||||
| 271 | |||||||||||||||
| 272 | sub usageerr | ||||||||||||||
| 273 | 0 | ▶ | { | ||||||||||||
|
Subroutine
usageerr: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 274 | 0 | my ($msg, @args) = @_; | |||||||||||||
| 275 | |||||||||||||||
| 276 | 0 | state $printforhelp = g_('Use --help for program usage information.'); | |||||||||||||
| 277 | |||||||||||||||
| 278 | 0 | ▶ | $msg = sprintf $msg, @args if @args; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 279 | 0 | warn report(REPORT_ERROR, $msg); | |||||||||||||
| 280 | 0 | warn "\n$printforhelp\n"; | |||||||||||||
| 281 | 0 | exit(2); | |||||||||||||
| 282 | } | ||||||||||||||
| 283 | |||||||||||||||
| 284 | =head1 CHANGES | ||||||||||||||
| 285 | |||||||||||||||
| 286 | =head2 Version 0.xx | ||||||||||||||
| 287 | |||||||||||||||
| 288 | This is a private module. | ||||||||||||||
| 289 | |||||||||||||||
| 290 | =cut | ||||||||||||||
| 291 | |||||||||||||||
| 292 | 573 | 1; | |||||||||||||