| 1 | ########################################################################### | ||||||||||
| 2 | # | ||||||||||
| 3 | # Formatting.pm | ||||||||||
| 4 | # | ||||||||||
| 5 | # Copyright (C) 1999 Raphael Manfredi. | ||||||||||
| 6 | # Copyright (C) 2002-2017 Mark Rogaski, mrogaski@cpan.org; | ||||||||||
| 7 | # all rights reserved. | ||||||||||
| 8 | # | ||||||||||
| 9 | # See the README file included with the | ||||||||||
| 10 | # distribution for license information. | ||||||||||
| 11 | # | ||||||||||
| 12 | ########################################################################## | ||||||||||
| 13 | |||||||||||
| 14 | 16 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 16 | require Exporter; | |||||||||
| 16 | |||||||||||
| 17 | ######################################################################## | ||||||||||
| 18 | package Log::Agent::Formatting; | ||||||||||
| 19 | |||||||||||
| 20 | 16 | ▶ | use vars qw(@ISA @EXPORT_OK); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 16 | @ISA = qw(Exporter); | |||||||||
| 23 | 16 | @EXPORT_OK = qw(format_args tag_format_args); | |||||||||
| 24 | |||||||||||
| 25 | 16 | require Log::Agent::Message; | |||||||||
| 26 | |||||||||||
| 27 | # | ||||||||||
| 28 | # adjust_fmt | ||||||||||
| 29 | # | ||||||||||
| 30 | # We process syslog's %m macro as being the current error message ($!) in | ||||||||||
| 31 | # the first argument only. Doing it at this level means it will be supported | ||||||||||
| 32 | # independently from the driver they'll choose. It's also done BEFORE any | ||||||||||
| 33 | # log-related system call, thus ensuring that $! retains its original value. | ||||||||||
| 34 | # | ||||||||||
| 35 | 16 | ▶ | if ($] >= 5.005) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 100 | ▶ | eval q{ # if VERSION >= 5.005 | ||||||||
|
Subroutine
adjust_fmt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 37 | # 5.005 and later version grok /(?<!)/ | ||||||||||
| 38 | sub adjust_fmt { | ||||||||||
| 39 | my $fmt = shift; | ||||||||||
| 40 | $fmt =~ s/((?<!%)(?:%%)*)%m/$Log::Agent::OS_Error/g; | ||||||||||
| 41 | return $fmt; | ||||||||||
| 42 | } | ||||||||||
| 43 | } | ||||||||||
| 44 | } else { | ||||||||||
| 45 | 0 | eval q{ # else /* VERSION < 5.005 */ | |||||||||
| 46 | # pre-5.005 does not grok /(?<!)/ | ||||||||||
| 47 | sub adjust_fmt { | ||||||||||
| 48 | my $fmt = shift; | ||||||||||
| 49 | $fmt =~ s/%%/\01/g; | ||||||||||
| 50 | $fmt =~ s/%m/$Log::Agent::OS_Error/g; | ||||||||||
| 51 | $fmt =~ s/\01/%%/g; | ||||||||||
| 52 | return $fmt; | ||||||||||
| 53 | } | ||||||||||
| 54 | } | ||||||||||
| 55 | } # endif /* VERSION >= 5.005 */ | ||||||||||
| 56 | |||||||||||
| 57 | # | ||||||||||
| 58 | # whine | ||||||||||
| 59 | # | ||||||||||
| 60 | # This is a local hack of carp | ||||||||||
| 61 | # | ||||||||||
| 62 | 0 | ▶ | sub whine { | ||||||||
|
Subroutine
whine: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 63 | 0 | my $msg = shift; | |||||||||
| 64 | 0 | ▶ | unless (chomp $msg) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | my($package, $filename, $line) = caller 2; | |||||||||
| 66 | 0 | $msg .= " at $filename line $line."; | |||||||||
| 67 | } | ||||||||||
| 68 | 0 | warn "$msg\n"; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | # | ||||||||||
| 72 | # tag_format_args | ||||||||||
| 73 | # | ||||||||||
| 74 | # Arguments: | ||||||||||
| 75 | # | ||||||||||
| 76 | # $caller caller information, done firstly | ||||||||||
| 77 | # $priority priority information, done secondly | ||||||||||
| 78 | # $tags list of user-defined tags, done lastly | ||||||||||
| 79 | # $ary arguments for sprintf() | ||||||||||
| 80 | # | ||||||||||
| 81 | # Returns a Log::Agent::Message object, which, when stringified, prints | ||||||||||
| 82 | # the string itself. | ||||||||||
| 83 | # | ||||||||||
| 84 | 100 | ▶ | sub tag_format_args { | ||||||||
|
Subroutine
tag_format_args: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||
| 85 | 100 | my ($caller, $priority, $tags, $ary) = @_; | |||||||||
| 86 | 100 | my $msg = adjust_fmt(shift @$ary); | |||||||||
| 87 | |||||||||||
| 88 | # This bit of tomfoolery is intended to make debugging of | ||||||||||
| 89 | # programs a bit easier by prechecking input to sprintf() | ||||||||||
| 90 | # for errors. I usually prefer lazy error checking, but | ||||||||||
| 91 | # this seems to be an appropriate exception. | ||||||||||
| 92 | 100 | ▶ | if (my @arglist = $msg =~ /\%[^\%]*[csduoxefgXEGbpniDUOF]|\%\%/g) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 16 | ▶ | BEGIN { no warnings } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: not called CC=1 SCAR=0.0
| |||||||||||
| 94 | 6 | my $argcnt = grep !/\%\%/, @arglist; | |||||||||
| 95 | 6 | ▶ | if (grep {! defined} @$ary[0..($argcnt - 1)]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | whine("Use of uninitialized value in sprintf"); | |||||||||
| 97 | } | ||||||||||
| 98 | 6 | $msg = sprintf $msg, @$ary; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 100 | my $str = Log::Agent::Message->make($msg); | |||||||||
| 102 | 100 | ▶ | $caller->insert($str) if defined $caller; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 100 | ▶ | $priority->insert($str) if defined $priority; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 100 | ▶ | if (defined $tags) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 4 | foreach my $tag (@$tags) { | |||||||||
| 106 | 6 | $tag->insert($str); | |||||||||
| 107 | } | ||||||||||
| 108 | } | ||||||||||
| 109 | 100 | return $str; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 16 | 1; | |||||||||