GX/Logger.pm

filter: Statementstmt 87.0% 87 / 100 Branchbran 65.0% 13 / 20 Conditioncond 43.4% 10 / 23 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 85.0% 17 / 20 Podpod 100% 8 / 8 totaltotal 74.0% 137 / 185 CC 23 SCAR 11.9
CC
23
Cov
77%
log
5.5
disable
4.4
enable
4.4
CRAP 3.3
« GX/Meta/Attribute/Object.pm ↑ Summary GX/Request.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/Logger.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::Logger;
7  
8 2 ▶use GX::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 # ----------------------------------------------------------------------------------------------------------------------
14 # Class data
15 # ----------------------------------------------------------------------------------------------------------------------
16  
17 2 my @LEVELS = qw( trace debug notice warning error fatal );
18 2 my %LEVELS = do { my $i; map { $_ => 1 << $i++ } @LEVELS };
19  
20  
21 # ----------------------------------------------------------------------------------------------------------------------
22 # Class setup
23 # ----------------------------------------------------------------------------------------------------------------------
24  
25 2 ▶use GX::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 extends 'GX::Component::Singleton';
28  
29 2 has 'default_level' => (
30 isa => 'String',
31 initialize => 1,
32 default => 'notice',
33 accessors => {
34 '_get_default_level' => { type => 'get' },
35 '_set_default_level' => { type => 'set' }
36 }
37 );
38  
39 2 has 'fh' => (
40 isa => 'Scalar',
41 initialize => 1,
42 accessor => undef
43 );
44  
45 2 has 'fh_pid' => (
46 isa => 'Scalar',
47 initialize => 1,
48 accessor => undef
49 );
50  
51 2 has 'level' => (
52 isa => 'Scalar',
53 initialize => 1,
54 default => 0,
55 accessors => {
56 '_get_level' => { type => 'get' },
57 '_set_level' => { type => 'set' }
58 }
59 );
60  
61 2 build;
62  
63  
64 # ----------------------------------------------------------------------------------------------------------------------
65 # Export mechanism
66 # ----------------------------------------------------------------------------------------------------------------------
67  
68 2 __PACKAGE__->_install_import_method;
69  
70  
71 # ----------------------------------------------------------------------------------------------------------------------
72 # Public methods
73 # ----------------------------------------------------------------------------------------------------------------------
74  
75 0 ▶sub default_level {
Subroutine default_level: not called CC=1 SCAR=6.9
Pod documented
76  
77 0 return $_[0]->instance->_get_default_level;
78  
79 }
80  
81 12 ▶sub disable {
Subroutine disable: called CC=4 SCAR=14.9
Pod documented
82  
83 12 my $self = shift->instance;
84  
85 12 for ( @_ ) {
86 12 ▶ defined && exists $LEVELS{$_} or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and exists $LEVELS{$_} or next 12 0 12
Condition33%1/3
defined $_ and exists $LEVELS{$_}
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $_ and exists $LEVELS{$_}
defined $_ exists $LEVELS{$_}
Truth table33%1/3
A and B
A= defined $_B= exists $LEVELS{$_}
ABresult
0X0
100
111
87 12 $self->{'level'} &= ~$LEVELS{$_};
88 }
89  
90 12 return;
91  
92 }
93  
94 14 ▶sub disable_all {
Subroutine disable_all: called CC=1 SCAR=0.0
Pod documented
95  
96 14 my $self = shift->instance;
97  
98 14 $self->{'level'} = 0;
99  
100 14 return;
101  
102 }
103  
104 24 ▶sub enable {
Subroutine enable: called CC=4 SCAR=14.9
Pod documented
105  
106 24 my $self = shift->instance;
107  
108 24 for ( @_ ) {
109 54 ▶ defined && exists $LEVELS{$_} or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and exists $LEVELS{$_} or next 54 0 54
Condition33%1/3
defined $_ and exists $LEVELS{$_}
!ll&&!rl&&r
0054
MC/DC0%0/2
defined $_ and exists $LEVELS{$_}
defined $_ exists $LEVELS{$_}
Truth table33%1/3
A and B
A= defined $_B= exists $LEVELS{$_}
ABresult
0X0
100
111
110 54 $self->{'level'} |= $LEVELS{$_};
111 }
112  
113 24 return;
114  
115 }
116  
117 3 ▶sub enable_all {
Subroutine enable_all: called CC=2 SCAR=6.9
Pod documented
118  
119 3 my $self = shift->instance;
120  
121 3 $self->{'level'} |= $_ for values %LEVELS;
122  
123 3 return;
124  
125 }
126  
127 36 ▶sub is_enabled {
Subroutine is_enabled: called CC=3 SCAR=12.5
Pod documented
128  
129 36 my $self = shift->instance;
130  
131 36 ▶ return defined $_[0] && exists $LEVELS{$_[0]} && $self->{'level'} & $LEVELS{$_[0]};
Condition50%3/6
defined $_[0] && exists $LEVELS{$_[0]} && $self->{'level'} & $LEVELS{$_[0]}
!ll&&!rl&&r
01818
defined $_[0] and exists $LEVELS{$_[0]}
!ll&&!rl&&r
0036
MC/DC33%1/3
defined $_[0] && exists $LEVELS{$_[0]} && $self->{'level'} & $LEVELS{$_[0]}
defined $_[0] exists $LEVELS{$_[0]} $self->{'level'} & $LEVELS{$_[0]}
Truth table50%2/4
A and B && C
A= defined $_[0]B= exists $LEVELS{$_[0]}C= $self->{'level'} & $LEVELS{$_[0]}
ABCresult
0XX0
10X0
1100
1111
132  
133 }
134  
135 27 ▶sub levels {
Subroutine levels: called CC=2 SCAR=6.9
Pod documented
136  
137 27 my $self = shift->instance;
138  
139 27 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 12 15 27
140 12 $self->disable_all;
141 12 $self->enable( @_ );
142 }
143  
144 27 return grep { $self->{'level'} & $LEVELS{$_} } @LEVELS;
145  
146 }
147  
148 24 ▶sub log {
Subroutine log: called CC=5 SCAR=17.1
Pod documented
149  
150 24 my $self = shift->instance;
151  
152 24 my $level;
153  
154 24 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 24 0 24
155 24 $level = shift;
156 24 ▶ return unless defined $level && exists $LEVELS{$level};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level and exists $LEVELS{$level} 0 24 24
Condition33%1/3
defined $level and exists $LEVELS{$level}
!ll&&!rl&&r
0024
MC/DC0%0/2
defined $level and exists $LEVELS{$level}
defined $level exists $LEVELS{$level}
Truth table33%1/3
A and B
A= defined $levelB= exists $LEVELS{$level}
ABresult
0X0
100
111
157 }
158 else {
159 0 $level = $self->_get_default_level;
160 }
161  
162 24 ▶ return unless $self->_get_level & $LEVELS{$level};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_get_level & $LEVELS{$level} 12 12 24
163  
164 12 $self->_log( $level, @_ );
165  
166 12 return 1;
167  
168 }
169  
170 {
171  
172 2 for ( @LEVELS ) {
173  
174 12 my $name = $_;
175 12 my $level = $LEVELS{$name};
176  
177 __PACKAGE__->meta->add_method(
178 24 ▶ $name => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
179 24 my $self = shift->instance;
180 24 ▶ $self->{'level'} & $level or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"level"} & $level or return 12 12 24
181 12 $self->_log( $name, @_ );
182 12 return 1;
183 }
184 12 );
185  
186 }
187  
188 }
189  
190  
191 # ----------------------------------------------------------------------------------------------------------------------
192 # Internal methods
193 # ----------------------------------------------------------------------------------------------------------------------
194  
195 1 ▶sub __finalize {
Subroutine __finalize: called CC=1 SCAR=0.0
196  
197 1 my $self = shift;
198  
199 1 $self->enable_all;
200  
201 1 return;
202  
203 }
204  
205  
206 # ----------------------------------------------------------------------------------------------------------------------
207 # Private methods
208 # ----------------------------------------------------------------------------------------------------------------------
209  
210 0 ▶sub _clear_fh {
Subroutine _clear_fh: not called CC=1 SCAR=6.9
211  
212 0 my $self = shift;
213  
214 0 $self->{'fh'} = undef;
215 0 $self->{'fh_pid'} = undef;
216  
217 0 return;
218  
219 }
220  
221 24 ▶sub _get_fh {
Subroutine _get_fh: called CC=3 SCAR=11.0
222  
223 24 my $self = shift;
224  
225 24 ▶ if ( ! $self->{'fh'} || $self->{'fh_pid'} != $$ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"fh"} or $self->{"fh_pid"} != $$) 1 23 24
Condition66%2/3
not $self->{"fh"} or $self->{"fh_pid"} != $$
l!l&&r!l&&!r
1023
MC/DC50%1/2
not $self->{"fh"} or $self->{"fh_pid"} != $$
not $self->{"fh"} $self->{"fh_pid"} != $$
Truth table66%2/3
not(A) or B
A= not $self->{"fh"}B= $self->{"fh_pid"} != $$
ABresult
1X1
011
000
226 1 $self->{'fh'} = $self->_initialize_fh;
227 1 $self->{'fh_pid'} = $$;
228 }
229  
230 24 return $self->{'fh'};
231  
232 }
233  
234 1 ▶sub _initialize_fh {
Subroutine _initialize_fh: called CC=1 SCAR=0.0
235  
236 1 return \*STDERR;
237  
238 }
239  
240 24 ▶sub _log {
Subroutine _log: called CC=4 SCAR=14.2
241  
242 24 my $self = shift;
243 24 my $level = shift;
244  
245 24 ▶ my $prefix = '[' . ref( $self->application || $self ) . '] [' . $level . '] ';
Condition33%1/3
$self->application || $self
l!l&&r!l&&!r
0240
MC/DC0%0/2
$self->application || $self
$self->application $self
Truth table33%1/3
A || B
A= $self->applicationB= $self
ABresult
1X1
011
000
246  
247 24 for ( @_ ) {
248  
249 24 ▶ my $message = $_ // next;
Condition50%1/2
$_ // (next)
l!l
240
MC/DC0%0/1
$_ // (next)
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
250  
251 24 $message =~ s/\n+$//;
252 24 $message =~ s/\n/\\n/g;
253  
254 24 $self->_get_fh->print( $prefix . $message . "\n" );
255  
256 }
257  
258 24 return;
259  
260 }
261  
262 1 ▶sub _setup_config {
Subroutine _setup_config: called CC=3 SCAR=14.8
263  
264 1 my $self = shift;
265 1 my $args = shift;
266  
267 1 ▶ if ( exists $args->{'levels'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"levels"}) 0 1 1
268  
269 0 my $levels = delete $args->{'levels'};
270  
271 0 ▶ if ( ref $levels ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $levels ne "ARRAY") 0 0 0
272 0 throw "Invalid option (\"levels\" must be an array reference)";
273 }
274  
275 0 $self->levels( @$levels );
276  
277 }
278  
279 1 $self->SUPER::_setup_config( $args );
280  
281 1 return;
282  
283 }
284  
285 0 ▶sub _start {
Subroutine _start: not called CC=1 SCAR=6.9
286  
287 0 my $self = shift;
288  
289 0 $self->_clear_fh;
290  
291 0 return;
292  
293 }
294  
295 1 ▶sub _validate_class_name {
Subroutine _validate_class_name: called CC=1 SCAR=0.0
296  
297 1 return $_[1] =~ /^(?:[_a-zA-Z]\w*::)+?Logger(?:::[_a-zA-Z]\w*)+$/;
298  
299 }
300  
301  
302 2 1;
303  
304 __END__
« GX/Meta/Attribute/Object.pm ↑ Summary GX/Request.pm »