3 use constant NONE => 0;
4 use constant INFO => 1;
5 use constant DEBUG1 => 2;
6 use constant DEBUG2 => 4;
7 use constant QUERY => 8;
8 use constant TRACE => 16;
9 use constant ALL => 31;
11 use constant FILE_TARGET => 0;
12 use constant STDERR_TARGET => 1;
14 use POSIX qw(strftime);
16 my $data_dumper_available;
19 eval("use Data::Dumper");
20 $data_dumper_available = $@ ? 0 : 1;
29 $self->{"calldepth"} = 0;
30 $self->{"file"} = "/tmp/lx-office-debug.log";
31 $self->{"target"} = FILE_TARGET;
35 $self->{ $_[0] } = $_[1];
44 my ($self, $target, $file) = @_;
46 if ((FILE_TARGET == $target) && $file) {
47 $self->{"file"} = $file;
48 $self->{"target"} = FILE_TARGET;
50 } elsif (STDERR_TARGET == $target) {
51 $self->{"target"} = STDERR_TARGET;
56 my ($self, $level) = @_;
59 return 1 unless ($global_level & TRACE); # ignore if traces aren't active
60 return 1 if $level && !($global_level & $level); # ignore if level of trace isn't active
62 my ($package, $filename, $line, $subroutine) = caller(1);
63 my ($dummy1, $self_filename, $self_line) = caller(0);
65 my $indent = " " x $self->{"calldepth"}++;
67 if (!defined($package)) {
68 $self->_write('sub' . $level, $indent . "\\ top-level?\n");
70 $self->_write('sub' . $level, $indent
71 . "\\ ${subroutine} in "
72 . "${self_filename}:${self_line} called from "
73 . "${filename}:${line}\n");
79 my ($self, $level) = @_;
82 return 1 unless ($global_level & TRACE); # ignore if traces aren't active
83 return 1 if $level && !($global_level & $level); # ignore if level of trace isn't active
85 my ($package, $filename, $line, $subroutine) = caller(1);
86 my ($dummy1, $self_filename, $self_line) = caller(0);
88 my $indent = " " x --$self->{"calldepth"};
90 if (!defined($package)) {
91 $self->_write('sub' . $level, $indent . "/ top-level?\n");
93 $self->_write('sub' . $level, $indent . "/ ${subroutine} in " . "${self_filename}:${self_line}\n");
99 my ($self, $level, $message) = @_;
100 $self->_write(level2string($level), $message) if (($self->{"level"} | $global_level) & $level || !$level);
104 my ($self, $level, $name, $variable) = @_;
106 if ($data_dumper_available) {
107 $self->message($level, "dumping ${name}:\n" . Dumper($variable));
109 $self->message($level,
110 "dumping ${name}: Data::Dumper not available; "
111 . "variable cannot be dumped");
115 sub enable_sub_tracing {
117 $self->{level} | TRACE;
120 sub disable_sub_tracing {
122 $self->{level} & ~ TRACE;
126 my ($self, $prefix, $message) = @_;
127 my $date = strftime("%Y-%m-%d %H:%M:%S $$ ${prefix}: ", localtime(time()));
132 if ((FILE_TARGET == $self->{"target"})
133 && open(FILE, ">>" . $self->{"file"})) {
134 print(FILE "${date}${message}\n");
137 } elsif (STDERR_TARGET == $self->{"target"}) {
138 print(STDERR "${date}${message}\n");
143 # use $_[0] as a bit mask and return levelstrings separated by /
144 join '/', qw(info debug1 debug2 query trace)[ grep { (reverse split //, sprintf "%05b", $_[0])[$_] } 0..4 ]