blib/lib/Log/Message/Simple.pm

filter: Statementstmt 89.3% 67 / 75 Branchbran 56.2% 9 / 16 Conditioncond 38.4% 5 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 19 / 19 Podpod 50.0% 3 / 6 totaltotal 75.1% 103 / 137 CC 15 SCAR 12.4
CC
15
Cov
78%
msg
7.0
debug
7.0
error
5.5
CRAP 3.5
↑ Summary
1 package Log::Message::Simple;
2 3 ▶use if $] > 5.017, 'deprecate';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Log::Message private => 0;;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶ use vars qw[$VERSION];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 $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<Log::Message> 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<STDOUT> (or actually
63 C<$MSG_FH>, see the C<GLOBAL VARIABLES> section below), if the
64 C<VERBOSE> option is true.
65 The C<VERBOSE> 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<STDOUT> (or
72 actually C<$DEBUG_FH>, see the C<GLOBAL VARIABLES> section below),
73 if the C<VERBOSE> option is true.
74 The C<VERBOSE> 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<STDERR> (or actually
81 C<$ERROR_FH>, see the C<GLOBAL VARIABLES> sections below), if the
82 C<VERBOSE> option is true.
83 The C<VERBOSE> 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 1 ▶ sub msg {
Subroutine msg: called CC=4 SCAR=19.4
Pod undocumented
92 1 my $self = shift;
93 1 ▶ my $verbose = shift || 0;
Condition50%1/2
shift() || 0
l!l
01
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
94  
95 ### so you don't want us to print the msg? ###
96 1 ▶ return if defined $verbose && $verbose == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $verbose and $verbose == 0 1 0 1
Condition33%1/3
defined $verbose and $verbose == 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $verbose and $verbose == 0
defined $verbose $verbose == 0
Truth table33%1/3
A and B
A= defined $verboseB= $verbose == 0
ABresult
0X0
100
111
97  
98 0 my $old_fh = select $Log::Message::Simple::MSG_FH;
99 0 print '['. $self->tag (). '] ' . $self->message . "\n";
100 0 select $old_fh;
101  
102 0 return;
103 }
104  
105 1 ▶ sub debug {
Subroutine debug: called CC=4 SCAR=19.4
Pod undocumented
106 1 my $self = shift;
107 1 ▶ my $verbose = shift || 0;
Condition50%1/2
shift() || 0
l!l
01
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
108  
109 ### so you don't want us to print the msg? ###
110 1 ▶ return if defined $verbose && $verbose == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $verbose and $verbose == 0 1 0 1
Condition33%1/3
defined $verbose and $verbose == 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $verbose and $verbose == 0
defined $verbose $verbose == 0
Truth table33%1/3
A and B
A= defined $verboseB= $verbose == 0
ABresult
0X0
100
111
111  
112 0 my $old_fh = select $Log::Message::Simple::DEBUG_FH;
113 0 print '['. $self->tag (). '] ' . $self->message . "\n";
114 0 select $old_fh;
115  
116 0 return;
117 }
118  
119 1 ▶ sub error {
Subroutine error: called CC=5 SCAR=17.1
Pod undocumented
120 1 my $self = shift;
121 1 my $verbose = shift;
122 1 ▶ $verbose = 1 unless defined $verbose; # default to true
Branch: 1 branch
BranchTrueFalseTotal
unless defined $verbose 1 0 1
123  
124 ### so you don't want us to print the error? ###
125 1 ▶ return if defined $verbose && $verbose == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $verbose and $verbose == 0 0 1 1
Condition33%1/3
defined $verbose and $verbose == 0
!ll&&!rl&&r
010
MC/DC0%0/2
defined $verbose and $verbose == 0
defined $verbose $verbose == 0
Truth table33%1/3
A and B
A= defined $verboseB= $verbose == 0
ABresult
0X0
100
111
126  
127 1 my $old_fh = select $Log::Message::Simple::ERROR_FH;
128  
129 1 my $msg = '['. $self->tag . '] ' . $self->message;
130  
131 1 ▶ print $Log::Message::Simple::STACKTRACE_ON_ERROR
Branch: 1 branch
BranchTrueFalseTotal
$Log::Message::Simple::STACKTRACE_ON_ERROR ? : 0 1 1
132 ? Carp::shortmess($msg)
133 : $msg . "\n";
134  
135 1 select $old_fh;
136  
137 1 return;
138 }
139 }
140  
141 =head2 carp();
142  
143 Provides functionality equal to C<Carp::carp()> 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<Carp::croak()> 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<Carp::confess()> 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<Carp::cluck()> 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<Log::Message::Simple> is
174 implemented using C<Log::Message>, consult its manpage for the
175 function C<retrieve> 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<TRACE> option is
180 true all items are returned with C<Carp::longmess> output, rather than
181 just the message.
182 C<TRACE> 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<Log::Message::Simple> is implemented using C<Log::Message>, consult its
188 manpage for the function C<retrieve> to see what is returned and how
189 to use the items.
190  
191 =cut
192  
193 3 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
194 3 ▶ use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
195 3 ▶ use Params::Check qw[ check ];
Subroutine BEGIN: called CC=1 SCAR=0.0
196 3 ▶ use vars qw[ @EXPORT @EXPORT_OK %EXPORT_TAGS @ISA ];;
Subroutine BEGIN: called CC=1 SCAR=0.0
197  
198 3 @ISA = 'Exporter';
199 3 @EXPORT = qw[error msg debug];
200 3 @EXPORT_OK = qw[carp cluck croak confess];
201  
202 3 %EXPORT_TAGS = (
203 STD => \@EXPORT,
204 CARP => \@EXPORT_OK,
205 ALL => [ @EXPORT, @EXPORT_OK ],
206 );
207  
208 3 my $log = new Log::Message;
209  
210 3 for my $func ( @EXPORT, @EXPORT_OK ) {
211 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
212  
213 ### up the carplevel for the carp emulation
214 ### functions
215 7 ▶ *$func = sub { local $Carp::CarpLevel += 2
Subroutine __ANON__: called CC=2 SCAR=6.9
216 7 ▶ if grep { $_ eq $func } @EXPORT_OK;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $func;} @Log::Message::Simple::EXPORT_OK 4 3 7
217  
218 7 my $msg = shift;
219 7 $log->store(
220 message => $msg,
221 tag => uc $func,
222 level => $func,
223 extra => [@_]
224 );
225 21 };
226 }
227  
228 7 ▶ sub flush {
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
229 7 return reverse $log->flush;
230 }
231  
232 49 ▶ sub stack {
Subroutine stack: called CC=1 SCAR=0.0
Pod documented
233 49 return $log->retrieve( chrono => 1 );
234 }
235  
236 28 ▶ sub stack_as_string {
Subroutine stack_as_string: called CC=3 SCAR=11.4
Pod documented
237 28 my $class = shift;
238 28 ▶ my $trace = shift() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 0 28 28
239  
240 return join $/, map {
241 28 ▶ '[' . $_->tag . '] [' . $_->when . '] ' .
Branch: 1 branch
BranchTrueFalseTotal
$trace ? : 0 14 14
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<error()> 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<msg()> 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<debug()> are being
265 printed. This default to C<*STDOUT>.
266  
267 =item $STACKTRACE_ON_ERROR
268  
269 If this option is set to C<true>, every call to C<error()> will
270 generate a stacktrace using C<Carp::shortmess()>.
271 Defaults to C<false>
272  
273 =back
274  
275 =cut
276  
277 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
278 3 ▶ use vars qw[ $ERROR_FH $MSG_FH $DEBUG_FH $STACKTRACE_ON_ERROR ];
Subroutine BEGIN: called CC=1 SCAR=0.0
279  
280 3 local $| = 1;
281 3 $ERROR_FH = \*STDERR;
282 3 $MSG_FH = \*STDOUT;
283 3 $DEBUG_FH = \*STDOUT;
284  
285 3 $STACKTRACE_ON_ERROR = 0;
286 }
287  
288  
289 3 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:
↑ Summary