File Coverage

blib/lib/Log/Message/Simple.pm
Criterion Covered Total %
statement 65 73 89.0
branch 9 16 56.2
condition 5 13 38.4
subroutine 19 19 100.0
pod 3 6 50.0
total 101 127 79.5


line stmt bran cond sub pod time code
1             package Log::Message::Simple;
2 3     3   7431 use if $] > 5.017, 'deprecate';
  3         36  
  3         21  
3              
4 3     3   5322 use strict;
  3         8  
  3         94  
5 3     3   3679 use Log::Message private => 0;;
  3         104818  
  3         25  
6              
7             BEGIN {
8 3     3   666 use vars qw[$VERSION];
  3         5  
  3         114  
9 3     3   1094 $VERSION = '0.10';
10             }
11              
12              
13             =pod
14              
15             =head1 NAME
16              
17             Log::Message::Simple - Simplified interface to Log::Message
18              
19             =head1 SYNOPSIS
20              
21             use Log::Message::Simple qw[msg error debug
22             carp croak cluck confess];
23              
24             use Log::Message::Simple qw[:STD :CARP];
25              
26             ### standard reporting functionality
27             msg( "Connecting to database", $verbose );
28             error( "Database connection failed: $@", $verbose );
29             debug( "Connection arguments were: $args", $debug );
30              
31             ### standard carp functionality
32             carp( "Wrong arguments passed: @_" );
33             croak( "Fatal: wrong arguments passed: @_" );
34             cluck( "Wrong arguments passed -- including stacktrace: @_" );
35             confess("Fatal: wrong arguments passed -- including stacktrace: @_" );
36              
37             ### retrieve individual message
38             my @stack = Log::Message::Simple->stack;
39             my @stack = Log::Message::Simple->flush;
40              
41             ### retrieve the entire stack in printable form
42             my $msgs = Log::Message::Simple->stack_as_string;
43             my $trace = Log::Message::Simple->stack_as_string(1);
44              
45             ### redirect output
46             local $Log::Message::Simple::MSG_FH = \*STDERR;
47             local $Log::Message::Simple::ERROR_FH = \*STDERR;
48             local $Log::Message::Simple::DEBUG_FH = \*STDERR;
49              
50             ### force a stacktrace on error
51             local $Log::Message::Simple::STACKTRACE_ON_ERROR = 1
52              
53             =head1 DESCRIPTION
54              
55             This module provides standardized logging facilities using the
56             C module.
57              
58             =head1 FUNCTIONS
59              
60             =head2 msg("message string" [,VERBOSE])
61              
62             Records a message on the stack, and prints it to C (or actually
63             C<$MSG_FH>, see the C section below), if the
64             C option is true.
65             The C option defaults to false.
66              
67             Exported by default, or using the C<:STD> tag.
68              
69             =head2 debug("message string" [,VERBOSE])
70              
71             Records a debug message on the stack, and prints it to C (or
72             actually C<$DEBUG_FH>, see the C section below),
73             if the C option is true.
74             The C option defaults to false.
75              
76             Exported by default, or using the C<:STD> tag.
77              
78             =head2 error("error string" [,VERBOSE])
79              
80             Records an error on the stack, and prints it to C (or actually
81             C<$ERROR_FH>, see the C sections below), if the
82             C option is true.
83             The C options defaults to true.
84              
85             Exported by default, or using the C<:STD> tag.
86              
87             =cut
88              
89             { package Log::Message::Handlers;
90              
91             sub msg {
92 1     1 0 1204 my $self = shift;
93 1   50     10 my $verbose = shift || 0;
94              
95             ### so you don't want us to print the msg? ###
96 1 50 33     16 return if defined $verbose && $verbose == 0;
97              
98 0         0 my $old_fh = select $Log::Message::Simple::MSG_FH;
99 0         0 print '['. $self->tag (). '] ' . $self->message . "\n";
100 0         0 select $old_fh;
101              
102 0         0 return;
103             }
104              
105             sub debug {
106 1     1 0 1258 my $self = shift;
107 1   50     46 my $verbose = shift || 0;
108              
109             ### so you don't want us to print the msg? ###
110 1 50 33     11 return if defined $verbose && $verbose == 0;
111              
112 0         0 my $old_fh = select $Log::Message::Simple::DEBUG_FH;
113 0         0 print '['. $self->tag (). '] ' . $self->message . "\n";
114 0         0 select $old_fh;
115              
116 0         0 return;
117             }
118              
119             sub error {
120 1     1 0 1177 my $self = shift;
121 1         4 my $verbose = shift;
122 1 50       5 $verbose = 1 unless defined $verbose; # default to true
123              
124             ### so you don't want us to print the error? ###
125 1 50 33     10 return if defined $verbose && $verbose == 0;
126              
127 1         19 my $old_fh = select $Log::Message::Simple::ERROR_FH;
128              
129 1         8 my $msg = '['. $self->tag . '] ' . $self->message;
130              
131 1 50       26 print $Log::Message::Simple::STACKTRACE_ON_ERROR
132             ? Carp::shortmess($msg)
133             : $msg . "\n";
134              
135 1         3 select $old_fh;
136              
137 1         5 return;
138             }
139             }
140              
141             =head2 carp();
142              
143             Provides functionality equal to C while still logging
144             to the stack.
145              
146             Exported by using the C<:CARP> tag.
147              
148             =head2 croak();
149              
150             Provides functionality equal to C while still logging
151             to the stack.
152              
153             Exported by using the C<:CARP> tag.
154              
155             =head2 confess();
156              
157             Provides functionality equal to C while still logging
158             to the stack.
159              
160             Exported by using the C<:CARP> tag.
161              
162             =head2 cluck();
163              
164             Provides functionality equal to C while still logging
165             to the stack.
166              
167             Exported by using the C<:CARP> tag.
168              
169             =head1 CLASS METHODS
170              
171             =head2 Log::Message::Simple->stack()
172              
173             Retrieves all the items on the stack. Since C is
174             implemented using C, consult its manpage for the
175             function C to see what is returned and how to use the items.
176              
177             =head2 Log::Message::Simple->stack_as_string([TRACE])
178              
179             Returns the whole stack as a printable string. If the C option is
180             true all items are returned with C output, rather than
181             just the message.
182             C defaults to false.
183              
184             =head2 Log::Message::Simple->flush()
185              
186             Removes all the items from the stack and returns them. Since
187             C is implemented using C, consult its
188             manpage for the function C to see what is returned and how
189             to use the items.
190              
191             =cut
192              
193             BEGIN {
194 3     3   20 use Exporter;
  3         4  
  3         157  
195 3     3   15 use Params::Check qw[ check ];
  3         6  
  3         209  
196 3     3   22 use vars qw[ @EXPORT @EXPORT_OK %EXPORT_TAGS @ISA ];;
  3         5  
  3         571  
197              
198 3     3   54 @ISA = 'Exporter';
199 3         8 @EXPORT = qw[error msg debug];
200 3         10 @EXPORT_OK = qw[carp cluck croak confess];
201              
202 3         36 %EXPORT_TAGS = (
203             STD => \@EXPORT,
204             CARP => \@EXPORT_OK,
205             ALL => [ @EXPORT, @EXPORT_OK ],
206             );
207              
208 3         21 my $log = new Log::Message;
209              
210 3         1259 for my $func ( @EXPORT, @EXPORT_OK ) {
211 3     3   15 no strict 'refs';
  3         5  
  3         826  
212              
213             ### up the carplevel for the carp emulation
214             ### functions
215 28         86 *$func = sub { local $Carp::CarpLevel += 2
216 7 100   7   4444 if grep { $_ eq $func } @EXPORT_OK;
217              
218 7         16 my $msg = shift;
219 7         52 $log->store(
220             message => $msg,
221             tag => uc $func,
222             level => $func,
223             extra => [@_]
224             );
225 21         263 };
226             }
227              
228             sub flush {
229 7     7 1 4186 return reverse $log->flush;
230             }
231              
232             sub stack {
233 49     49 1 15116 return $log->retrieve( chrono => 1 );
234             }
235              
236             sub stack_as_string {
237 28     28 1 21019 my $class = shift;
238 28 50       73 my $trace = shift() ? 1 : 0;
239              
240 14 50       3230 return join $/, map {
241 28         70 '[' . $_->tag . '] [' . $_->when . '] ' .
242             ($trace ? $_->message . ' ' . $_->longmess
243             : $_->message);
244             } __PACKAGE__->stack;
245             }
246             }
247              
248             =head1 GLOBAL VARIABLES
249              
250             =over 4
251              
252             =item $ERROR_FH
253              
254             This is the filehandle all the messages sent to C are being
255             printed. This defaults to C<*STDERR>.
256              
257             =item $MSG_FH
258              
259             This is the filehandle all the messages sent to C are being
260             printed. This default to C<*STDOUT>.
261              
262             =item $DEBUG_FH
263              
264             This is the filehandle all the messages sent to C are being
265             printed. This default to C<*STDOUT>.
266              
267             =item $STACKTRACE_ON_ERROR
268              
269             If this option is set to C, every call to C will
270             generate a stacktrace using C.
271             Defaults to C
272              
273             =back
274              
275             =cut
276              
277             BEGIN {
278 3     3   20 use vars qw[ $ERROR_FH $MSG_FH $DEBUG_FH $STACKTRACE_ON_ERROR ];
  3         5  
  3         290  
279              
280 3     3   20 local $| = 1;
281 3         6 $ERROR_FH = \*STDERR;
282 3         7 $MSG_FH = \*STDOUT;
283 3         5 $DEBUG_FH = \*STDOUT;
284              
285 3         100 $STACKTRACE_ON_ERROR = 0;
286             }
287              
288              
289             1;
290              
291             # Local variables:
292             # c-indentation-style: bsd
293             # c-basic-offset: 4
294             # indent-tabs-mode: nil
295             # End:
296             # vim: expandtab shiftwidth=4: