File Coverage

blib/lib/Bot/Cobalt/Logger/Output.pm
Criterion Covered Total %
statement 51 55 92.7
branch 8 14 57.1
condition 3 3 100.0
subroutine 14 15 93.3
pod 3 3 100.0
total 79 90 87.7


line stmt bran cond sub pod time code
1             package Bot::Cobalt::Logger::Output;
2             $Bot::Cobalt::Logger::Output::VERSION = '0.021001';
3 7     7   13375 use Carp;
  7         9  
  7         341  
4 7     7   379 use strictures 2;
  7         1072  
  7         209  
5              
6 7     7   1242 use Bot::Cobalt::Common qw/:types :string/;
  7         9  
  7         35  
7 7     7   1297 use POSIX ();
  7         7566  
  7         147  
8 7     7   22 use Try::Tiny;
  7         6  
  7         262  
9              
10 7     7   852 use Moo;
  7         11477  
  7         31  
11              
12             has time_format => (
13             is => 'rw',
14             isa => Str,
15 3     3   77 builder => sub { "%Y-%m-%d %H:%M:%S" }, # strftime
16             );
17              
18             has log_format => (
19             is => 'rw',
20             isa => Str,
21 3     3   94 builder => sub { "%level %time (%pkg%) %msg" }, # rplprintf
22             );
23              
24              
25             has _outputs => (
26             is => 'rwp',
27             isa => HashRef,
28             default => sub { +{} },
29             );
30              
31              
32             sub add {
33 4     4 1 2765 my ($self, @args) = @_;
34            
35 4 100 100     26 unless (@args && @args % 2 == 0) {
36 2         239 confess "add() expects an even number of arguments, ",
37             "mapping an Output class to a HASH of constructor arguments"
38             }
39            
40 2         5 my $prefix = 'Bot::Cobalt::Logger::Output::' ;
41            
42 2         40 CONFIG: while (my ($alias, $opts) = splice @args, 0, 2) {
43 3 50       12 confess "Can't add $alias, opts are not a HASH"
44             unless ref $opts eq 'HASH';
45              
46             confess "Can't add $alias, no type specified"
47 3 50       7 unless $opts->{type};
48              
49 3         8 my $target_pkg = $prefix . delete $opts->{type};
50              
51 3         3 { local $@;
  3         4  
52 3         183 eval "require $target_pkg";
53            
54 3 50       22 if (my $err = $@) {
55 0         0 carp "Could not add logger $alias: $err";
56             next CONFIG
57 0         0 }
58             }
59              
60             my $new_obj = try {
61 3     3   128 $target_pkg->new(%$opts)
62             } catch {
63 0     0   0 carp "Could not add logger $alias; new() died: $_";
64             undef
65 3 50       26 } or next CONFIG;
  0         0  
66              
67 3         77 $self->_outputs->{$alias} = $new_obj;
68             } ## CONFIG
69              
70             1
71 2         9 }
72              
73             sub del {
74 1     1 1 3 my ($self, @aliases) = @_;
75 1         1 my $x;
76              
77 1         2 for my $alias (@aliases) {
78 2 50       19 ++$x if delete $self->_outputs->{$alias}
79             }
80              
81             $x
82 1         5 }
83              
84             sub get {
85 1     1 1 310 my ($self, $alias) = @_;
86 1         4 $self->_outputs->{$alias}
87             }
88              
89              
90             ## Private.
91             sub _format {
92 6     6   9 my ($self, $level, $caller, @strings) = @_;
93            
94 6         87 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 7     7   680 my $self = shift;
111              
112 7         9 for my $alias (keys %{ $self->_outputs }) {
  7         32  
113 6         11 my $output = $self->_outputs->{$alias};
114 6 50       38 $output->_write(
115             ## Output classes can provide their own _format
116             $output->can('_format') ? $output->_format( @_ )
117             : $self->_format( @_ )
118             )
119             }
120              
121             1
122 7         13 }
123              
124             1;
125             __END__