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

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 SCAR 0.1
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Bot/Cobalt/Logger.pm ↑ Summary blib/lib/Bot/Cobalt/Plugin/Auth.pm »
1 package Bot::Cobalt::Logger::Output;
2 $Bot::Cobalt::Logger::Output::VERSION = '0.021002';
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Bot::Cobalt::Common qw/:types :string/;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use POSIX ();
8 use Try::Tiny;
9  
10 use Moo;
11  
12 has time_format => (
13 is => 'rw',
14 isa => Str,
15 builder => sub { "%Y-%m-%d %H:%M:%S" }, # strftime
16 );
17  
18 has log_format => (
19 is => 'rw',
20 isa => Str,
21 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 my ($self, @args) = @_;
34
35 unless (@args && @args % 2 == 0) {
36 confess "add() expects an even number of arguments, ",
37 "mapping an Output class to a HASH of constructor arguments"
38 }
39
40 my $prefix = 'Bot::Cobalt::Logger::Output::' ;
41
42 CONFIG: while (my ($alias, $opts) = splice @args, 0, 2) {
43 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 unless $opts->{type};
48  
49 my $target_pkg = $prefix . delete $opts->{type};
50  
51 { local $@;
52 eval "require $target_pkg";
53
54 if (my $err = $@) {
55 carp "Could not add logger $alias: $err";
56 next CONFIG
57 }
58 }
59  
60 my $new_obj = try {
61 $target_pkg->new(%$opts)
62 } catch {
63 carp "Could not add logger $alias; new() died: $_";
64 undef
65 } or next CONFIG;
66  
67 $self->_outputs->{$alias} = $new_obj;
68 } ## CONFIG
69  
70 1
71 }
72  
73 sub del {
74 my ($self, @aliases) = @_;
75 my $x;
76  
77 for my $alias (@aliases) {
78 ++$x if delete $self->_outputs->{$alias}
79 }
80  
81 $x
82 }
83  
84 sub get {
85 my ($self, $alias) = @_;
86 $self->_outputs->{$alias}
87 }
88  
89  
90 ## Private.
91 sub _format {
92 my ($self, $level, $caller, @strings) = @_;
93
94 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 my $self = shift;
111  
112 for my $alias (keys %{ $self->_outputs }) {
113 my $output = $self->_outputs->{$alias};
114 $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 }
123  
124 1;
125 __END__
« blib/lib/Bot/Cobalt/Logger.pm ↑ Summary blib/lib/Bot/Cobalt/Plugin/Auth.pm »