File Coverage

blib/lib/Log/Agent/File_Pool.pm
Criterion Covered Total %
statement 25 25 100.0
branch 7 8 87.5
condition 2 3 66.6
subroutine 7 7 100.0
pod 0 6 0.0
total 41 49 83.6


line stmt bran cond sub pod time code
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     8   55 use strict;
  8         18  
  8         2773  
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             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             sub make {
35 8     8 0 24 my $self = bless {}, shift;
36 8         58 $self->{info} = {};
37 8         43 return $self;
38             }
39              
40             #
41             # Attribute access
42             #
43              
44 98     98 0 182 sub info { $_[0]->{'info'} }
45              
46             #
47             # file_pool -- "once" routine
48             #
49             # Return the main pool
50             #
51             sub file_pool {
52 50   66 50 0 162 return $POOL || ($POOL = Log::Agent::File_Pool->make());
53             }
54              
55             #
56             # ->put
57             #
58             # Put new entry in pool.
59             #
60             sub put {
61 31     31 0 56 my $self = shift;
62 31         66 my ($path, $file, $rotate) = @_;
63              
64 31         97 my $info = $self->info;
65 31 100       77 if (exists $info->{$path}) {
66 1         3 $info->{$path}->[2]++; # refcnt
67             } else {
68 30         123 $info->{$path} = [$file, $rotate, 1];
69             }
70             }
71              
72             #
73             # ->get
74             #
75             # Get record for existing entry, undef if none.
76             #
77             sub get {
78 31     31 0 45 my $self = shift;
79 31         62 my ($path) = @_;
80 31         64 my $aref = $self->info->{$path};
81 31 100       102 return defined $aref ? @$aref : ();
82             }
83              
84             #
85             # ->remove
86             #
87             # Remove record.
88             # Returns true when file is definitively removed (no more reference on it).
89             #
90             sub remove {
91 19     19 0 27 my $self = shift;
92 19         37 my ($path) = @_;
93 19         34 my $item = $self->info->{$path};
94 19 100       42 return 1 unless defined $item;
95 17 50       34 return 0 if --$item->[2];
96              
97             #
98             # Reference count reached 0
99             #
100              
101 17         30 delete $self->info->{$path};
102 17         72 return 1;
103             }
104              
105             1; # for require