package Log::Error;
use 5.008004;
use strict;
use Cwd;
use CGI::Carp qw(carpout);
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use vars qw(@EXPORT @ISA $VERSION);
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(
a_log
e_log
);
$VERSION = '0.01';
my $localtime = localtime;
our $dir;
BEGIN
{
if ($::dir) {
$dir = $::dir;
}
else {
$0 =~ '(.*[\\\/])\w+\.\w+$';
$dir = $1;
}
}
# Set up main error log file
my $log_file = "e_log.txt";
#Croak will get the line number of the calling program, not the library itself.
open(LOG, ">>$dir/$log_file") or croak "Unable to append to error log: $!";
carpout(*LOG);
##### ***subroutine who print in Appplication Specific Error***
sub a_log{
my $a_localtime = localtime;
my ($a_log, @data) = @_;
#To determine the name of the currently running function, including its package by using the built-in function "caller".
my $sub_al = (caller(0))[3];
#Make sure that the filename doesn't have spaces or extensions like exe or bat on it,
#otherwise it will create file as app_log.txt instead.
unless($a_log=~/^[a-zA-Z_]+\.(?!bat$|exe$)[a-zA-Z_]{3}$/gi)
{
$a_log="a_log.txt"
}
open(LOGAPP, ">>$dir$a_log")
or croak "Unable to append to $a_log: $!";
#print LOGAPP "[$localtime] $a_log : @data ::::: $!\n";
print LOGAPP "[$a_localtime] $a_log : @data \n";
close LOGAPP or die "Cannot close log file:: $a_log : $!\n";
}
sub e_log{
my $e_localtime = localtime;
my ($e_log, @e_data) = @_;
#To determine the name of the currently running function, including its package by using the built-in function "caller".
my $sub_el = (caller(0))[3];
#Make sure that the filename doesn't have spaces or extensions like exe or bat on it,
#otherwise it will create file as app_log.txt instead.
unless($e_log=~/^[a-zA-Z_]+\.(?!bat$|exe$)[a-zA-Z_]{3}$/gi)
{
$e_log="errorapp_log.txt"
}
open(ERRAPPLOG, ">>$dir$e_log")
or croak "Unable to append to $e_log: $!";
print ERRAPPLOG "[$e_localtime] $e_log : @e_data\n";
close ERRAPPLOG or die "Cannot close log file:: $e_log : $!\n";
}
1;
__END__
=head1 DESCRIPTION
Errors trapped by -w (Warning) will issue a warning and report the line number of the error.
Die will cause the program to die and report the line number of the error.
All the errors will be printed to e_log.txt in the same directory where the program that caused the error is running.
Called like:
&e_log('my_file.txt','Oh no, $hit hit the fan.');
2006-05-03 Retitled by [Arunbear], as per Monastery [id://341118|guidelines]
Original title: 'Module Question'
I don't even know where to start looking for this bug,It's opening in the "wrong" place, so let's start with the open call:
open(LOG, ">>$dir/$log_file") or croak "Unable to append to error log: $!";So it uses $dir .. now to see where that's set, which is in BEGIN .. and has this snippet:
$0 =~ '(.*[\\\/])\w+\.\w+$';
$dir = $1;
Several important items here:
$dir = $1 if $0 =~ m#/.*[\\/])\w+\.\w+$#;
use File::Basename; $dir = dirname($0);
BEGIN
{
if ($::dir) {
$dir = $::dir;
}
else {
$0 =~ '(.*[\\\/])\w+\.\w+$';
$dir = $1;
}
}
use File::Basename; my $dir = dirname($0);
use FindBin qw($Bin); my $dir = $Bin;-imran
If you want to always use the directory the script is in, in theory you should be using FindBin, but it is rubbish. Instead, look at tye's mechanism to derive the directory from $0, in node 41213. rel2abs can be found in File::Spec as a class method, or in File::Spec::Functions as a function. (Both come with Perl.)
Using File::Basename, you can then extract the directory the script is in.
perlmonks.org content © perlmonks.org and Anonymous Monk, bart, CountOrlok, davidrw
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03