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 BACKTRACE_ON_ERROR => 32;
10 use constant ALL => 63;
12 use constant FILE_TARGET => 0;
13 use constant STDERR_TARGET => 1;
15 use POSIX qw(strftime);
17 my $data_dumper_available;
23 eval("use Data::Dumper");
24 $data_dumper_available = $@ ? 0 : 1;
34 $self->{"calldepth"} = 0;
35 $self->{"file"} = "/tmp/lx-office-debug.log";
36 $self->{"target"} = FILE_TARGET;
40 $self->{ $_[0] } = $_[1];
49 my ($self, $target, $file) = @_;
51 if ((FILE_TARGET == $target) && $file) {
52 $self->{"file"} = $file;
53 $self->{"target"} = FILE_TARGET;
55 } elsif (STDERR_TARGET == $target) {
56 $self->{"target"} = STDERR_TARGET;
61 my ($self, $level) = @_;
64 return 1 unless ($global_level & TRACE); # ignore if traces aren't active
65 return 1 if $level && !($global_level & $level); # ignore if level of trace isn't active
67 my ($package, $filename, $line, $subroutine) = caller(1);
68 my ($dummy1, $self_filename, $self_line) = caller(0);
70 my $indent = " " x $self->{"calldepth"}++;
72 if (!defined($package)) {
73 $self->_write('sub' . $level, $indent . "\\ top-level?\n");
75 $self->_write('sub' . $level, $indent
76 . "\\ ${subroutine} in "
77 . "${self_filename}:${self_line} called from "
78 . "${filename}:${line}\n");
84 my ($self, $level) = @_;
87 return 1 unless ($global_level & TRACE); # ignore if traces aren't active
88 return 1 if $level && !($global_level & $level); # ignore if level of trace isn't active
90 my ($package, $filename, $line, $subroutine) = caller(1);
91 my ($dummy1, $self_filename, $self_line) = caller(0);
93 my $indent = " " x --$self->{"calldepth"};
95 if (!defined($package)) {
96 $self->_write('sub' . $level, $indent . "/ top-level?\n");
98 $self->_write('sub' . $level, $indent . "/ ${subroutine} in " . "${self_filename}:${self_line}\n");
106 return 1 unless ($global_level & BACKTRACE_ON_ERROR);
108 $self->message(BACKTRACE_ON_ERROR, "Starting full caller dump:");
110 while (my ($dummy, $filename, $line, $subroutine) = caller $level) {
111 $self->message(BACKTRACE_ON_ERROR, " ${subroutine} from ${filename}:${line}");
119 my ($self, $level, $message) = @_;
121 $self->_write(level2string($level), $message) if (($self->{"level"} | $global_level) & $level || !$level);
125 my ($self, $level, $name, $variable) = @_;
127 if ($data_dumper_available) {
128 $self->message($level, "dumping ${name}:\n" . Dumper($variable));
130 $self->message($level,
131 "dumping ${name}: Data::Dumper not available; "
132 . "variable cannot be dumped");
136 sub enable_sub_tracing {
138 $self->{level} | TRACE;
141 sub disable_sub_tracing {
143 $self->{level} & ~ TRACE;
147 my ($self, $prefix, $message) = @_;
148 my $date = strftime("%Y-%m-%d %H:%M:%S $$ ${prefix}: ", localtime(time()));
153 if ((FILE_TARGET == $self->{"target"})
154 && open(FILE, ">>" . $self->{"file"})) {
155 print(FILE "${date}${message}\n");
158 } elsif (STDERR_TARGET == $self->{"target"}) {
159 print(STDERR "${date}${message}\n");
164 # use $_[0] as a bit mask and return levelstrings separated by /
165 join '/', qw(info debug1 debug2 query trace error_call_trace)[ grep { (reverse split //, sprintf "%05b", $_[0])[$_] } 0..5 ]