Add timestamp to ModPython exceptions to aid in tracking down time-of-failure
authorStephen Soltesz <soltesz@cs.princeton.edu>
Tue, 26 Apr 2011 19:23:30 +0000 (15:23 -0400)
committerStephen Soltesz <soltesz@cs.princeton.edu>
Tue, 26 Apr 2011 19:23:30 +0000 (15:23 -0400)
Add more detailed stack traces to plc_api.php to identify users and arguments of failed API commands.

ModPython.py
php/plc_api.php

index 1a5b43e..07a0fda 100644 (file)
@@ -8,6 +8,7 @@
 #
 
 import sys
+import time
 import traceback
 import xmlrpclib
 from mod_python import apache
@@ -57,5 +58,6 @@ def handler(req):
 
     except Exception, err:
         # Log error in /var/log/httpd/(ssl_)?error_log
-        print >> log, err, traceback.format_exc()
+        t = "[" + time.ctime() + "] [error]"
+        print >> log, t, err, traceback.format_exc()
         return apache.HTTP_INTERNAL_SERVER_ERROR
index 4850bfa..f704cd4 100644 (file)
@@ -43,6 +43,31 @@ class PLCAPI
     $this->multicall = false;
   }
 
+  function rec_join ($arg) {
+    if ( is_array($arg) ) {
+        $ret = "";
+        foreach ( $arg as $i ) {
+            $l = $this->rec_join($i);
+            # ignore html code.
+            if ( $l[0] != "<" ) { $ret .= $l . ", "; }
+        }
+        return $ret;
+    } else {
+        settype($arg, "string");
+        return $arg;
+    }
+  }
+
+  function backtrace_php () {
+    $backtrace = debug_backtrace();
+    $msg = "";
+    foreach( $backtrace as $line ) {
+        $msg .= "File '". $line['file'] . "' line " . $line['line'] . "\n";
+        $msg .= "    " . $line['function'] . "( "  . $this->rec_join($line['args']) . ")\n";
+    }
+    return $msg;
+  }
+
   function error_log($error_msg, $backtrace_level = 1)
   {
     $backtrace = debug_backtrace();
@@ -52,8 +77,13 @@ class PLCAPI
     $error_line='PLCAPI error:  ' . $error_msg ;
     if ($file) $error_line .= ' in file ' . $file;
     if ($line) $error_line .= ' on line ' . $line;
-    $this->errors[] = $error_line;
-    error_log($error_line);
+    $this->errors[] = $error_line
+    # TODO: setup a config variable for more detailed stack traces, for API errors.
+    if ( TRUE ){
+      error_log($error_line);
+    } else {
+       error_log($this->backtrace_php());
+    }
   }
 
   function error()