1
frankblack
PHP-Debugging without XOOPS
  • 2009/7/29 13:27

  • frankblack

  • Just can't stay away

  • Posts: 830

  • Since: 2005/6/13


Hope this is the right forum and someone is interested and perhaps this isn't covered anywhere else?

I was always a bit annoyed about the XOOPS-internal debugging especially when working with (x)ajax. This gives you always (mostly) problems.

So I ended up using FireBug + FirePHP as addons for Firefox. I rewrote class/logger/render.php to output the debug to FirePHP. With this solution you are highly flexible.

As a module developer you can write any kind of debug notice you want without destroying the layout with echos or even writing to a logfile.

Example:
$fb->log($thevariwanttodisplay);


Of course you could adapt render.php to your needs like displaying server vars etc.

You can get FirePHP HERE

And here is the code for class/logger/render.php (hope the textsanitizer won't mess up):
/**
 * XOOPS Logger renderer
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * 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.
 *
 * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         kernel
 * @subpackage      logger
 * @since           2.3.0
 * @author          Skalpa Keo 
 * @author          Taiwen Jiang 
 * @version         $Id: render.php 2633 2009-01-10 03:09:41Z phppp $
 *
 * @todo            Not well written, just keep as it is. Refactored in 3.0
 */

defined'XOOPS_ROOT_PATH' ) or die();
//ob_start();

global $xoopsUser;

if (
is_object($xoopsUser)) {
require_once 
XOOPS_ROOT_PATH.'/FirePHPCore/FirePHP.class.php';
$fb FirePHP::getInstance(true);
$ret '';

$this->addExtra'Included files'count get_included_files() ) . ' files' );
$memory 0;
if ( 
function_exists'memory_get_usage' ) ) {
    
$memory memory_get_usage() . ' bytes';
} else {
    
$os = isset( $_ENV['OS'] ) ? $_ENV['OS'] : $_SERVER['OS'];
    if ( 
strposstrtolower$os ), 'windows') !== false ) {
        
$out = array();
        
exec('tasklist /FI "PID eq ' getmypid() . '" /FO LIST'$out );
        
$memory substr$out[5], strpos$out[5], ':') + 1) . ' [Estimated]';
    }
}
if ( 
$memory ) {
    
$this->addExtra'Memory usage'$memory );
}

if ( empty(
$mode) || $mode == 'errors' ) {
    
$types = array(
        
E_USER_NOTICE => 'Notice',
        
E_USER_WARNING => 'Warning',
        
E_USER_ERROR => 'Error',
        
E_NOTICE => 'Notice',
        
E_WARNING => 'Warning',
        
E_STRICT => 'Strict',
    );
    
$fberrors = array();

    foreach ( 
$this->errors as $error ) {
        
$fberrorstype = isset( $types$error['errno'] ] ) ? $types$error['errno'] ] : 'Unknown';
        
$fberrors[] = array($fberrorstype.sprintf": %s in file %s line %s"$this->sanitizePath($error['errstr']), $this->sanitizePath($error['errfile']), $error['errline'] ));
    }
if (!empty(
$fberrors)) $fb->table('Errors'$fberrors);
}

if ( empty(
$mode) || $mode == 'queries' ) {
    
$fbqueries = array();

    
$pattern '/b' preg_quote($GLOBALS['xoopsDB']->prefix()) . '_/i';

    foreach (
$this->queries as $q) {
        
$sql preg_replace($pattern''$q['sql']);
        if (isset(
$q['error'])) {
            
$fbqueries[] = array(htmlentities($sql).' Error number: '.$q['errno'].' Error message: '.$q['error']);
        } else {
            
$fbqueries[] = array(htmlentities($sql));
        }
    }
    
$fbqueries[] = array('Total: '.count($this->queries).' queries');

$fb->table('Queries'$fbqueries);
}

