package Log;
use FileHandle;
use POSIX;

## The constructor method
sub new {
   ## Throw out the first parameter, the class name
   shift @_;

   ## Use the arguments given to the constructor
   my ($LogPathname) = @_;

   ## Used for our object reference
   my ($self) = {};

   ## Setup variables that will be maintained inside the object reference
   $self->{LOG_PATHNAME}        = $LogPathname;
   $self->{OFFSET}              = 0;
   $self->{LAST_MATCH}          = 0;
   $self->{LAST_RECORD}         = undef;

   ## Obtain an object reference for this package
   bless($self);

   ## Return it to the caller
   return $self;
}

## This method will get the log pathname
sub getLogPathname {
   my ($self) = @_;
   return $self->{LOG_PATHNAME};
}

## This method will get the current size of the log file
sub getLogSize {
   my ($self) = @_;
   return (stat($self->{LOG_PATHNAME}))[7];
}

## This method will get the current offset into the log file
sub getOffset {
   my ($self) = @_;
   return $self->{OFFSET};
}

## This method will set a new offset for the log file
sub setOffset {
   my ($self, $offset) = @_;
   $self->{OFFSET} = $offset;
   return $self->{OFFSET};
}

## This method will get the last record
sub getLastRecord {
   my ($self);
   return $self->{LAST_RECORD};
}

## This method will set the last record
sub setLastRecord {
   my ($self, $record) = @_;
   $self->{LAST_RECORD} = $record;
   return $self->{LAST_RECORD};
}

## This method will set the last match value to true
sub setLastMatch {
   my ($self) = @_;
   $self->{LAST_MATCH} = 1;
   return $self->{LAST_MATCH};
}

## This method will set the last match value to false
sub resetLastMatch {
   my ($self) = @_;
   $self->{LAST_MATCH} = 0;
   return $self->{LAST_MATCH};
}

## This method will get the last match value
sub getLastMatch {
   my ($self) = @_;
   return $self->{LAST_MATCH};
}

## This method will open the log file for reading and return a file handle
sub openLog {
   my ($self) = @_;
   my ($fh) = FileHandle->new();

   ## attempt to open log file
   unless ($fh->open($self->{LOG_PATHNAME})) {
      ## failed to open log file
      return undef;
   }

   ## seek to current offset
   $fh->seek($self->{OFFSET}, SEEK_SET);

   ## return the file handle
   return $fh;
}

1;
