Projekt

Allgemein

Profil

Herunterladen (4,06 KB) Statistiken
| Zweig: | Markierung: | Revision:
d319704a Moritz Bunkus
package LXDebug;

142f7c2c Moritz Bunkus
use constant NONE => 0;
use constant INFO => 1;
use constant DEBUG1 => 2;
use constant DEBUG2 => 4;
use constant QUERY => 8;
use constant TRACE => 16;
use constant BACKTRACE_ON_ERROR => 32;
use constant ALL => 63;
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;

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 {
4b17bfa8 Moritz Bunkus
my ($self) = @_;

142f7c2c Moritz Bunkus
return 1 unless ($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) {
$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);

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;