| 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
Condition100%3/3
@args and @args % 2 == 0
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | confess "Can't add $alias, opts are not a HASH" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | unless ref $opts eq 'HASH'; | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | confess "Can't add $alias, no type specified" | ||||||||||||||||||||||||||||
| 47 | 2 | ▶ | unless $opts->{type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||