ErrorHandling.pm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. package Dpkg::ErrorHandling;
  14. use strict;
  15. use warnings;
  16. our $VERSION = '0.02';
  17. our @EXPORT_OK = qw(
  18. report
  19. );
  20. our @EXPORT = qw(
  21. report_options
  22. info
  23. notice
  24. warning
  25. error
  26. errormsg
  27. syserr
  28. subprocerr
  29. usageerr
  30. );
  31. use Exporter qw(import);
  32. use Dpkg ();
  33. use Dpkg::Gettext;
  34. my $quiet_warnings = 0;
  35. my $info_fh = \*STDOUT;
  36. sub report_options
  37. {
  38. my (%options) = @_;
  39. if (exists $options{quiet_warnings}) {
  40. $quiet_warnings = $options{quiet_warnings};
  41. }
  42. if (exists $options{info_fh}) {
  43. $info_fh = $options{info_fh};
  44. }
  45. }
  46. sub report(@)
  47. {
  48. my ($type, $msg) = (shift, shift);
  49. $msg = sprintf($msg, @_) if (@_);
  50. return "$Dpkg::PROGNAME: $type: $msg\n";
  51. }
  52. sub info($;@)
  53. {
  54. print { $info_fh } report(g_('info'), @_) if (!$quiet_warnings);
  55. }
  56. sub notice
  57. {
  58. warn report(g_('notice'), @_) if not $quiet_warnings;
  59. }
  60. sub warning($;@)
  61. {
  62. warn report(g_('warning'), @_) if (!$quiet_warnings);
  63. }
  64. sub syserr($;@)
  65. {
  66. my $msg = shift;
  67. die report(g_('error'), "$msg: $!", @_);
  68. }
  69. sub error($;@)
  70. {
  71. die report(g_('error'), @_);
  72. }
  73. sub errormsg($;@)
  74. {
  75. print { *STDERR } report(g_('error'), @_);
  76. }
  77. sub subprocerr(@)
  78. {
  79. my ($p) = (shift);
  80. $p = sprintf($p, @_) if (@_);
  81. require POSIX;
  82. if (POSIX::WIFEXITED($?)) {
  83. error(g_('%s gave error exit status %s'), $p, POSIX::WEXITSTATUS($?));
  84. } elsif (POSIX::WIFSIGNALED($?)) {
  85. error(g_('%s died from signal %s'), $p, POSIX::WTERMSIG($?));
  86. } else {
  87. error(g_('%s failed with unknown exit code %d'), $p, $?);
  88. }
  89. }
  90. my $printforhelp = g_('Use --help for program usage information.');
  91. sub usageerr(@)
  92. {
  93. my ($msg) = (shift);
  94. $msg = sprintf($msg, @_) if (@_);
  95. warn "$Dpkg::PROGNAME: $msg\n\n";
  96. warn "$printforhelp\n";
  97. exit(2);
  98. }
  99. 1;