File Coverage

blib/lib/Log/Dispatch/CronoDir.pm
Criterion Covered Total %
statement 73 73 100.0
branch 19 24 79.1
condition 8 12 66.6
subroutine 14 14 100.0
pod 1 2 50.0
total 115 125 92.0


line stmt bran cond sub pod time code
1             package Log::Dispatch::CronoDir;
2 2     2   16540 use 5.008001;
  2         4  
3 2     2   6 use strict;
  2         2  
  2         30  
4 2     2   12 use warnings;
  2         1  
  2         60  
5 2     2   408 use parent qw(Log::Dispatch::Output);
  2         228  
  2         6  
6              
7             our $VERSION = "0.06";
8              
9 2     2   22700 use File::Path qw(make_path);
  2         3  
  2         82  
10 2     2   7 use Params::Validate qw(validate SCALAR BOOLEAN);
  2         2  
  2         65  
11 2     2   6 use Scalar::Util qw(openhandle);
  2         2  
  2         1126  
12              
13             Params::Validate::validation_options(allow_extra => 1);
14              
15             sub new {
16 9     9 1 4756 my ($proto, %args) = @_;
17 9   33     36 my $class = ref $proto || $proto;
18 9         13 my $self = bless {}, $class;
19 9         34 $self->_basic_init(%args);
20 9         657 $self->_init(%args);
21 7         18 $self;
22             }
23              
24             sub _init {
25 9     9   9 my $self = shift;
26 9         267 my %args = validate(
27             @_,
28             { dirname_pattern => { type => SCALAR },
29             permissions => {
30             type => SCALAR,
31             optional => 1,
32             },
33             filename => { type => SCALAR },
34             mode => {
35             type => SCALAR,
36             default => '>>',
37             },
38             binmode => {
39             type => SCALAR,
40             optional => 1,
41             },
42             autoflush => {
43             type => BOOLEAN,
44             default => 1,
45             },
46             }
47             );
48              
49 7         42 my @rules;
50 7         30 $args{dirname_pattern} =~ s{ \% (\w) }{
51             $1 eq 'Y' ? do {
52 7         11 push @rules, { pos => 5, offset => 1900 };
53 7         18 '%04d';
54             } : $1 eq 'm' ? do {
55 7         11 push @rules, { pos => 4, offset => 1 };
56 7         13 '%02d';
57 21 50       50 } : $1 eq 'd' ? do {
    100          
    100          
58 7         8 push @rules, { pos => 3, offset => 0 };
59 7         10 '%02d';
60             } : '';
61             }egx;
62              
63 7         12 $self->{_rules} = \@rules;
64 7         19 $self->{_dirname_pattern} = $args{dirname_pattern};
65 7         8 $self->{_permissions} = $args{permissions};
66 7         7 $self->{_filename} = $args{filename};
67 7         6 $self->{_mode} = $args{mode};
68 7         6 $self->{_binmode} = $args{binmode};
69 7         7 $self->{_autoflush} = $args{autoflush};
70              
71 7         10 $self->_get_current_fh;
72             }
73              
74 3     3   107 sub _localtime { localtime }
75              
76             sub _find_current_dir {
77 11     11   9 my $self = shift;
78 11         17 my @now = _localtime();
79             sprintf(
80             $self->{_dirname_pattern},
81 11         50 map { $now[ $_->{pos} ] + $_->{offset} } @{ $self->{_rules} },
  33         74  
  11         20  
82             );
83             }
84              
85             sub _get_current_fh {
86 11     11   9 my $self = shift;
87 11         14 my $dirname = $self->_find_current_dir;
88              
89 11 100 100     51 if (!exists $self->{_current_dir} || $dirname ne $self->{_current_dir}) {
90             close $self->{_current_fh}
91 8 100 66     28 if $self->{_current_fh} and openhandle($self->{_current_fh});
92              
93 8         1818 make_path $dirname;
94 8         12 $self->{_current_dir} = $dirname;
95 8         74 $self->{_current_filepath} = File::Spec->catfile($dirname, $self->{_filename});
96              
97 8 100       21 if (defined $self->{_permissions}) {
98 1 50       16 chmod $self->{_permissions}, $dirname
99             or die "Failed chmod $dirname to $self->{_permissions}: $!";
100             }
101              
102             open my $fh, $self->{_mode}, $self->{_current_filepath}
103 8 50       380 or die "Failed opening file $self->{current_filepath} to write: $!";
104              
105 8 100       21 binmode $fh, $self->{_binmode} if $self->{_binmode};
106              
107             do {
108 8         19 my $oldfh = select $fh;
109 8         12 $| = 1;
110 8         17 select $oldfh;
111 8 50       13 } if $self->{_autoflush};
112              
113 8         22 $self->{_current_fh} = $fh;
114             }
115              
116 11         202 $self->{_current_fh};
117             }
118              
119             sub log_message {
120 4     4 0 1902 my ($self, %args) = @_;
121 4         5 print { $self->_get_current_fh } $args{message}
122 4 50       5 or die "Cannot write to file $self->{_current_file}: $!";
123             }
124              
125             sub DESTROY {
126 9     9   4406 my $self = shift;
127             close $self->{_current_fh}
128 9 100 66     146 if $self->{_current_fh} and openhandle($self->{_current_fh});
129             }
130              
131             1;
132             __END__