prettified
[plcapi.git] / php / plc_api.php
index cb7257a..9a08ad2 100644 (file)
@@ -8,10 +8,6 @@
 // Mark Huang <mlhuang@cs.princeton.edu>
 // Copyright (C) 2005-2006 The Trustees of Princeton University
 //
-// $Id$
-// $URL$
-//
-//
 
 require_once 'plc_config.php';
 
@@ -43,14 +39,51 @@ 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 = "";
+    $len = count($backtrace);
+    $cnt = 1;
+    foreach( array_reverse($backtrace) as $line ) {
+        $msg .= "File '". $line['file'] . "' line " . $line['line'] . "\n";
+        $msg .= "    " . $line['function'] . "( "  . $this->rec_join($line['args']) . ")\n";
+        $cnt += 1;
+        if ( $cnt == $len ) { break; }
+    }
+    return $msg;
+  }
+
   function error_log($error_msg, $backtrace_level = 1)
   {
     $backtrace = debug_backtrace();
     $file = $backtrace[$backtrace_level]['file'];
     $line = $backtrace[$backtrace_level]['line'];
 
-    $this->errors[] = 'PLCAPI error:  ' . $error_msg . ' in ' . $file . ' on line ' . $line;
-    error_log(end($this->errors));
+    $error_line='PLCAPI error:  ' . $error_msg ;
+    if ($file) $error_line .= ' in file ' . $file;
+    if ($line) $error_line .= ' on line ' . $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()