Rework the "Trace debugging banner" to be usage with stuffs like XmlRpc calls or...
authorx2003bruneau <x2003bruneau@839d8a87-29fc-0310-9880-83ba4fa771e5>
Thu, 22 Feb 2007 14:19:40 +0000 (14:19 +0000)
committerx2003bruneau <x2003bruneau@839d8a87-29fc-0310-9880-83ba4fa771e5>
Thu, 22 Feb 2007 14:19:40 +0000 (14:19 +0000)
git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1510 839d8a87-29fc-0310-9880-83ba4fa771e5

15 files changed:
classes/mmlist.php
classes/platalpage.php
classes/plbacktrace.php [new file with mode: 0644]
classes/xdb.php
classes/xmlrpcclient.php
configs/platal.ini
htdocs/css/keynote.css
include/banana/forum.inc.php
include/banana/ml.inc.php
include/banana/moderate.inc.php
include/platal.inc.php
modules/banana.php
modules/lists.php
templates/skin/common.backtrace.tpl [moved from templates/skin/common.database-debug.tpl with 83% similarity]
templates/skin/common.devel.tpl

index db5a463..7062820 100644 (file)
@@ -28,6 +28,9 @@ class MMList extends XmlrpcClient
         $dom = is_null($fqdn) ? $globals->mail->domain : $fqdn;
         $url = "http://$uid:$pass@{$globals->lists->rpchost}:{$globals->lists->rpcport}/$dom";
         parent::__construct($url);
+        if ($globals->debug & 1) {
+            $this->bt = new PlBacktrace('MMList');
+        }   
     }
 
     function __call($method, $args)
index dc75267..243c4b1 100644 (file)
@@ -114,17 +114,17 @@ class PlatalPage extends Smarty
         }
 
         if ($globals->debug & 1) {
-            $this->assign('db_trace', XDB::trace_format($this, 'skin/common.database-debug.tpl'));
+            PlBacktrace::clean();
+            $this->assign_by_ref('backtraces', PlBacktrace::$bt);
         }
 
         $this->assign('validate', true);
         error_reporting(0);
         $result = $this->fetch($skin);
