| 1 | package Log::Dynamic; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 10 | $VERSION = 0.04; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 10 | my $PKG = __PACKAGE__; | |||||||||||||||||||||||||||
| 12 | 10 | my $MODE = '>>'; # By default we append | |||||||||||||||||||||||||||
| 13 | 10 | my $UCASE = 1; # By default we force types to upper case | |||||||||||||||||||||||||||
| 14 | 10 | my $TYPES = undef; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Constructor | ||||||||||||||||||||||||||||
| 17 | 10 | ▶ | sub open { | ||||||||||||||||||||||||||
|
Subroutine
open: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 10 | my $class = shift; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Catch an object call | ||||||||||||||||||||||||||||
| 21 | 10 | ▶ | $class = ref $class || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 10 | return bless _init({@_}), $class; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Initialize our params | ||||||||||||||||||||||||||||
| 27 | 10 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=8 SCAR=21.5
| |||||||||||||||||||||||||||||
| 28 | 10 | my $args = shift; | |||||||||||||||||||||||||||
| 29 | 10 | my $fh; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 10 | ▶ | unless ($args->{'file'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 1 | croak "$PKG: Must supply file: Log::Dynamic->open(file => 'foo')"; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Override append mode to clobber mode if requested | ||||||||||||||||||||||||||||
| 36 | 9 | ▶ | if (defined $args->{'mode'} && $args->{'mode'} =~ m/^clobber$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args->{"mode"} and $args->{"mode"} =~ /^clobber$/
MC/DC100%2/2
defined $args->{"mode"} and $args->{"mode"} =~ /^clobber$/
defined $args->{"mode"}
$args->{"mode"} =~ /^clobber$/
Truth table100%3/3
A and B
A= defined $args->{"mode"}B= $args->{"mode"} =~ /^clobber$/
| |||||||||||||||||||||||||||||
| 37 | 2 | $MODE = '>'; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Override ucase mode if requested | ||||||||||||||||||||||||||||
| 41 | 9 | ▶ | if (exists $args->{'ucase'} && !$args->{'ucase'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args->{"ucase"} and not $args->{"ucase"}
MC/DC0%0/2
exists $args->{"ucase"} and not $args->{"ucase"}
exists $args->{"ucase"}
not $args->{"ucase"}
Truth table66%2/3
A and not(B)
A= exists $args->{"ucase"}B= not $args->{"ucase"}
| |||||||||||||||||||||||||||||
| 42 | 0 | $UCASE = 0; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 9 | _init_types($args->{'types'},$args->{'invalid_type'}); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 9 | ▶ | if ($args->{'file'} =~ /STD(?:OUT|ERR)/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | $fh = uc $args->{'file'}; | |||||||||||||||||||||||||||
| 49 | } else { | ||||||||||||||||||||||||||||
| 50 | 9 | ▶ | CORE::open($fh, $MODE, $args->{'file'}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | or croak "$PKG: Failed to open file '$args->{file}': $!"; | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 9 | return \$fh; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Type initialization was a large enough chunk of code that | ||||||||||||||||||||||||||||
| 58 | # I felt it should be pulled into its own subroutine. | ||||||||||||||||||||||||||||
| 59 | 9 | ▶ | sub _init_types { | ||||||||||||||||||||||||||
|
Subroutine
_init_types: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 60 | 9 | ▶ | my $types = shift || return; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || (return)
MC/DC100%1/1
shift() || (return)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | my $handler = shift || \&_invalid_type; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || \&Log::Dynamic::_invalid_type
MC/DC100%1/1
shift() || \&Log::Dynamic::_invalid_type
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # If provided the invalid type handler must be a coderef | ||||||||||||||||||||||||||||
| 64 | 2 | ▶ | croak "$PKG: Value for the 'invalid_type' param must be a code ref" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | unless ref $handler eq 'CODE'; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # A Smudge of error checking. Non-empty array ref please | ||||||||||||||||||||||||||||
| 68 | 2 | ▶ | croak "$PKG: Value for the 'types' param must be an array ref" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | unless ref $types eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | croak "$PKG: Value for the 'types' param must not be an empty list" | ||||||||||||||||||||||||||||
| 72 | 2 | ▶ | unless @{ $types }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # We have types! Make a hash of types for easy lookup. | ||||||||||||||||||||||||||||
| 75 | # Note that we explicitly register dump here as a valid | ||||||||||||||||||||||||||||
| 76 | # type. If we didn't the sub that validates types would | ||||||||||||||||||||||||||||
| 77 | # crap out when calling dump(). Seems a bit hacky, I know... | ||||||||||||||||||||||||||||
| 78 | # but for the time being I think this solution is OK. | ||||||||||||||||||||||||||||
| 79 | 2 | $TYPES = { map { $_ => 1 } @{ $types }, 'dump' }; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Store our invalid type handler. | ||||||||||||||||||||||||||||
| 82 | 2 | $TYPES->{'_handle_invalid'} = $handler; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # For those of you that decide you want to use the standard | ||||||||||||||||||||||||||||
| 86 | # constructor notation of new(), here you go. | ||||||||||||||||||||||||||||
| 87 | 1 | ▶ | sub new { shift->open(@_) } | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # O'hai. Sry, we closing... | ||||||||||||||||||||||||||||
| 90 | 16 | ▶ | sub close { close ${(shift)} } | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Base logging function | ||||||||||||||||||||||||||||
| 93 | 9 | ▶ | sub log { | ||||||||||||||||||||||||||
|
Subroutine
log: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 9 | my $fh = shift; # File handle reference | |||||||||||||||||||||||||||
| 95 | 9 | ▶ | my $type = shift || return; # Message type, REQUIRED | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 96 | 9 | ▶ | my $msg = shift || return; # Message body, REQUIRED | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || (return)
MC/DC100%1/1
shift() || (return)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 97 | 8 | my $time = scalar localtime; # Formatted timestamp | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 8 | _validate_type($type); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Formatted caller info. Because custom types are essentially | ||||||||||||||||||||||||||||
| 102 | # wrapper functions for log() we need to check up one more | ||||||||||||||||||||||||||||
| 103 | # level to get the correct caller information. | ||||||||||||||||||||||||||||
| 104 | my $call = join(' ', | ||||||||||||||||||||||||||||
| 105 | map { | ||||||||||||||||||||||||||||
| 106 | 8 | ▶ | (caller(1))[$_] # Called using $log->[custom type]() | ||||||||||||||||||||||||||
|
Condition66%2/3
(caller 1)[$_] || (caller 0)[$_]
MC/DC0%0/2
(caller 1)[$_] || (caller 0)[$_]
(caller 1)[$_]
(caller 0)[$_]
Truth table66%2/3
A || B
A= (caller 1)[$_]B= (caller 0)[$_]
| |||||||||||||||||||||||||||||
| 107 | || # - OR - | ||||||||||||||||||||||||||||
| 108 | (caller(0))[$_] # Called using $log->log() | ||||||||||||||||||||||||||||
| 109 | } 0..2 | ||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # Output formatted log entry. We turn off strict refs so that | ||||||||||||||||||||||||||||
| 113 | # we can print to STDERR and STDOUT witout perl spitting an | ||||||||||||||||||||||||||||
| 114 | # error and dying. | ||||||||||||||||||||||||||||
| 115 | 10 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 116 | 8 | ▶ | print {$$fh} "$time [".($UCASE?uc($type):$type)."] $msg ($call)\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: called CC=7 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 1 | my $log = shift; | |||||||||||||||||||||||||||
| 121 | 1 | my $args = shift; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | ▶ | unless (ref $args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | carp "$PKG: dump() requires a hash ref of arg=>value pairs"; | |||||||||||||||||||||||||||
| 125 | 0 | return; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 1 | ▶ | unless ($args->{'data'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | carp "$PKG: dump() requires the 'data' argument be supplied"; | |||||||||||||||||||||||||||
| 130 | 0 | return; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # Set defaults | ||||||||||||||||||||||||||||
| 134 | 1 | ▶ | $args->{'dump_type'} ||= 'dump'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"dump_type"} ||= "dump"
MC/DC0%0/1
$args->{"dump_type"} ||= "dump"
$args->{"dump_type"}
Truth table50%1/2
A
A= $args->{"dump_type"}
| |||||||||||||||||||||||||||||
| 135 | 1 | ▶ | $args->{'dump_name'} ||= 'anonymous data'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"dump_name"} ||= "anonymous data"
MC/DC0%0/1
$args->{"dump_name"} ||= "anonymous data"
$args->{"dump_name"}
Truth table50%1/2
A
A= $args->{"dump_name"}
| |||||||||||||||||||||||||||||
| 136 | 1 | ▶ | $args->{'begin_msg'} ||= 'BEGIN dump for:'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"begin_msg"} ||= "BEGIN dump for:"
MC/DC0%0/1
$args->{"begin_msg"} ||= "BEGIN dump for:"
$args->{"begin_msg"}
Truth table50%1/2
A
A= $args->{"begin_msg"}
| |||||||||||||||||||||||||||||
| 137 | 1 | ▶ | $args->{'end_msg' } ||= 'END dump for:'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"end_msg"} ||= "END dump for:"
MC/DC0%0/1
$args->{"end_msg"} ||= "END dump for:"
$args->{"end_msg"}
Truth table50%1/2
A
A= $args->{"end_msg"}
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | $log->log($args->{'dump_type'}, | ||||||||||||||||||||||||||||
| 140 | $args->{'begin_msg'} . " '$args->{dump_name}'\n" . | ||||||||||||||||||||||||||||
| 141 | Dumper($args->{'data'}) . | ||||||||||||||||||||||||||||
| 142 | 1 | $args->{'end_msg'} . " '$args->{dump_name}'" | |||||||||||||||||||||||||||
| 143 | ); | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 8 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 147 | 8 | my $log = shift; | |||||||||||||||||||||||||||
| 148 | 8 | my $type = (our $AUTOLOAD = $AUTOLOAD); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 8 | ▶ | return if $type =~ /::DESTROY$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 8 | $type =~ s/.*::(.+)$/$1/; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 8 | _validate_type($type); | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # Define a subroutine for our new type. Since this new | ||||||||||||||||||||||||||||
| 156 | # sub just turns around and calls log() with a set value | ||||||||||||||||||||||||||||
| 157 | # for the $type variable you can probably lable this a | ||||||||||||||||||||||||||||
| 158 | # form of function currying. Weeeeee =) | ||||||||||||||||||||||||||||
| 159 | { | ||||||||||||||||||||||||||||
| 160 | 10 | ▶ | no strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 161 | 10 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | *$type = sub { shift->log($type,@_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # Log with our new type | ||||||||||||||||||||||||||||
| 166 | 6 | $log->log($type,@_); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | # Chage the ucase value | ||||||||||||||||||||||||||||
| 170 | 1 | ▶ | sub ucase { shift;$UCASE = shift || 0} | ||||||||||||||||||||||||||
|
Subroutine
ucase: called CC=2 SCAR=7.2
Pod
documented
Condition50%1/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # Valid log type | ||||||||||||||||||||||||||||
| 173 | 16 | ▶ | sub _validate_type { | ||||||||||||||||||||||||||
|
Subroutine
_validate_type: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 174 | 16 | my $type = shift; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 16 | ▶ | if (defined $TYPES and not $TYPES->{$type}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $TYPES and not $TYPES->{$type}
MC/DC100%2/2
defined $TYPES and not $TYPES->{$type}
defined $TYPES
not $TYPES->{$type}
Truth table100%3/3
A and not(B)
A= defined $TYPESB= not $TYPES->{$type}
| |||||||||||||||||||||||||||||
| 177 | 2 | $TYPES->{'_handle_invalid'}->($type); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 1 | ▶ | sub _invalid_type { | ||||||||||||||||||||||||||
|
Subroutine
_invalid_type: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 182 | 1 | my $type = shift; | |||||||||||||||||||||||||||
| 183 | 1 | croak "$PKG: Type '$type' was not specified as a valid type"; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | # Cleanup... Just close the file handle | ||||||||||||||||||||||||||||
| 187 | 9 | ▶ | sub DESTROY { shift->close } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 10 | 1; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | __END__ | ||||||||||||||||||||||||||||