| 1 | package Log::Dispatch::Config; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 16 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 16 | $VERSION = "1.04"; | |||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | 16 | ▶ | use Log::Dispatch 2.00; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 16 | ▶ | use base qw(Log::Dispatch); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | # caller depth: can be changed from outside | ||||||||||||||||||||||||||||||||||||
| 11 | 16 | $Log::Dispatch::Config::CallerDepth = 0; | |||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | sub _croak { require Carp; Carp::croak(@_); } | ||||||||||||||||||||||||||||||||||
|
Subroutine
_croak: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||
| 15 | # accessor for symblic reference | ||||||||||||||||||||||||||||||||||||
| 16 | 98 | ▶ | sub __instance { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__instance: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 17 | 98 | my $class = shift; | |||||||||||||||||||||||||||||||||||
| 18 | 16 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 19 | 98 | my $instance = "$class\::_instance"; | |||||||||||||||||||||||||||||||||||
| 20 | 98 | ▶ | $$instance = shift if @_; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 21 | 98 | return $$instance; | |||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||
| 24 | 16 | ▶ | sub _configurator_for { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_configurator_for: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 25 | 16 | my($class, $stuff) = @_; | |||||||||||||||||||||||||||||||||||
| 26 | 16 | ▶ | return $stuff if UNIVERSAL::isa($stuff, 'Log::Dispatch::Configurator'); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 27 | 13 | require Log::Dispatch::Configurator::AppConfig; | |||||||||||||||||||||||||||||||||||
| 28 | 13 | return Log::Dispatch::Configurator::AppConfig->new($stuff); | |||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||
| 31 | 16 | ▶ | sub configure { | ||||||||||||||||||||||||||||||||||
|
Subroutine
configure: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 32 | 16 | my($class, $stuff) = @_; | |||||||||||||||||||||||||||||||||||
| 33 | 16 | ▶ | _croak "no config file or configurator supplied" unless $stuff; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 34 | 16 | my $config = $class->_configurator_for($stuff); | |||||||||||||||||||||||||||||||||||
| 35 | 16 | $config->myinit; | |||||||||||||||||||||||||||||||||||
| 36 | 16 | $class->__instance($config); | |||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | sub configure_and_watch { | ||||||||||||||||||||||||||||||||||
|
Subroutine
configure_and_watch: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 40 | 2 | my $class = shift; | |||||||||||||||||||||||||||||||||||
| 41 | 2 | $class->configure(@_); | |||||||||||||||||||||||||||||||||||
| 42 | 2 | $class->__instance->should_watch(1); # tells conf to watch config file | |||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||
| 45 | # backward compatibility | ||||||||||||||||||||||||||||||||||||
| 46 | 2 | ▶ | sub Log::Dispatch::instance { __PACKAGE__->instance; } | ||||||||||||||||||||||||||||||||||
|
Subroutine
instance: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||
| 48 | 28 | ▶ | sub instance { | ||||||||||||||||||||||||||||||||||
|
Subroutine
instance: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 49 | 28 | my $class = shift; | |||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||
| 51 | 28 | ▶ | my $instance = $class->__instance or _croak "configure not yet called."; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 52 | 28 | ▶ | if ($instance->isa('Log::Dispatch::Config')) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 53 | # reload singleton on the fly | ||||||||||||||||||||||||||||||||||||
| 54 | 12 | ▶ | $class->reload if $instance->needs_reload; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||
| 56 | else { | ||||||||||||||||||||||||||||||||||||
| 57 | # first time call: $_instance is L::D::Configurator::* | ||||||||||||||||||||||||||||||||||||
| 58 | 16 | $class->__instance($class->create_instance($instance)); | |||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||
| 60 | 27 | return $class->__instance; | |||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||
| 63 | 12 | ▶ | sub needs_reload { | ||||||||||||||||||||||||||||||||||
|
Subroutine
needs_reload: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 64 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 65 | 12 | ▶ | return $self->{config}->should_watch && $self->{config}->needs_reload; | ||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->{'config'}->should_watch && $self->{'config'}->needs_reload
MC/DC100%2/2
$self->{'config'}->should_watch && $self->{'config'}->needs_reload
$self->{'config'}->should_watch
$self->{'config'}->needs_reload
Truth table100%3/3
A && B
A= $self->{'config'}->should_watchB= $self->{'config'}->needs_reload
| |||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||
| 68 | 5 | ▶ | sub reload { | ||||||||||||||||||||||||||||||||||
|
Subroutine
reload: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 69 | 5 | my $proto = shift; | |||||||||||||||||||||||||||||||||||
| 70 | 5 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||
| 71 | 5 | my $instance = $class->__instance; | |||||||||||||||||||||||||||||||||||
| 72 | 5 | $instance->{config}->reload; | |||||||||||||||||||||||||||||||||||
| 73 | 5 | $class->__instance($class->create_instance($instance->{config})); | |||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||
| 76 | 21 | ▶ | sub create_instance { | ||||||||||||||||||||||||||||||||||
|
Subroutine
create_instance: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 77 | 21 | my($class, $config) = @_; | |||||||||||||||||||||||||||||||||||
| 78 | 21 | $config->{LDC_ctime} = time; # creation time | |||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||
| 80 | 21 | my $global = $config->get_attrs_global; | |||||||||||||||||||||||||||||||||||
| 81 | 20 | my $callback = $class->format_to_cb($global->{format}, 0); | |||||||||||||||||||||||||||||||||||
| 82 | 20 | my %dispatchers; | |||||||||||||||||||||||||||||||||||
| 83 | 20 | foreach my $disp (@{$global->{dispatchers}}) { | |||||||||||||||||||||||||||||||||||
| 84 | 32 | $dispatchers{$disp} = $class->config_dispatcher( | |||||||||||||||||||||||||||||||||||
| 85 | $disp, $config->get_attrs($disp), | ||||||||||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||
| 88 | 20 | my %args; | |||||||||||||||||||||||||||||||||||
| 89 | 20 | ▶ | $args{callbacks} = $callback if defined $callback; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 90 | 20 | my $instance = $class->new(%args); | |||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||
| 92 | 20 | for my $dispname (keys %dispatchers) { | |||||||||||||||||||||||||||||||||||
| 93 | 32 | my $logclass = delete $dispatchers{$dispname}->{class}; | |||||||||||||||||||||||||||||||||||
| 94 | $instance->add( | ||||||||||||||||||||||||||||||||||||
| 95 | $logclass->new( | ||||||||||||||||||||||||||||||||||||
| 96 | name => $dispname, | ||||||||||||||||||||||||||||||||||||
| 97 | 32 | %{$dispatchers{$dispname}}, | |||||||||||||||||||||||||||||||||||
| 98 | ), | ||||||||||||||||||||||||||||||||||||
| 99 | ); | ||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||
| 102 | 20 | $instance->{config} = $config; | |||||||||||||||||||||||||||||||||||
| 103 | 20 | return $instance; | |||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | 32 | ▶ | sub config_dispatcher { | ||||||||||||||||||||||||||||||||||
|
Subroutine
config_dispatcher: called CC=5 SCAR=16.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 107 | 32 | my($class, $disp, $var) = @_; | |||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||
| 109 | 32 | ▶ | my $dispclass = $var->{class} or _croak "class param missing for $disp"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||
| 111 | 32 | eval qq{require $dispclass}; | |||||||||||||||||||||||||||||||||||
| 112 | 32 | ▶ | _croak $@ if $@ && $@ !~ /locate/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ and not $@ =~ /locate/
MC/DC0%0/2
$@ and not $@ =~ /locate/
$@
not $@ =~ /locate/
Truth table33%1/3
A and not(B)
A= $@B= not $@ =~ /locate/
| |||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||
| 114 | 32 | ▶ | if (exists $var->{format}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 115 | 27 | $var->{callbacks} = $class->format_to_cb(delete $var->{format}, 2); | |||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||
| 117 | 32 | return $var; | |||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||
| 120 | 47 | ▶ | sub format_to_cb { | ||||||||||||||||||||||||||||||||||
|
Subroutine
format_to_cb: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 121 | 47 | my($class, $format, $stack) = @_; | |||||||||||||||||||||||||||||||||||
| 122 | 47 | ▶ | return undef unless defined $format; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||
| 124 | # caller() called only when necessary | ||||||||||||||||||||||||||||||||||||
| 125 | 27 | my $needs_caller = $format =~ /%[FLP]/; | |||||||||||||||||||||||||||||||||||
| 126 | 6 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||
| 127 | 6 | my %p = @_; | |||||||||||||||||||||||||||||||||||
| 128 | 6 | $p{p} = delete $p{level}; | |||||||||||||||||||||||||||||||||||
| 129 | 6 | $p{m} = delete $p{message}; | |||||||||||||||||||||||||||||||||||
| 130 | 6 | $p{n} = "\n"; | |||||||||||||||||||||||||||||||||||
| 131 | 6 | $p{'%'} = '%'; | |||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||
| 133 | 6 | ▶ | if ($needs_caller) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 134 | 4 | my $depth = 0; | |||||||||||||||||||||||||||||||||||
| 135 | 4 | ▶ | $depth++ while caller($depth) =~ /^Log::Dispatch/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 136 | 4 | $depth += $Log::Dispatch::Config::CallerDepth; | |||||||||||||||||||||||||||||||||||
| 137 | 4 | @p{qw(P F L)} = caller($depth); | |||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||
| 140 | 6 | my $log = $format; | |||||||||||||||||||||||||||||||||||
| 141 | 6 | $log =~ s{ | |||||||||||||||||||||||||||||||||||
| 142 | (%d(?:{(.*?)})?)| # $1: datetime $2: datetime fmt | ||||||||||||||||||||||||||||||||||||
| 143 | (?:%([%pmFLPn])) # $3: others | ||||||||||||||||||||||||||||||||||||
| 144 | }{ | ||||||||||||||||||||||||||||||||||||
| 145 | 25 | ▶ | if ($1 && $2) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$1 and $2
MC/DC100%2/2
$1 and $2
$1
$2
Truth table100%3/3
A and B
A= $1B= $2
| |||||||||||||||||||||||||||||||||||||
| 146 | 1 | _strftime($2); | |||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||
| 148 | elsif ($1) { | ||||||||||||||||||||||||||||||||||||
| 149 | 5 | scalar localtime; | |||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||
| 151 | elsif ($3) { | ||||||||||||||||||||||||||||||||||||
| 152 | 19 | $p{$3}; | |||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||
| 154 | }egx; | ||||||||||||||||||||||||||||||||||||
| 155 | 6 | return $log; | |||||||||||||||||||||||||||||||||||
| 156 | 27 | }; | |||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||
| 159 | { | ||||||||||||||||||||||||||||||||||||
| 160 | 16 | ▶ | use vars qw($HasTimePiece); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 161 | 16 | ▶ | BEGIN { eval { require Time::Piece; $HasTimePiece = 1 }; } | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||
| 163 | 1 | ▶ | sub _strftime { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_strftime: called CC=2 SCAR=8.4
| |||||||||||||||||||||||||||||||||||||
| 164 | 1 | my $fmt = shift; | |||||||||||||||||||||||||||||||||||
| 165 | 1 | ▶ | if ($HasTimePiece) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 166 | 1 | return Time::Piece->new->strftime($fmt); | |||||||||||||||||||||||||||||||||||
| 167 | } else { | ||||||||||||||||||||||||||||||||||||
| 168 | 0 | require POSIX; | |||||||||||||||||||||||||||||||||||
| 169 | 0 | return POSIX::strftime($fmt, localtime); | |||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||
| 174 | 16 | 1; | |||||||||||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||||||||||