| 1 | ########################################################################### | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # File_Pool.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 | 8 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | ######################################################################## | ||||||||||||||||||||
| 17 | package Log::Agent::File_Pool; | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # | ||||||||||||||||||||
| 20 | # A pool of all created file objects, along with their rotation policies | ||||||||||||||||||||
| 21 | # | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 8 | my $POOL = undef; # only one instance | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | # | ||||||||||||||||||||
| 26 | # ->make | ||||||||||||||||||||
| 27 | # | ||||||||||||||||||||
| 28 | # Creation routine. | ||||||||||||||||||||
| 29 | # | ||||||||||||||||||||
| 30 | # Attributes: | ||||||||||||||||||||
| 31 | # info records path -> | ||||||||||||||||||||
| 32 | # [Log::Agent::File objects, rotation policies, refcnt] | ||||||||||||||||||||
| 33 | # | ||||||||||||||||||||
| 34 | 8 | ▶ | sub make { | ||||||||||||||||||
|
Subroutine
make: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 35 | 8 | my $self = bless {}, shift; | |||||||||||||||||||
| 36 | 8 | $self->{info} = {}; | |||||||||||||||||||
| 37 | 8 | return $self; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | # | ||||||||||||||||||||
| 41 | # Attribute access | ||||||||||||||||||||
| 42 | # | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 98 | ▶ | sub info { $_[0]->{'info'} } | ||||||||||||||||||
|
Subroutine
info: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | # | ||||||||||||||||||||
| 47 | # file_pool -- "once" routine | ||||||||||||||||||||
| 48 | # | ||||||||||||||||||||
| 49 | # Return the main pool | ||||||||||||||||||||
| 50 | # | ||||||||||||||||||||
| 51 | 50 | ▶ | sub file_pool { | ||||||||||||||||||
|
Subroutine
file_pool: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||
| 52 | 50 | ▶ | return $POOL || ($POOL = Log::Agent::File_Pool->make()); | ||||||||||||||||||
|
Condition66%2/3
$POOL || ($POOL = 'Log::Agent::File_Pool'->make)
MC/DC0%0/2
$POOL || ($POOL = 'Log::Agent::File_Pool'->make)
$POOL
($POOL = 'Log::Agent::File_Pool'->make)
Truth table66%2/3
A || B
A= $POOLB= ($POOL = 'Log::Agent::File_Pool'->make)
| |||||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | # | ||||||||||||||||||||
| 56 | # ->put | ||||||||||||||||||||
| 57 | # | ||||||||||||||||||||
| 58 | # Put new entry in pool. | ||||||||||||||||||||
| 59 | # | ||||||||||||||||||||
| 60 | 31 | ▶ | sub put { | ||||||||||||||||||
|
Subroutine
put: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 61 | 31 | my $self = shift; | |||||||||||||||||||
| 62 | 31 | my ($path, $file, $rotate) = @_; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 31 | my $info = $self->info; | |||||||||||||||||||
| 65 | 31 | ▶ | if (exists $info->{$path}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | 1 | $info->{$path}->[2]++; # refcnt | |||||||||||||||||||
| 67 | } else { | ||||||||||||||||||||
| 68 | 30 | $info->{$path} = [$file, $rotate, 1]; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | # | ||||||||||||||||||||
| 73 | # ->get | ||||||||||||||||||||
| 74 | # | ||||||||||||||||||||
| 75 | # Get record for existing entry, undef if none. | ||||||||||||||||||||
| 76 | # | ||||||||||||||||||||
| 77 | 31 | ▶ | sub get { | ||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 78 | 31 | my $self = shift; | |||||||||||||||||||
| 79 | 31 | my ($path) = @_; | |||||||||||||||||||
| 80 | 31 | my $aref = $self->info->{$path}; | |||||||||||||||||||
| 81 | 31 | ▶ | return defined $aref ? @$aref : (); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | # | ||||||||||||||||||||
| 85 | # ->remove | ||||||||||||||||||||
| 86 | # | ||||||||||||||||||||
| 87 | # Remove record. | ||||||||||||||||||||
| 88 | # Returns true when file is definitively removed (no more reference on it). | ||||||||||||||||||||
| 89 | # | ||||||||||||||||||||
| 90 | 19 | ▶ | sub remove { | ||||||||||||||||||
|
Subroutine
remove: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 91 | 19 | my $self = shift; | |||||||||||||||||||
| 92 | 19 | my ($path) = @_; | |||||||||||||||||||
| 93 | 19 | my $item = $self->info->{$path}; | |||||||||||||||||||
| 94 | 19 | ▶ | return 1 unless defined $item; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 17 | ▶ | return 0 if --$item->[2]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | # | ||||||||||||||||||||
| 98 | # Reference count reached 0 | ||||||||||||||||||||
| 99 | # | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 17 | delete $self->info->{$path}; | |||||||||||||||||||
| 102 | 17 | return 1; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 8 | 1; # for require | |||||||||||||||||||