File Coverage

blib/lib/Log/Minimal/Instance.pm
Criterion Covered Total %
statement 73 73 100.0
branch 45 58 77.5
condition 4 6 66.6
subroutine 13 13 100.0
pod 2 2 100.0
total 137 152 90.1


line stmt bran cond sub pod time code
1             package Log::Minimal::Instance;
2              
3 2     2   179776 use strict;
  2         6  
  2         81  
4 2     2   10 use warnings;
  2         5  
  2         64  
5 2     2   870 use parent 'Log::Minimal';
  2         325  
  2         12  
6 2     2   54904 use File::Stamped;
  2         32824  
  2         79  
7 2     2   26 use File::Spec;
  2         4  
  2         182  
8              
9             our $VERSION = '0.06';
10              
11             BEGIN {
12             # for object methods
13 2     2   6 for my $level (qw/crit warn info debug croak/) {
14 10         14 for my $suffix (qw/f ff d/) {
15 30         45 my $method = $level.$suffix;
16              
17 30 100       149 my $parent_code = Log::Minimal->can( ($suffix eq 'd') ? $level."f" : $method );
18              
19 2     2   11 no strict 'refs';
  2         5  
  2         309  
20             my $code = sub {
21 19     19   7924 my $self = shift;
22 19   100     90 local $Log::Minimal::TRACE_LEVEL = ($Log::Minimal::TRACE_LEVEL||0) + 1;
23 19         55 local $Log::Minimal::LOG_LEVEL = $self->{level};
24 19         39 local $Log::Minimal::PRINT = $self->{_print};
25 19 50       114 $parent_code->( ($suffix eq 'd') ? Log::Minimal::ddf(@_) : @_ );
26 30         155 };
27 30         36 *{$method} = $code;
  30         1867  
28             }
29             }
30             }
31              
32             sub new {
33 13     13 1 64274 my ($class, %args) = @_;
34              
35 13 100       64 my $pattern = exists $args{pattern} ? $args{pattern} : undef;
36 13 100       54 my $symlink = exists $args{symlink} ? $args{symlink} : undef;
37 13 100       54 my $base_dir = exists $args{base_dir} ? $args{base_dir} : '.';
38 13 100       50 my $iomode = exists $args{iomode} ? $args{iomode} : '>>:utf8';
39 13 100       44 my $rotationtime = exists $args{rotationtime} ? $args{rotationtime} : 1;
40 13 100       44 my $autoflush = exists $args{autoflush} ? $args{autoflush} : 1;
41 13 100       39 my $close_after_write = exists $args{close_after_write} ? $args{close_after_write} : 1;
42 13 50       40 my $auto_make_dir = exists $args{auto_make_dir} ? $args{auto_make_dir} : 0;
43 13 50       39 my $callback = exists $args{callback} ? $args{callback} : undef;
44              
45 13         19 my $fh;
46 13 100       36 if ($pattern) {
47 6         27 $pattern = $class->_build_pattern($base_dir, $pattern);
48 6         22 $symlink = $class->_build_pattern($base_dir, $symlink);
49 6 50       91 $fh = File::Stamped->new(
    50          
    100          
50             defined $pattern ? (pattern => $pattern) : (),
51             defined $callback ? (callback => $callback) : (),
52             defined $symlink ? (symlink => $symlink) : (),
53             iomode => $iomode,
54             autoflush => $autoflush,
55             close_after_write => $close_after_write,
56             rotationtime => $rotationtime,
57             );
58             }
59             else {
60 7         26 $fh = *STDERR;
61             }
62              
63             bless {
64             level => $args{level} || 'DEBUG',
65             base_dir => $base_dir,
66             iomode => $iomode,
67             rotationtime => $rotationtime,
68             autoflush => $autoflush,
69             close_after_write => $close_after_write,
70             auto_make_dir => $auto_make_dir,
71              
72             _fh => $fh,
73             _print => sub {
74 10     10   992 my ($time, $type, $message, $trace) = @_;
75 10         17 print {$fh} "$time [$type] $message at $trace\n"
  10         245  
76             },
77 13   50     1122 }, $class;
78             }
79              
80             sub log_to {
81 8     8 1 516 my ($self, $opts, @args) = @_;
82              
83 8 100       39 if (ref $opts eq 'ARRAY') {
    100          
84 3         17 $opts = {
85             pattern => $opts->[0],
86             symlink => $opts->[1],
87             };
88             }
89             elsif (!ref $opts) {
90 3         30 $opts = { pattern => $opts };
91             }
92              
93 8         18 my ($pattern, $symlink, $callback);
94 8 100       33 my $base_dir = defined $opts->{base_dir} ? $opts->{base_dir} : $self->{base_dir};
95 8         29 $pattern = $self->_build_pattern($base_dir, $opts->{pattern});
96 8         28 $symlink = $self->_build_pattern($base_dir, $opts->{symlink});
97 8 50       29 $callback = exists $opts->{callback} ? $opts->{callback} : undef;
98              
99 8 50       147 my $fh = File::Stamped->new(
    50          
    100          
    50          
    50          
    50          
    50          
    50          
100             defined $pattern ? (pattern => $pattern) : (),
101             defined $callback ? (callback => $callback) : (),
102             defined $symlink ? (symlink => $symlink) : (),
103             iomode => defined $opts->{iomode} ? $opts->{iomode} : $self->{iomode},
104             autoflush => defined $opts->{autoflush} ? $opts->{autoflush} : $self->{autoflush},
105             close_after_write => defined $opts->{close_after_write} ? $opts->{close_after_write} : $self->{close_after_write},
106             rotationtime => defined $opts->{rotationtime} ? $opts->{rotationtime} : $self->{rotationtime},
107             auto_make_dir => defined $opts->{auto_make_dir} ? $opts->{auto_make_dir} : $self->{auto_make_dir},
108             );
109              
110 8         817 local $self->{_fh} = $fh;
111             local $self->{_print} = sub {
112 7     7   624 my ($time, $type, $message, $trace) = @_;
113 7         13 print {$fh} "$time $message at $trace\n";
  7         56  
114 8         45 };
115              
116 8   50     42 local $Log::Minimal::TRACE_LEVEL = ($Log::Minimal::TRACE_LEVEL||0) + 1;
117 8         14 local $Log::Minimal::LOG_LEVEL = 'DEBUG'; # Must be logging!
118 8         30 $self->critf(@args);
119             }
120              
121             sub _build_pattern {
122 28     28   57 my ($self, $base_dir, $pattern) = @_;
123 28 100       77 return unless defined $pattern;
124              
125 21 100       193 unless (File::Spec->file_name_is_absolute($pattern)) {
126 6         54 $pattern = File::Spec->catfile($base_dir, $pattern);
127             }
128 21         63 return $pattern;
129             }
130              
131             1;
132              
133             __END__