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 1     1   869 use strict;
  1         2  
  1         36  
10 1     1   5 use warnings;
  1         2  
  1         33  
11 1     1   6 use version;
  1         1  
  1         6  
12 1     1   75 use Carp qw/carp croak confess/;
  1         1  
  1         54  
13 1     1   4 use Data::Dumper qw/Dumper/;
  1         1  
  1         35  
14 1     1   4 use English qw/ -no_match_vars /;
  1         2  
  1         5  
15 1     1   335 use base qw/Exporter/;
  1         1  
  1         116  
16 1     1   4 use overload '""' => \&name;
  1         1  
  1         8  
17 1     1   617 use Time::HiRes qw/sleep/;
  1         1157  
  1         3  
18              
19             our $VERSION = version->new('0.3.3');
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__
82              
83             =head1 NAME
84              
85             Log::Deep::File - Object for keeping track of info related to a log file.
86              
87             =head1 VERSION
88              
89             This documentation refers to Log::Deep::File version 0.3.3.
90              
91             =head1 SYNOPSIS
92              
93             use Log::Deep::File;
94              
95             # Create a new object
96             my $file = Log::Deep::File->new('deep.log');
97              
98             # read the log file
99             while ( my $line = $file->line ) {
100             # so stuff
101             ...
102             }
103              
104             # use the file name in a string
105             print "Finished reading the file '$file'\n";
106              
107             # reset the handle so that we can start again
108             $file->reset;
109              
110             =head1 DESCRIPTION
111              
112             =head1 SUBROUTINES/METHODS
113              
114             =head3 C<new ( $name )>
115              
116             Param: C<$name> - string - The log file name to be tracked
117              
118             Return: Log::Deep::File - A new object
119              
120             Description: Creates a new object and opens the specified file.
121              
122             =head3 C<line ( )>
123              
124             Return: The next line read from the log file or undef if the end of the file
125             has been reached
126              
127             Description: Reads the next line of the log file.
128              
129             =head3 C<name ( )>
130              
131             Return: The name of the log file
132              
133             =head3 C<reset ( )>
134              
135             Description: Resets the file handle so that it can be attempted to be read
136             again at a later time.
137              
138             =head1 DIAGNOSTICS
139              
140             =head1 CONFIGURATION AND ENVIRONMENT
141              
142             =head1 DEPENDENCIES
143              
144             =head1 INCOMPATIBILITIES
145              
146             =head1 BUGS AND LIMITATIONS
147              
148             There are no known bugs in this module.
149              
150             Please report problems to Ivan Wills (ivan.wills@gmail.com).
151              
152             Patches are welcome.
153              
154             =head1 AUTHOR
155              
156             Ivan Wills - (ivan.wills@gmail.com)
157              
158             =head1 LICENSE AND COPYRIGHT
159              
160             Copyright (c) 2009 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
161             All rights reserved.
162              
163             This module is free software; you can redistribute it and/or modify it under
164             the same terms as Perl itself. See L<perlartistic>. This program is
165             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
166             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
167             PARTICULAR PURPOSE.
168              
169             =cut