File Coverage

blib/lib/Log/Dispatch/Channels.pm
Criterion Covered Total %
statement 74 74 100.0
branch 14 14 100.0
condition 3 3 100.0
subroutine 17 17 100.0
pod 12 12 100.0
total 120 120 100.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             package Log::Dispatch::Channels;
3             our $VERSION = '0.01';
4              
5 5     5   149926 use strict;
  5         11  
  5         230  
6 5     5   28 use warnings;
  5         10  
  5         216  
7 5     5   5881 use Log::Dispatch;
  5         115672  
  5         177  
8 5     5   56 use Carp;
  5         11  
  5         5292  
9              
10             # ABSTRACT: Adds separate logging channels to Log::Dispatch
11              
12              
13              
14             sub new {
15 5     5 1 1289 my $class = shift;
16              
17 5         34 my $self = bless {
18             channels => {},
19             outputs => {},
20             }, $class;
21              
22 5         27 return $self;
23             }
24              
25              
26             sub add_channel {
27 13     13 1 117 my $self = shift;
28 13         19 my $name = shift;
29              
30 13 100       272 carp "Channel $name already exists!"
31             if exists $self->{channels}{$name};
32              
33 13         113 $self->{channels}{$name} = Log::Dispatch->new(@_);
34             }
35              
36              
37             sub remove_channel {
38 1     1 1 3 my $self = shift;
39 1         3 my $name = shift;
40              
41 1         20 return delete $self->{channels}{$name};
42             }
43              
44             sub _forward_to_channels {
45 33     33   50 my $self = shift;
46 33         45 my $channels = shift;
47 33         42 my $method = shift;
48 10         41 my @channels = !defined $channels
49 33 100       111 ? (keys %{ $self->{channels} })
    100          
50             : ref $channels
51             ? @$channels
52             : ($channels);
53              
54             # XXX: sort of a hack - the return value is only used by would_log, which
55             # just wants a boolean
56 33         43 my $ret = 0;
57 33         58 for my $channel (@channels) {
58 57 100       140 if (exists $self->{channels}{$channel}) {
59 56         217 my $methodret = $self->{channels}{$channel}->$method(@_);
60 54   100     3319 $ret ||= $methodret;
61             }
62             else {
63 1         116 carp "Channel $channel doesn't exist";
64             }
65             }
66 31         227 return $ret;
67             }
68              
69              
70             sub add {
71 18     18 1 3627 my $self = shift;
72 18         26 my $output = shift;
73 18         33 my %args = @_;
74              
75 18 100       76 carp "Output " . $output->name . " already exists!"
76             if exists $self->{outputs}{$output->name};
77              
78 18         309 $self->_forward_to_channels($args{channels}, 'add', $output);
79 18         60 $self->{outputs}{$output->name} = $output;
80             }
81              
82              
83             sub remove {
84 4     4 1 449 my $self = shift;
85 4         8 my $name = shift;
86 4         11 my %args = @_;
87              
88 4         32 $self->_forward_to_channels(undef, 'remove', $name);
89 4         52 return delete $self->{outputs}{$name};
90             }
91              
92              
93             sub log {
94 6     6 1 605 my $self = shift;
95 6         23 my %args = @_;
96 6         13 my $channels = delete $args{channels};
97              
98 6         21 $self->_forward_to_channels($channels, 'log', %args);
99             }
100              
101              
102             sub log_and_die {
103 1     1 1 62 my $self = shift;
104 1         5 my %args = @_;
105 1         5 my $channels = delete $args{channels};
106              
107 1         4 $self->_forward_to_channels($channels, 'log_and_die', %args);
108             }
109              
110              
111             sub log_and_croak {
112 1     1 1 583 my $self = shift;
113 1         4 my %args = @_;
114 1         3 my $channels = delete $args{channels};
115              
116 1         6 $self->_forward_to_channels($channels, 'log_and_croak', %args);
117             }
118              
119              
120             sub log_to {
121 1     1 1 6 my $self = shift;
122 1         4 my %args = @_;
123 1         2 my $output = delete $args{name};
124              
125 1         6 $self->{outputs}{$output}->log(%args);
126             }
127              
128              
129             sub would_log {
130 3     3 1 530 my $self = shift;
131 3         6 my $level = shift;
132 3         7 my %args = @_;
133 3         7 my $channels = delete $args{channels};
134              
135 3         8 return $self->_forward_to_channels($channels, 'would_log', $level);
136             }
137              
138              
139             sub output {
140 13     13 1 9004 my $self = shift;
141 13         19 my $name = shift;
142              
143 13 100       83 return $self->{outputs}{$name} if exists $self->{outputs}{$name};
144 1         8 return undef;
145             }
146              
147              
148             sub channel {
149 11     11 1 12383 my $self = shift;
150 11         15 my $name = shift;
151              
152 11 100       72 return $self->{channels}{$name} if exists $self->{channels}{$name};
153 1         4 return undef;
154             }
155              
156              
157             1;
158              
159             __END__