package LXDebug;
-use constant NONE => 0;
-use constant INFO => 1;
-use constant DEBUG1 => 2;
-use constant DEBUG2 => 3;
-use constant QUERY => 4;
+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;
use constant FILE_TARGET => 0;
use constant STDERR_TARGET => 1;
use POSIX qw(strftime);
+use YAML;
+
my $data_dumper_available;
+our $global_level;
+our $watch_form;
+
BEGIN {
eval("use Data::Dumper");
$data_dumper_available = $@ ? 0 : 1;
$global_level = NONE;
- $global_trace_subs = 0;
+ $watch_form = 0;
}
sub new {
$self->{"file"} = "/tmp/lx-office-debug.log";
$self->{"target"} = FILE_TARGET;
$self->{"level"} = 0;
- $self->{"trace_subs"} = 0;
while ($_[0]) {
$self->{ $_[0] } = $_[1];
sub enter_sub {
my ($self, $level) = @_;
+ $level *= 1;
- return 1 if $global_trace_subs < $level;
-
- if (!$self->{"trace_subs"} && !$global_trace_subs) {
- return 1;
- }
+ 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
my ($package, $filename, $line, $subroutine) = caller(1);
my ($dummy1, $self_filename, $self_line) = caller(0);
- my $indent = " " x $self->{"calldepth"};
- $self->{"calldepth"} += 1;
+ my $indent = " " x $self->{"calldepth"}++;
if (!defined($package)) {
- $self->_write('sub', $indent . "\\ top-level?\n");
+ $self->_write('sub' . $level, $indent . "\\ top-level?\n");
} else {
- $self->_write('sub', $indent
+ $self->_write('sub' . $level, $indent
. "\\ ${subroutine} in "
. "${self_filename}:${self_line} called from "
. "${filename}:${line}\n");
sub leave_sub {
my ($self, $level) = @_;
+ $level *= 1;
- return 1 if $global_trace_subs < $level;
-
- if (!$self->{"trace_subs"} && !$global_trace_subs) {
- return 1;
- }
+ 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
my ($package, $filename, $line, $subroutine) = caller(1);
my ($dummy1, $self_filename, $self_line) = caller(0);
- $self->{"calldepth"} -= 1;
- my $indent = " " x $self->{"calldepth"};
+ my $indent = " " x --$self->{"calldepth"};
if (!defined($package)) {
- $self->_write('sub', $indent . "/ top-level?\n");
+ $self->_write('sub' . $level, $indent . "/ top-level?\n");
} else {
- $self->_write('sub', $indent . "/ ${subroutine} in " . "${self_filename}:${self_line}\n");
+ $self->_write('sub' . $level, $indent . "/ ${subroutine} in " . "${self_filename}:${self_line}\n");
}
return 1;
}
-sub message {
- my ($self, $level, $message) = @_;
- my ($log_level) = $self->{"level"};
+sub show_backtrace {
+ my ($self) = @_;
- if ($global_level && ($global_level > $log_level)) {
- $log_level = $global_level;
- }
+ return 1 unless ($global_level & BACKTRACE_ON_ERROR);
- if ($log_level >= $level) {
- $self->_write(INFO == $level ? "info"
- : DEBUG1 == $level ? "debug1"
- : DEBUG2 == $level ? "debug2"
- : QUERY == $level ? "query":"",
- $message );
+ $self->message(BACKTRACE_ON_ERROR, "Starting full caller dump:");
+ my $level = 0;
+ while (my ($dummy, $filename, $line, $subroutine) = caller $level) {
+ $self->message(BACKTRACE_ON_ERROR, " ${subroutine} from ${filename}:${line}");
+ $level++;
}
+
+ return 1;
+}
+
+sub message {
+ my ($self, $level, $message) = @_;
+
+ $self->_write(level2string($level), $message) if (($self->{"level"} | $global_level) & $level || !$level);
}
sub dump {
my ($self, $level, $name, $variable) = @_;
if ($data_dumper_available) {
- $self->message($level, "dumping ${name}:\n" . Dumper($variable));
+ my $dumper = Data::Dumper->new([$variable]);
+ $dumper->Sortkeys(1);
+ $self->message($level, "dumping ${name}:\n" . $dumper->Dump());
} else {
$self->message($level,
"dumping ${name}: Data::Dumper not available; "
}
}
+sub dump_yaml {
+ my ($self, $level, $name, $variable) = @_;
+
+ $self->message($level, "dumping ${name}:\n" . YAML::Dump($variable));
+}
+
+sub dump_sql_result {
+ my ($self, $level, $prefix, $results) = @_;
+
+ if (!$results || !scalar @{ $results }) {
+ $self->message($level, "Empty result set");
+ return;
+ }
+
+ my %column_lengths = map { $_, length $_ } keys %{ $results->[0] };
+
+ 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;
+
+ $prefix .= ' ' if $prefix;
+
+ $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' : ''));
+}
+
sub enable_sub_tracing {
my ($self) = @_;
- $self->{"trace_subs"} = 1;
+ $self->{level} | TRACE;
}
sub disable_sub_tracing {
my ($self) = @_;
- $self->{"trace_subs"} = 0;
+ $self->{level} & ~ TRACE;
}
sub _write {
print(FILE "${date}${message}\n");
close(FILE);
- } elsif (STDERR_TARGET == $self->{"target"}) {
+ } elsif (STDERR_TARGET == $self->{"target"}) {
print(STDERR "${date}${message}\n");
}
}
+sub level2string {
+ # use $_[0] as a bit mask and return levelstrings separated by /
+ join '/', qw(info debug1 debug2 query trace error_call_trace)[ grep { (reverse split //, sprintf "%05b", $_[0])[$_] } 0..5 ]
+}
+
1;