blib/lib/Class/Easy/Log.pm

filter: Statementstmt 87.0% 101 / 116 Branchbran 73.6% 28 / 38 Conditioncond 63.3% 19 / 30 MC/DCmcdc 35.2% 6 / 17 Subroutinesub 88.2% 15 / 17 Podpod 0.0% 0 / 8 totaltotal 74.7% 169 / 226 CC 30 SCAR 24.6
CC
30
Cov
80%
logger
23.9
_parse_layout
11.3
configure_driver
2.1
CRAP 11.7
« blib/lib/Class/Easy/MRO.pm ↑ Summary blib/lib/Class/Easy.pm »
1 package Class::Easy::Log;
2 # $Id: Log.pm,v 1.3 2009/07/20 18:00:10 apla Exp $
3  
4 5 ▶use Class::Easy::Import;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Class::Easy::Log::Tie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Class::Easy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # log4perl has categories, layouts and appenders
9 5 our $default_layout = '[%P] [%M(%L)] [%c] %m%n';
10  
11 5 Class::Easy::make_accessor (__PACKAGE__, 'category');
12 5 Class::Easy::make_accessor (__PACKAGE__, 'tied');
13 5 Class::Easy::make_accessor (__PACKAGE__, 'layout');
14  
15 5 my $driver_config = {};
16 5 our $int_loggers = {
17 default => bless {
18 category => 'default', broker => '', tied => 0
19 }, __PACKAGE__
20 };
21  
22 5 my $java_mappings = {
23 L => 'line',
24 P => 'pid',
25 r => 'ts_start',
26 R => 'ts_log',
27 c => 'category',
28 C => 'package',
29 d => 'date',
30 F => 'file',
31 H => 'hostname',
32 l => 'where',
33 m => 'message',
34 M => 'method',
35 n => 'newline',
36 p => 'priority',
37 T => 'stack',
38 };
39  
40 5 our $hostname;
41 5 ▶if (Class::Easy::try_to_use ('Sys::Hostname')) {
Branch: 1 branch
BranchTrueFalseTotal
if (Class::Easy::try_to_use("Sys::Hostname")) 5 0 5
42 5 $hostname = Sys::Hostname->can('hostname')->();
43 }
44  
45 Class::Easy::Log->configure_driver (
46 5 id => 'log4perl', package => 'Log::Log4perl', constructor => 'get_logger',
47 log => 'debug', # default logging level
48
49 );
50  
51 # basic logger: logger ('sql');
52 # log4perl logger: logger (log4perl => 'sql');
53 # also you'll need to configure log4perl somewhere:
54 # Log::Log4perl::init (...);
55 # Class::Easy::Log->configure_driver (
56 # type => 'log4perl', package => 'Log::Log4perl', constructor => 'get_logger'
57 # );
58  
59 5 ▶sub configure_driver {
Subroutine configure_driver: called CC=2 SCAR=7.6
Pod undocumented
60 5 my $class = shift;
61 5 my $params = {@_};
62
63 5 ▶ if (Class::Easy::try_to_use ($params->{package})) {
Branch: 1 branch
BranchTrueFalseTotal
if (Class::Easy::try_to_use($params->{"package"})) 0 5 5
64 0 $driver_config->{$params->{id}} = $params;
65 }
66 }
67  
68 13 ▶sub logger { # create logger
Subroutine logger: called CC=16 SCAR=31.8
Pod undocumented
69
70 13 my $driver_id;
71 my $category;
72 13 my $appender;
73
74 13 my $ref;
75
76 13 ▶ if (defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 2 11 13
77 2 $ref = ref \$_[1];
78 }
79
80 13 ▶ unless (@_) { # if type omitted, we use current package name as type
Branch: 4 branches
BranchTrueFalseTotal
if (@_) { } 12 1 13
if (do { scalar @_ == 2 and $ref eq 'GLOB' and defined *{$_[1];}{'IO'} }) { } 2 10 12
elsif (@_ == 2 || @_ == 1 and exists $$driver_config{$_[0]}) { } 0 10 10
elsif (@_ == 1) { } 10 0 10
81 1 $category = (caller)[0];
82 2 ▶ } elsif (scalar (@_) == 2 and $ref eq 'GLOB' and defined *{$_[1]}{IO}) {
Condition66%4/6
scalar @_ == 2 and $ref eq "GLOB" and defined *{$_[1];}{"IO"}
!ll&&!rl&&r
1002
scalar @_ == 2 and $ref eq "GLOB"
!ll&&!rl&&r
1002
MC/DC33%1/3
scalar @_ == 2 and $ref eq "GLOB" and defined *{$_[1];}{"IO"}
scalar @_ == 2 $ref eq "GLOB" defined *{$_[1];}{"IO"}
Truth table50%2/4
A and B and C
A= scalar @_ == 2B= $ref eq "GLOB"C= defined *{$_[1];}{"IO"}
ABCresult
0XX0
10X0
1100
1111
83 2 $category = $_[0];
84 2 $appender = $_[1];
85 ▶ } elsif ((@_ == 2 or @_ == 1) and exists $driver_config->{$_[0]}) {
Condition33%2/6
@_ == 2 || @_ == 1 and exists $$driver_config{$_[0]}
!ll&&!rl&&r
0100
@_ == 2 or @_ == 1
l!l&&r!l&&!r
0100
MC/DC0%0/3
@_ == 2 || @_ == 1 and exists $$driver_config{$_[0]}
@_ == 2 @_ == 1 exists $$driver_config{$_[0]}
Truth table20%1/5
A or B and C
A= @_ == 2B= @_ == 1C= exists $$driver_config{$_[0]}
ABCresult
00X0
1X00
0100
1X11
0111
86 0 $driver_id = $_[0];
87 0 ▶ $category = @_ == 1 ? (caller)[0] : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
88 } elsif (@_ == 1) {
89 10 $category = $_[0];
90 } else {
91 0 die "you must use logger (), logger (driver), logger (category) or logger (driver => category)";
92 }
93
94 13 my $self;
95
96 13 ▶ unless (defined $driver_id) { # basic internal driver require no processing
Branch: 2 branches
BranchTrueFalseTotal
if (defined $driver_id) { } 0 13 13
if (do { defined $driver_config->{$driver_id} }) 0 0 0
97
98 13 my $existing_logger = $int_loggers->{$category};
99
100 13 ▶ $self = $existing_logger || bless {
Condition100%2/2
$existing_logger || bless({'category', $category, 'broker', ''}, 'Class::Easy::Log')
l!l
94
MC/DC100%1/1
$existing_logger || bless({'category', $category, 'broker', ''}, 'Class::Easy::Log')
$existing_logger
Truth table100%2/2
A
A= $existing_logger
Aresult
11
00
101 category => $category,
102 broker => '',
103 }, 'Class::Easy::Log';
104
105 13 ▶ unless (defined $existing_logger) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $existing_logger) 4 9 13
106 4 $int_loggers->{$category} = $self;
107
108 7 ▶ Class::Easy::make_accessor ((caller)[0], 'log_'.$category, default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
109 7 my $caller1 = [caller (1)];
110 7 my $caller0 = [caller];
111  
112 7 unshift @_, $category, $self, $caller1, $caller0;
113 7 goto &_wrapper;
114 4 });
115  
116 1 ▶ Class::Easy::make_accessor ((caller)[0], 'timer_'.$category, default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
117 1 Class::Easy::Timer->new (@_, $self)
118 4 });
119 }
120  
121 0 } elsif (defined $driver_config->{$driver_id}) { # driver defined
122 0 my $driver = $driver_config->{$driver_id};
123 0 $self = $driver->{package}->can ($driver->{constructor})->($driver->{package}, $category);
124  
125 0 ▶ Class::Easy::make_accessor ((caller)[0], 'log_'.$category, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 0 goto &{$self->can ($driver->{log})};
127 0 });
128
129 # make_accessor ((caller)[0], 'log_'.$type, default => \&Class::Easy::Log::message);
130 }
131
132 13 ▶ if ($appender) {
Branch: 1 branch
BranchTrueFalseTotal
if ($appender) 2 11 13
133 2 $self->appender ($appender);
134 }
135
136 13 return $self;
137 }
138  
139 12 ▶sub appender {
Subroutine appender: called CC=2 SCAR=6.9
Pod undocumented
140 12 my $self = shift;
141 # my $appender = shift;
142
143 12 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 8 4 12
144 8 $self->{tied} = 1;
145 8 tie $self->{broker} => 'Class::Easy::Log::Tie', $_[0];
146 } else {
147 4 $self->{tied} = 0;
148 4 untie $self->{broker};
149 }
150  
151 }
152  
153 # example usage:
154 # logger (sql); # create sub log_sql
155 # log_sql ('message'); # log message, but nobody receive this message
156 # logger (sql => 'STDERR'); # now any log messages go to the STDERR
157  
158 23 ▶sub _parse_layout {
Subroutine _parse_layout: called CC=11 SCAR=24.3
159 23 my $logger = shift;
160
161 23 ▶ $logger->{layout} ||= $default_layout;
Condition66%2/3
$logger->{"layout"} ||= $Class::Easy::Log::default_layout
l!l&&r!l&&!r
1670
MC/DC0%0/2
$logger->{"layout"} ||= $Class::Easy::Log::default_layout
$logger->{"layout"} $Class::Easy::Log::default_layout
Truth table66%2/3
A ||= B
A= $logger->{"layout"}B= $Class::Easy::Log::default_layout
ABresult
1X1
011
000
162
163 return $logger
164 23 ▶ if defined $logger->{_layout} and $logger->{layout} eq $logger->{_layout};
Branch: 1 branch
BranchTrueFalseTotal
if defined $logger->{"_layout"} and $logger->{"layout"} eq $logger->{"_layout"} 14 9 23
Condition66%2/3
defined $logger->{"_layout"} and $logger->{"layout"} eq $logger->{"_layout"}
!ll&&!rl&&r
9014
MC/DC50%1/2
defined $logger->{"_layout"} and $logger->{"layout"} eq $logger->{"_layout"}
defined $logger->{"_layout"} $logger->{"layout"} eq $logger->{"_layout"}
Truth table66%2/3
A and B
A= defined $logger->{"_layout"}B= $logger->{"layout"} eq $logger->{"_layout"}
ABresult
0X0
100
111
165
166 9 my $layout = $logger->{layout};
167
168 9 my $layout_format = '';
169 9 my @layout_fields = ();
170 9 ▶ while ($layout =~ /([^\%]*)\%([^\%cCdFHlLmMnpPrRTxX]*)([\%cCdFHlLmMnpPrRTxX])/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($layout =~ /([^\%]*)\%([^\%cCdFHlLmMnpPrRTxX]*)([\%cCdFHlLmMnpPrRTxX])/g) 52 9 61
171
172 52 $layout_format .= "$1\%$2";
173 52 ▶ if ($3 eq 'L' or $3 eq 'P') {
Branch: 3 branches
BranchTrueFalseTotal
if ($3 eq 'L' or $3 eq 'P') { } 16 36 52
elsif ($3 eq 'r' or $3 eq 'R') { } 0 36 36
elsif ($3 eq '%') { } 1 35 36
Condition100%3/3
$3 eq "L" or $3 eq "P"
l!l&&r!l&&!r
8836
MC/DC100%2/2
$3 eq "L" or $3 eq "P"
$3 eq "L" $3 eq "P"
Truth table100%3/3
A or B
A= $3 eq "L"B= $3 eq "P"
ABresult
1X1
011
000
174 16 $layout_format .= 'd';
175 ▶ } elsif ($3 eq 'r' or $3 eq 'R') {
Condition33%1/3
$3 eq "r" or $3 eq "R"
l!l&&r!l&&!r
0036
MC/DC0%0/2
$3 eq "r" or $3 eq "R"
$3 eq "r" $3 eq "R"
Truth table33%1/3
A or B
A= $3 eq "r"B= $3 eq "R"
ABresult
1X1
011
000
176 0 $layout_format .= 'd';
177 } elsif ($3 eq '%') {
178 1 $layout_format .= '%';
179 } else {
180 35 $layout_format .= 's';
181 }
182 52 ▶ push @layout_fields, $java_mappings->{$3}
Branch: 1 branch
BranchTrueFalseTotal
unless $3 eq "%" 51 1 52
183 unless $3 eq '%';
184 }
185 # TODO: create more failsafe solution
186 9 $layout_format .= substr ($layout, length($layout_format));
187
188 9 $logger->{_layout_format} = $layout_format;
189 9 $logger->{_layout_fields} = \@layout_fields;
190 9 $logger->{_layout} = $layout;
191
192 9 return $logger;
193 }
194  
195 21 ▶sub _format_log {
Subroutine _format_log: called CC=1 SCAR=0.0
196 21 my $self = shift;
197
198 21 my $time = time;
199
200 my $values = {
201 pid => $$,
202 category => $self->{category},
203 21 newline => "\n",
204 ts_start => $time - $^T,
205 hostname => $hostname, # doesn't reflect hostname changes in runtime
206 date => $time,
207 @_
208 };
209  
210 # TODO: make sure all these values supported
211 # R => 'ts_log', # use timer_${logger} instead
212 # C => 'package', # useless, because we have %M = method
213 # F => 'file', # who cares about script files?
214 # l => 'where', # wtf?
215 # p => 'priority', # log level, if written not for robots
216 # T => 'stack', # everything loves java stacks
217 # TODO: add date formatting support
218
219 # use Data::Dumper;
220 # warn Dumper $self->{_layout_fields};
221 # warn Dumper [map {$values->{$_}} @{$self->{_layout_fields}}];
222
223 # warn $self->{_layout_format}, join (', ', @{$self->{_layout_fields}}), (join ', ', map {
224 # $values->{$_}
225 # } @{$self->{_layout_fields}});
226
227 return sprintf ($self->{_layout_format}, (map {
228 126 $values->{$_}
229 21 } @{$self->{_layout_fields}}));
230
231 }
232  
233 20 ▶sub _wrapper {
Subroutine _wrapper: called CC=2 SCAR=6.9
234 20 my $category = shift;
235 20 my $logger = shift;
236 20 my $caller1 = shift;
237 20 my $caller0 = shift;
238
239 20 ▶ my $sub = $caller1->[3] || 'main';
Condition100%2/2
$caller1->[3] || 'main'
l!l
614
MC/DC100%1/1
$caller1->[3] || 'main'
$caller1->[3]
Truth table100%2/2
A
A= $caller1->[3]
Aresult
11
00
240 20 my $line = $caller0->[2];
241
242 # my ($package, $filename, $line, $subroutine, $hasargs, $wantarray,
243 # $evaltext, $is_require, $hints, $bitmask)
244
245 20 $logger->_parse_layout;
246
247 20 $logger->{broker} = $logger->_format_log (
248 message => join ('', @_),
249 method => $sub,
250 line => $line
251 );
252
253 20 return 1;
254 }
255  
256 9 ▶sub debug {
Subroutine debug: called CC=1 SCAR=0.0
Pod undocumented
257 9 my $caller1 = [caller (1)];
258 9 my $caller0 = [caller];
259  
260 9 unshift @_, 'default', $int_loggers->{default}, $caller1, $caller0;
261  
262 9 goto &_wrapper;
263 }
264  
265 0 ▶sub debug_depth {
Subroutine debug_depth: not called CC=1 SCAR=6.9
Pod undocumented
266 0 my $caller1 = [caller (2)];
267 0 my $caller0 = [caller (1)];
268  
269 0 unshift @_, 'default', $int_loggers->{default}, $caller1, $caller0;
270  
271 0 goto &_wrapper;
272 }
273  
274 1 ▶sub critical {
Subroutine critical: called CC=2 SCAR=7.0
Pod undocumented
275 1 ▶ my $sub = (caller (1))[3] || 'main';
Condition50%1/2
(caller 1)[3] || 'main'
l!l
10
MC/DC0%0/1
(caller 1)[3] || 'main'
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
276 1 my $line = (caller)[2];
277
278 1 my $logger = logger ('DIE')->_parse_layout;
279
280 1 die $logger->_format_log (
281 message => join ('', @_),
282 method => $sub,
283 line => $line
284 );
285 }
286  
287 2 ▶sub catch_stderr {
Subroutine catch_stderr: called CC=1 SCAR=0.0
Pod undocumented
288 2 my $ref = shift;
289 2 tie *STDERR => 'Class::Easy::Log::Tie', $ref;
290 }
291  
292 1 ▶sub release_stderr {
Subroutine release_stderr: called CC=1 SCAR=0.0
Pod undocumented
293 1 untie *STDERR;
294 }
295  
296 5 1;
« blib/lib/Class/Easy/MRO.pm ↑ Summary blib/lib/Class/Easy.pm »