Log::Deep::File - Object for keeping track of info related to a log file.


Log-Deep documentation Contained in the Log-Deep distribution.

Index


Code Index:

NAME

Top

Log::Deep::File - Object for keeping track of info related to a log file.

VERSION

Top

This documentation refers to Log::Deep::File version 0.3.1.

SYNOPSIS

Top

   use Log::Deep::File;

   # Create a new object
   my $file = Log::Deep::File->new('deep.log');

   # read the log file
   while ( my $line = $file->line ) {
       # so stuff
       ...
   }

   # use the file name in a string
   print "Finished reading the file '$file'\n";

   # reset the handle so that we can start again
   $file->reset;

DESCRIPTION

Top

SUBROUTINES/METHODS

Top

new ( $name )

Param: $name - string - The log file name to be tracked

Return: Log::Deep::File - A new object

Description: Creates a new object and opens the specified file.

line ( )

Return: The next line read from the log file or undef if the end of the file has been reached

Description: Reads the next line of the log file.

name ( )

Return: The name of the log file

reset ( )

Description: Resets the file handle so that it can be attempted to be read again at a later time.

DIAGNOSTICS

Top

CONFIGURATION AND ENVIRONMENT

Top

DEPENDENCIES

Top

INCOMPATIBILITIES

Top

BUGS AND LIMITATIONS

Top

There are no known bugs in this module.

Please report problems to Ivan Wills (ivan.wills@gmail.com).

Patches are welcome.

AUTHOR

Top

Ivan Wills - (ivan.wills@gmail.com)

LICENSE AND COPYRIGHT

Top


Log-Deep documentation Contained in the Log-Deep distribution.

package Log::Deep::File;

# Created on: 2009-05-30 22:58:50
# Create by:  Ivan Wills
# $Id$
# $Revision$, $HeadURL$, $Date$
# $Revision$, $Source$, $Date$

use strict;
use warnings;
use version;
use Carp qw/carp croak confess/;
use Data::Dumper qw/Dumper/;
use English qw/ -no_match_vars /;
use base qw/Exporter/;
use overload '""' => \&name;
use Time::HiRes qw/sleep/;

our $VERSION     = version->new('0.3.1');
our @EXPORT_OK   = qw//;
our %EXPORT_TAGS = ();

sub new {
	my $caller = shift;
	my $class  = ref $caller ? ref $caller : $caller;
	my ($name) = @_;
	my $self   = { name => $name };

	bless $self, $class;

	open $self->{handle}, '<', $name or warn "Could not open $name: $OS_ERROR\n" and return;

	return $self;
}

sub line {
	my ($self) = @_;

	my $fh   = $self->{handle};
	my $line = <$fh>;
	my $count = 0;

	if (defined $line) {
		while ( $line !~ /\n$/xms ) {
			# guarentee that we have a full log line, ie if we read a line before it has been completely written
			$line .= <$fh>;

			if ($count++ > 200) {
				# give up if after 2s we still don't have a full line
				last;
			}
			else {
				# sleep a little to give the logging process time to write the rest of the line
				sleep 0.01;
				# reset the handle so that we can read more
				$self->reset;
			}
		}
	}

	$self->{count}++;

	return $line;
}

sub name { $_[0]->{name} }

sub reset {
	my ($self) = @_;

	# reset the file handle so that it can be read again;
	seek $self->{handle}, 0, 1;

	$self->{count} = 0;

	return;
}

1;

__END__