XF 2.1 Debug Mode

Ozzy47

Well-known member
In my listener file I have this. I am listening to the visitor_setup.

PHP:
<?php

namespace OzzModz\DebugDevelopment;

class Listener
{
    public static function appSetup(\XF\Entity\User &$visitor)
    {
        // Get options
        $options = \XF::options();

        // Get variables
        $opt1 = $options->ozzmodzDebug;
        $opt2 = $options->ozzmodzDevelopment;
        $groups = $options->ozzmodzDebugDevGroups;


        // Enable debug mode
        if ($opt1 && $visitor->isMemberOf($groups))
        {
            \XF::$debugMode = true;
        }

        // Enable development mode
        if ($opt2 && $visitor->isMemberOf($groups))
        {
            \XF::$developmentMode = true;
        }
          
        // Show errors
        if ($opt1 || $opt2 && $visitor->isMemberOf($groups))
        {
            @ini_set('display_errors', true);
        }
    } 
}
Which enables debug mode in the front end and development mode in the back end.

Now when you enable debug mode from the src/config.php file and you click on the page render time, you get something like this:
debug 1.webp

When you enable debug mode with my addon you get this.
debug 2.webp

Anyone have a clue why the results are different?
 
Anyone have a clue why the results are different?
Yes :)

The DB connection is already established if you enable debug mode via an Add-on and logQueries will not have been enabled.

PHP:
$container['db'] = function ($c)
{
    $config = $c['config'];

    $dbConfig = $config['db'];
    $adapterClass = $dbConfig['adapterClass'];
    unset($dbConfig['adapterClass']);

    /** @var \XF\Db\AbstractAdapter $db */
    $db = new $adapterClass($dbConfig, $config['fullUnicode']);
    if (\XF::$debugMode)
    {
        $db->logQueries(true);
    }
    
    return $db
}

You can enable it manually, but the output will still be a bit different as it will not be enabled for queries executed prior to visitor_setup.
 
The database adapter is being initialized before \XF::$debugMode is set to true, so it never hits the conditional to log queries. I would probably recommend using app_setup instead of visitor_setup to hook as early as possible, but in either case you'd likely have to enable logging manually since you're fetching the visitor object before enabling it:

PHP:
\XF::db()->logQueries(true);


Edit: It looks like you probably can't fetch the visitor yet from app_setup, so visitor_setup it is.
 
The database adapter is being initialized before \XF::$debugMode is set to true, so it never hits the conditional to log queries. I would probably recommend using app_setup instead of visitor_setup to hook as early as possible, but in either case you'd likely have to enable logging manually since you're fetching the visitor object before enabling it:

PHP:
\XF::db()->logQueries(true);

This worked perfectly, \XF::db()->logQueries(true);

I can't use app_setup because I also check if the visitor is in an allowed usergroup in order to see the debug line.
 
I would probably recommend using app_setup instead of visitor_setup to hook as early as possible
That was my initial thought too, but at this code event the visitor has not yet been set up and you can't check usergroups for permission to enable debug mode.

Personally, I just enable debug mode via config.php for my IP address (which is static) if a certain cookie is set.

This way I can easily enable/disable debug mode via browser if needed
 
Top Bottom