File Coverage

blib/lib/Log/Agent/Rotate.pm
Criterion Covered Total %
statement 23 38 60.5
branch 3 18 16.6
condition n/a
subroutine 12 13 92.3
pod 8 11 72.7
total 46 80 57.5


line stmt bran cond sub pod time code
1             ###########################################################################
2             #
3             # Rotate.pm
4             #
5             # Copyright (c) 2000 Raphael Manfredi.
6             # Copyright (c) 2002-2015 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 6     6   44678 use strict;
  6         13  
  6         244  
15              
16             ###########################################################################
17             package Log::Agent::Rotate;
18              
19 6     6   5384 use Getargs::Long qw(ignorecase);
  6         72485  
  6         51  
20              
21             #
22             # File rotating policy
23             #
24              
25             our $VERSION = "1.001";
26             $VERSION = eval $VERSION;
27              
28             BEGIN {
29             sub BACKLOG () {0}
30             sub UNZIPPED () {1}
31             sub MAX_SIZE () {2}
32             sub MAX_WRITE () {3}
33             sub MAX_TIME () {4}
34             sub IS_ALONE () {5}
35             sub SINGLE_HOST () {6}
36             sub FILE_PERM () {7}
37             }
38              
39             #
40             # ->make
41             #
42             # Creation routine.
43             #
44             # Attributes:
45             # backlog amount of old files to keep (0 for none)
46             # unzipped amount of old files to NOT compress (defaults to 1)
47             # max_size maximum amount of bytes in file
48             # max_write maximum amount of bytes to write in file
49             # max_time maximum amount of time to keep open
50             # is_alone hint: only one instance is busy manipulating the logfiles
51             # single_host hint: access to logfiles always made via one host
52             #
53             sub make {
54 12     12 0 1654 my $self = bless [], shift;
55              
56             (
57 12         219 $self->[BACKLOG],
58             $self->[UNZIPPED],
59             $self->[MAX_SIZE],
60             $self->[MAX_WRITE],
61             $self->[MAX_TIME],
62             $self->[IS_ALONE],
63             $self->[SINGLE_HOST],
64             $self->[FILE_PERM]
65             ) = xgetargs(@_,
66             -backlog => ['i', 7],
67             -unzipped => ['i', 1],
68             -max_size => ['i', 1_048_576],
69             -max_write => ['i', 0],
70             -max_time => ['s', "0"],
71             -is_alone => ['i', 0],
72             -single_host => ['i', 0],
73             -file_perm => ['i', 0666]
74             );
75              
76 12 50       7294 $self->[MAX_TIME] = seconds_in_period($self->[MAX_TIME])
77             if $self->[MAX_TIME];
78              
79 12         48 return $self;
80             }
81              
82             #
83             # seconds_in_period
84             #
85             # Converts a period into a number of seconds.
86             #
87             sub seconds_in_period {
88 0     0 0 0 my ($p) = @_;
89              
90 0         0 $p =~ s|^(\d+)||;
91 0         0 my $base = int($1); # Number of elementary periods
92 0         0 my $u = "s"; # Default Unit
93 0 0       0 $u = substr($1, 0, 1) if $p =~ /^\s*(\w+)$/;
94 0         0 my $sec;
95              
96 0 0       0 if ($u eq 'm') {
    0          
    0          
    0          
    0          
    0          
97 0         0 $sec = 60; # One minute = 60 seconds
98             } elsif ($u eq 'h') {
99 0         0 $sec = 3600; # One hour = 3600 seconds
100             } elsif ($u eq 'd') {
101 0         0 $sec = 86400; # One day = 24 hours
102             } elsif ($u eq 'w') {
103 0         0 $sec = 604800; # One week = 7 days
104             } elsif ($u eq 'M') {
105 0         0 $sec = 2592000; # One month = 30 days
106             } elsif ($u eq 'y') {
107 0         0 $sec = 31536000; # One year = 365 days
108             } else {
109 0         0 $sec = 1; # Unrecognized: defaults to seconds
110             }
111              
112 0         0 return $base * $sec;
113             }
114              
115             #
116             # Attribute access
117             #
118              
119 62     62 1 229 sub backlog { $_[0]->[BACKLOG] }
120 31     31 1 80 sub unzipped { $_[0]->[UNZIPPED] }
121 130     130 1 545 sub max_size { $_[0]->[MAX_SIZE] }
122 34     34 1 155 sub max_write { $_[0]->[MAX_WRITE] }
123 34     34 1 146 sub max_time { $_[0]->[MAX_TIME] }
124 98     98 1 419 sub is_alone { $_[0]->[IS_ALONE] }
125 12     12 1 146 sub single_host { $_[0]->[SINGLE_HOST] }
126 34     34 1 111 sub file_perm { $_[0]->[FILE_PERM] }
127              
128             #
129             # There's no set_xxx() routines: those objects are passed by reference and
130             # never "expanded", i.e. passed by copy. Modifying any of the attributes
131             # would then lead to strange effects.
132             #
133              
134             #
135             # ->is_same
136             #
137             # Compare settings of $self with that of $other
138             #
139             sub is_same {
140 4     4 0 2660 my $self = shift;
141 4         10 my ($other) = @_;
142 4         19 for (my $i = 0; $i < @$self; $i++) {
143 26 100       97 return 0 if $self->[$i] != $other->[$i];
144             }
145 3         9 return 1;
146             }
147              
148             1; # for require
149             __END__