lib/Test/Smoke/LogMixin.pm

filter: Statementstmt 96.3% 52 / 54 Branchbran 92.8% 13 / 14 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 15 / 15 Podpod 100% 4 / 4 totaltotal 96.8% 92 / 95 CC 11 SCAR 9.3
CC
11
Cov
96%
verbosity
5.1
_log_message
4.0
log_info
2.0
CRAP 2.5
« blib/lib/Test/Smoke/Poster/Base.pm ↑ Summary blib/lib/Test/Smoke/App/Options.pm »
1 package Test::Smoke::LogMixin;
2 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 50 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 50 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 50 ▶BEGIN { $|++ }
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 50 our $VERSION = '0.002';
8  
9 50 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 50 our @EXPORT = qw/verbosity log_warn log_info log_debug/;
11  
12 50 our $USE_TIMESTAMP = 1;
13  
14 50 require POSIX;
15  
16 =head1 NAME
17  
18 Test::Smoke::LogMixin - "Role" that adds logging methods to "traditional" objects.
19  
20 =head1 SYNOPSIS
21  
22 package MyPackage;
23 use warnings;
24 use strict;
25 use Test::Smoke::LogMixin;
26  
27 sub new {
28 my $class = shift;
29 my %selfish = @_;
30 $selfish{_verbose} ||= 0;
31 return bless \%selfish, $class;
32 }
33 1;
34  
35 package main;
36 use MyPackage;
37 my $o = MyPackage->new(_verbose => 2);
38 $o->log_debug("This will end up in the log");
39  
40 =head1 DESCRIPTION
41  
42 This package with these mixin-methods acts like a role to extend your traditional (created with
43 C<bless()>) object with 4 new methods. It has some extra
44 L<Test::Smoke::App::Base> logic to determine the log-level (by looking at C<<
45 $app->option('verbose') >>). For other object types it tries to fiend if there
46 are methods by the name C<verbose> or C<v>, or maybe the keys C<_verbose> or
47 C<_v> (See also L<Test::Smoke::ObjectBase>).
48  
49 The three log methods use the C<sprintf()> way of composing strings whenever
50 more than 1 argument is passed!
51  
52 =head2 $app->verbosity
53  
54 Return the verbosity of this app.
55  
56 =head3 Arguments
57  
58 None.
59  
60 =head3 Returns
61  
62 The value of either C<_verbose> or C<_v>
63  
64 =cut
65  
66 2778 ▶sub verbosity {
Subroutine verbosity: called CC=5 SCAR=16.2
Pod documented
67 2778 my $self = shift;
68 2778 ▶ if ($self->isa('Test::Smoke::App::Base')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa("Test::Smoke::App::Base")) 59 2719 2778
69 59 return $self->option('verbose');
70 }
71 2719 for my $vfield (qw/verbose v/) {
72 2767 ▶ return $self->$vfield if $self->can($vfield) or exists $self->{"_$vfield"};
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($vfield) or exists $$self{"_$vfield"} 2719 48 2767
Condition100%3/3
$self->can($vfield) or exists $$self{"_$vfield"}
l!l&&r!l&&!r
202369648
MC/DC100%2/2
$self->can($vfield) or exists $$self{"_$vfield"}
$self->can($vfield) exists $$self{"_$vfield"}
Truth table100%3/3
A or B
A= $self->can($vfield)B= exists $$self{"_$vfield"}
ABresult
1X1
011
000
73 }
74 0 my $struct = Data::Dumper->new([$self])->Terse(1)->Sortkeys(1)->Indent(1)->Dump;
75 0 die "Could not find a verbosity option: $struct\n";
76 }
77  
78 =head2 $app->log_warn($fmt, @values)
79  
80 C<< prinf $fmt, @values >> to the currently selected filehandle.
81  
82 =head3 Arguments
83  
84 Positional.
85  
86 =over
87  
88 =item $fmt => a (s)printf format
89  
90 The format gets an extra new line if one wasn't present.
91  
92 =item @values => optional vaules for the template.
93  
94 =back
95  
96 =head3 Returns
97  
98 use in void context.
99  
100 =head3 Exceptions
101  
102 None.
103  
104 =cut
105  
106 89 ▶sub log_warn {
Subroutine log_warn: called CC=1 SCAR=0.0
Pod documented
107 89 my $self = shift;
108  
109 89 print _log_message(@_);
110 }
111  
112 =head2 $app->log_info($fmt, @values)
113  
114 C<< prinf $fmt, @values >> to the currently selected filehandle if the 'verbose'
115 option is set.
116  
117 =head3 Arguments
118  
119 Positional.
120  
121 =over
122  
123 =item $fmt => a (s)printf format
124  
125 The format gets an extra new line if one wasn't present.
126  
127 =item @values => optional vaules for the template.
128  
129 =back
130  
131 =head3 Returns
132  
133 use in void context.
134  
135 =head3 Exceptions
136  
137 None.
138  
139 =cut
140  
141 1092 ▶sub log_info {
Subroutine log_info: called CC=2 SCAR=6.9
Pod documented
142 1092 my $self = shift;
143 1092 ▶ return if !$self->verbosity;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->verbosity 1033 59 1092
144  
145 59 print _log_message(@_);
146 }
147  
148 =head2 $app->log_debug($fmt, @values)
149  
150 C<< prinf $fmt, @values >> to the currently selected filehandle if the 'verbose'
151 option is set to a value > 1.
152  
153 =head3 Arguments
154  
155 Positional.
156  
157 =over
158  
159 =item $fmt => a (s)printf format
160  
161 The format gets an extra new line if one wasn't present.
162  
163 =item @values => optional vaules for the template.
164  
165 =back
166  
167 =head3 Returns
168  
169 use in void context.
170  
171 =head3 Exceptions
172  
173 None.
174  
175 =cut
176  
177 1686 ▶sub log_debug {
Subroutine log_debug: called CC=2 SCAR=6.9
Pod documented
178 1686 my $self = shift;
179 1686 ▶ return if $self->verbosity < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbosity < 2 1679 7 1686
180  
181 7 print _log_message(@_);
182 }
183  
184 # Compose the message to be logged.
185 155 ▶sub _log_message {
Subroutine _log_message: called CC=4 SCAR=13.9
186 155 (my $fmt = shift) =~ s/\n*\z//;
187  
188 155 ▶ my $stamp = $USE_TIMESTAMP
Branch: 2 branches
BranchTrueFalseTotal
$Test::Smoke::LogMixin::USE_TIMESTAMP ? : 68 87 155
$^O eq 'MSWin32' ? : 0 68 68
189 ? $^O eq 'MSWin32'
190 ? POSIX::strftime("[%Y-%m-%d %H:%M:%SZ] ", gmtime)
191 : POSIX::strftime("[%Y-%m-%d %H:%M:%S%z] ", localtime)
192 : "";
193  
194 # use the $stamp for every line.
195 # sprintf iff @_;
196 155 ▶ my @message = split(/\n/, @_ ? sprintf("$fmt", @_) : ($fmt));
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 120 35 155
197 155 return join("\n", map "$stamp$_", @message) . "\n";
198 }
199  
200 =head1 NAME
201  
202 Test::Smoke::Logger - Helper object for logging.
203  
204 =head1 SYNOPSIS
205  
206 use Test::Smoke::LogMixin;
207 my $logger = Test::Smoke::Logger->new(v => 1);
208 $logger->log_warn("Minimal log level"); # v >= 0
209 $logger->log_info("Medium log level"); # v <= 1
210 $logger->log_debug("High log level"); # v > 1
211  
212 =head1 DESCRIPTION
213  
214 =head2 Test::Smoke::Logger->new(%arguments)
215  
216 Return a logger instance.
217  
218 =head3 Arguments
219  
220 Named, hash:
221  
222 =over
223  
224 =item v => <0|1|2>
225  
226 =back
227  
228 =head3 Returns
229  
230 The L<Test::Smoke::Logger> instance.
231  
232 =cut
233  
234 package Test::Smoke::Logger;
235 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
236 50 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
237 50 ▶use base 'Test::Smoke::ObjectBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
238 50 ▶use Test::Smoke::LogMixin;
Subroutine BEGIN: called CC=1 SCAR=0.0
239 50 Test::Smoke::LogMixin->import();
240  
241 113 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
242 113 my $class = shift;
243 113 my %raw = @_;
244 113 ▶ my $self = { _verbose => $raw{v} || 0 };
Condition100%2/2
$raw{'v'} || 0
l!l
3110
MC/DC100%1/1
$raw{'v'} || 0
$raw{'v'}
Truth table100%2/2
A
A= $raw{'v'}
Aresult
11
00
245 113 return bless $self, $class;
246 }
247  
248 50 1;
249  
250 =head1 COPYRIGHT
251  
252 (c) 2020, All rights reserved.
253  
254 * Abe Timmerman <abeltje@cpan.org>
255  
256 This library is free software; you can redistribute it and/or modify
257 it under the same terms as Perl itself.
258  
259 See:
260  
261 * <http://www.perl.com/perl/misc/Artistic.html>,
262 * <http://www.gnu.org/copyleft/gpl.html>
263  
264 This program is distributed in the hope that it will be useful,
265 but WITHOUT ANY WARRANTY; without even the implied warranty of
266 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
267  
268 =cut
« blib/lib/Test/Smoke/Poster/Base.pm ↑ Summary blib/lib/Test/Smoke/App/Options.pm »