| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package CPANPLUS::Error; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 20 |  |  | 20 |  | 129 | use strict; | 
|  | 20 |  |  |  |  | 40 |  | 
|  | 20 |  |  |  |  | 734 |  | 
| 4 | 20 |  |  | 20 |  | 173 | use vars qw[$VERSION]; | 
|  | 20 |  |  |  |  | 55 |  | 
|  | 20 |  |  |  |  | 1372 |  | 
| 5 |  |  |  |  |  |  | $VERSION = "0.9910"; | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 20 |  |  | 20 |  | 11834 | use Log::Message private => 0;; | 
|  | 20 |  |  |  |  | 159638 |  | 
|  | 20 |  |  |  |  | 133 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | =pod | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 NAME | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | CPANPLUS::Error - error handling for CPANPLUS | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | use CPANPLUS::Error qw[cp_msg cp_error]; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | This module provides the error handling code for the CPANPLUS | 
| 22 |  |  |  |  |  |  | libraries, and is mainly intended for internal use. | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | =head1 FUNCTIONS | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | =head2 cp_msg("message string" [,VERBOSE]) | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | Records a message on the stack, and prints it to C (or actually | 
| 29 |  |  |  |  |  |  | C<$MSG_FH>, see the C section below), if the | 
| 30 |  |  |  |  |  |  | C option is true. | 
| 31 |  |  |  |  |  |  | The C option defaults to false. | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =head2 msg() | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | An alias for C. | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =head2 cp_error("error string" [,VERBOSE]) | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | Records an error on the stack, and prints it to C (or actually | 
| 40 |  |  |  |  |  |  | C<$ERROR_FH>, see the C sections below), if the | 
| 41 |  |  |  |  |  |  | C option is true. | 
| 42 |  |  |  |  |  |  | The C options defaults to true. | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | =head2 error() | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | An alias for C. | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =head1 CLASS METHODS | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | =head2 CPANPLUS::Error->stack() | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | Retrieves all the items on the stack. Since C is | 
| 53 |  |  |  |  |  |  | implemented using C, consult its manpage for the | 
| 54 |  |  |  |  |  |  | function C to see what is returned and how to use the items. | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | =head2 CPANPLUS::Error->stack_as_string([TRACE]) | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | Returns the whole stack as a printable string. If the C option is | 
| 59 |  |  |  |  |  |  | true all items are returned with C output, rather than | 
| 60 |  |  |  |  |  |  | just the message. | 
| 61 |  |  |  |  |  |  | C defaults to false. | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | =head2 CPANPLUS::Error->flush() | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | Removes all the items from the stack and returns them. Since | 
| 66 |  |  |  |  |  |  | C is  implemented using C, consult its | 
| 67 |  |  |  |  |  |  | manpage for the function C to see what is returned and how | 
| 68 |  |  |  |  |  |  | to use the items. | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =cut | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 0 |  |  |  |  | 0 | BEGIN { | 
| 73 | 20 |  |  | 20 |  | 4847 | use Exporter; | 
|  | 20 |  |  |  |  | 43 |  | 
|  | 20 |  |  |  |  | 758 |  | 
| 74 | 20 |  |  | 20 |  | 110 | use Params::Check   qw[check]; | 
|  | 20 |  |  |  |  | 34 |  | 
|  | 20 |  |  |  |  | 769 |  | 
| 75 | 20 |  |  | 20 |  | 112 | use vars            qw[@EXPORT @ISA $ERROR_FH $MSG_FH]; | 
|  | 20 |  |  |  |  | 39 |  | 
|  | 20 |  |  |  |  | 2039 |  | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 20 |  |  | 20 |  | 417 | @ISA        = 'Exporter'; | 
| 78 | 20 |  |  |  |  | 86 | @EXPORT     = qw[cp_error cp_msg error msg]; | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 20 |  |  |  |  | 99 | my $log     = new Log::Message; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 20 |  |  |  |  | 8547 | for my $func ( @EXPORT ) { | 
| 83 | 20 |  |  | 20 |  | 146 | no strict 'refs'; | 
|  | 20 |  |  |  |  | 39 |  | 
|  | 20 |  |  |  |  | 5841 |  | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 80 |  |  |  |  | 164 | my $prefix  = 'cp_'; | 
| 86 | 80 |  |  |  |  | 109 | my $name    = $func; | 
| 87 | 80 |  |  |  |  | 529 | $name       =~ s/^$prefix//g; | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | *$func = sub { | 
| 90 | 667 |  |  | 667 |  | 25768 | my $msg     = shift; | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | ### no point storing non-messages | 
| 93 | 667 | 50 |  |  |  | 2076 | return unless defined $msg; | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 667 |  |  |  |  | 8928 | $log->store( | 
| 96 |  |  |  |  |  |  | message => $msg, | 
| 97 |  |  |  |  |  |  | tag     => uc $name, | 
| 98 |  |  |  |  |  |  | level   => $prefix . $name, | 
| 99 |  |  |  |  |  |  | extra   => [@_] | 
| 100 |  |  |  |  |  |  | ); | 
| 101 | 80 |  |  |  |  | 2130 | }; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | sub flush { | 
| 105 | 49 |  |  | 49 | 1 | 30812 | my @foo = $log->flush; | 
| 106 | 49 | 100 |  |  |  | 877 | return unless @foo; | 
| 107 | 45 |  |  |  |  | 915 | return reverse @foo; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | sub stack { | 
| 111 | 62 |  |  | 62 | 1 | 21525 | return $log->retrieve( chrono => 1 ); | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | sub stack_as_string { | 
| 115 | 59 |  |  | 59 | 1 | 22512 | my $class = shift; | 
| 116 | 59 | 100 |  |  |  | 305 | my $trace = shift() ? 1 : 0; | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | return join $/, map { | 
| 119 | 59 | 100 |  |  |  | 349 | '[' . $_->tag . '] [' . $_->when . '] ' . | 
|  | 698 |  |  |  |  | 58693 |  | 
| 120 |  |  |  |  |  |  | ($trace ? $_->message . ' ' . $_->longmess | 
| 121 |  |  |  |  |  |  | : $_->message); | 
| 122 |  |  |  |  |  |  | } __PACKAGE__->stack; | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  | } | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =head1 GLOBAL VARIABLES | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =over 4 | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =item $ERROR_FH | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | This is the filehandle all the messages sent to C are being | 
| 133 |  |  |  |  |  |  | printed. This defaults to C<*STDERR>. | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =item $MSG_FH | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | This is the filehandle all the messages sent to C are being | 
| 138 |  |  |  |  |  |  | printed. This default to C<*STDOUT>. | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =back | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =cut | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | local $| = 1; | 
| 145 |  |  |  |  |  |  | $ERROR_FH   = \*STDERR; | 
| 146 |  |  |  |  |  |  | $MSG_FH     = \*STDOUT; | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | package # Hide from Pause | 
| 149 |  |  |  |  |  |  | Log::Message::Handlers; | 
| 150 | 20 |  |  | 20 |  | 165 | use Carp (); | 
|  | 20 |  |  |  |  | 109 |  | 
|  | 20 |  |  |  |  | 5685 |  | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | { | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | sub cp_msg { | 
| 155 | 581 |  |  | 581 | 0 | 764564 | my $self    = shift; | 
| 156 | 581 |  |  |  |  | 1593 | my $verbose = shift; | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | ### so you don't want us to print the msg? ### | 
| 159 | 581 | 100 | 100 |  |  | 5743 | return if defined $verbose && $verbose == 0; | 
| 160 |  |  |  |  |  |  |  | 
| 161 | 13 |  |  |  |  | 82 | my $old_fh = select $CPANPLUS::Error::MSG_FH; | 
| 162 |  |  |  |  |  |  |  | 
| 163 | 13 |  |  |  |  | 124 | print '['. $self->tag . '] ' . $self->message . "\n"; | 
| 164 | 13 |  |  |  |  | 9783 | select $old_fh; | 
| 165 |  |  |  |  |  |  |  | 
| 166 | 13 |  |  |  |  | 72 | return; | 
| 167 |  |  |  |  |  |  | } | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | sub cp_error { | 
| 170 | 86 |  |  | 86 | 0 | 115951 | my $self    = shift; | 
| 171 | 86 |  |  |  |  | 383 | my $verbose = shift; | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | ### so you don't want us to print the error? ### | 
| 174 | 86 | 50 | 66 |  |  | 459 | return if defined $verbose && $verbose == 0; | 
| 175 |  |  |  |  |  |  |  | 
| 176 | 86 |  |  |  |  | 630 | my $old_fh = select $CPANPLUS::Error::ERROR_FH; | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | ### is only going to be 1 for now anyway ### | 
| 179 |  |  |  |  |  |  | ### C::I may not be loaded, so do a can() check first | 
| 180 | 86 | 50 |  |  |  | 1584 | my $cb      = CPANPLUS::Internals->can('_return_all_objects') | 
| 181 |  |  |  |  |  |  | ? (CPANPLUS::Internals->_return_all_objects)[0] | 
| 182 |  |  |  |  |  |  | : undef; | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | ### maybe we didn't initialize an internals object (yet) ### | 
| 185 | 86 | 100 |  |  |  | 670 | my $debug   = $cb ? $cb->configure_object->get_conf('debug') : 0; | 
| 186 | 86 |  |  |  |  | 974 | my $msg     =  '['. $self->tag . '] ' . $self->message . "\n"; | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | ### i'm getting this warning in the test suite: | 
| 189 |  |  |  |  |  |  | ### Ambiguous call resolved as CORE::warn(), qualify as such or | 
| 190 |  |  |  |  |  |  | ### use & at CPANPLUS/Error.pm line 57. | 
| 191 |  |  |  |  |  |  | ### no idea where it's coming from, since there's no 'sub warn' | 
| 192 |  |  |  |  |  |  | ### anywhere to be found, but i'll mark it explicitly nonetheless | 
| 193 |  |  |  |  |  |  | ### --kane | 
| 194 | 86 | 50 |  |  |  | 4974 | print $debug ? Carp::shortmess($msg) : $msg . "\n"; | 
| 195 |  |  |  |  |  |  |  | 
| 196 | 86 |  |  |  |  | 1212 | select $old_fh; | 
| 197 |  |  |  |  |  |  |  | 
| 198 | 86 |  |  |  |  | 482 | return; | 
| 199 |  |  |  |  |  |  | } | 
| 200 |  |  |  |  |  |  | } | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | 1; | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | # Local variables: | 
| 205 |  |  |  |  |  |  | # c-indentation-style: bsd | 
| 206 |  |  |  |  |  |  | # c-basic-offset: 4 | 
| 207 |  |  |  |  |  |  | # indent-tabs-mode: nil | 
| 208 |  |  |  |  |  |  | # End: | 
| 209 |  |  |  |  |  |  | # vim: expandtab shiftwidth=4: |