line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Smoke::LogMixin; |
2
|
43
|
|
|
43
|
|
760
|
use warnings; |
|
43
|
|
|
|
|
76
|
|
|
43
|
|
|
|
|
1441
|
|
3
|
43
|
|
|
43
|
|
243
|
use strict; |
|
43
|
|
|
|
|
99
|
|
|
43
|
|
|
|
|
803
|
|
4
|
43
|
|
|
43
|
|
21455
|
use Data::Dumper; |
|
43
|
|
|
|
|
225167
|
|
|
43
|
|
|
|
|
2371
|
|
5
|
43
|
|
|
43
|
|
1775
|
BEGIN { $|++ } |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.002'; |
8
|
|
|
|
|
|
|
|
9
|
43
|
|
|
43
|
|
302
|
use Exporter 'import'; |
|
43
|
|
|
|
|
99
|
|
|
43
|
|
|
|
|
20621
|
|
10
|
|
|
|
|
|
|
our @EXPORT = qw/verbosity log_warn log_info log_debug/; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $USE_TIMESTAMP = 1; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
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) object with 4 new methods. It has some extra |
44
|
|
|
|
|
|
|
L 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 or C, or maybe the keys C<_verbose> or |
47
|
|
|
|
|
|
|
C<_v> (See also L). |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
The three log methods use the C 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
|
|
|
|
|
|
|
sub verbosity { |
67
|
2620
|
|
|
2620
|
1
|
3907
|
my $self = shift; |
68
|
2620
|
100
|
|
|
|
12477
|
if ($self->isa('Test::Smoke::App::Base')) { |
69
|
59
|
|
|
|
|
299
|
return $self->option('verbose'); |
70
|
|
|
|
|
|
|
} |
71
|
2561
|
|
|
|
|
5315
|
for my $vfield (qw/verbose v/) { |
72
|
2575
|
100
|
100
|
|
|
15157
|
return $self->$vfield if $self->can($vfield) or exists $self->{"_$vfield"}; |
73
|
|
|
|
|
|
|
} |
74
|
0
|
|
|
|
|
0
|
my $struct = Data::Dumper->new([$self])->Terse(1)->Sortkeys(1)->Indent(1)->Dump; |
75
|
0
|
|
|
|
|
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
|
|
|
|
|
|
|
sub log_warn { |
107
|
80
|
|
|
80
|
1
|
5931
|
my $self = shift; |
108
|
|
|
|
|
|
|
|
109
|
80
|
|
|
|
|
621
|
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
|
|
|
|
|
|
|
sub log_info { |
142
|
1027
|
|
|
1027
|
1
|
4127
|
my $self = shift; |
143
|
1027
|
100
|
|
|
|
2786
|
return if !$self->verbosity; |
144
|
|
|
|
|
|
|
|
145
|
54
|
|
|
|
|
306
|
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
|
|
|
|
|
|
|
sub log_debug { |
178
|
1593
|
|
|
1593
|
1
|
4303
|
my $self = shift; |
179
|
1593
|
100
|
|
|
|
4813
|
return if $self->verbosity < 2; |
180
|
|
|
|
|
|
|
|
181
|
5
|
|
|
|
|
43
|
print _log_message(@_); |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# Compose the message to be logged. |
185
|
|
|
|
|
|
|
sub _log_message { |
186
|
139
|
|
|
139
|
|
1669
|
(my $fmt = shift) =~ s/\n*\z//; |
187
|
|
|
|
|
|
|
|
188
|
139
|
50
|
|
|
|
4311
|
my $stamp = $USE_TIMESTAMP |
|
|
100
|
|
|
|
|
|
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
|
139
|
100
|
|
|
|
1986
|
my @message = split(/\n/, @_ ? sprintf("$fmt", @_) : ($fmt)); |
197
|
139
|
|
|
|
|
4558
|
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 instance. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=cut |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
package Test::Smoke::Logger; |
235
|
43
|
|
|
43
|
|
347
|
use warnings; |
|
43
|
|
|
|
|
88
|
|
|
43
|
|
|
|
|
1770
|
|
236
|
43
|
|
|
43
|
|
326
|
use strict; |
|
43
|
|
|
|
|
108
|
|
|
43
|
|
|
|
|
1242
|
|
237
|
43
|
|
|
43
|
|
248
|
use base 'Test::Smoke::ObjectBase'; |
|
43
|
|
|
|
|
83
|
|
|
43
|
|
|
|
|
16436
|
|
238
|
43
|
|
|
43
|
|
320
|
use Test::Smoke::LogMixin; |
|
43
|
|
|
|
|
98
|
|
|
43
|
|
|
|
|
4649
|
|
239
|
|
|
|
|
|
|
Test::Smoke::LogMixin->import(); |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
sub new { |
242
|
115
|
|
|
115
|
|
1344
|
my $class = shift; |
243
|
115
|
|
|
|
|
784
|
my %raw = @_; |
244
|
115
|
|
100
|
|
|
988
|
my $self = { _verbose => $raw{v} || 0 }; |
245
|
115
|
|
|
|
|
819
|
return bless $self, $class; |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
1; |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=head1 COPYRIGHT |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
(c) 2020, All rights reserved. |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
* Abe Timmerman |
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
|
|
|
|
|
|
|
* , |
262
|
|
|
|
|
|
|
* |
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 |