-        $ttime  = sprintf('Temps total: %.02fs<br />', microtime_float() - $TIME_BEGIN);
+        $ttime  = sprintf('Temps total: %.02fs<br />', microtime(true) - $TIME_BEGIN);
         $replc  = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
 
         if ($globals->debug & 2) {
-
             $fd = fopen($this->compile_dir."/valid.html","w");
             fwrite($fd, $result);
             fclose($fd);
diff --git a/classes/plbacktrace.php b/classes/plbacktrace.php
new file mode 100644 (file)
index 0000000..6427896
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+/** Ligth debugging tool to generate execution backtrace
+ */
+class PlBacktrace
+{
+    static public $bt = array();
+
+    public $traces    = array();
+    public $totaltime = 0.0;
+    public $error     = false;
+
+    function __construct($name, array $init = array(), $sizef = 'rows', $timef = 'exectime', $errorf = 'error')
+    {
+        PlBacktrace::$bt[$name] = $this;
+        foreach ($init as &$entry) {
+            $this->add($entry, $sizef, $timef, $errorf);
+        }
+    }
+
+    private function add(array &$entry, $sizef = 'rows', $timef = 'exectime', $errorf = 'error')
+    {
+        $trace = array();
+        $trace['action'] = $entry['action'];
+        unset($entry['action']);
+        $trace['exectime'] = @$entry[$timef];
+        $this->totaltime += $trace['exectime'];
+        unset($entry[$timef]);
+        $trace['rows'] = @$entry[$sizef];
+        unset($entry[$sizef]);
+        $trace['error'] = @$entry[$errorf];
+        unset($entry[$errorf]);
+        if ($trace['error']) {
+            $this->error = true;
+        }
+        $trace['data'] = array($entry);
+        $this->traces[] =& $trace;
+    }
+
+    public function newEvent($action, $rows = 0, $error = null, array $userdata = array())
+    {
+        $trace = array('action' => $action, 'time' => 0);
+        $this->traces[] =& $trace;
+        $this->update($rows, $error, $userdata);
+    }
+
+    public function start($action)
+    {
+        $trace = array('action' => $action, 'starttime' => microtime(true));
+        $this->traces[] =& $trace;
+    }
+
+    public function stop($rows = 0, $error = null, array $userdata = array())
+    {
+        $time = microtime(true);
+        if (!$this->traces) {
+            return;
+        }
+        $trace =& $this->traces[count($this->traces) - 1];
+        $trace['time'] = $time - $trace['starttime'];
+        unset($trace['starttime']);
+        $this->totaltime += $trace['time'];
+        $this->update($rows, $error, $userdata);
+    }
+
+    public function update($rows = 0, $error = null, array $userdata = array())
+    {
+        $trace =& $this->traces[count($this->traces) - 1];
+        $trace['rows']  = $rows;
+        $trace['error'] = $error;
+        $trace['data']  = $userdata;
+        if ($trace['error']) {
+            $this->error = true;
+        }
+    }
+
+    public static function clean()
+    {
+        foreach (PlBacktrace::$bt as $name=>&$entry) {
+            if (!$entry->traces) {
+                unset(PlBacktrace::$bt[$name]);
+            }
+        }
+    }
+}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>
index 6d0223f..1bb976f 100644 (file)
@@ -27,11 +27,12 @@ class XDB
     {
         global $globals;
         XDB::$mysqli = new mysqli($globals->dbhost, $globals->dbuser, $globals->dbpwd, $globals->dbdb);
-        if (mysqli_connect_errno() && $globals->debug & 1) {
-            $GLOBALS['XDB::trace_data'][] = array('query' => 'MySQLI connection', 'explain' => array(),
-                                                  'error' => mysqli_connect_error(), 'exectime' => 0, 'rows' => 0);
-            $GLOBALS['XDB::error'] = true;
-            return false;
+        if ($globals->debug & 1) {
+            $bt = new PlBacktrace('MySQL');
+            if (mysqli_connect_errno()) {
+                $bt->newEvent("MySQLI connection", 0, mysqli_connect_error());
+                return false;
+            }
         }
         XDB::$mysqli->autocommit(true);
         XDB::$mysqli->set_charset($globals->dbcharset);
@@ -89,24 +90,15 @@ class XDB
                     $res->free();
                 }
             }
-            $trace_data = array('query' => XDB::_reformatQuery($query), 'explain' => $explain);
-            $time_start = microtime();
+            PlBacktrace::$bt['MySQL']->start(XDB::_reformatQuery($query));
         }
 
         $res = XDB::$mysqli->query($query);
         
         if ($globals->debug & 1) {
-            list($ue, $se) = explode(" ", microtime());
-            list($us, $ss) = explode(" ", $time_start);
-            $time = intval((($ue - $us) + ($se - $ss)) * 1000);            
-            $trace_data['error'] = XDB::$mysqli->error;
-            $trace_data['errno'] = XDB::$mysqli->errno;
-            $trace_data['exectime'] = $time;
-            $trace_data['rows'] = @$res->num_rows ? $res->num_rows : XDB::$mysqli->affected_rows;
-            $GLOBALS['XDB::trace_data'][] = $trace_data;
-            if (XDB::$mysqli->errno) {
-                $GLOBALS['XDB::error'] = true;
-            }
+            PlBacktrace::$bt['MySQL']->stop(@$res->num_rows ? $res->num_rows : XDB::$mysqli->affected_rows,
+                                            XDB::$mysqli->error,
+                                            $explain);
         }
         return $res;
     }
