1 package Log::Message::Simple;
4 use Log::Message private => 0;;
20 use Log::Message::Simple qw[msg error debug
21 carp croak cluck confess];
23 use Log::Message::Simple qw[:STD :CARP];
25 ### standard reporting functionality
26 msg( "Connecting to database", $verbose );
27 error( "Database connection failed: $@", $verbose );
28 debug( "Connection arguments were: $args", $debug );
30 ### standard carp functionality
31 carp( "Wrong arguments passed: @_" );
32 croak( "Fatal: wrong arguments passed: @_" );
33 cluck( "Wrong arguments passed -- including stacktrace: @_" );
34 confess("Fatal: wrong arguments passed -- including stacktrace: @_" );
36 ### retrieve individual message
37 my @stack = Log::Message::Simple->stack;
38 my @stack = Log::Message::Simple->flush;
40 ### retrieve the entire stack in printable form
41 my $msgs = Log::Message::Simple->stack_as_string;
42 my $trace = Log::Message::Simple->stack_as_string(1);
45 local $Log::Message::Simple::MSG_FH = \*STDERR;
46 local $Log::Message::Simple::ERROR_FH = \*STDERR;
47 local $Log::Message::Simple::DEBUG_FH = \*STDERR;
49 ### force a stacktrace on error
50 local $Log::Message::Simple::STACKTRACE_ON_ERROR = 1
54 This module provides standardized logging facilities using the
55 C<Log::Message> module.
59 =head2 msg("message string" [,VERBOSE])
61 Records a message on the stack, and prints it to C<STDOUT> (or actually
62 C<$MSG_FH>, see the C<GLOBAL VARIABLES> section below), if the
63 C<VERBOSE> option is true.
64 The C<VERBOSE> option defaults to false.
66 Exported by default, or using the C<:STD> tag.
68 =head2 debug("message string" [,VERBOSE])
70 Records a debug message on the stack, and prints it to C<STDOUT> (or
71 actually C<$DEBUG_FH>, see the C<GLOBAL VARIABLES> section below),
72 if the C<VERBOSE> option is true.
73 The C<VERBOSE> option defaults to false.
75 Exported by default, or using the C<:STD> tag.
77 =head2 error("error string" [,VERBOSE])
79 Records an error on the stack, and prints it to C<STDERR> (or actually
80 C<$ERROR_FH>, see the C<GLOBAL VARIABLES> sections below), if the
81 C<VERBOSE> option is true.
82 The C<VERBOSE> options defaults to true.
84 Exported by default, or using the C<:STD> tag.
88 { package Log::Message::Handlers;
92 my $verbose = shift || 0;
94 ### so you don't want us to print the msg? ###
95 return if defined $verbose && $verbose == 0;
97 my $old_fh = select $Log::Message::Simple::MSG_FH;
98 print '['. $self->tag (). '] ' . $self->message . "\n";
106 my $verbose = shift || 0;
108 ### so you don't want us to print the msg? ###
109 return if defined $verbose && $verbose == 0;
111 my $old_fh = select $Log::Message::Simple::DEBUG_FH;
112 print '['. $self->tag (). '] ' . $self->message . "\n";
121 $verbose = 1 unless defined $verbose; # default to true
123 ### so you don't want us to print the error? ###
124 return if defined $verbose && $verbose == 0;
126 my $old_fh = select $Log::Message::Simple::ERROR_FH;
128 my $msg = '['. $self->tag . '] ' . $self->message;
130 print $Log::Message::Simple::STACKTRACE_ON_ERROR
131 ? Carp::shortmess($msg)
142 Provides functionality equal to C<Carp::carp()>, whilst still logging
145 Exported by by using the C<:CARP> tag.
149 Provides functionality equal to C<Carp::croak()>, whilst still logging
152 Exported by by using the C<:CARP> tag.
156 Provides functionality equal to C<Carp::confess()>, whilst still logging
159 Exported by by using the C<:CARP> tag.
163 Provides functionality equal to C<Carp::cluck()>, whilst still logging
166 Exported by by using the C<:CARP> tag.
170 =head2 Log::Message::Simple->stack()
172 Retrieves all the items on the stack. Since C<Log::Message::Simple> is
173 implemented using C<Log::Message>, consult its manpage for the
174 function C<retrieve> to see what is returned and how to use the items.
176 =head2 Log::Message::Simple->stack_as_string([TRACE])
178 Returns the whole stack as a printable string. If the C<TRACE> option is
179 true all items are returned with C<Carp::longmess> output, rather than
181 C<TRACE> defaults to false.
183 =head2 Log::Message::Simple->flush()
185 Removes all the items from the stack and returns them. Since
186 C<Log::Message::Simple> is implemented using C<Log::Message>, consult its
187 manpage for the function C<retrieve> to see what is returned and how
194 use Params::Check qw[ check ];
195 use vars qw[ @EXPORT @EXPORT_OK %EXPORT_TAGS @ISA ];;
198 @EXPORT = qw[error msg debug];
199 @EXPORT_OK = qw[carp cluck croak confess];
204 ALL => [ @EXPORT, @EXPORT_OK ],
207 my $log = new Log::Message;
209 for my $func ( @EXPORT, @EXPORT_OK ) {
212 ### up the carplevel for the carp emulation
214 *$func = sub { local $Carp::CarpLevel += 2
215 if grep { $_ eq $func } @EXPORT_OK;
228 return reverse $log->flush;
232 return $log->retrieve( chrono => 1 );
235 sub stack_as_string {
237 my $trace = shift() ? 1 : 0;
239 return join $/, map {
240 '[' . $_->tag . '] [' . $_->when . '] ' .
241 ($trace ? $_->message . ' ' . $_->longmess
243 } __PACKAGE__->stack;
247 =head1 GLOBAL VARIABLES
253 This is the filehandle all the messages sent to C<error()> are being
254 printed. This defaults to C<*STDERR>.
258 This is the filehandle all the messages sent to C<msg()> are being
259 printed. This default to C<*STDOUT>.
263 This is the filehandle all the messages sent to C<debug()> are being
264 printed. This default to C<*STDOUT>.
266 =item $STACKTRACE_ON_ERROR
268 If this option is set to C<true>, every call to C<error()> will
269 generate a stacktrace using C<Carp::shortmess()>.
275 use vars qw[ $ERROR_FH $MSG_FH $DEBUG_FH $STACKTRACE_ON_ERROR ];
278 $ERROR_FH = \*STDERR;
280 $DEBUG_FH = \*STDOUT;
282 $STACKTRACE_ON_ERROR = 0;
289 # c-indentation-style: bsd
291 # indent-tabs-mode: nil
293 # vim: expandtab shiftwidth=4: