kivitendo/SL/LXDebug.pm @ 11e2bd11
d319704a | Moritz Bunkus | package LXDebug;
|
||
d8e671e9 | Sven Schöling | use constant NONE => 0;
|
||
use constant INFO => 1;
|
||||
use constant DEBUG1 => 1 << 1;
|
||||
use constant DEBUG2 => 1 << 2;
|
||||
use constant QUERY => 1 << 3;
|
||||
use constant TRACE => 1 << 4;
|
||||
use constant BACKTRACE_ON_ERROR => 1 << 5;
|
||||
use constant ALL => (1 << 6) - 1;
|
||||
use constant DEVEL => INFO | QUERY | TRACE | BACKTRACE_ON_ERROR;
|
||||
d319704a | Moritz Bunkus | |||
5d0421a3 | Moritz Bunkus | use constant FILE_TARGET => 0;
|
||
use constant STDERR_TARGET => 1;
|
||||
d319704a | Moritz Bunkus | |||
use POSIX qw(strftime);
|
||||
325974c2 | Moritz Bunkus | use YAML;
|
||
d319704a | Moritz Bunkus | my $data_dumper_available;
|
||
e7191bc2 | Moritz Bunkus | our $global_level;
|
||
our $watch_form;
|
||||
d319704a | Moritz Bunkus | BEGIN {
|
||
eval("use Data::Dumper");
|
||||
$data_dumper_available = $@ ? 0 : 1;
|
||||
$global_level = NONE;
|
||||
e7191bc2 | Moritz Bunkus | $watch_form = 0;
|
||
d319704a | Moritz Bunkus | }
|
||
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 | }
|
||
142f7c2c | Moritz Bunkus | sub show_backtrace {
|
||
8c7e4493 | Moritz Bunkus | my ($self, $force) = @_;
|
||
4b17bfa8 | Moritz Bunkus | |||
8c7e4493 | Moritz Bunkus | return 1 unless ($force || ($global_level & BACKTRACE_ON_ERROR));
|
||
4b17bfa8 | Moritz Bunkus | |||
142f7c2c | Moritz Bunkus | $self->message(BACKTRACE_ON_ERROR, "Starting full caller dump:");
|
||
4b17bfa8 | Moritz Bunkus | my $level = 0;
|
||
while (my ($dummy, $filename, $line, $subroutine) = caller $level) {
|
||||
eef466c1 | Moritz Bunkus | $self->message(BACKTRACE_ON_ERROR, " ${subroutine} from ${filename}:${line}");
|
||
4b17bfa8 | Moritz Bunkus | $level++;
|
||
}
|
||||
return 1;
|
||||
}
|
||||
d319704a | Moritz Bunkus | sub message {
|
||
my ($self, $level, $message) = @_;
|
||||
bbb608a0 | Moritz Bunkus | |||
6c38a7cf | Sven Schöling | $self->_write(level2string($level), $message) if (($self->{"level"} | $global_level) & $level || !$level);
|
||
d319704a | Moritz Bunkus | }
|
||
sub dump {
|
||||
my ($self, $level, $name, $variable) = @_;
|
||||
if ($data_dumper_available) {
|
||||
8c7e4493 | Moritz Bunkus | my $password;
|
||
if ($variable && ('Form' eq ref $variable) && defined $variable->{password}) {
|
||||
$password = $variable->{password};
|
||||
$variable->{password} = 'X' x 8;
|
||||
}
|
||||
91aac6c6 | Moritz Bunkus | my $dumper = Data::Dumper->new([$variable]);
|
||
$dumper->Sortkeys(1);
|
||||
$self->message($level, "dumping ${name}:\n" . $dumper->Dump());
|
||||
d87c3bf7 | Moritz Bunkus | |||
8c7e4493 | Moritz Bunkus | $variable->{password} = $password if (defined $password);
|
||
d87c3bf7 | Moritz Bunkus | # Data::Dumper does not reset the iterator belonging to this hash
|
||
# if 'Sortkeys' is true. Therefore clear the iterator manually.
|
||||
# See "perldoc -f each".
|
||||
8c7e4493 | Moritz Bunkus | if ($variable && (('HASH' eq ref $variable) || ('Form' eq ref $variable))) {
|
||
d87c3bf7 | Moritz Bunkus | keys %{ $variable };
|
||
}
|
||||
d319704a | Moritz Bunkus | } else {
|
||
$self->message($level,
|
||||
"dumping ${name}: Data::Dumper not available; "
|
||||
. "variable cannot be dumped");
|
||||
}
|
||||
}
|
||||
325974c2 | Moritz Bunkus | sub dump_yaml {
|
||
my ($self, $level, $name, $variable) = @_;
|
||||
$self->message($level, "dumping ${name}:\n" . YAML::Dump($variable));
|
||||
}
|
||||
c4c2f760 | Moritz Bunkus | sub dump_sql_result {
|
||
my ($self, $level, $prefix, $results) = @_;
|
||||
if (!$results || !scalar @{ $results }) {
|
||||
$self->message($level, "Empty result set");
|
||||
return;
|
||||
}
|
||||
6e00b261 | Moritz Bunkus | my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
|
||
c4c2f760 | Moritz Bunkus | |||
foreach my $row (@{ $results }) {
|
||||
map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row };
|
||||
}
|
||||
my @sorted_names = sort keys %column_lengths;
|
||||
my $format = join '|', map { '%' . $column_lengths{$_} . 's' } @sorted_names;
|
||||
54272ac3 | Moritz Bunkus | $prefix .= ' ' if $prefix;
|
||
c4c2f760 | Moritz Bunkus | |||
$self->message($level, $prefix . sprintf($format, @sorted_names));
|
||||
$self->message($level, $prefix . join('+', map { '-' x $column_lengths{$_} } @sorted_names));
|
||||
foreach my $row (@{ $results }) {
|
||||
$self->message($level, $prefix . sprintf($format, map { $row->{$_} } @sorted_names));
|
||||
}
|
||||
$self->message($level, $prefix . sprintf('(%d row%s)', scalar @{ $results }, scalar @{ $results } > 1 ? 's' : ''));
|
||||
}
|
||||
d319704a | Moritz Bunkus | 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);
|
||||
20a08305 | Moritz Bunkus | } elsif (STDERR_TARGET == $self->{"target"}) {
|
||
d319704a | Moritz Bunkus | print(STDERR "${date}${message}\n");
|
||
}
|
||||
}
|
||||
bce420e0 | Sven Schöling | sub level2string {
|
||
# use $_[0] as a bit mask and return levelstrings separated by /
|
||||
4b17bfa8 | Moritz Bunkus | join '/', qw(info debug1 debug2 query trace error_call_trace)[ grep { (reverse split //, sprintf "%05b", $_[0])[$_] } 0..5 ]
|
||
bce420e0 | Sven Schöling | }
|
||
d319704a | Moritz Bunkus | 1;
|