kivitendo/SL/LXDebug.pm @ 874ae842
d319704a | Moritz Bunkus | package LXDebug;
|
||
081a4f97 | Moritz Bunkus | use constant NONE => 0;
|
||
use constant INFO => 1;
|
||||
5d0421a3 | Moritz Bunkus | use constant DEBUG1 => 2;
|
||
bce420e0 | Sven Schöling | use constant DEBUG2 => 4;
|
||
use constant QUERY => 8;
|
||||
use constant TRACE => 16;
|
||||
use constant ALL => 31;
|
||||
d319704a | Moritz Bunkus | |||
5d0421a3 | Moritz Bunkus | use constant FILE_TARGET => 0;
|
||
use constant STDERR_TARGET => 1;
|
||||
d319704a | Moritz Bunkus | |||
use POSIX qw(strftime);
|
||||
my $data_dumper_available;
|
||||
BEGIN {
|
||||
eval("use Data::Dumper");
|
||||
$data_dumper_available = $@ ? 0 : 1;
|
||||
$global_level = NONE;
|
||||
}
|
||||
sub new {
|
||||
my $type = shift;
|
||||
my $self = {};
|
||||
$self->{"calldepth"} = 0;
|
||||
$self->{"file"} = "/tmp/lx-office-debug.log";
|
||||
$self->{"target"} = FILE_TARGET;
|
||||
$self->{"level"} = 0;
|
||||
while ($_[0]) {
|
||||
$self->{ $_[0] } = $_[1];
|
||||
shift;
|
||||
shift;
|
||||
}
|
||||
bless($self, $type);
|
||||
}
|
||||
sub set_target {
|
||||
my ($self, $target, $file) = @_;
|
||||
if ((FILE_TARGET == $target) && $file) {
|
||||
$self->{"file"} = $file;
|
||||
$self->{"target"} = FILE_TARGET;
|
||||
} elsif (STDERR_TARGET == $target) {
|
||||
$self->{"target"} = STDERR_TARGET;
|
||||
}
|
||||
}
|
||||
sub enter_sub {
|
||||
8c6efb2a | Moritz Bunkus | my ($self, $level) = @_;
|
||
bce420e0 | Sven Schöling | $level *= 1;
|
||
8c6efb2a | Moritz Bunkus | |||
bce420e0 | Sven Schöling | return 1 unless ($global_level & TRACE); # ignore if traces aren't active
|
||
return 1 if $level && !($global_level & $level); # ignore if level of trace isn't active
|
||||
d319704a | Moritz Bunkus | |||
my ($package, $filename, $line, $subroutine) = caller(1);
|
||||
my ($dummy1, $self_filename, $self_line) = caller(0);
|
||||
bce420e0 | Sven Schöling | my $indent = " " x $self->{"calldepth"}++;
|
||
d319704a | Moritz Bunkus | |||
if (!defined($package)) {
|
||||
bce420e0 | Sven Schöling | $self->_write('sub' . $level, $indent . "\\ top-level?\n");
|
||
d319704a | Moritz Bunkus | } else {
|
||
bce420e0 | Sven Schöling | $self->_write('sub' . $level, $indent
|
||
54e4131e | Moritz Bunkus | . "\\ ${subroutine} in "
|
||
d319704a | Moritz Bunkus | . "${self_filename}:${self_line} called from "
|
||
. "${filename}:${line}\n");
|
||||
}
|
||||
54e4131e | Moritz Bunkus | return 1;
|
||
d319704a | Moritz Bunkus | }
|
||
sub leave_sub {
|
||||
8c6efb2a | Moritz Bunkus | my ($self, $level) = @_;
|
||
bce420e0 | Sven Schöling | $level *= 1;
|
||
8c6efb2a | Moritz Bunkus | |||
bce420e0 | Sven Schöling | return 1 unless ($global_level & TRACE); # ignore if traces aren't active
|
||
return 1 if $level && !($global_level & $level); # ignore if level of trace isn't active
|
||||
d319704a | Moritz Bunkus | |||
my ($package, $filename, $line, $subroutine) = caller(1);
|
||||
my ($dummy1, $self_filename, $self_line) = caller(0);
|
||||
bce420e0 | Sven Schöling | my $indent = " " x --$self->{"calldepth"};
|
||
d319704a | Moritz Bunkus | |||
if (!defined($package)) {
|
||||
bce420e0 | Sven Schöling | $self->_write('sub' . $level, $indent . "/ top-level?\n");
|
||
d319704a | Moritz Bunkus | } else {
|
||
bce420e0 | Sven Schöling | $self->_write('sub' . $level, $indent . "/ ${subroutine} in " . "${self_filename}:${self_line}\n");
|
||
d319704a | Moritz Bunkus | }
|
||
54e4131e | Moritz Bunkus | return 1;
|
||
d319704a | Moritz Bunkus | }
|
||
sub message {
|
||||
my ($self, $level, $message) = @_;
|
||||
bce420e0 | Sven Schöling | $self->_write(level2string($level), $message) if (($self->{"level"} | $global_level) & $level);
|
||
d319704a | Moritz Bunkus | }
|
||
sub dump {
|
||||
my ($self, $level, $name, $variable) = @_;
|
||||
if ($data_dumper_available) {
|
||||
$self->message($level, "dumping ${name}:\n" . Dumper($variable));
|
||||
} else {
|
||||
$self->message($level,
|
||||
"dumping ${name}: Data::Dumper not available; "
|
||||
. "variable cannot be dumped");
|
||||
}
|
||||
}
|
||||
sub enable_sub_tracing {
|
||||
my ($self) = @_;
|
||||
bce420e0 | Sven Schöling | $self->{level} | TRACE;
|
||
d319704a | Moritz Bunkus | }
|
||
sub disable_sub_tracing {
|
||||
my ($self) = @_;
|
||||
bce420e0 | Sven Schöling | $self->{level} & ~ TRACE;
|
||
d319704a | Moritz Bunkus | }
|
||
sub _write {
|
||||
my ($self, $prefix, $message) = @_;
|
||||
my $date = strftime("%Y-%m-%d %H:%M:%S $$ ${prefix}: ", localtime(time()));
|
||||
local *FILE;
|
||||
chomp($message);
|
||||
if ((FILE_TARGET == $self->{"target"})
|
||||
&& open(FILE, ">>" . $self->{"file"})) {
|
||||
print(FILE "${date}${message}\n");
|
||||
close(FILE);
|
||||
} elsif (STDERR_TARGET == $self->{"target"}) {
|
||||
print(STDERR "${date}${message}\n");
|
||||
}
|
||||
}
|
||||
bce420e0 | Sven Schöling | sub level2string {
|
||
# use $_[0] as a bit mask and return levelstrings separated by /
|
||||
join '/', qw(info debug1 debug2 query trace)[ grep { (reverse split //, sprintf "%05b", $_[0])[$_] } 0..4 ]
|
||||
}
|
||||
d319704a | Moritz Bunkus | 1;
|