lib/Mail/Decency/Helper/Logger.pm

filter: Statementstmt 28.0% 30 / 107 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 23.0% 6 / 26 Podpod 88.8% 8 / 9 totaltotal 21.8% 44 / 201 CC 28 SCAR 38.5
CC
28
Cov
19%
BUILD
132.0
__ANON__
90.0
__ANON__
20.0
CRAP 46.9
« lib/Mail/Decency/Policy/DNSBL.pm ↑ Summary lib/Mail/Decency/Policy/Core/CWLCBL.pm »
1 package Mail::Decency::Helper::Logger;
2  
3 24 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 24 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 24 ▶use Sys::Syslog qw/ :standard :macros /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use Scalar::Util qw/ weaken /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use File::Path qw/ make_path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use Carp qw/ carp /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Mail::Decency::Helper::Logger
15  
16 =head1 DESCRIPTION
17  
18 Helper modules for Decency policies or content filters
19  
20 =cut
21  
22 24 has prefix => ( is => 'rw', isa => 'Str', default => '' );
23 24 has syslog => ( is => 'rw', isa => 'Bool' );
24 24 has console => ( is => 'rw', isa => 'Bool' );
25 24 has directory => ( is => 'rw', isa => 'Str' );
26 24 has log_level => ( is => 'rw', default => 0 );
27 24 has disabled => ( is => 'rw', isa => 'Bool', default => 0 );
28 0 ▶has _log_level => ( is => 'ro', isa => 'HashRef', default => sub { {
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 error => 0,
30 info => 1,
31 verbose => 2,
32 debug0 => 3,
33 debug1 => 4,
34 debug2 => 5,
35 debug3 => 6,
36 24 } } );
37 0 ▶has _log_file_handles => ( is => 'rw', isa => 'HashRef', default => sub { {} } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
38 0 ▶has _log_file_inodes => ( is => 'rw', isa => 'HashRef', default => sub { {} } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
39 24 has _log_method => ( is => 'rw', isa => 'CodeRef' );
40  
41  
42  
43 0 ▶sub BUILD {
Subroutine BUILD: not called CC=11 SCAR=48.8
Pod undocumented
44 0 my ( $self, $args_ref ) = @_;
45
46 0 my @print = ();
47
48 # determine log level
49 my $log_level = defined $args_ref->{ log_level }
50 ? $args_ref->{ log_level }
51 0 ▶ : "info"
Branch: 1 branch
BranchTrueFalseTotal
defined $args_ref->{'log_level'} ? : 0 0 0
52 ;
53 $log_level = $log_level !~ /^\d+$/
54 ? ( defined $self->_log_level->{ $log_level }
55 0 ▶ ? $self->_log_level->{ $log_level }
Branch: 2 branches
BranchTrueFalseTotal
!($log_level =~ /^\d+$/) ? : 0 0 0
defined $self->_log_level->{$log_level} ? : 0 0 0
56 : 1
57 )
58 : $log_level
59 ;
60 0 $self->log_level( $log_level );
61 0 push @print, "LogLevel: $log_level";
62
63 # determine output
64 0 foreach my $output( qw/ syslog console directory / ) {
65 $self->$output( $args_ref->{ $output } )
66 0 ▶ if $args_ref->{ $output };
Branch: 1 branch
BranchTrueFalseTotal
if $args_ref->{$output} 0 0 0
67 }
68
69
70 0 my @methods = ();
71
72 # enable syslog
73 0 ▶ if ( $self->syslog ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->syslog) 0 0 0
74 0 openlog( "decency", "ndelay,pid", "local0" );
75 0 ▶ push @methods, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
76 0 my ( $self, $int_log_level, $str_log_level, $msg ) = @_;
77 0 ▶ my $level = $int_log_level == 0
Branch: 2 branches
BranchTrueFalseTotal
$int_log_level == 0 ? : 0 0 0
$int_log_level == 1 ? : 0 0 0
78 ? LOG_ERR
79 : ( $int_log_level == 1
80 ? LOG_INFO
81 : LOG_DEBUG
82 )
83 ;
84 0 ▶ my $suffix = $level == LOG_DEBUG
Branch: 1 branch
BranchTrueFalseTotal
$level == 7 ? : 0 0 0
85 ? "/$str_log_level"
86 : ""
87 ;
88 0 Sys::Syslog::syslog( $level => "[". $self->prefix. $suffix. "]: $msg" );
89 0 };
90 0 push @print, "Enable Syslog";
91 }
92
93 # enable console
94 0 ▶ if ( $self->console ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->console) 0 0 0
95 0 ▶ push @methods, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 my ( $self, $int_log_level, $str_log_level, $msg ) = @_;
97 0 warn "[$$/". localtime(). "/". $self->prefix. "/$str_log_level]: $msg\n";
98 0 };
99 0 push @print, "Enable Console";
100 }
101
102 # enable directory
103 0 ▶ if ( $self->directory ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->directory) 0 0 0
104 0 ▶ make_path( $self->directory, { mode => 0700 } )
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->directory 0 0 0
105 unless -d $self->directory;
106 0 ▶ die "Could not create log directory '". $self->directory. "'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->directory 0 0 0
107 unless -d $self->directory;
108
109 0 ▶ my $log_sub = sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
110 0 my ( $file, $self, $int_log_level, $str_log_level, $msg ) = @_;
111 0 my ( undef, $inode ) = stat( $file );
112
113 0 my $fh = $self->_log_file_handles->{ $file };
114 0 ▶ if ( ! $fh || ! -f $file || ! defined $self->_log_file_inodes->{ $file } || $inode != $self->_log_file_inodes->{ $file } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fh && -f $file && defined $self->_log_file_inodes->{$file} or $inode != $self->_log_file_inodes->{$file}) 0 0 0
Condition0%0/9
not $fh && -f $file && defined $self->_log_file_inodes->{$file} or $inode != $self->_log_file_inodes->{$file}
l!l&&r!l&&!r
000
$fh && -f $file && defined $self->_log_file_inodes->{$file}
!ll&&!rl&&r
000
$fh and -f $file
!ll&&!rl&&r
000
MC/DC0%0/4
not $fh && -f $file && defined $self->_log_file_inodes->{$file} or $inode != $self->_log_file_inodes->{$file}
$fh -f $file defined $self->_log_file_inodes->{$file} $inode != $self->_log_file_inodes->{$file}
Truth table0%0/5
not(A and B && C) or D
A= $fhB= -f $fileC= defined $self->_log_file_inodes->{$file}D= $inode != $self->_log_file_inodes->{$file}
ABCDresult
0XXX1
10XX1
110X1
11111
11100
115 0 ▶ eval { close $fh if $fh; };
Branch: 1 branch
BranchTrueFalseTotal
if $fh 0 0 0
116 0 ▶ my $mode = -f $file ? ">>" : ">";
Branch: 1 branch
BranchTrueFalseTotal
-f $file ? : 0 0 0
117 0 ▶ open $fh, $mode, $file
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $mode, $file or carp("Cannot open '${file}' for write/append: $!") 0 0 0
118 or carp "Cannot open '$file' for write/append: $!";
119 0 $self->_log_file_handles->{ $file } = $fh;
120 0 $self->_log_file_inodes->{ $file } = $inode;
121 }
122 0 ▶ print $fh "[$$/". localtime(). "/". $self->prefix. "/$str_log_level]: $msg\n"
Branch: 1 branch
BranchTrueFalseTotal
print $fh "[${$}/" . (localtime()) . '/' . $self->prefix . "/$str_log_level]: $msg\n" or carp("Failed print to '${file}': $!") 0 0 0
123 or carp "Failed print to '$file': $!";
124 0 };
125
126 0 my $dir = $self->directory;
127 my $log_sub_ref = {
128 0 ▶ error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 0 $log_sub->( "$dir/error.log", @_ );
130 },
131 0 ▶ info => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 $log_sub->( "$dir/info.log", @_ );
133 },
134 0 ▶ debug => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 $log_sub->( "$dir/debug.log", @_ );
136 }
137 0 };
138 0 my $log_sub_map_ref = {
139 0 => "error",
140 1 => "info",
141 };
142
143 0 ▶ push @methods, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
144 0 my ( $self, $int_log_level, $str_log_level, $msg ) = @_;
145 0 ▶ my $name = $log_sub_map_ref->{ $int_log_level } || "debug";
Condition0%0/2
$log_sub_map_ref->{$int_log_level} || 'debug'
l!l
00
MC/DC0%0/1
$log_sub_map_ref->{$int_log_level} || 'debug'
$log_sub_map_ref->{$int_log_level}
Truth table0%0/2
A
A= $log_sub_map_ref->{$int_log_level}
Aresult
11
00
146 0 $log_sub_ref->{ $name }->( $self, $int_log_level, $str_log_level, $msg );
147 0 };
148 0 push @print, "Enable Directory";
149 }
150
151 # build the logger method
152 0 ▶ $self->_log_method( sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
153 0 my ( $self, $log_level, $msg ) = @_;
154 0 ▶ my $int_log_level = $self->_log_level->{ $log_level } || 0;
Condition0%0/2
$self->_log_level->{$log_level} || 0
l!l
00
MC/DC0%0/1
$self->_log_level->{$log_level} || 0
$self->_log_level->{$log_level}
Truth table0%0/2
A
A= $self->_log_level->{$log_level}
Aresult
11
00
155 return
156 0 ▶ if $int_log_level > $self->log_level;
Branch: 1 branch
BranchTrueFalseTotal
if $int_log_level > $self->log_level 0 0 0
157 0 $_->( $self, $int_log_level, $log_level, $msg ) for @methods;
158 0 } );
159
160 0 $self->log( info => "Inited Logger: ". join( ", ", @print ) );
161
162 0 return $self;
163 }
164  
165 =head2 log
166  
167 =cut
168  
169 0 ▶sub log {
Subroutine log: not called CC=2 SCAR=17.9
Pod documented
170 0 my ( $self, $log_level, $msg ) = @_;
171 0 ▶ return if $self->disabled;
Branch: 1 branch
BranchTrueFalseTotal
if $self->disabled 0 0 0
172 0 $self->_log_method->( $self, $log_level, $msg );
173 }
174  
175 =head2 error
176  
177 Log error level
178  
179 =cut
180  
181 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
182 0 my ( $self, $msg ) = @_;
183 0 $self->log( error => $msg );
184 }
185  
186 =head2 info
187  
188 Log info level
189  
190 =cut
191  
192 0 ▶sub info {
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
193 0 my ( $self, $msg ) = @_;
194 0 $self->log( info => $msg );
195 }
196  
197 =head2 debug0
198  
199 Log debug0 level
200  
201 =cut
202  
203 0 ▶sub debug0 {
Subroutine debug0: not called CC=1 SCAR=6.9
Pod documented
204 0 my ( $self, $msg ) = @_;
205 0 $self->log( debug0 => $msg );
206 }
207  
208 =head2 debug1
209  
210 Log debug1 level
211  
212 =cut
213  
214 0 ▶sub debug1 {
Subroutine debug1: not called CC=1 SCAR=6.9
Pod documented
215 0 my ( $self, $msg ) = @_;
216 0 $self->log( debug1 => $msg );
217 }
218  
219 =head2 debug2
220  
221 Log debug2 level
222  
223 =cut
224  
225 0 ▶sub debug2 {
Subroutine debug2: not called CC=1 SCAR=6.9
Pod documented
226 0 my ( $self, $msg ) = @_;
227 0 $self->log( debug2 => $msg );
228 }
229  
230 =head2 debug3
231  
232 Log debug3 level
233  
234 =cut
235  
236 0 ▶sub debug3 {
Subroutine debug3: not called CC=1 SCAR=6.9
Pod documented
237 0 my ( $self, $msg ) = @_;
238 0 $self->log( debug3 => $msg );
239 }
240  
241  
242  
243 =head2 clone
244  
245 Returns new instance of self
246  
247 =cut
248  
249 0 ▶sub clone {
Subroutine clone: not called CC=2 SCAR=17.9
Pod documented
250 0 my ( $self, $prefix ) = @_;
251 0 my $clone = bless { %$self }, ref( $self );
252 0 ▶ $clone->prefix( $prefix ) if $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 0 0
253 0 return $clone;
254 }
255  
256  
257  
258  
259  
260 =head1 AUTHOR
261  
262 Ulrich Kautz <uk@fortrabbit.de>
263  
264 =head1 COPYRIGHT
265  
266 Copyright (c) 2010 the L</AUTHOR> as listed above
267  
268 =head1 LICENCSE
269  
270 This library is free software and may be distributed under the same terms as perl itself.
271  
272 =cut
273  
274 24 1;
275  
« lib/Mail/Decency/Policy/DNSBL.pm ↑ Summary lib/Mail/Decency/Policy/Core/CWLCBL.pm »