ErrorHandling.pm 2.4 KB

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