ErrorHandling.pm 2.3 KB

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