blib/lib/Bot/Cobalt/Logger/Output.pm

filter: Statementstmt 37.7% 23 / 61 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 37.5% 6 / 16 Podpod 100% 3 / 3 totaltotal 31.6% 32 / 101 CC 12 SCAR 32.1
CC
12
Cov
29%
add
72.0
del
12.0
_write
12.0
CRAP 24.7
« blib/lib/Bot/Cobalt/IRC/FloodChk.pm ↑ Summary blib/lib/Bot/Cobalt/Core/Role/EasyAccessors.pm »
1 package Bot::Cobalt::Logger::Output;
2 1 $Bot::Cobalt::Logger::Output::VERSION = '0.021001';
3 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Bot::Cobalt::Common qw/:types :string/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 has time_format => (
13 is => 'rw',
14 isa => Str,
15 0 ▶ builder => sub { "%Y-%m-%d %H:%M:%S" }, # strftime
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 1 );
17  
18 has log_format => (
19 is => 'rw',
20 isa => Str,
21 0 ▶ builder => sub { "%level %time (%pkg%) %msg" }, # rplprintf
Subroutine _build_log_format: not called CC=1 SCAR=6.9
22 1 );
23  
24  
25 has _outputs => (
26 is => 'rwp',
27 isa => HashRef,
28 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 1 );
30  
31  
32 sub add {
33 0 ▶ my ($self, @args) = @_;
Subroutine add: not called CC=8 SCAR=42.8
Pod documented
34
35 0 ▶ unless (@args && @args % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args and @args % 2 == 0) 0 0 0
Condition0%0/3
@args and @args % 2 == 0
!ll&&!rl&&r
000
MC/DC0%0/2
@args and @args % 2 == 0
@args @args % 2 == 0
Truth table0%0/3
A and B
A= @argsB= @args % 2 == 0
ABresult
0X0
100
111
36 0 confess "add() expects an even number of arguments, ",
37 "mapping an Output class to a HASH of constructor arguments"
38 }
39
40 0 my $prefix = 'Bot::Cobalt::Logger::Output::' ;
41
42 0 ▶ CONFIG: while (my ($alias, $opts) = splice @args, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@args, 0, 2)) 0 0 0
43 0 ▶ confess "Can't add $alias, opts are not a HASH"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts eq "HASH" 0 0 0
44 unless ref $opts eq 'HASH';
45  
46 confess "Can't add $alias, no type specified"
47 0 ▶ unless $opts->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"type"} 0 0 0
48  
49 0 my $target_pkg = $prefix . delete $opts->{type};
50  
51 0 { local $@;
52 0 eval "require $target_pkg";
53
54 0 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 0 0
55 0 carp "Could not add logger $alias: $err";
56 next CONFIG
57 0 }
58 }
59  
60 my $new_obj = try {
61 0 ▶ $target_pkg->new(%$opts)
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 } catch {
63 0 ▶ carp "Could not add logger $alias; new() died: $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 undef
65 0 ▶ } or next CONFIG;
Branch: 1 branch
BranchTrueFalseTotal
my $new_obj = &try(sub { $target_pkg->new(%$opts); } , &catch(sub { carp("Could not add logger $alias; new() died: $_"); undef; } )) or next CONFIG 0 0 0
66  
67 0 $self->_outputs->{$alias} = $new_obj;
68 } ## CONFIG
69  
70 1
71 0 }
72  
73 sub del {
74 0 ▶ my ($self, @aliases) = @_;
Subroutine del: not called CC=3 SCAR=24.8
Pod documented
75 0 my $x;
76  
77 0 for my $alias (@aliases) {
78 0 ▶ ++$x if delete $self->_outputs->{$alias}
Branch: 1 branch
BranchTrueFalseTotal
if delete $self->_outputs->{$alias} 0 0 0
79 }
80  
81 $x
82 0 }
83  
84 sub get {
85 0 ▶ my ($self, $alias) = @_;
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
86 0 $self->_outputs->{$alias}
87 }
88  
89  
90 ## Private.
91 sub _format {
92 0 ▶ my ($self, $level, $caller, @strings) = @_;
Subroutine _format: not called CC=1 SCAR=6.9
93
94 0 rplprintf( $self->log_format, {
95 level => $level,
96  
97 ## Actual message.
98 msg => join(' ', @strings),
99  
100 time => POSIX::strftime( $self->time_format, localtime ),
101  
102 pkg => $caller->[0],
103 file => $caller->[1],
104 line => $caller->[2],
105 sub => $caller->[3],
106 }) . "\n"
107 }
108  
109 sub _write {
110 0 ▶ my $self = shift;
Subroutine _write: not called CC=3 SCAR=24.8
111  
112 0 for my $alias (keys %{ $self->_outputs }) {
113 0 my $output = $self->_outputs->{$alias};
114 0 ▶ $output->_write(
Branch: 1 branch
BranchTrueFalseTotal
$output->can('_format') ? : 0 0 0
115 ## Output classes can provide their own _format
116 $output->can('_format') ? $output->_format( @_ )
117 : $self->_format( @_ )
118 )
119 }
120  
121 1
122 0 }
123  
124 1 1;
125 __END__
« blib/lib/Bot/Cobalt/IRC/FloodChk.pm ↑ Summary blib/lib/Bot/Cobalt/Core/Role/EasyAccessors.pm »