@@ -138,27 +130,11 @@ class XDB
 
     public static function errno()
     {
-        global $globals;
-        if ($globals->debug & 1) {
-            $count = count($GLOBALS['XDB::trace_data']);
-            if (!$count) {
-                return 0;
-            }
-            return $GLOBALS['XDB::trace_data'][$count - 1]['errno'];
-        }
         return XDB::$mysqli->errno;
     }
 
     public static function error()
     {       
-        global $globals;
-        if ($globals->debug & 1) {
-            $count = count($GLOBALS['XDB::trace_data']);
-            if (!$count) {
-                return null;
-            }
-            return $GLOBALS['XDB::trace_data'][$count - 1]['error'];
-        }   
         return XDB::$mysqli->error;
     }
 
@@ -192,12 +168,6 @@ class XDB
             die(var_export($var, true).' is not a valid for a database entry');
         }
     }
-
-    public static function trace_format(&$page, $template = 'skin/common.database-debug.tpl') {
-        $page->assign('trace_data', @$GLOBALS['XDB::trace_data']);
-        $page->assign('db_error', @$GLOBALS['XDB::error']);
-        return $page->fetch($template);
-    }
 }
 
 class XOrgDBResult
index e16f19f..b33fcb3 100644 (file)
@@ -32,6 +32,7 @@ class XmlrpcClient
 {
     private $url;
     private $urlparts;
+    public $bt = null;
 
     public function __construct($url)
     {
@@ -99,8 +100,21 @@ class XmlrpcClient
     public function __call($method, $args)
     {
         $query  = xmlrpc_encode_request($method, $args);
+        if ($this->bt) {
+            $this->bt->start($method . "\n" . var_export($args, true));
+        }
         $answer = $this->http_post($query, $this->urlparts);
+        if ($this->bt) {
+            $this->bt->stop();
+        }
         $result = $this->find_and_decode_xml($answer);
+        if ($this->bt) {
+            if (isset($result['faultCode'])) {
+                $this->bt->update(0, $result['faultString']);
+            } else {
+                $this->bt->update(count($result));
+            }
+        }
 
         if (is_array($result) && isset($result['faultCode'])) {
             trigger_error("Error in xmlrpc call $function\n".
index bfe0b10..e9de050 100644 (file)
@@ -12,6 +12,7 @@ web_pass = "***"
 
 table_prefix = "banana_"
 spool_root   = "/var/spool/banana"
+mbox_helper  = "/usr/bin/banana-mbox-helper"
 
 [Geoloc]
 webservice_url = ""
index c73eceb..7d7a02b 100644 (file)
@@ -487,8 +487,8 @@ div.adresse {
 
 #dev a { text-decoration: none; }
 
-#db-trace div.hide       { display: none; }
-#db-trace:hover div.hide { display: block }
+.backtrace div.hide       { display: none; }
+.backtrace:hover div.hide { display: block }
 
 #suid {
     color: red;
index a713c22..bd8ea66 100644 (file)
@@ -62,6 +62,7 @@ class ForumsBanana extends Banana
         if (S::has_perms()) {
             Banana::$msgshow_mimeparts[] = 'source';
         }
+        Banana::$debug_nntp = ($globals->debug & 1);
         parent::__construct($params);
     }
 
index 134c34a..9cc5ec0 100644 (file)
@@ -44,6 +44,8 @@ class MLBanana extends Banana
         Banana::$spool_root = $globals->banana->spool_root;
         Banana::$spool_boxlist = false;
         Banana::$msgedit_canattach = true;
+        Banana::$debug_mbox = ($globals->debug & 1);
+        Banana::$mbox_helper = $globals->banana->mbox_helper;
         if (S::has_perms()) {
             Banana::$msgshow_mimeparts[] = 'source';
         }    
index 3dccaaf..138c653 100644 (file)
@@ -236,6 +236,11 @@ class BananaMLInterface implements BananaProtocoleInterface
     {
         return ModerationBanana::$domain . '_' . ModerationBanana::$listname;
     }
+
+    public function backtrace()
+    {
+        return null;
+    }
 }
 
 // vim:set et sw=4 sts=4 ts=4 enc=utf-8:
index ae1a15c..046807a 100644 (file)
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-function microtime_float()
-{
-    list($usec, $sec) = explode(' ', microtime());
-    return ((float)$usec + (float)$sec);
-}
-$TIME_BEGIN = microtime_float();
+$TIME_BEGIN = microtime(true);
 
 date_default_timezone_set('Europe/Paris');
 
index 855b4db..2bf9797 100644 (file)
@@ -157,6 +157,7 @@ class BananaModule extends PLModule
         $page->assign('banana_res', $res);
         $page->addCssInline($banana->css());
         $page->addCssLink('banana.css');
+        new PlBacktrace('NNTP', $banana->backtrace(), 'response', 'time');
     }
 }
 
index 6846729..24a63a3 100644 (file)
@@ -383,6 +383,7 @@ class ListsModule extends PLModule
             $page->assign('banana', $banana->run());
             $page->addCssInline($banana->css());
             $page->addCssLink('banana.css');
+            new PlBacktrace('MBox', $banana->backtrace(), 'response', 'time');
         } else {
             $page->kill("La liste n'existe pas ou tu n'as pas le droit de la consulter");
         }
similarity index 83%
rename from templates/skin/common.database-debug.tpl
rename to templates/skin/common.backtrace.tpl
index 5860075..436e923 100644 (file)
 {**************************************************************************}
 
 
-{foreach item=query from=$trace_data}
-{if $query.explain}
-{assign var=cols value=$query.explain[0]|@count}
+{foreach item=query from=$trace->traces}
+{if $query.data}
+{assign var=cols value=$query.data[0]|@count}
 {else}
 {assign var=cols value=1}
 {/if}
 <table class="bicol" style="width: 75%; font-size: smaller; margin-left:2px; margin-top: 3px;">
   <tr class="impair">
     <td colspan="{$cols}">
-      <strong>QUERY:</strong>
-      <pre style="padding: 0; margin: 0;">{$query.query}</pre>
+      <strong>ACTION:</strong>
+      <pre style="padding: 0; margin: 0;">{$query.action}</pre>
       <br/>
     </td>
   </tr>
   <tr>
     <td colspan="{$cols}">
       <strong>INFO:</strong><br />
-      {$query.rows} enregistrement{if $query.rows > 1}s{/if} en {$query.exectime}ms
+      {$query.rows} ligne{if $query.rows > 1}s{/if} en {$query.exectime|string_format:"%.3f"}s
     </td>
   </tr>
   {/if}
-{if $query.explain}
+{if $query.data}
   <tr>
-    {foreach key=key item=item from=$query.explain[0]}
+    {foreach key=key item=item from=$query.data[0]}
     <th style="font-size: smaller">{$key}</th>
     {/foreach}
   </tr>
-  {foreach item=explain_row from=$query.explain}
+  {foreach item=data_row from=$query.data}
   <tr class="impair">
-    {foreach item=item from=$explain_row}
+    {foreach item=item from=$data_row}
     <td class="center" style="font-size: smaller">{$item}</td>
     {/foreach}
   </tr>
index 6c54645..656167f 100644 (file)
 {**************************************************************************}
 
 {if #globals.debug#}
-{if $db_trace && $db_trace neq "\n\n"}
-  <div id="db-trace">
+{foreach from=$backtraces key=bt_name item=trace}
+  <div class="backtrace">
     <h1>
-      {if $db_error}<span style="color: #f00">{/if}
-      Trace de l'exĂ©cution de cette page sur mysql (hover me)
-      {if $db_error}</span>{/if}
+      {if $trace->error}<span style="color: #f00">{/if}
+      ExĂ©cution de {$bt_name} en {$trace->totaltime|string_format:"%.3f"}s (hover-me pour la trace)
+      {if $trace->error}</span>{/if}
     </h1>
     <div class="hide">
-      {$db_trace|smarty:nodefaults}
+      {include file="skin/common.backtrace.tpl"}
     </div>
   </div>
-{/if}
+{/foreach}
 
 {if $validate}
   <div id="dev">