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

filter: Statementstmt 93.6% 59 / 63 Branchbran 62.5% 10 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 94.4% 17 / 18 Podpod 100% 3 / 3 totaltotal 89.5% 94 / 105 CC 12 SCAR 12.7
CC
12
Cov
88%
add
8.8
del
3.0
_write
3.0
CRAP 3.6
« blib/lib/Bot/Cobalt/Conf/File/Core.pm ↑ Summary blib/lib/Bot/Cobalt/Plugin/Games/Magic8.pm »
1 package Bot::Cobalt::Logger::Output;
2 2 $Bot::Cobalt::Logger::Output::VERSION = '0.021003';
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 2 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 has time_format => (
13 is => 'rw',
14 isa => Str,
15 1 ▶ builder => sub { "%Y-%m-%d %H:%M:%S" }, # strftime
Subroutine _build_time_format: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
16 2 );
17  
18 has log_format => (
19 is => 'rw',
20 isa => Str,
21 1 ▶ builder => sub { "%level %time (%pkg%) %msg" }, # rplprintf
Subroutine _build_log_format: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
22 2 );
23  
24  
25 has _outputs => (
26 is => 'rwp',
27 isa => HashRef,
28 2 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 2 );
30  
31  
32 3 ▶sub add {
Subroutine add: called CC=8 SCAR=21.7
Pod documented
33 3 my ($self, @args) = @_;
34
35 3 ▶ unless (@args && @args % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args and @args % 2 == 0) 2 1 3
Condition100%3/3
@args and @args % 2 == 0
!ll&&!rl&&r
111
MC/DC100%2/2
@args and @args % 2 == 0
@args @args % 2 == 0
Truth table100%3/3
A and B
A= @argsB= @args % 2 == 0
ABresult
0X0
100
111
36 2 confess "add() expects an even number of arguments, ",
37 "mapping an Output class to a HASH of constructor arguments"
38 }
39
40 1 my $prefix = 'Bot::Cobalt::Logger::Output::' ;
41
42 1 ▶ CONFIG: while (my ($alias, $opts) = splice @args, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@args, 0, 2)) 2 1 3
43 2 ▶ confess "Can't add $alias, opts are not a HASH"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts eq "HASH" 0 2 2
44 unless ref $opts eq 'HASH';
45  
46 confess "Can't add $alias, no type specified"
47 2 ▶ unless $opts->{type};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"type"} 0 2 2
48  
49 2 my $target_pkg = $prefix . delete $opts->{type};
50  
51 2 { local $@;
52 2 eval "require $target_pkg";
53
54 2 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 2 2
55 0 carp "Could not add logger $alias: $err";
56 next CONFIG
57 0 }
58 }
59  
60 2 ▶ my $new_obj = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 2 $target_pkg->new(%$opts)
62 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=2.0
63 0 carp "Could not add logger $alias; new() died: $_";
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 2 0 2
66  
67 2 $self->_outputs->{$alias} = $new_obj;
68 } ## CONFIG
69  
70 1
71 1 }
72  
73 1 ▶sub del {
Subroutine del: called CC=3 SCAR=11.1
Pod documented
74 1 my ($self, @aliases) = @_;
75 1 my $x;
76  
77 1 for my $alias (@aliases) {
78 2 ▶ ++$x if delete $self->_outputs->{$alias}
Branch: 1 branch
BranchTrueFalseTotal
if delete $self->_outputs->{$alias} 2 0 2
79 }
80  
81 $x
82 1 }
83  
84 1 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
85 1 my ($self, $alias) = @_;
86 1 $self->_outputs->{$alias}
87 }
88  
89  
90 ## Private.
91 2 ▶sub _format {
Subroutine _format: called CC=1 SCAR=0.0
92 2 my ($self, $level, $caller, @strings) = @_;
93
94 2 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 3 ▶sub _write {
Subroutine _write: called CC=3 SCAR=11.0
110 3 my $self = shift;
111  
112 3 for my $alias (keys %{ $self->_outputs }) {
113 2 my $output = $self->_outputs->{$alias};
114 2 ▶ $output->_write(
Branch: 1 branch
BranchTrueFalseTotal
$output->can('_format') ? : 0 2 2
115 ## Output classes can provide their own _format
116 $output->can('_format') ? $output->_format( @_ )
117 : $self->_format( @_ )
118 )
119 }
120  
121 1
122 3 }
123  
124 2 1;
125 __END__
« blib/lib/Bot/Cobalt/Conf/File/Core.pm ↑ Summary blib/lib/Bot/Cobalt/Plugin/Games/Magic8.pm »