1 package CPANPLUS::Error;
5 use Log::Message private => 0;;
15 use CPANPLUS::Error qw[cp_msg cp_error];
19 This module provides the error handling code for the CPANPLUS
20 libraries, and is mainly intended for internal use.
24 =head2 cp_msg("message string" [,VERBOSE])
26 Records a message on the stack, and prints it to C<STDOUT> (or actually
27 C<$MSG_FH>, see the C<GLOBAL VARIABLES> section below), if the
28 C<VERBOSE> option is true.
29 The C<VERBOSE> option defaults to false.
33 An alias for C<cp_msg>.
35 =head2 cp_error("error string" [,VERBOSE])
37 Records an error on the stack, and prints it to C<STDERR> (or actually
38 C<$ERROR_FH>, see the C<GLOBAL VARIABLES> sections below), if the
39 C<VERBOSE> option is true.
40 The C<VERBOSE> options defaults to true.
44 An alias for C<cp_error>.
48 =head2 CPANPLUS::Error->stack()
50 Retrieves all the items on the stack. Since C<CPANPLUS::Error> is
51 implemented using C<Log::Message>, consult its manpage for the
52 function C<retrieve> to see what is returned and how to use the items.
54 =head2 CPANPLUS::Error->stack_as_string([TRACE])
56 Returns the whole stack as a printable string. If the C<TRACE> option is
57 true all items are returned with C<Carp::longmess> output, rather than
59 C<TRACE> defaults to false.
61 =head2 CPANPLUS::Error->flush()
63 Removes all the items from the stack and returns them. Since
64 C<CPANPLUS::Error> is implemented using C<Log::Message>, consult its
65 manpage for the function C<retrieve> to see what is returned and how
72 use Params::Check qw[check];
73 use vars qw[@EXPORT @ISA $ERROR_FH $MSG_FH];
76 @EXPORT = qw[cp_error cp_msg error msg];
78 my $log = new Log::Message;
80 for my $func ( @EXPORT ) {
85 $name =~ s/^$prefix//g;
90 ### no point storing non-messages
91 return unless defined $msg;
96 level => $prefix . $name,
103 return reverse $log->flush;
107 return $log->retrieve( chrono => 1 );
110 sub stack_as_string {
112 my $trace = shift() ? 1 : 0;
114 return join $/, map {
115 '[' . $_->tag . '] [' . $_->when . '] ' .
116 ($trace ? $_->message . ' ' . $_->longmess
118 } __PACKAGE__->stack;
122 =head1 GLOBAL VARIABLES
128 This is the filehandle all the messages sent to C<error()> are being
129 printed. This defaults to C<*STDERR>.
133 This is the filehandle all the messages sent to C<msg()> are being
134 printed. This default to C<*STDOUT>.
138 $ERROR_FH = \*STDERR;
141 package Log::Message::Handlers;
150 ### so you don't want us to print the msg? ###
151 return if defined $verbose && $verbose == 0;
153 my $old_fh = select $CPANPLUS::Error::MSG_FH;
155 print '['. $self->tag . '] ' . $self->message . "\n";
165 ### so you don't want us to print the error? ###
166 return if defined $verbose && $verbose == 0;
168 my $old_fh = select $CPANPLUS::Error::ERROR_FH;
170 ### is only going to be 1 for now anyway ###
171 ### C::I may not be loaded, so do a can() check first
172 my $cb = CPANPLUS::Internals->can('_return_all_objects')
173 ? (CPANPLUS::Internals->_return_all_objects)[0]
176 ### maybe we didn't initialize an internals object (yet) ###
177 my $debug = $cb ? $cb->configure_object->get_conf('debug') : 0;
178 my $msg = '['. $self->tag . '] ' . $self->message . "\n";
180 ### i'm getting this warning in the test suite:
181 ### Ambiguous call resolved as CORE::warn(), qualify as such or
182 ### use & at CPANPLUS/Error.pm line 57.
183 ### no idea where it's coming from, since there's no 'sub warn'
184 ### anywhere to be found, but i'll mark it explicitly nonetheless
186 print $debug ? Carp::shortmess($msg) : $msg . "\n";
197 # c-indentation-style: bsd
199 # indent-tabs-mode: nil
201 # vim: expandtab shiftwidth=4: