3 use constant NONE => 0;
4 use constant INFO => 1;
5 use constant DEBUG1 => 2;
6 use constant DEBUG2 => 3;
7 use constant QUERY => 4;
9 use constant FILE_TARGET => 0;
10 use constant STDERR_TARGET => 1;
12 use POSIX qw(strftime);
14 my $data_dumper_available;
17 eval("use Data::Dumper");
18 $data_dumper_available = $@ ? 0 : 1;
21 $global_trace_subs = 0;
28 $self->{"calldepth"} = 0;
29 $self->{"file"} = "/tmp/lx-office-debug.log";
30 $self->{"target"} = FILE_TARGET;
32 $self->{"trace_subs"} = 0;
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) = @_;
58 return 1 if $global_trace_subs < $level;
60 if (!$self->{"trace_subs"} && !$global_trace_subs) {
64 my ($package, $filename, $line, $subroutine) = caller(1);
65 my ($dummy1, $self_filename, $self_line) = caller(0);
67 my $indent = " " x $self->{"calldepth"};
68 $self->{"calldepth"} += 1;
70 if (!defined($package)) {
71 $self->_write('sub', $indent . "\\ top-level?\n");
73 $self->_write('sub', $indent
74 . "\\ ${subroutine} in "
75 . "${self_filename}:${self_line} called from "
76 . "${filename}:${line}\n");
82 my ($self, $level) = @_;
84 return 1 if $global_trace_subs < $level;
86 if (!$self->{"trace_subs"} && !$global_trace_subs) {
90 my ($package, $filename, $line, $subroutine) = caller(1);
91 my ($dummy1, $self_filename, $self_line) = caller(0);
93 $self->{"calldepth"} -= 1;
94 my $indent = " " x $self->{"calldepth"};
96 if (!defined($package)) {
97 $self->_write('sub', $indent . "/ top-level?\n");
99 $self->_write('sub', $indent . "/ ${subroutine} in " . "${self_filename}:${self_line}\n");
105 my ($self, $level, $message) = @_;
106 my ($log_level) = $self->{"level"};
108 if ($global_level && ($global_level > $log_level)) {
109 $log_level = $global_level;
112 if ($log_level >= $level) {
113 $self->_write(INFO == $level ? "info"
114 : DEBUG1 == $level ? "debug1"
115 : DEBUG2 == $level ? "debug2"
116 : QUERY == $level ? "query":"",
122 my ($self, $level, $name, $variable) = @_;
124 if ($data_dumper_available) {
125 $self->message($level, "dumping ${name}:\n" . Dumper($variable));
127 $self->message($level,
128 "dumping ${name}: Data::Dumper not available; "
129 . "variable cannot be dumped");
133 sub enable_sub_tracing {
135 $self->{"trace_subs"} = 1;
138 sub disable_sub_tracing {
140 $self->{"trace_subs"} = 0;
144 my ($self, $prefix, $message) = @_;
145 my $date = strftime("%Y-%m-%d %H:%M:%S $$ ${prefix}: ", localtime(time()));
150 if ((FILE_TARGET == $self->{"target"})
151 && open(FILE, ">>" . $self->{"file"})) {
152 print(FILE "${date}${message}\n");
155 } elsif (STDERR_TARGET == $self->{"target"}) {
156 print(STDERR "${date}${message}\n");