if ( empty(
$mode) || $mode == 'blocks' ) {
    
$fbblocks = array();

    foreach (
$this->blocks as $b) {
        if (
$b['cached']) {
            
$fbblocks[] = array(htmlspecialchars($b['name']).': Cached (regenerates every '.intval($b['cachetime']).' seconds)');
        } else {
            
$fbblocks[] = array(htmlspecialchars($b['name']).': No Cache');
        }
    }
    
$fbblocks[] = array('Total: '.count($this->blocks).' blocks');

    if (
count($this->blocks) > 0$fb->table('Blocks'$fbblocks);
}

if ( empty(
$mode) || $mode == 'extra' ) {
    
$fbextra = array();

    foreach (
$this->extra as $ex) {
        
$fbextra[] = array(htmlspecialchars($ex['name']).': '.htmlspecialchars($ex['msg']));
    }

    
$fb->table('Extra'$fbextra);
}

if ( empty(
$mode) || $mode == 'timers' ) {
    
$fbtimers = array();

    foreach ( 
$this->logstart as $k => $v ) {
        
$fbtimers[] = array(htmlspecialchars($k).' took ' sprintf"%.03f"$this->dumpTime($k) ) . ' seconds to load.');
    }

    
$fb->table('Timers'$fbtimers);
}
}

?>


Output is still looking a bit ugly, but better than before.

Hope this is helpful to someone?

edit: added the query for xoopsUser in order to hide the debug for guests

2
hervet
Re: PHP-Debugging without XOOPS
  • 2009/7/29 14:15

  • hervet

  • Friend of XOOPS

  • Posts: 2267

  • Since: 2003/11/4


Hello frank,

Quote:

frankblack wrote:
Hope this is helpful to someone?

Yes to me !
This is an excellent initiative.
Hope this code will be part of new versions.

Bye,
Hervé

3
frankblack
Re: PHP-Debugging without XOOPS
  • 2009/7/29 14:20

  • frankblack

  • Just can't stay away

  • Posts: 830

  • Since: 2005/6/13


Thx for the flowers!

Maybe you can add some extra? Re-writing was made very quickly since I discovered this solution this afternoon.

I tried to display all used vars, but it did not work out for me.

Edit: you have to place the folder called "FirePHPCore" with its contents to your XOOPS-root.

4
frankblack
Re: PHP-Debugging without XOOPS
  • 2009/7/29 15:35

  • frankblack

  • Just can't stay away

  • Posts: 830

  • Since: 2005/6/13


Okay, just tried to add smarty-debug and found a snippet for doing this. Unfortunately this code is a bit outdated and shows up not collapsed in FirePHP. So use this at your own risk.

