Plugin Directory

Changeset 389435


Ignore:
Timestamp:
05/26/2011 04:14:59 PM (15 years ago)
Author:
kunalb
Message:

Initial refactor. Now supports collecting errors and hooks.

Location:
kb-debug/trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • kb-debug/trunk/kb_debug.php

    r389433 r389435  
    11<?php
    2 
    32/*
    43    Plugin Name: KB_DEBUG
     
    3332*/
    3433
    35 /* 1. Logging hooks, notices and warnings and displaying at the end. */
    36 
    37 global $kb_gettext_counter; $kb_gettext_counter = 0;
    38 global $kb_filters; $kb_filters = 0;
    39 global $kb_filters_used; $kb_filters_used = 0;
    40 
    41 /**
    42  * Log all errors to a global array and dump later.
    43  *
    44  * @global Array $kb_notices
    45  */
    46 global $kb_notices;
    47 $kb_notices = Array();
    48 
    49 /**
    50  * Only display notices/warnings/etc. following the given filters.
    51  *
    52  * @global Array $kb_display_keywords
    53  */
    54 global $kb_display_keywords;
    55 
    56 /**
    57  * Custom error handler -- stores all the errors in the global array.
    58  *
    59  * @uses $kb_notices
    60  */
    61 function ep_error_handler( $errno, $errstr, $errfile, $errline, $errcontext ) {
    62     global $kb_notices;
    63 
    64     $kb_notices[] = Array(
    65         'no'        => $errno,
    66         'str'       => $errstr,
    67         'file'      => $errfile,
    68         'line'      => $errline,
    69         'context'   => $errcontext
    70     );
    71 
    72     return 1;
     34/**
     35 * The URL for the plugin.
     36 * @global string KB_DEBUG_RELURL
     37 */
     38define( 'KB_DEBUG_RELURL', content_url() . "/mu-plugins" );
     39
     40/**
     41 * The main (and only) file for KB_Debug, divided into classes based on functionality.
     42 * @package KB_Debug
     43 * @author Kunal Bhalla
     44 * @version 0.1
     45 */
     46
     47/**
     48 * The Base Class used to save errors and warnings information, if any.
     49 * @package KB_Debug
     50 */
     51class KB_Debug_Errors {
     52
     53    /**
     54     * Store any errors, notices, warnings or debug information.
     55     * @access private
     56     * @var Array
     57     */
     58    private $logged;
     59
     60    /**
     61     * Counters for filters available, filters used and gettexts.
     62     * @access private
     63     * @var Array
     64     */
     65    private $counters;
     66
     67    /**
     68     * Constructor. Initializes $counters and $logged.
     69     */
     70    public function __construct() {
     71        $this->logged = Array();
     72
     73        $this->counters = new STDClass();
     74        $this->counters->errors   = 0;
     75        $this->counters->warnings = 0;
     76        $this->counters->strict   = 0;
     77        $this->counters->notices  = 0;
     78
     79        add_action( 'shutdown', Array( &$this, 'display' ) );
     80        set_error_handler( Array( &$this, 'log' ) );
     81
     82        wp_enqueue_style( 'kb-debug-css', KB_DEBUG_RELURL . "/kb-debug.css" );
     83    }
     84
     85    /**
     86     * Displays the logged data, if any.
     87     * @access public
     88     */
     89    public function display() {
     90        echo "<div class = 'kb-debug-box'><div class = 'effect-border'>";
     91        echo "<h2>Errors and Warnings</h2>";   
     92        echo "<ul>";
     93        foreach( $this->logged as $log ) {
     94            $type = 'Strict';
     95            switch ($log['no']) {
     96                case E_ERROR: $type = 'E_ERROR'; break;
     97                case E_WARNING: $type = 'E_WARNING'; break;
     98                case E_NOTICE: $type = 'E_NOTICE'; break;
     99                case E_STRICT: $type = 'E_STRICT'; break;
     100                case E_USER_NOTICE: $type = 'E_USER_NOTICE'; break;
     101                case E_USER_WARNING: $type = 'E_USER_WARNING'; break;
     102            }
     103
     104            echo "<li class = 'kb-debug-{$type}'>";
     105
     106            echo "<h3>{$type}</h3>";
     107            echo "<h4>{$log['line']}, {$log['file']}.</h4>";
     108
     109            echo "<p>{$log['str']}</p>";
     110
     111            echo "<h4>Context</h4>";
     112            echo "<pre style = 'max-height: 100px;'>"; print_r($log['context']); echo "</pre>";
     113
     114
     115            echo "</li>";
     116        }
     117        echo "</ul>";
     118
     119        echo "</div></div>";
     120    }
     121
     122    /**
     123     * Log all error based data.
     124     * @access public
     125     */
     126    public function log( $errno, $errstr, $errfile, $errline, $errcontext ) {
     127        $this->logged[] = Array(
     128            'no'        => $errno,
     129            'str'       => $errstr,
     130            'file'      => $errfile,
     131            'line'      => $errline,
     132            'context'   => $errcontext
     133        );
     134
     135        return 1;
     136    }
     137
    73138}
    74139
    75140/**
    76  * Display the errors.
    77  *
    78  * Displays the errors on shutdown -- as well as
    79  * constants, if KB_DISPLAY_CONSTANTS is set to true.
    80  * Will show hooks if KB_DISPLAY_HOOKS is set to true.
    81  *
    82  * @uses $kb_notices
    83  */
    84 function kb_display_errors() {
    85     global $kb_notices;
    86     global $kb_gettext_counter; global $kb_filters; global $kb_filters_used;
    87 
    88     if ( defined( 'KB_DISPLAY_CONSTANTS' ) || isset( $_GET['KB_DISPLAY_CONSTANTS'] ) ) {
    89         $defined_constants = get_defined_constants( true );
    90         $defined_constants = $defined_constants['user'];
    91         ksort( $defined_constants );
    92         trigger_error( kb_dump( $defined_constants ) );
    93     }
    94 
    95     if ( defined( 'KB_FORCE_HIDE' ) || isset( $_GET['KB_FORCE_HIDE'] ) )
    96         return;
    97    
    98     echo "<div id = 'kb-debug-results'>";
     141 * Logs all hook information including arguments passed and functions run on each hook.
     142 * @package KB_Debug
     143 */
     144class KB_Debug_Hooks {
     145
     146    /**
     147     * Store any Hook information
     148     * @access private
     149     * @var Array
     150     */
     151    private $logged;
     152
     153    /**
     154     * Counters for filters available, filters used and gettexts.
     155     * @access private
     156     * @var Array
     157     */
     158    private $counters;
     159
     160    /**
     161     * Constructor. Initializes $counters and $logged.
     162     */
     163    public function __construct() {
     164        $this->logged = Array();
    99165       
    100     foreach( $kb_notices as $notice ) {
    101         extract( $notice );
    102 
    103         switch ($no) {
    104             case E_ERROR: $type = 'Error'; break;
    105             case E_WARNING: $type = 'Warning'; break;
    106             case E_NOTICE: $type = 'Notice'; break;
    107             case E_STRICT: $type = 'Strict'; break;
    108             case E_USER_NOTICE: $type = 'Debug'; break;
    109             case E_USER_WARNING: $type = 'Hook'; break;
    110             default: $type = 'Strict';
     166        $this->counters = new STDClass();
     167        $this->counters->actions  = 0;
     168        $this->counters->used     = 0;
     169        $this->counters->gettexts = 0;
     170
     171        add_action( 'all', Array( &$this, 'log' ) );
     172        add_action( 'shutdown', Array( &$this, 'display' ) );
     173
     174        wp_enqueue_style( 'kb-debug-css', KB_DEBUG_RELURL . "/kb-debug.css" );
     175    }
     176
     177    /**
     178     * Displays the logged data, if any.
     179     * @access public
     180     */
     181    public function display() {
     182        echo "<div class = 'kb-debug-box'><div class = 'effect-border'>";
     183        echo "<h2>Hooks List (Total {$this->counters->actions}, {$this->counters->gettexts} gettext calls omitted)</h2>";
     184        echo "<ul>";
     185        foreach( $this->logged as $log ) {
     186            echo "<li>";
     187            echo "<h3>{$log['name']}</h3>";
     188       
     189            echo "<h4>Arguments</h4>";
     190            if ( !empty( $log['vars'] ) ) {
     191                echo "<pre>";print_r( $log['vars'] );echo "</pre>";
     192            } else echo "<pre>None.</pre>";
     193           
     194            echo "<h4>Functions</h4>";
     195            echo "<pre>";print_r( $log['funcs'] );echo "</pre>";   
     196           
     197            echo "</li>";
    111198        }
    112 
    113         switch ($type) {
    114             case 'Hook':
    115                 if ( defined( 'KB_DISPLAY_HOOKS' ) || isset($_GET['KB_DISPLAY_HOOKS']) )
    116                     echo "<div class = 'kb_Hook'>$str</div>"; break;
    117             default: echo "<div class = 'kb_$type'>$type: $str<br />Line $line, $file</div>";
    118         }
    119     }
    120 
    121     echo "</div>";
    122 
    123     if ( defined( 'KB_DISPLAY_HOOKS' ) )
    124         echo "<div style = 'kb_disp_hook'>$kb_gettext_counter gettext calls; $kb_filters actions/filters. $kb_filters_used filters used.</div>";
    125 }
    126 
    127 /**
    128  * A buffered version of var_dump
    129  *
    130  * I really should rename this, considering
    131  * kb is (are?) my initials.
    132  *
    133  * @param mixed $var The data to log
    134  */
    135 function kb_dump( $ivars ) {
    136     ob_start();
    137     var_dump( $ivars );
    138     return ob_get_clean();
    139 }
    140 
    141 /**
    142  * Saves all hooks data.
    143  *
    144  * Attached to the all hook. Records details about the
    145  * hook called, and all the arguments provided, etc.
    146  *
    147  * @param $args
    148  * @param $vars
    149  */
    150 function kb_log_hooks( $args, $vars = '' ) {
    151     global $wp_filter, $wp_query, $kb_gettext_counter, $kb_filters, $kb_filters_used;
    152 
    153     if ( $args != 'gettext' && $args != 'gettext_with_context' ) {
    154             $functions_called = "";
     199        echo "</ul>";
     200
     201        echo "</div></div>";
     202    }
     203
     204    /**
     205     * Log all hook based data.
     206     * @access public
     207     */
     208    public function log( $args, $vars = '' ) {
     209        global $wp_filter, $wp_query;
     210
     211        if ( $args != 'gettext' && $args != 'gettext_with_context' ) {
    155212            if ( array_key_exists( $args, $wp_filter ) ) {
    156213                $funcs = $wp_filter[$args];
    157214                ksort($funcs);
    158                 $functions_called = ( array_key_exists( $args, $wp_filter ) )? kb_dump( $funcs ) : "" ;
     215                $this->counters->used++;
     216
     217                $this->logged[] = Array( 'vars' => $vars, 'funcs' => $funcs, 'name' => $args );
    159218            }
    160             trigger_error( "$args<div class = 'kb_hook_vars'>" . kb_dump( $vars ) . "</div><div class = 'kb_fn_called'>"  . $functions_called . "</div>", E_USER_WARNING );
    161             $kb_filters++;
    162     } else $kb_gettext_counter++;
    163 
    164     if ( array_key_exists( $args, $wp_filter ) )
    165         $kb_filters_used++;
     219
     220            $this->counters->actions++;
     221        } else $this->counters->gettexts++;
     222    }
     223
    166224}
    167225
    168 //Make PHP use my custom handler to log messages instead of directly displaying them.
    169 set_error_handler( 'ep_error_handler' );
    170 
    171 //Log all errors.
    172 add_action( 'all', 'kb_log_hooks' );
    173 
    174 //I _need_ jquery to allow expanding text.
    175 wp_enqueue_script( 'jquery' );
    176 
    177 //Log everything, but display only if WP_DEBUG is set to true
    178 if ( defined( 'WP_DEBUG' ) && WP_DEBUG )
    179     //And don't mess up any ajax stuff either.
    180     if ( !array_key_exists( 'HTTP_X_REQUESTED_WITH', $_SERVER ) || ( array_key_exists( 'HTTP_X_REQUESTED_WITH' , $_SERVER ) && $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest' ) )
    181         add_action( 'shutdown', 'kb_display_errors' );
    182 
    183 /* 2. Reset capabilities to initial state if KB_RESET_CAPS is set in $_GET */
    184 
    185 /**
    186  * Reverts capabilities to default state.
    187  *
    188  * Useful while debugging.
    189  */
    190 function kb_reset_caps() {
    191     global $wpdb;
    192     $key = $wpdb->prefix . 'user_roles';
    193 
    194     //Bye, bye, existing caps
    195     delete_option( $key );
    196 
    197     //Repopulate
    198     require_once( "/home/kunalb/dev/eventpress/wp-admin/includes/schema.php" );
    199     populate_roles();
     226/**
     227 * Check the $_GET variable and initialize classes accordingly.
     228 */
     229if (defined ('WP_DEBUG') && WP_DEBUG) {
     230    if( isset( $_GET['KB_Debug_Errors'] ) )
     231        new KB_Debug_Errors();
     232    if( isset( $_GET['KB_Debug_Hooks'] ) )
     233        new KB_Debug_Hooks();
    200234}
    201 if (isset( $_GET['KB_RESET_CAPS'] ))
    202     add_action( 'init', kb_reset_caps );
    203 
Note: See TracChangeset for help on using the changeset viewer.