From d3f26be98b879dc9df9be60fffe2f1e152b3b218 Mon Sep 17 00:00:00 2001 From: x2003bruneau Date: Thu, 22 Feb 2007 14:19:40 +0000 Subject: [PATCH] Rework the "Trace debugging banner" to be usage with stuffs like XmlRpc calls or banana protocoles' backtraces. git-svn-id: svn+ssh://murphy/home/svn/platal/trunk@1510 839d8a87-29fc-0310-9880-83ba4fa771e5 --- classes/mmlist.php | 3 + classes/platalpage.php | 6 +- classes/plbacktrace.php | 107 +++++++++++++++++++++ classes/xdb.php | 50 ++-------- classes/xmlrpcclient.php | 14 +++ configs/platal.ini | 1 + htdocs/css/keynote.css | 4 +- include/banana/forum.inc.php | 1 + include/banana/ml.inc.php | 2 + include/banana/moderate.inc.php | 5 + include/platal.inc.php | 7 +- modules/banana.php | 1 + modules/lists.php | 1 + ...mon.database-debug.tpl => common.backtrace.tpl} | 20 ++-- templates/skin/common.devel.tpl | 14 +-- 15 files changed, 168 insertions(+), 68 deletions(-) create mode 100644 classes/plbacktrace.php rename templates/skin/{common.database-debug.tpl => common.backtrace.tpl} (83%) diff --git a/classes/mmlist.php b/classes/mmlist.php index db5a463..7062820 100644 --- a/classes/mmlist.php +++ b/classes/mmlist.php @@ -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) diff --git a/classes/platalpage.php b/classes/platalpage.php index dc75267..243c4b1 100644 --- a/classes/platalpage.php +++ b/classes/platalpage.php @@ -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
', microtime_float() - $TIME_BEGIN); + $ttime = sprintf('Temps total: %.02fs
', microtime(true) - $TIME_BEGIN); $replc = "VALIDATION HTML INACTIVE
"; 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 index 0000000..6427896 --- /dev/null +++ b/classes/plbacktrace.php @@ -0,0 +1,107 @@ +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: +?> diff --git a/classes/xdb.php b/classes/xdb.php index 6d0223f..1bb976f 100644 --- a/classes/xdb.php +++ b/classes/xdb.php @@ -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 diff --git a/classes/xmlrpcclient.php b/classes/xmlrpcclient.php index e16f19f..b33fcb3 100644 --- a/classes/xmlrpcclient.php +++ b/classes/xmlrpcclient.php @@ -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". diff --git a/configs/platal.ini b/configs/platal.ini index bfe0b10..e9de050 100644 --- a/configs/platal.ini +++ b/configs/platal.ini @@ -12,6 +12,7 @@ web_pass = "***" table_prefix = "banana_" spool_root = "/var/spool/banana" +mbox_helper = "/usr/bin/banana-mbox-helper" [Geoloc] webservice_url = "" diff --git a/htdocs/css/keynote.css b/htdocs/css/keynote.css index c73eceb..7d7a02b 100644 --- a/htdocs/css/keynote.css +++ b/htdocs/css/keynote.css @@ -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; diff --git a/include/banana/forum.inc.php b/include/banana/forum.inc.php index a713c22..bd8ea66 100644 --- a/include/banana/forum.inc.php +++ b/include/banana/forum.inc.php @@ -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); } diff --git a/include/banana/ml.inc.php b/include/banana/ml.inc.php index 134c34a..9cc5ec0 100644 --- a/include/banana/ml.inc.php +++ b/include/banana/ml.inc.php @@ -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'; } diff --git a/include/banana/moderate.inc.php b/include/banana/moderate.inc.php index 3dccaaf..138c653 100644 --- a/include/banana/moderate.inc.php +++ b/include/banana/moderate.inc.php @@ -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: diff --git a/include/platal.inc.php b/include/platal.inc.php index ae1a15c..046807a 100644 --- a/include/platal.inc.php +++ b/include/platal.inc.php @@ -19,12 +19,7 @@ * 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'); diff --git a/modules/banana.php b/modules/banana.php index 855b4db..2bf9797 100644 --- a/modules/banana.php +++ b/modules/banana.php @@ -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'); } } diff --git a/modules/lists.php b/modules/lists.php index 6846729..24a63a3 100644 --- a/modules/lists.php +++ b/modules/lists.php @@ -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"); } diff --git a/templates/skin/common.database-debug.tpl b/templates/skin/common.backtrace.tpl similarity index 83% rename from templates/skin/common.database-debug.tpl rename to templates/skin/common.backtrace.tpl index 5860075..436e923 100644 --- a/templates/skin/common.database-debug.tpl +++ b/templates/skin/common.backtrace.tpl @@ -21,17 +21,17 @@ {**************************************************************************} -{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} @@ -46,19 +46,19 @@ {/if} -{if $query.explain} +{if $query.data} - {foreach key=key item=item from=$query.explain[0]} + {foreach key=key item=item from=$query.data[0]} {/foreach} - {foreach item=explain_row from=$query.explain} + {foreach item=data_row from=$query.data} - {foreach item=item from=$explain_row} + {foreach item=item from=$data_row} {/foreach} diff --git a/templates/skin/common.devel.tpl b/templates/skin/common.devel.tpl index 6c54645..656167f 100644 --- a/templates/skin/common.devel.tpl +++ b/templates/skin/common.devel.tpl @@ -21,18 +21,18 @@ {**************************************************************************} {if #globals.debug#} -{if $db_trace && $db_trace neq "\n\n"} -
+{foreach from=$backtraces key=bt_name item=trace} +

- {if $db_error}{/if} - Trace de l'exécution de cette page sur mysql (hover me) - {if $db_error}{/if} + {if $trace->error}{/if} + Exécution de {$bt_name} en {$trace->totaltime|string_format:"%.3f"}s (hover-me pour la trace) + {if $trace->error}{/if}

- {$db_trace|smarty:nodefaults} + {include file="skin/common.backtrace.tpl"}
-{/if} +{/foreach} {if $validate}
-- 2.1.4
- QUERY: -
{$query.query}
+ ACTION: +
{$query.action}

INFO:
- {$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
{$key}
{$item}