### package Log;
#
# -*- Perl -*-
#
# Copyright (C) 1996 Craig Ruefenacht <ruefenac@cs.utah.edu>,
#                    Jason Lauman <lauman@cs.utah.edu> and the
#                    University of Utah Computer Science Department
#
# $Id: Log.pm,v 1.1.1.1 2000/05/03 19:55:12 wright Exp $
#
# This is part of rust 1.0 Beta 2
#
# Abstract: Log package for check-q server
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the source distribution of Inquire, in the license
# directory. If you don't have a copy of it, write to:
#
# Free Software Foundation, Inc.
# 675 Mass Ave
# Cambridge, MA 02139, USA.

#$main::Log = \&Log;
$Log = \&Log;

%LOG = ();
%FILE = ();

sub Log {
	my($handle,$string) = @_;
	print "handle = $handle\n";
	if( exists $LOG{$handle} ) {
		my($loghandle) = $LOG{$handle};
		print "loghandle = \"$loghandle\"\tstring = \"$string\"\n";
		eval("print $loghandle \$string");
		print SNS_LOG 	"This is a log string\n";
		print $loghandle "This is a string\n";
	}
	return 1;	
}

sub Log::Init {
	my($handle,$file) = @_;
	$LOG{$handle}	= "${handle}_LOG";
	$FILE{$handle} = $file;
	return 1;
}

sub Log::Begin {
	my($handle) = @_;
	my($OP);
	my($loghandle) = $LOG{$handle};	
	my($file) = $FILE{$handle};
	if( -e $file ) { open($loghandle,">>$file") || &$Error("open: $!"); }
	else { open($loghandle,">$file") || &$Error("open: $!"); }
	my($date) = `date`;
	chop($date);
	print $loghandle "*" x 10,"  ";
	print $loghandle "Beginning log on $date  ";
	print $loghandle "*" x 10,"\n";
	return 1;
}

sub Log::End {
	my($handle) = @_;
	my($loghandle) = $LOG{$handle};	
	my($date) = `date`;
	chop($date);
	print $loghandle "*" x 10,"  ";
	print $loghandle "Ending log on $date  ";
	print $loghandle "*" x 10,"\n";
	return close($loghandle);
}

sub Log::New {
	my($handle) = @_;
	my($file) = $FILE{$handle};	
	my($status) = &End($handle);
	my($i,$newi);
	for($i=9;$i>=1;--$i) {
		$newi = $i + 1;
		if( -e "$file.$i" ) {
			rename("$file.$i","$file.$newi") || &$Error("rename: $!");		
		}
	}
	if( -e $file ) {
		rename($file,"$file.1") || &$Error("rename: $!");		
	}
	if( $status ) { &Begin($handle); }
	return 1;
}

1;
