| 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
| |||||||||||||||||||||||||||||
| 53 | # defaults which may differ from Sys::Syslog | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $option{option} = "ndelay,pid" unless $option{option}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 123 | 0 | $message = string_trim($message); | |||||||||||||||||||||||||||
| 124 | 0 | $separator = " "; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ($SplitLines and $message =~ /\n/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$No::Worries::Syslog::SplitLines and $message =~ /\n/
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/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||