File Coverage

blib/lib/Log/Dispatch/Base.pm
Criterion Covered Total %
statement 49 49 100.0
branch 8 10 80.0
condition 4 5 80.0
subroutine 10 10 100.0
pod 0 3 0.0
total 71 77 92.2


line stmt bran cond sub pod time code
1             package Log::Dispatch::Base;
2              
3 29     29   205 use strict;
  29         79  
  29         815  
4 29     29   143 use warnings;
  29         59  
  29         616  
5              
6 29     29   120 use Carp ();
  29         71  
  29         1156  
7             use Log::Dispatch::Vars
8 29     29   159 qw( %CanonicalLevelNames %LevelNamesToNumbers @OrderedLevels );
  29         56  
  29         2918  
9 29     29   150 use Scalar::Util qw( refaddr );
  29         53  
  29         11434  
10              
11             our $VERSION = '2.69';
12              
13             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
14             sub _level_as_number {
15 187     187   265 my $self = shift;
16 187         253 my $level = shift;
17              
18 187         492 my $level_name = $self->level_is_valid($level);
19 186 100       386 return unless $level_name;
20              
21 185         447 return $LevelNamesToNumbers{$level_name};
22             }
23             ## use critic
24              
25             sub level_is_valid {
26 202     202 0 7424 shift;
27 202         253 my $level = shift;
28              
29 202 100       486 if ( !defined $level ) {
30 1         173 Carp::croak('Logging level was not provided');
31             }
32              
33 201 100 66     820 if ( $level =~ /\A[0-9]+\z/ && $level <= $#OrderedLevels ) {
34 15         44 return $OrderedLevels[$level];
35             }
36              
37 186         653 return $CanonicalLevelNames{$level};
38             }
39              
40             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
41             sub _apply_callbacks {
42 86     86   185 my $self = shift;
43 86         241 my %p = @_;
44              
45 86         285 my $msg = delete $p{message};
46 86         195 for my $cb ( @{ $self->{callbacks} } ) {
  86         202  
47 89         505 $msg = $cb->( message => $msg, %p );
48             }
49              
50 86         309 return $msg;
51             }
52              
53             sub add_callback {
54 5     5 0 2758 my $self = shift;
55 5         8 my $value = shift;
56              
57 5 50       36 Carp::carp("given value $value is not a valid callback")
58             unless ref $value eq 'CODE';
59              
60 5   100     19 $self->{callbacks} ||= [];
61 5         7 push @{ $self->{callbacks} }, $value;
  5         12  
62              
63 5         8 return;
64             }
65              
66             sub remove_callback {
67 2     2 0 1031 my $self = shift;
68 2         4 my $cb = shift;
69              
70 2 50       7 Carp::carp("given value $cb is not a valid callback")
71             unless ref $cb eq 'CODE';
72              
73 2         7 my $cb_id = refaddr $cb;
74             $self->{callbacks}
75 2         3 = [ grep { refaddr $_ ne $cb_id } @{ $self->{callbacks} } ];
  3         11  
  2         5  
76              
77 2         4 return;
78             }
79              
80             1;
81              
82             # ABSTRACT: Code shared by dispatch and output objects.
83              
84             __END__
85              
86             =pod
87              
88             =encoding UTF-8
89              
90             =head1 NAME
91              
92             Log::Dispatch::Base - Code shared by dispatch and output objects.
93              
94             =head1 VERSION
95              
96             version 2.69
97              
98             =head1 SYNOPSIS
99              
100             use Log::Dispatch::Base;
101              
102             ...
103              
104             @ISA = qw(Log::Dispatch::Base);
105              
106             =head1 DESCRIPTION
107              
108             Unless you are me, you probably don't need to know what this class
109             does.
110              
111             =for Pod::Coverage .*
112              
113             =head1 SUPPORT
114              
115             Bugs may be submitted at L<https://github.com/houseabsolute/Log-Dispatch/issues>.
116              
117             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
118              
119             =head1 SOURCE
120              
121             The source code repository for Log-Dispatch can be found at L<https://github.com/houseabsolute/Log-Dispatch>.
122              
123             =head1 AUTHOR
124              
125             Dave Rolsky <autarch@urth.org>
126              
127             =head1 COPYRIGHT AND LICENSE
128              
129             This software is Copyright (c) 2019 by Dave Rolsky.
130              
131             This is free software, licensed under:
132              
133             The Artistic License 2.0 (GPL Compatible)
134              
135             The full text of the license can be found in the
136             F<LICENSE> file included with this distribution.
137              
138             =cut