blib/lib/No/Worries/Syslog.pm

filter: Statementstmt 63.1% 65 / 103 Branchbran 29.4% 10 / 34 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 59.0% 13 / 22 Podpod 100% 8 / 8 totaltotal 55.8% 96 / 172 CC 20 SCAR 28.2
CC
20
Cov
54%
_syslog_any
56.0
syslog_open
30.0
log2syslog
20.0
CRAP 16.9
« blib/lib/No/Worries/Proc.pm ↑ Summary blib/lib/No/Worries/Stat.pm »
1 #+##############################################################################
2 # #
3 # File: No/Worries/Syslog.pm #
4 # #
5 # Description: syslog handling without worries #
6 # #
7 #-##############################################################################
8  
9 #
10 # module definition
11 #
12  
13 package No::Worries::Syslog;
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use 5.005; # need the four-argument form of substr()
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 our $VERSION = "1.7";
18 2 our $REVISION = sprintf("%d.%02d", q$Revision: 1.24 $ =~ /(\d+)\.(\d+)/);
19  
20 #
21 # used modules
22 #
23  
24 2 ▶use Encode qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use No::Worries::Die qw(dief);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use No::Worries::Export qw(export_control);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use No::Worries::Log qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use No::Worries::String qw(string_trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Params::Validate qw(validate :types);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Sys::Syslog qw(openlog closelog syslog);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use URI::Escape qw(uri_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 #
34 # global variables
35 #
36  
37 2 our($MaximumLength, $SplitLines);
38  
39 #
40 # open a "connection" to syslog via openlog()
41 #
42  
43 2 my %syslog_open_options = (
44 ident => { optional => 1, type => SCALAR, regex => qr/^[\w\-\.\/]+$/ },
45 option => { optional => 1, type => SCALAR, regex => qr/^\w+(,\w+)*$/ },
46 facility => { optional => 1, type => SCALAR, regex => qr/^\w+$/ },
47 );
48  
49 0 ▶sub syslog_open (@) {
Subroutine syslog_open: not called CC=5 SCAR=34.0
Pod documented
50 0 my(%option);
51  
52 0 ▶ %option = validate(@_, \%syslog_open_options) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
53 # defaults which may differ from Sys::Syslog
54 0 ▶ $option{option} = "ndelay,pid" unless $option{option};
Branch: 1 branch
BranchTrueFalseTotal
unless $option{"option"} 0 0 0
55 # we do not allow "nofatal" as this clashes with our error handling
56 0 ▶ $option{option} =~ s/\bnofatal\b//g if $option{option};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"option"} 0 0 0
57 # simply call openlog() now...
58 0 eval { openlog($option{ident}, $option{option}, $option{facility}) };
59 0 ▶ dief("cannot openlog(): %s", $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
60 }
61  
62 #
63 # close a "connection" to syslog via closelog()
64 #
65  
66 0 ▶sub syslog_close () {
Subroutine syslog_close: not called CC=2 SCAR=17.9
Pod documented
67 # simply call closelog()
68 0 eval { closelog() };
69 0 ▶ dief("cannot closelog(): %s", $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
70 }
71  
72 #
73 # sanitize a string so that it can safely be given to syslog
74 #
75  
76 10 ▶sub syslog_sanitize ($) {
Subroutine syslog_sanitize: called CC=6 SCAR=17.9
Pod documented
77 10 my($string) = @_;
78 10 my($flags, $tmp);
79  
80 10 $flags = "";
81 # 1: try to UTF-8 encode it if it has the UTF-8 flag set
82 10 ▶ if (Encode::is_utf8($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($string)) 3 7 10
83 # we use Encode::FB_DEFAULT to replace invalid characters
84 3 local $@ = ""; # preserve $@!
85 3 $tmp = Encode::encode("UTF-8", $string, Encode::FB_DEFAULT);
86 3 ▶ unless ($tmp eq $string) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmp eq $string) 2 1 3
87 # encoded is indeed different, use it
88 2 $string = $tmp;
89 2 $flags .= "U";
90 }
91 }
92 # 2: silently trim trailing spaces and replace tabs
93 10 $string =~ s/\s+$//;
94 10 $string =~ s/\t/ /g;
95 # 3: try to URI escape non-printable characters plus # % \ `
96 10 $tmp = uri_escape($string, q{\x00-\x1f\x7f-\xff\x23\x25\x5c\x60});
97 10 ▶ unless ($tmp eq $string) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmp eq $string) 5 5 10
98 # escaped is indeed different, use it
99 5 $string = $tmp;
100 5 $flags .= "E";
101 }
102 # 4: truncate if it is too long, taking into acount the possible flags
103 10 $tmp = length($string) - $MaximumLength + 4;
104 10 ▶ if ($tmp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp > 0) 1 9 10
105 1 substr($string, $MaximumLength - 4, $tmp, "");
106 1 $flags .= "T";
107 }
108 # 5: append the flags to keep track of what happened to the string
109 10 ▶ $string .= "#$flags" if $flags;
Branch: 1 branch
BranchTrueFalseTotal
if $flags 6 4 10
110 # that should be enough...
111 10 return($string);
112 }
113  
114 #
115 # handy wrappers around syslog()
116 #
117  
118 0 ▶sub _syslog_any ($$@) {
Subroutine _syslog_any: not called CC=7 SCAR=40.3
119 0 my($priority, $prefix, $message, @arguments) = @_;
120 0 my($separator, $string);
121  
122 0 ▶ $message = sprintf($message, @arguments) if @arguments;
Branch: 1 branch
BranchTrueFalseTotal
if @arguments 0 0 0
123 0 $message = string_trim($message);
124 0 $separator = " ";
125 0 ▶ if ($SplitLines and $message =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($No::Worries::Syslog::SplitLines and $message =~ /\n/) { } 0 0 0
Condition0%0/3
$No::Worries::Syslog::SplitLines and $message =~ /\n/
!ll&&!rl&&r
000
MC/DC0%0/2
$No::Worries::Syslog::SplitLines and $message =~ /\n/
$No::Worries::Syslog::SplitLines $message =~ /\n/
Truth table0%0/3
A and B
A= $No::Worries::Syslog::SplitLinesB= $message =~ /\n/
ABresult
0X0
100
111
126 # multiple syslog entries
127 0 foreach my $line (split(/\n/, $message)) {
128 0 $string = syslog_sanitize("[$prefix]$separator$line");
129 0 eval { syslog($priority, $string) };
130 0 ▶ dief("cannot syslog(): %s", $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
131 0 $separator = "+";
132 }
133 } else {
134 # one syslog entry
135 0 $string = syslog_sanitize("[$prefix]$separator$message");
136 0 eval { syslog($priority, $string) };
137 0 ▶ dief("cannot syslog(): %s", $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
138 }
139 }
140  
141 0 ▶sub syslog_error ($@) { _syslog_any("err", "ERROR", @_) }
Subroutine syslog_error: not called CC=1 SCAR=6.9
Pod documented
142 0 ▶sub syslog_warning ($@) { _syslog_any("warning", "WARNING", @_) }
Subroutine syslog_warning: not called CC=1 SCAR=6.9
Pod documented
143 0 ▶sub syslog_info ($@) { _syslog_any("info", "INFO", @_) }
Subroutine syslog_info: not called CC=1 SCAR=6.9
Pod documented
144 0 ▶sub syslog_debug ($@) { _syslog_any("debug", "DEBUG", @_) }
Subroutine syslog_debug: not called CC=1 SCAR=6.9
Pod documented
145  
146 #
147 # No::Worries::Log-compatible handler
148 #
149  
150 0 ▶sub log2syslog ($) {
Subroutine log2syslog: not called CC=4 SCAR=30.0
Pod documented
151 0 my($info) = @_;
152  
153 0 ▶ if ($info->{level} eq "error") {
Branch: 3 branches
BranchTrueFalseTotal
if ($info->{'level'} eq 'error') { } 0 0 0
elsif ($info->{'level'} eq 'warning') { } 0 0 0
elsif ($info->{'level'} eq 'info') { } 0 0 0
154 0 syslog_error($info->{message});
155 } elsif ($info->{level} eq "warning") {
156 0 syslog_warning($info->{message});
157 } elsif ($info->{level} eq "info") {
158 0 syslog_info($info->{message});
159 } else {
160 0 syslog_debug($info->{message}); # for debug _and_ trace
161 }
162 0 return(1);
163 }
164  
165 #
166 # module initialization
167 #
168  
169 2 $MaximumLength = 1000;
170 2 $SplitLines = 1;
171  
172 #
173 # export control
174 #
175  
176 1 ▶sub import : method {
Subroutine import: called CC=1 SCAR=0.0
177 1 my($pkg, %exported);
178  
179 1 $pkg = shift(@_);
180 1 grep($exported{$_}++,
181 map("syslog_$_", qw(open close sanitize error warning info debug)));
182 0 ▶ $exported{"log2syslog"} = sub { $No::Worries::Log::Handler = \&log2syslog };
Subroutine __ANON__: not called CC=1 SCAR=1.2
183 1 export_control(scalar(caller()), $pkg, \%exported, @_);
184 }
185  
186 2 1;
187  
188 __DATA__
« blib/lib/No/Worries/Proc.pm ↑ Summary blib/lib/No/Worries/Stat.pm »