Step 1: edit class/template.php should be line 56 from
if ( $xoopsConfig['debug_mode'] == ) {

to
if ( $xoopsConfig['debug_mode'] == ) {


Step 2: edit include/common.php should be line 182 from
if ( $xoopsConfig['debug_mode'] == || $xoopsConfig['debug_mode'] == ) {

to
if ( $xoopsConfig['debug_mode'] == || $xoopsConfig['debug_mode'] == || $xoopsConfig['debug_mode'] == ) {


Step 3: edit class/smarty/internals/core.display_debug_console.php and replace the complete code with this
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty debug_console function plugin
 *
 * Type:     core

 * Name:     display_debug_console

 * Purpose:  display the javascript debug console window
 * @param array Format: null
 * @param Smarty
 */

if (!defined('XOOPS_ROOT_PATH')) die('XOOPS_ROOT_PATH not defined');
require_once 
XOOPS_ROOT_PATH.'/FirePHPCore/FirePHP.class.php';

function 
smarty_core_display_debug_console($params, &$smarty)
{
    
//get required debug variables
    
$assigned_vars $smarty->_tpl_vars;
    
ksort($assigned_vars);

    
$config_vars = array();
    if(@
is_array($smarty->_config[0])){
        
$config_vars $smarty->_config[0];
        
ksort($config_vars);
    }

    
$firephp FirePHP::getInstance(true);

    
$firephp->group('Smarty Debug Output');
    
/*Log template files*/
    
$firephp->group('included templates & config files (load time in seconds)');
    foreach( 
$smarty->_smarty_debug_info as $tml ){
        
$msg str_repeat('--'$tml['depth']);
        
$msg .= ( $tml['depth'] != ) ? '>' '';
        if (isset(
$tml['exec_time'])) $getthetime ' (' substr($tml['exec_time'], 07) . 's)';
        else 
$getthetime '';
        
$msg .= $tml['filename'] . $getthetime;
        
$firephp->log($msg);
    }
    
$firephp->groupEnd(); //end group 'included templates &...'


    /*Log assigned template variables*/
    
$firephp->group('assigned template variables');
    foreach( 
$assigned_vars as $key => $value ){
        
$firephp->log($value'{$' $key '}');
    }
    
$firephp->groupEnd(); //end group 'assigned template variables'


    /*Log assigned config file variables (outer template scope)*/
    
$firephp->group('assigned config file variables (outer template scope)');
    
/*Check if there is something in the config*/
    
if(!empty($config_vars)){
        foreach( 
$config_vars as $key => $value ){
            
$firephp->log($value'{#' $key '#}');
        }
    } else{
        
$firephp->log("No configuration values available");
    }
    
$firephp->groupEnd(); //end group 'assigned config file variables (outer template scope)'


    
$firephp->groupEnd(); //end group 'Smarty Debug Output'

}

/* vim: set expandtab: */

?>


Have fun!

5
culex
Re: PHP-Debugging without XOOPS
  • 2009/7/29 15:58

  • culex

  • Module Developer

  • Posts: 711

  • Since: 2004/9/23


Nice one :) Very handy indeed.
Programming is like sex:
One mistake and you have to support it for the rest of your life.

6
Mamba
Re: PHP-Debugging without XOOPS
  • 2009/7/29 16:09

  • Mamba

  • Moderator

  • Posts: 11409

  • Since: 2004/4/23


Excellent initiative!

I think, Trabis did some work on PQP, so maybe this could be merged, if possible and if it makes sense.

BTW - did anybody look into Webgrind?
Support XOOPS => DONATE
Use 2.5.11 | Docs | Modules | Bugs

7
frankblack
Re: PHP-Debugging without XOOPS
  • 2009/7/29 16:13

  • frankblack

  • Just can't stay away

  • Posts: 830

  • Since: 2005/6/13


webgrind needs xdebug and this means php.ini has to be changed. This is impossible for some. So this is no real alternative yet.

8
Mamba
Re: PHP-Debugging without XOOPS
  • 2009/7/29 16:25

  • Mamba

  • Moderator

  • Posts: 11409

  • Since: 2004/4/23


Quote:
webgrind needs xdebug and this means php.ini has to be changed. This is impossible for some. So this is no real alternative yet.

I assume, most if not all developers could change php.ini on their development computer, right? And normal users don't need it, anyway, correct?
Support XOOPS => DONATE
Use 2.5.11 | Docs | Modules | Bugs

9
frankblack
Re: PHP-Debugging without XOOPS
  • 2009/7/29 16:29

  • frankblack

  • Just can't stay away

  • Posts: 830

  • Since: 2005/6/13


Partially correct! EVERYONE needs some kind of debugger. What about those not able to change the php.ini sitting there with a blank page and no debug information is shown?

So debug at its best should work for all, not only for the developers.

10
frankblack
Re: PHP-Debugging without XOOPS
  • 2009/7/29 22:07

  • frankblack

  • Just can't stay away

  • Posts: 830

  • Since: 2005/6/13


I made a small package for you.

Install Firebug and FirePHP for Firefox and set up as told on firephp.org (just a few clicks).

Download ZIP, unpack and upload files / folders via FTP. Backup the files that have to be overwritten. Also make a backup of the database, because two entries have to be made.

Browse the file http://www.yourdomain.com/adddebug.php. Delete this immediately afterwards.

Visit Administration / System / General Settings. There are now two more options for the debug modes: FirePHP and FirePHP + Guest.

The last option enables debugging even when you are not logged in as an user. But this option is more important for developers developing locally.

Installation and use of the new debug modes at your own risk!

Good luck!

edit: did I mention that this only for PHP5 and XOOPS 2.3.3?

smarty-debug is now un-collapsed and database-errors have now their own section. I am still looking for a good way to display ALL used variables.

EDIT2: Damn, I should burn in hell. The ZIP missed some files. ZIP should be OK now. Sorry! Working in the night is not so good in my age.

Login

Who's Online

365 user(s) are online (247 user(s) are browsing Support Forums)


Members: 0


Guests: 365


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits