package Log::File;
use Exporter;
use FileHandle;
@ISA= qw(Exporter);
@EXPORT=(MakeEntry, SeparateLogEntries, Path, Close);
sub new
{
my($type)= shift; my($this)= {};
bless $this, $type; $this->Initialize(@_); $this->PrepareLogFile(); return $this; }
sub Initialize
{
my($this)= shift; my(%parameters)= @_; my($terse)= ( 1 == 1 );
undef $this->{'fileHandle'};
foreach $parameter (keys(%parameters))
{ $this->{$parameter}= $parameters{$parameter};
}
$this->{'defaultPath'}= "/var/log/"
unless defined($this->{'defaultPath'});
unless (defined($this->{'newLineDelimiter'}))
{
$this->{'newLineDelimiter'}= "\n";
}
unless (defined($this->{'separator'}))
{
$this->{'separator'}= $this->{'newLineDelimiter'} . "-"
. $this->{'newLineDelimiter'}. $this->{'newLineDelimiter'};
}
unless (defined($this->{'directoryPathDelimiter'}))
{
$this->{'directoryPathDelimiter'}= "/";
}
unless (defined($this->{'name'}))
{
$this->{'name'}= "log";
}
unless (defined($this->{'logFileName'}))
{
my($timeTag)= $this->TimeTag($terse);
while (-e $this->{'name'}.".".$timeTag)
{ $timeTag++; }
$this->{'logFileName'}= $this->{'name'}.".".$timeTag;
}
if (defined($this->{'directory'}))
{
$this->{'directory'}.= $this->{'directoryPathDelimiter'}
unless ($this->{'directory'}=~ /$this->{'directoryPathDelimiter'}$/);
mkdir($this->{'directory'}, 0755) unless -d $this->{'directory'};
}
unless (-d $this->{'directory'})
{ unless (mkdir($this->{'directory'}, 0755))
{ print "Unable to create the requested log directory <",
$this->{'directory'}, ">: $!", $this->{'newLineDelimiter'};
print "Will try present working directory for logs.",
$this->{'newLineDelimiter'};
$this->{'directory'}= $this->{'defaultPath'} . $this->{'name'}
. $this->{'directoryPathDelimiter'};
unless (-d $this->{'directory'})
{ unless (mkdir($this->{'directory'}, 0755))
{ print "Unable to create the default log directory <",
$this->{'directory'}, ">: $!", $this->{'newLineDelimiter'};
print "Will try present working directory for logs.",
$this->{'newLineDelimiter'};
$this->{'directory'}= "";
}
}
}
}
}
sub PrepareLogFile
{
my($this)= shift; my($fh)= $this->{'fileHandle'};
if ($fh)
{ print $fh "Ignored an attempt to prepare the log file again",
$this->{'newLineDelimiter'};
}
else
{ $fh= $this->{'fileHandle'}=
new FileHandle ">".$this->{'directory'}.$this->{'logFileName'};
}
if ($fh)
{ $fh->autoflush(1); print $fh "Starting log: ", TimeTag(), ".", $this->{'newLineDelimiter'};
}
else
{
print "Could not write in my log; will ignore further log entries."
. $this->{'newLineDelimiter'};
}
}
sub MakeEntry
{
my($this)= shift; my($fh)= $this->{'fileHandle'};
print $fh join("", @_, $this->{'newLineDelimiter'})
if $fh;
}
sub SeparateLogEntries
{
my($this)= shift; my($fh)= $this->{'fileHandle'};
print $fh $this->{'separator'}
if $fh;
}
sub Path
{
my($this)= shift;
return $this->{'directory'} . $this->{'logFileName'};
}
sub Close
{
my($this)= shift; my($fh)= $this->{'fileHandle'};
if ($fh)
{ $this->SeparateLogEntries();
print $fh "Closing log: ", TimeTag(), ".", $this->{'newLineDelimiter'};
undef($this->{'fileHandle'}); }
}
sub TimeTag()
{
my($terse)= shift; my($second, $minute, $hour, $day, $month, $year)=
localtime(time());
$month++; $minute= "0".$minute if ($minute < 10);
$second= "0".$second if ($second < 10);
$month= "0".$month if ($month < 10);
$day= "0".$day if ($day < 10);
$year+= 1900;
return "$year$month$day$hour$minute$second"
if $terse;
return "$hour:$minute:$second $month/$day/$year";
}
1;