ErrorHandling.pm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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_PROGNAME
  19. REPORT_COMMAND
  20. REPORT_STATUS
  21. REPORT_DEBUG
  22. REPORT_INFO
  23. REPORT_NOTICE
  24. REPORT_WARN
  25. REPORT_ERROR
  26. report_pretty
  27. report_color
  28. report
  29. );
  30. our @EXPORT = qw(
  31. report_options
  32. debug
  33. info
  34. notice
  35. warning
  36. error
  37. errormsg
  38. syserr
  39. printcmd
  40. subprocerr
  41. usageerr
  42. );
  43. use Exporter qw(import);
  44. use Term::ANSIColor;
  45. use Dpkg ();
  46. use Dpkg::Gettext;
  47. my $quiet_warnings = 0;
  48. my $debug_level = 0;
  49. my $info_fh = \*STDOUT;
  50. my $use_color = 0;
  51. sub setup_color
  52. {
  53. my $mode = $ENV{'DPKG_COLORS'} // 'auto';
  54. if ($mode eq 'auto') {
  55. ## no critic (InputOutput::ProhibitInteractiveTest)
  56. $use_color = 1 if -t *STDOUT or -t *STDERR;
  57. } elsif ($mode eq 'always') {
  58. $use_color = 1;
  59. } else {
  60. $use_color = 0;
  61. }
  62. }
  63. setup_color();
  64. use constant {
  65. REPORT_PROGNAME => 1,
  66. REPORT_COMMAND => 2,
  67. REPORT_STATUS => 3,
  68. REPORT_INFO => 4,
  69. REPORT_NOTICE => 5,
  70. REPORT_WARN => 6,
  71. REPORT_ERROR => 7,
  72. REPORT_DEBUG => 8,
  73. };
  74. my %report_mode = (
  75. REPORT_PROGNAME() => {
  76. color => 'bold',
  77. },
  78. REPORT_COMMAND() => {
  79. color => 'bold magenta',
  80. },
  81. REPORT_STATUS() => {
  82. color => 'clear',
  83. # We do not translate this name because the untranslated output is
  84. # part of the interface.
  85. name => 'status',
  86. },
  87. REPORT_DEBUG() => {
  88. color => 'clear',
  89. # We do not translate this name because it is a developer interface
  90. # and all debug messages are untranslated anyway.
  91. name => 'debug',
  92. },
  93. REPORT_INFO() => {
  94. color => 'green',
  95. name => g_('info'),
  96. },
  97. REPORT_NOTICE() => {
  98. color => 'yellow',
  99. name => g_('notice'),
  100. },
  101. REPORT_WARN() => {
  102. color => 'bold yellow',
  103. name => g_('warning'),
  104. },
  105. REPORT_ERROR() => {
  106. color => 'bold red',
  107. name => g_('error'),
  108. },
  109. );
  110. sub report_options
  111. {
  112. my (%options) = @_;
  113. if (exists $options{quiet_warnings}) {
  114. $quiet_warnings = $options{quiet_warnings};
  115. }
  116. if (exists $options{debug_level}) {
  117. $debug_level = $options{debug_level};
  118. }
  119. if (exists $options{info_fh}) {
  120. $info_fh = $options{info_fh};
  121. }
  122. }
  123. sub report_name
  124. {
  125. my $type = shift;
  126. return $report_mode{$type}{name} // '';
  127. }
  128. sub report_color
  129. {
  130. my $type = shift;
  131. return $report_mode{$type}{color} // 'clear';
  132. }
  133. sub report_pretty
  134. {
  135. my ($msg, $color) = @_;
  136. if ($use_color) {
  137. return colored($msg, $color);
  138. } else {
  139. return $msg;
  140. }
  141. }
  142. sub _progname_prefix
  143. {
  144. return report_pretty("$Dpkg::PROGNAME: ", report_color(REPORT_PROGNAME));
  145. }
  146. sub _typename_prefix
  147. {
  148. my $type = shift;
  149. return report_pretty(report_name($type), report_color($type));
  150. }
  151. sub report(@)
  152. {
  153. my ($type, $msg) = (shift, shift);
  154. $msg = sprintf($msg, @_) if (@_);
  155. my $progname = _progname_prefix();
  156. my $typename = _typename_prefix($type);
  157. return "$progname$typename: $msg\n";
  158. }
  159. sub debug
  160. {
  161. my $level = shift;
  162. print report(REPORT_DEBUG, @_) if $level <= $debug_level;
  163. }
  164. sub info($;@)
  165. {
  166. print { $info_fh } report(REPORT_INFO, @_) if not $quiet_warnings;
  167. }
  168. sub notice
  169. {
  170. warn report(REPORT_NOTICE, @_) if not $quiet_warnings;
  171. }
  172. sub warning($;@)
  173. {
  174. warn report(REPORT_WARN, @_) if not $quiet_warnings;
  175. }
  176. sub syserr($;@)
  177. {
  178. my $msg = shift;
  179. die report(REPORT_ERROR, "$msg: $!", @_);
  180. }
  181. sub error($;@)
  182. {
  183. die report(REPORT_ERROR, @_);
  184. }
  185. sub errormsg($;@)
  186. {
  187. print { *STDERR } report(REPORT_ERROR, @_);
  188. }
  189. sub printcmd
  190. {
  191. my (@cmd) = @_;
  192. print { *STDERR } report_pretty(" @cmd\n", report_color(REPORT_COMMAND));
  193. }
  194. sub subprocerr(@)
  195. {
  196. my ($p) = (shift);
  197. $p = sprintf($p, @_) if (@_);
  198. require POSIX;
  199. if (POSIX::WIFEXITED($?)) {
  200. error(g_('%s gave error exit status %s'), $p, POSIX::WEXITSTATUS($?));
  201. } elsif (POSIX::WIFSIGNALED($?)) {
  202. error(g_('%s died from signal %s'), $p, POSIX::WTERMSIG($?));
  203. } else {
  204. error(g_('%s failed with unknown exit code %d'), $p, $?);
  205. }
  206. }
  207. my $printforhelp = g_('Use --help for program usage information.');
  208. sub usageerr(@)
  209. {
  210. my ($msg) = (shift);
  211. $msg = sprintf($msg, @_) if (@_);
  212. warn report(REPORT_ERROR, $msg);
  213. warn "\n$printforhelp\n";
  214. exit(2);
  215. }
  216. 1;