#!/usr/local/bin/perl -w
#
# This software has been created by Genome Research Limited (GRL). 
# GRL hereby grants permission to use, copy, modify and distribute 
# this software and its documentation for non-commercial purposes 
# without fee at the user's own risk on the basis set out below.
# GRL neither undertakes nor accepts any duty whether contractual or 
# otherwise in connection with the software, its use or the use of 
# any derivative, and makes no representations or warranties, express
# or implied, concerning the software, its suitability, fitness for
# a particular purpose or non-infringement.
# In no event shall the authors of the software or GRL be responsible 
# or liable for any loss or damage whatsoever arising in any way 
# directly or indirectly out of the use of this software or its 
# derivatives, even if advised of the possibility of such damage.
# Our software can be freely distributed under the conditions set out 
# above, and must contain this copyright notice.
#
# $Id: PI_log.pm,v 1.3 2001/03/19 15:04:50 holroyd Exp $
#
# $Log: PI_log.pm,v $
# Revision 1.3  2001/03/19  15:04:50  holroyd
# *** empty log message ***
#
# Revision 1.2  2001/03/19  14:58:39  holroyd
# *** empty log message ***
#
# Revision 1.1  2001/03/19  14:38:26  holroyd
# Initial revision
#
#

BEGIN {
    unshift(@INC, "$ENV{'BADGER'}/bin");
    unshift(@INC, "."); 
}

package PI_log;

use strict;
use Mail::Mailer;

sub new {
  my ( $type,$log_file ) = @_;
  my $command;
  my $self = {
	      logfile => $log_file,
	     };
  bless ($self,$type);
  return ($self);
  
}

sub write_log {
  my ( $self,$log ) = @_;

  unless( open( LOG, ">> $self->{logfile}" )) {
    $self->mail( $log,$! );
    return;
  }
  print LOG $log;
  close( LOG );
}

sub mail {
  my ( $self,$log,$error ) = @_;
  my $mailer = Mail::Mailer->new();
  $mailer->open({ From    => "plateinfo",
		  To      => "holroyd\@sanger.ac.uk",
		  Subject => "ERROR: Trying to write to log",
		});
  printf $mailer "$log\n$error";
  return;
}

1;
