use
5.006;
our
@ISA
=
qw(Exporter)
;
our
%EXPORT_TAGS
= (
'all'
=> [
qw(
)
] );
our
@EXPORT_OK
= ( @{
$EXPORT_TAGS
{
'all'
} } );
our
@EXPORT
=
qw(
)
;
our
$VERSION
=
'0.02'
;
my
$package
= __PACKAGE__;
my
$dfh
=
""
;
local
*DFH
;
sub
new {
my
$class
=
shift
;
my
$debug
=
shift
|| 1;
my
$self
= {};
$self
->{
'DEBUG'
} =
$debug
if
defined
$debug
;
bless
$self
,
$class
;
return
$self
;
}
sub
timeStamp() {
my
$self
=
shift
;
my
$timestamp
=
shift
|| 1;
$self
->{
'TIMESTAMP'
} =
$timestamp
if
defined
$timestamp
;
return
$self
->{
'TIMESTAMP'
};
}
sub
detail() {
my
$self
=
shift
;
my
$detail
=
shift
|| 1;
$self
->{
'DETAIL'
} =
$detail
if
defined
$detail
;
return
$self
->{
'DETAIL'
};
}
sub
debug {
my
$self
=
shift
;
my
$debug
=
shift
;
$self
->{
'DEBUG'
} =
$debug
if
defined
$debug
;
return
$self
->{
'DEBUG'
};
}
sub
debugFile {
my
$self
=
shift
;
my
$debugfile
=
shift
;
$self
->{
'DEBUGFILE'
} =
$debugfile
if
defined
$debugfile
;
return
$self
->{
'DEBUGFILE'
};
}
sub
initialize {
my
$self
=
shift
;
if
((
$self
->{
'DEBUG'
}) && (
$self
->{
'DEBUGFILE'
}))
{
open
(FH,
"+>> $self->{'DEBUGFILE'}"
) ||
die
print
STDERR
"error:($package):could not open $self->{'DEBUGFILE'}.\n"
;
*DFH
=
*FH
;
print
STDERR
"Logging to $self->{'DEBUGFILE'}\n"
;
}
elsif
((
$self
->{
'DEBUG'
}) && (!
defined
(
$self
->{
'DEBUGFILE'
})))
{
open
(FH,
">&STDOUT"
) ||
die
print
STDERR
"error:($package):could not open STDERR.\n"
;
*DFH
=
*FH
;
print
STDERR
"Logging to STDOUT\n"
;
}
else
{
open
(FH,
"+>> /dev/null"
) ||
die
print
STDERR
"error:($package):could not open /dev/null.\n"
;
*DFH
=
*FH
;
print
STDERR
"Logging to /dev/null\n"
;
}
select
((
select
(DFH),$| = 1)[0]);
if
(
*DFH
)
{
print
DFH
"debug:($package): initialized.\n"
||
die
print
STDERR
"error:($package):could not write to "
.
$self
->debugfilehandle().
".\n"
;
}
else
{
print
STDERR
"error:($package) debug file handle does not exist.\n"
;
}
return
$self
;
}
sub
message {
my
$self
=
shift
;
my
$message
=
shift
;
my
$datetime
=
undef
;
my
$origin
=
undef
;
my
$msg
=
undef
;
if
(
$message
eq
""
) {
$message
=
"null"
;
}
if
(
$self
->{
'TIMESTAMP'
}) {
my
(
$sec
,
$min
,
$hour
,
$mday
,
$mon
,
$year
,
$wday
,
$yday
,
$isdst
) =
gmtime
(
time
);
$datetime
=
sprintf
(
"%04d-%02d-%02d.%02d:%02d:%02d"
,
$year
+1900,
$mon
+1,
$mday
,
$hour
,
$min
,
$sec
);
$msg
.=
"["
.
$datetime
.
"]:"
;
}
if
(
$self
->{
'DETAIL'
}) {
$origin
= (
caller
(0))[0];
$origin
.=
"("
.(
caller
())[2].
")"
;
$origin
.=
"->"
.(
caller
(0))[3];
$msg
.=
"["
.
$origin
.
"]:"
;
}
$msg
.=
$message
;
print
DFH
$msg
.
"\n"
;
}
1;