Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
10 KiB

  1. package Carp;
  2. =head1 NAME
  3. carp - warn of errors (from perspective of caller)
  4. cluck - warn of errors with stack backtrace
  5. (not exported by default)
  6. croak - die of errors (from perspective of caller)
  7. confess - die of errors with stack backtrace
  8. =head1 SYNOPSIS
  9. use Carp;
  10. croak "We're outta here!";
  11. use Carp qw(cluck);
  12. cluck "This is how we got here!";
  13. =head1 DESCRIPTION
  14. The Carp routines are useful in your own modules because
  15. they act like die() or warn(), but report where the error
  16. was in the code they were called from. Thus if you have a
  17. routine Foo() that has a carp() in it, then the carp()
  18. will report the error as occurring where Foo() was called,
  19. not where carp() was called.
  20. =head2 Forcing a Stack Trace
  21. As a debugging aid, you can force Carp to treat a croak as a confess
  22. and a carp as a cluck across I<all> modules. In other words, force a
  23. detailed stack trace to be given. This can be very helpful when trying
  24. to understand why, or from where, a warning or error is being generated.
  25. This feature is enabled by 'importing' the non-existent symbol
  26. 'verbose'. You would typically enable it by saying
  27. perl -MCarp=verbose script.pl
  28. or by including the string C<MCarp=verbose> in the L<PERL5OPT>
  29. environment variable.
  30. =head1 BUGS
  31. The Carp routines don't handle exception objects currently.
  32. If called with a first argument that is a reference, they simply
  33. call die() or warn(), as appropriate.
  34. =cut
  35. # This package is heavily used. Be small. Be fast. Be good.
  36. # Comments added by Andy Wardley <[email protected]> 09-Apr-98, based on an
  37. # _almost_ complete understanding of the package. Corrections and
  38. # comments are welcome.
  39. # The $CarpLevel variable can be set to "strip off" extra caller levels for
  40. # those times when Carp calls are buried inside other functions. The
  41. # $Max(EvalLen|(Arg(Len|Nums)) variables are used to specify how the eval
  42. # text and function arguments should be formatted when printed.
  43. $CarpLevel = 0; # How many extra package levels to skip on carp.
  44. $MaxEvalLen = 0; # How much eval '...text...' to show. 0 = all.
  45. $MaxArgLen = 64; # How much of each argument to print. 0 = all.
  46. $MaxArgNums = 8; # How many arguments to print. 0 = all.
  47. $Verbose = 0; # If true then make shortmess call longmess instead
  48. require Exporter;
  49. @ISA = ('Exporter');
  50. @EXPORT = qw(confess croak carp);
  51. @EXPORT_OK = qw(cluck verbose);
  52. @EXPORT_FAIL = qw(verbose); # hook to enable verbose mode
  53. # if the caller specifies verbose usage ("perl -MCarp=verbose script.pl")
  54. # then the following method will be called by the Exporter which knows
  55. # to do this thanks to @EXPORT_FAIL, above. $_[1] will contain the word
  56. # 'verbose'.
  57. sub export_fail {
  58. shift;
  59. $Verbose = shift if $_[0] eq 'verbose';
  60. return @_;
  61. }
  62. # longmess() crawls all the way up the stack reporting on all the function
  63. # calls made. The error string, $error, is originally constructed from the
  64. # arguments passed into longmess() via confess(), cluck() or shortmess().
  65. # This gets appended with the stack trace messages which are generated for
  66. # each function call on the stack.
  67. sub longmess {
  68. return @_ if ref $_[0];
  69. my $error = join '', @_;
  70. my $mess = "";
  71. my $i = 1 + $CarpLevel;
  72. my ($pack,$file,$line,$sub,$hargs,$eval,$require);
  73. my (@a);
  74. #
  75. # crawl up the stack....
  76. #
  77. while (do { { package DB; @a = caller($i++) } } ) {
  78. # get copies of the variables returned from caller()
  79. ($pack,$file,$line,$sub,$hargs,undef,$eval,$require) = @a;
  80. #
  81. # if the $error error string is newline terminated then it
  82. # is copied into $mess. Otherwise, $mess gets set (at the end of
  83. # the 'else {' section below) to one of two things. The first time
  84. # through, it is set to the "$error at $file line $line" message.
  85. # $error is then set to 'called' which triggers subsequent loop
  86. # iterations to append $sub to $mess before appending the "$error
  87. # at $file line $line" which now actually reads "called at $file line
  88. # $line". Thus, the stack trace message is constructed:
  89. #
  90. # first time: $mess = $error at $file line $line
  91. # subsequent times: $mess .= $sub $error at $file line $line
  92. # ^^^^^^
  93. # "called"
  94. if ($error =~ m/\n$/) {
  95. $mess .= $error;
  96. } else {
  97. # Build a string, $sub, which names the sub-routine called.
  98. # This may also be "require ...", "eval '...' or "eval {...}"
  99. if (defined $eval) {
  100. if ($require) {
  101. $sub = "require $eval";
  102. } else {
  103. $eval =~ s/([\\\'])/\\$1/g;
  104. if ($MaxEvalLen && length($eval) > $MaxEvalLen) {
  105. substr($eval,$MaxEvalLen) = '...';
  106. }
  107. $sub = "eval '$eval'";
  108. }
  109. } elsif ($sub eq '(eval)') {
  110. $sub = 'eval {...}';
  111. }
  112. # if there are any arguments in the sub-routine call, format
  113. # them according to the format variables defined earlier in
  114. # this file and join them onto the $sub sub-routine string
  115. if ($hargs) {
  116. # we may trash some of the args so we take a copy
  117. @a = @DB::args; # must get local copy of args
  118. # don't print any more than $MaxArgNums
  119. if ($MaxArgNums and @a > $MaxArgNums) {
  120. # cap the length of $#a and set the last element to '...'
  121. $#a = $MaxArgNums;
  122. $a[$#a] = "...";
  123. }
  124. for (@a) {
  125. # set args to the string "undef" if undefined
  126. $_ = "undef", next unless defined $_;
  127. if (ref $_) {
  128. # dunno what this is for...
  129. $_ .= '';
  130. s/'/\\'/g;
  131. }
  132. else {
  133. s/'/\\'/g;
  134. # terminate the string early with '...' if too long
  135. substr($_,$MaxArgLen) = '...'
  136. if $MaxArgLen and $MaxArgLen < length;
  137. }
  138. # 'quote' arg unless it looks like a number
  139. $_ = "'$_'" unless /^-?[\d.]+$/;
  140. # print high-end chars as 'M-<char>' or '^<char>'
  141. s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  142. s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  143. }
  144. # append ('all', 'the', 'arguments') to the $sub string
  145. $sub .= '(' . join(', ', @a) . ')';
  146. }
  147. # here's where the error message, $mess, gets constructed
  148. $mess .= "\t$sub " if $error eq "called";
  149. $mess .= "$error at $file line $line\n";
  150. }
  151. # we don't need to print the actual error message again so we can
  152. # change this to "called" so that the string "$error at $file line
  153. # $line" makes sense as "called at $file line $line".
  154. $error = "called";
  155. }
  156. # this kludge circumvents die's incorrect handling of NUL
  157. my $msg = \($mess || $error);
  158. $$msg =~ tr/\0//d;
  159. $$msg;
  160. }
  161. # shortmess() is called by carp() and croak() to skip all the way up to
  162. # the top-level caller's package and report the error from there. confess()
  163. # and cluck() generate a full stack trace so they call longmess() to
  164. # generate that. In verbose mode shortmess() calls longmess() so
  165. # you always get a stack trace
  166. sub shortmess { # Short-circuit &longmess if called via multiple packages
  167. goto &longmess if $Verbose;
  168. return @_ if ref $_[0];
  169. my $error = join '', @_;
  170. my ($prevpack) = caller(1);
  171. my $extra = $CarpLevel;
  172. my $i = 2;
  173. my ($pack,$file,$line);
  174. # when reporting an error, we want to report it from the context of the
  175. # calling package. So what is the calling package? Within a module,
  176. # there may be many calls between methods and perhaps between sub-classes
  177. # and super-classes, but the user isn't interested in what happens
  178. # inside the package. We start by building a hash array which keeps
  179. # track of all the packages to which the calling package belongs. We
  180. # do this by examining its @ISA variable. Any call from a base class
  181. # method (one of our caller's @ISA packages) can be ignored
  182. my %isa = ($prevpack,1);
  183. # merge all the caller's @ISA packages into %isa.
  184. @isa{@{"${prevpack}::ISA"}} = ()
  185. if(defined @{"${prevpack}::ISA"});
  186. # now we crawl up the calling stack and look at all the packages in
  187. # there. For each package, we look to see if it has an @ISA and then
  188. # we see if our caller features in that list. That would imply that
  189. # our caller is a derived class of that package and its calls can also
  190. # be ignored
  191. while (($pack,$file,$line) = caller($i++)) {
  192. if(defined @{$pack . "::ISA"}) {
  193. my @i = @{$pack . "::ISA"};
  194. my %i;
  195. @i{@i} = ();
  196. # merge any relevant packages into %isa
  197. @isa{@i,$pack} = ()
  198. if(exists $i{$prevpack} || exists $isa{$pack});
  199. }
  200. # and here's where we do the ignoring... if the package in
  201. # question is one of our caller's base or derived packages then
  202. # we can ignore it (skip it) and go onto the next (but note that
  203. # the continue { } block below gets called every time)
  204. next
  205. if(exists $isa{$pack});
  206. # Hey! We've found a package that isn't one of our caller's
  207. # clan....but wait, $extra refers to the number of 'extra' levels
  208. # we should skip up. If $extra > 0 then this is a false alarm.
  209. # We must merge the package into the %isa hash (so we can ignore it
  210. # if it pops up again), decrement $extra, and continue.
  211. if ($extra-- > 0) {
  212. %isa = ($pack,1);
  213. @isa{@{$pack . "::ISA"}} = ()
  214. if(defined @{$pack . "::ISA"});
  215. }
  216. else {
  217. # OK! We've got a candidate package. Time to construct the
  218. # relevant error message and return it. die() doesn't like
  219. # to be given NUL characters (which $msg may contain) so we
  220. # remove them first.
  221. (my $msg = "$error at $file line $line\n") =~ tr/\0//d;
  222. return $msg;
  223. }
  224. }
  225. continue {
  226. $prevpack = $pack;
  227. }
  228. # uh-oh! It looks like we crawled all the way up the stack and
  229. # never found a candidate package. Oh well, let's call longmess
  230. # to generate a full stack trace. We use the magical form of 'goto'
  231. # so that this shortmess() function doesn't appear on the stack
  232. # to further confuse longmess() about it's calling package.
  233. goto &longmess;
  234. }
  235. # the following four functions call longmess() or shortmess() depending on
  236. # whether they should generate a full stack trace (confess() and cluck())
  237. # or simply report the caller's package (croak() and carp()), respectively.
  238. # confess() and croak() die, carp() and cluck() warn.
  239. sub croak { die shortmess @_ }
  240. sub confess { die longmess @_ }
  241. sub carp { warn shortmess @_ }
  242. sub cluck { warn longmess @_ }
  243. 1;