package PGF::GapResolution::Warnings; =head1 NAME PGF::GapResolution::Warnings =head1 SYNOPSIS use PGF::GapResolution::Warnings; # The argument $myPath is optional. Default is current working # directory. This is the file path used when creating # the warnings file. # my $obj = PGF::GapResolution::Warnings->new( path=>$myPath, # optional; set path to create warnings file-default is cwd logger=>$objLogger # optional PGF::Utilities::Logger object. if defined, # warning will be added to log file by calling # logError. ); # Set the name of the sub project directory. This is used # within the warning message. Setting this will prepend # the message "WARNING for $subProjectDir: " to the # warning messages. # $obj->setSubProjectName($subProjectDir); # Adding warning messages. # $obj->add("file doesn't exist!"); $obj->add("failed to execute myscript"); # Getting warning messages. # my @warnings = $obj->getall(); my $numberOfWarnings = $obj->getNumberOfWarnings(); # Checks if file exists. Returns 1 if yes. If no, then # warning message is created and will be saved in warnings file. # If logger is defined during instantiation, message will also be # added to log file. # my $status = $obj->fileExists($myFile); # Create warnings file. File name is based on the client # program using this module plus the file extension # defined in the gap resolution config file parameter # errorHandling.logErrorInFileWithExtension= # The file path is determine based on the parameter that is # passed when the object is instantiated, or the current # working directory if no parameter is passed. # $obj->createFile(); my $file = $obj->getFile(); =head1 DESCRIPTION =head1 AUTHOR(s) Stephan Trong =head1 HISTORY =over =item * Stephan Trong 07/30/2009 Creation =back =cut #============================================================================# use strict; use Carp; use Carp qw(cluck); use Cwd; use File::Basename; use FindBin qw($RealBin); use lib "$FindBin::RealBin/../lib"; use PGF::Utilities::Properties; #============================================================================# sub new { my $class = shift; my %params = @_; my $outputDir = defined $params{path} ? $params{path} : getcwd; my $logger = defined $params{logger} ? $params{logger} : ''; my $configFile = defined $ENV{GAPRES_CONFIG} ? $ENV{GAPRES_CONFIG} : "$RealBin/../config/gapRes.config"; my $objProperty = PGF::Utilities::Properties->new(-configFile=>$configFile); $objProperty->setExceptionIfEntryNotFound(1); # confess if entry in config my $dieIfErrorDetected = $objProperty->getProperty( "errorHandling.dieIfErrorDetectedWhileProcessingSubProjectDir"); my $warningsFile = $objProperty->getProperty( "errorHandling.warningsFile"); $outputDir =~ s/\/+$//g; # remove trailing '/'. my $clientApp = basename($0); my $outputFile = "$outputDir/$warningsFile"; my $self = { outputFile=>$outputFile, warnings=>[], dieIfErrorDetected=>$dieIfErrorDetected, subProjectName=>'', logger=>$logger, client=>$clientApp, }; bless $self, $class; return $self; } #============================================================================# sub setLogFile { my $self = shift; my $file = shift; $self->{outputFile} = $file; } #============================================================================# sub setSubProjectName { my $self = shift; my $name = shift; $self->{subProjectName} = $name; } #============================================================================# sub fileExists { my $self = shift; my $file = shift; my $success = 0; if ( -s $file ) { $success = 1; } else { my $msg = "file $file is missing or has zero size.\n"; $self->{logger}->logError($msg) if $self->{logger}; $self->add($msg); } return $success; } #============================================================================# sub dieIfErrorDetected { my $self = shift; return $self->{dieIfErrorDetected}; } #============================================================================# sub createFile { my $self = shift; my $outputFile = $self->{outputFile}; unless (open WARNINGS_FH, ">>$outputFile" ) { confess "ERROR: failed to open file $outputFile: $!\n"; } print WARNINGS_FH "DATE: ",getCurrentDateAndTime(), " $self->{client}\n"; print WARNINGS_FH map "$_\n", @{$self->{warnings}}; close WARNINGS_FH; return $outputFile; } #============================================================================# sub getFile { my $self = shift; return $self->{outputFile}; } #============================================================================# sub add { my $self = shift; my $message = shift; if ( length $self->{subProjectName} ) { $message = "WARNING for $self->{subProjectName}: $message"; } push @{$self->{warnings}}, $message; if ( $self->dieIfErrorDetected ) { $self->createFile if $self->getNumberOfWarnings; confess "$message\n"; } else { print STDERR "$message\n"; } } #============================================================================# sub getall { my $self = shift; return @{$self->{warnings}}; } #============================================================================# sub getNumberOfWarnings { my $self = shift; if ( defined @{$self->{warnings}} ) { return scalar( @{$self->{warnings}} ); } else { return 0; } } #============================================================================# sub getCurrentDateAndTime { # Returns current date and time in format: # 'MM-DD-YYYY HH24:MI:SS' my ($sec,$min,$hour,$day,$mon,$year) = localtime(time); $mon++; $year+=1900; my $time = sprintf( "%02d/%02d/%04d %02d:%02d:%02d", $mon,$day,$year,$hour,$min,$sec ); return $time; } #============================================================================# 1;