syntax error at Logger.pm line 60, near "format $FH "
####
Can't coerce UNKNOWN to string in formline at Logger.pm line 94.
####
sub lwrite{
my($self,$msg,$value)=@_; # args == object, message and optional value
my $FH=*{$$self{FILEHANDLE}}; # Make a filehandle string from the blob
my $tstamp;
format $FH =
^<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$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) = localtime(time());
my @wdays = qw(Sun Mon Tue Wed Thu Fri Sat); # Make an array of 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]", $year, $mon, $mday, $hour, $min, $sec);
if(! write $FH ){ # 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 well if value == 1
}
sub log{
my ($self,$error,$value)=@_; # Args == object, error message and value
$self->clear; # Clear last error from the object
$$self{ERROR}=$error; # Add the error message to the object
push @{$$self{HISTORY}},$error; # Push the error into the history array
carp "$error\n" if $$self{CARP}; # Carp if an error with this object
$self->lwrite($error,$value); # Send the message and value to "lwrite" sub above
}
####
################################################################################
# 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 logfile!\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 the 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 write{
my($self,$msg,$value)=@_; # args == object, message and optional value
my $FH=*{$$self{FILEHANDLE}}; # Make a filehandle string from the blob
# Get the local time into an array
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time());
my @wdays = qw(Sun Mon Tue Wed Thu Fri Sat); # Make an array of 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
my $format = sprintf("[$weekday %4d-%02d-%02d %02d:%02d:%02d] $msg", $year, $mon, $mday, $hour, $min, $sec);
if(! print $FH "$format\n"){ # 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 well 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 message and value
$self->clear; # Clear last error from the object
$$self{ERROR}=$error; # Add the error message to the object
push @{$$self{HISTORY}},$error; # Push the error into the history array
carp "$error\n" if $$self{CARP}; # Carp if an error with this object
$self->write($error,$value); # Send the message and value to "write" sub above
}
1;
####
use Logger;
# Create the Logger object passing file name
my $log=Logger->new(LOG=>"FILE.LOG",CARP=>1);
# Call log method passing the message and 1 to print to screen or 0 not to print to screen.
$log->log("A",1);
$log->log("qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm ",1);
$log->log("12345690123456789012345678990",1);