Bei print_options() die Parameterübergabe umgestellt, sodass der Aufrufer das Verhalt...
[kivitendo-erp.git] / SL / LXDebug.pm
index ee70d03..6da1a72 100644 (file)
@@ -1,12 +1,13 @@
 package LXDebug;
 
-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 ALL    => 31;
+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;
 
 use constant FILE_TARGET   => 0;
 use constant STDERR_TARGET => 1;
@@ -15,11 +16,15 @@ use POSIX qw(strftime);
 
 my $data_dumper_available;
 
+our $global_level;
+our $watch_form;
+
 BEGIN {
   eval("use Data::Dumper");
   $data_dumper_available = $@ ? 0 : 1;
 
   $global_level      = NONE;
+  $watch_form        = 0;
 }
 
 sub new {
@@ -30,7 +35,6 @@ sub new {
   $self->{"file"}       = "/tmp/lx-office-debug.log";
   $self->{"target"}     = FILE_TARGET;
   $self->{"level"}      = 0;
-  $self->{"watchedvars"} = {};
 
   while ($_[0]) {
     $self->{ $_[0] } = $_[1];
@@ -57,8 +61,6 @@ sub enter_sub {
   my ($self, $level) = @_;
   $level *= 1;
 
-  check_watched_form_variables();
-
   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
 
@@ -82,8 +84,6 @@ sub leave_sub {
   my ($self, $level) = @_;
   $level *= 1;
 
-  $self->check_watched_form_variables();
-
   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
 
@@ -100,8 +100,24 @@ sub leave_sub {
   return 1;
 }
 
+sub show_backtrace {
+  my ($self) = @_;
+
+  return 1 unless ($global_level & BACKTRACE_ON_ERROR);
+
+  $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);
 }
 
@@ -146,29 +162,7 @@ sub _write {
 
 sub level2string {
   # use $_[0] as a bit mask and return levelstrings separated by /
-  join '/', qw(info debug1 debug2 query trace)[ grep { (reverse split //, sprintf "%05b", $_[0])[$_] } 0..4 ]
-}
-
-sub watch_form_variable {
-  my ($self, $var) = @_;
-
-  $self->{"watchedvars"}->{$var} = $main::form->{$var};
-  $self->_write("WATCH", "Adding \$form->{$var} with current value \"$main::form->{$var}\"");
-}
-
-sub check_watched_form_variables {
-  my ($self) = @_;
-
-  return unless $main::form;
-
-  foreach my $var (sort(keys(%{ $self->{"watchedvars"} }))) {
-    if ($main::form->{$var} ne $self->{"watchedvars"}->{$var}) {
-      $self->_write("WATCH", "Variable \$form->{$var} changed from \"" .
-                    $self->{"watchedvars"}->{$var} . "\" to \"" .
-                    $main::form->{$var} . "\"");
-      $self->{"watchedvars"}->{$var} = $main::form->{$var};
-    }
-  }
+  join '/', qw(info debug1 debug2 query trace error_call_trace)[ grep { (reverse split //, sprintf "%05b", $_[0])[$_] } 0..5 ]
 }
 
 1;