######################################################################
+##########
# NAME: Logger
# PURPOSE: Single interface for log output
# AUTHOR: Marty Gipson, Larry Stone
# VERSION: 2006.03.07 11:00
######################################################################
+##########
package Logger;
use strict;
use Carp;
use FileHandle;
use Time::HiRes qw/usleep/;
use vars qw /$VERSION $SEM $ms/;
# Logger is intended to be a single interface for all logging action.
+Each instance of Logger is intended to be an output for a particular
+type of log;
# Logger will save a txt file under c:\\Qagentlogs on windows side and
+ /root/Qagentlogs on linux
# When calling logger, the user should pass filename , message and '1'
+ to print output to screen.
#
#
sub new{
my($class,%args)=@_;
my $self = bless{
LOG => "c:\\ShopTestHarnessLogs\\$args{LOG}" || croak "No l
+ogfile!\n", # Need a name for the logfile or croak
CARP => $args{CARP} || undef,
FILEHANDLE => new FileHandle || croak "Unable to get filehandle\n"
+, # Croak if unable to get filehandle
ERROR => "",
HISTORY => [], # Create an array for history
},$class;
if(! $self->open_log()){
die"Unable to open $self->{LOG}\n"; # Croak if we can't open th
+e file
}
return $self; # Return the object
}
sub get_log{
my $self=shift;
return $$self{LOG}; # Return the log
}
sub open_log{
my $self=shift;
my $path="c:\\ShopTestHarnessLogs";
unless (opendir(DIR,$path)) {
system("md c:\\ShopTestHarnessLogs")
}
if(! open($$self{FILEHANDLE},">>$$self{LOG}")){
$self->log("Unable to open logfile\n");
return 0; # On failure, return 0
}
$$self{FILEHANDLE}->autoflush(1);
return 1; # On success, return 1
}
sub lwrite{
my($self,$msg,$value)=@_; # args == object, message and
+ optional value
my $FH=*{$$self{FILEHANDLE}}; # Make a filehandle string fr
+om the blob
my $tstamp;
format LOG=
^<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$tstamp, $msg
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$msg
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$msg
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$msg
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$msg
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$msg
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$msg
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$msg
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$msg
.
# Get the local time into an array
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti
+me(time());
my @wdays = qw(Sun Mon Tue Wed Thu Fri Sat); # Make an array o
+f weekday names
my $weekday = $wdays[$wday]; # Get today's weekday name
$year = $year + 1900; # Calculate the current year
$mon = $mon + 1; # Calculate the current month
# Now format the message with a time stamp in front of the message
$tstamp = sprintf("[$weekday %4d-%02d-%02d %02d:%02d:%02d] $msg",
+$year, $mon, $mday, $hour, $min, $sec);
if(! write LOG ){ # Log it if we cannot print to the file
$self->log("Unable to write to $$self{LOG}: $!\n");
}
print "\n$msg \n" if ($value ==1); # Print to monitor as w
+ell if value == 1
}
sub clear{ # Clear the error (make it undef)
my $self=shift;
$$self{ERROR}=undef;
}
sub log{
my ($self,$error,$value)=@_; # Args == object, error messag
+e and value
$self->clear; # Clear last error from the object
$$self{ERROR}=$error; # Add the error message to the ob
+ject
push @{$$self{HISTORY}},$error; # Push the error into the h
+istory array
carp "$error\n" if $$self{CARP}; # Carp if an error with th
+is object
$self->lwrite($error,$value); # Send the message and value
+to "lwrite" sub above
}
1;
|