File Coverage

blib/lib/Log/Deep/File.pm
Criterion Covered Total %
statement 27 52 51.9
branch 0 8 0.0
condition 0 2 0.0
subroutine 9 13 69.2
pod 4 4 100.0
total 40 79 50.6


line stmt bran cond sub pod time code
1             package Log::Deep::File;
2              
3             # Created on: 2009-05-30 22:58:50
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 2     2   9 use strict;
  2         2  
  2         64  
10 2     2   11 use warnings;
  2         3  
  2         62  
11 2     2   8 use version;
  2         3  
  2         15  
12 2     2   125 use Carp qw/carp croak confess/;
  2         2  
  2         149  
13 2     2   15 use Data::Dumper qw/Dumper/;
  2         2  
  2         116  
14 2     2   11 use English qw/ -no_match_vars /;
  2         3  
  2         14  
15 2     2   798 use base qw/Exporter/;
  2         3  
  2         183  
16 2     2   10 use overload '""' => \&name;
  2         2  
  2         19  
17 2     2   137 use Time::HiRes qw/sleep/;
  2         3  
  2         20  
18              
19             our $VERSION = version->new('0.3.5');
20             our @EXPORT_OK = qw//;
21             our %EXPORT_TAGS = ();
22              
23             sub new {
24 0     0 1   my $caller = shift;
25 0 0         my $class = ref $caller ? ref $caller : $caller;
26 0           my ($name) = @_;
27 0           my $self = { name => $name };
28              
29 0           bless $self, $class;
30              
31 0 0 0       open $self->{handle}, '<', $name or warn "Could not open $name: $OS_ERROR\n" and return;
32              
33 0           return $self;
34             }
35              
36             sub line {
37 0     0 1   my ($self) = @_;
38              
39 0           my $fh = $self->{handle};
40 0           my $line = <$fh>;
41 0           my $count = 0;
42              
43 0 0         if (defined $line) {
44 0           while ( $line !~ /\n$/xms ) {
45             # guarentee that we have a full log line, ie if we read a line before it has been completely written
46 0           $line .= <$fh>;
47              
48 0 0         if ($count++ > 200) {
49             # give up if after 2s we still don't have a full line
50 0           last;
51             }
52             else {
53             # sleep a little to give the logging process time to write the rest of the line
54 0           sleep 0.01;
55             # reset the handle so that we can read more
56 0           $self->reset;
57             }
58             }
59             }
60              
61 0           $self->{count}++;
62              
63 0           return $line;
64             }
65              
66 0     0 1   sub name { $_[0]->{name} }
67              
68             sub reset {
69 0     0 1   my ($self) = @_;
70              
71             # reset the file handle so that it can be read again;
72 0           seek $self->{handle}, 0, 1;
73              
74 0           $self->{count} = 0;
75              
76 0           return;
77             }
78              
79             1;
80              
81             __END__