Plugin Directory

Changeset 389439


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

Added support for logging debug messages as well as a corresponding screenshot.

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

Legend:

Unmodified
Added
Removed
  • kb-debug/trunk/README

    r389438 r389439  
    44Requires at least: WordPress 3.0
    55Tested up to: 3.2
    6 Stable tag: 0.1
     6Stable tag: 0.2
    77
    88Debug your plugins and themes.
     
    1010== Description ==
    1111
    12 To log and show a list of all warnings, errors and notices generated by your plugin, add a ?KB_Debug_Errors to your URL. To see a list of all hooks in the order they fire as well as what arguments they are called with and what functions run on them (again, in which order), add a ?KB_Debug_Hooks to the URL.
     12* Call `kb_debug` with any arguments that you want to log and they will be pretty printed at the end of the document.
     13* To log and show a list of all warnings, errors and notices generated by your plugin, add  `?KB_Debug_Errors` to your URL.
     14* To see a list of all hooks in the order they fire as well as what arguments they are called with and what functions run on them (again, in which order),
     15  add a `?KB_Debug_Hooks` to the URL.
    1316
    1417To see both just add ?KB_Debug_Errors&KB_Debug_Hooks. (Essentially $_GET[var] must be set to enable that feature).
     
    1619
    1720== Installation ==
    18 
    1921Upload `kb_debug.php` and 'kb-debug.css' to the `/wp-content/mu-plugins/` directory. (Create this directory if it doesn't exist).
    2022
     
    3133== Screenshots ==
    32341. Showing how errors and hooks appear on using this plugin.
     352. Showing how debugging information is shown in the plugin.
    3336
    3437== Changelog ==
     
    4043
    4144= 0.1 =
    42 First version.
     45* First version.
  • kb-debug/trunk/kb-debug.css

    r389435 r389439  
    1212
    1313.effect-border {
     14    border-top: solid 1px #fff;
     15    border-bottom: solid 1px #dfdfdf;
     16    border-radius: 3px 3px 0 0;
     17    padding: 10px;
     18}
     19
     20#kb-debug {
     21    background-color: #f0f0f0;
     22    color: #111;
     23    text-shadow: #fff 0 1px 0;
     24    border-radius: 3px 3px 0 0;
     25    border: solid 1px #ccc;
     26    margin: 10px;
     27    font-family: sans-serif;
     28    font-size: 12px;
     29    line-height: 20px;
     30}
     31
     32#kb-debug .effect-border {
    1433    border-top: solid 1px #fff;
    1534    border-bottom: solid 1px #dfdfdf;
     
    3554            border-bottom: solid #dfdfdf 1px;
    3655            padding: 2px;
     56            margin: 0;
    3757        }
    3858
     
    5474                width: 95%;
    5575                overflow: auto;
     76                padding: 10px;
     77                margin: 10px;
    5678            }
    5779
  • kb-debug/trunk/kb_debug.php

    r389436 r389439  
    4646
    4747/**
    48  * The Base Class used to save errors and warnings information, if any.
     48 * Used to save debugging information logged by the user.
     49 */
     50class KB_Debug {
     51
     52    /**
     53     * Store any errors, notices, warnings or debug information.
     54     * @access private
     55     * @var Array
     56     */
     57    private $logged;
     58
     59    /**
     60     * Constructor. Initializes values and assigns actions.
     61     */
     62    public function __construct() {
     63        $this->logged = Array();
     64
     65        add_action( 'shutdown', Array( &$this, 'display' ), 98 );
     66        wp_enqueue_style( 'kb-debug-css', KB_DEBUG_RELURL . "/kb-debug.css" );
     67    }
     68
     69    /**
     70     * Display all logged debugging information.
     71     * @access public
     72     */
     73    function display() {
     74        echo "<div class = 'kb-debug-box' id = 'kb-debug'><div class = 'effect-border'>";
     75        echo "<h2>Logged</h2>";
     76        echo "<ul>";
     77        foreach( $this->logged as $log ) {
     78            echo "<li>";
     79            echo "<h3>{$log->backtrace['line']}, {$log->backtrace['file']}</h3>";
     80            foreach( $log->data as $data ) {
     81                echo "<pre>"; print_r( $data ); echo "</pre>";
     82            }
     83            echo "</li>";
     84        }
     85        echo "</ul>";
     86        echo "</div></div>";
     87    }
     88
     89    /**
     90     * Log array of arguments passed to it. Later pretty printed with a backtrace using print_r.
     91     * Do not call this function directly.
     92     * @access public
     93     */
     94    function log( $arglist ) {
     95        $log = new STDClass();
     96        $log->data = $arglist;
     97        $backtrace = debug_backtrace();
     98        $log->backtrace = $backtrace[1];
     99
     100        $this->logged[] = $log;
     101    }
     102
     103}
     104 
     105
     106/**
     107 * Used to save errors and warnings information, if any.
    49108 * @package KB_Debug
    50109 */
     
    64123     */
    65124    private $counters;
     125
     126    /**
     127     * Warning in case an existing error handler is removed.
     128     * @access private
     129     * @var String
     130     */
     131    private $message;
    66132
    67133    /**
     
    77143        $this->counters->notices  = 0;
    78144
    79         add_action( 'shutdown', Array( &$this, 'display' ) );
    80         set_error_handler( Array( &$this, 'log' ) );
     145        add_action( 'shutdown', Array( &$this, 'display' ), 99 );
     146        $original_handler = set_error_handler( Array( &$this, 'log' ) );
     147
     148        $this->message = "";
     149        if( $original_handler != NULL )
     150            $this->message = "<p>Warning: An existing error handler was replaced." . print_r( $original_handler, true ) . "</p>";
    81151
    82152        wp_enqueue_style( 'kb-debug-css', KB_DEBUG_RELURL . "/kb-debug.css" );
     
    90160        echo "<div class = 'kb-debug-box'><div class = 'effect-border'>";
    91161        echo "<h2>Errors and Warnings</h2>";   
     162        echo $this->message;
    92163        echo "<ul>";
    93164        foreach( $this->logged as $log ) {
     
    112183            echo "<pre style = 'max-height: 100px;'>"; print_r($log['context']); echo "</pre>";
    113184
    114 
    115185            echo "</li>";
    116186        }
     
    170240
    171241        add_action( 'all', Array( &$this, 'log' ) );
    172         add_action( 'shutdown', Array( &$this, 'display' ) );
     242        add_action( 'shutdown', Array( &$this, 'display' ), 100 );
    173243
    174244        wp_enqueue_style( 'kb-debug-css', KB_DEBUG_RELURL . "/kb-debug.css" );
     
    228298 */
    229299if (defined ('WP_DEBUG') && WP_DEBUG) {
    230     if( isset( $_GET['KB_Debug_Errors'] ) )
     300   
     301    /**
     302     * Call this function to save any data. Records calling information via a stack trace.
     303     */
     304    function kb_debug() {
     305        static $kdb;
     306        if ( !isset( $kdb ) ) $kdb = new KB_Debug();
     307
     308        $kdb->log( func_get_args() );
     309    }
     310
     311    if( isset( $_GET['KB_Debug_Errors'] ) || ( defined( 'KB_DEBUG' ) && KB_DEBUG ) )
    231312        new KB_Debug_Errors();
    232     if( isset( $_GET['KB_Debug_Hooks'] ) )
     313    if( isset( $_GET['KB_Debug_Hooks'] ) || ( defined( 'KB_DEBUG' ) && KB_DEBUG ) )
    233314        new KB_Debug_Hooks();
    234315}
    235 
    236 
    237 /* 2. Reset capabilities to initial state if KB_RESET_CAPS is set in $_GET */
    238 
    239 /**
    240  * Reverts capabilities to default state.
    241  *
    242  * Useful while debugging.
    243  */
    244 function kb_reset_caps() {
    245     global $wpdb;
    246     $key = $wpdb->prefix . 'user_roles';
    247 
    248     //Bye, bye, existing caps
    249     delete_option( $key );
    250 
    251     //Repopulate
    252     require_once( "/home/kunalb/dev/eventpress/wp-admin/includes/schema.php" );
    253     populate_roles();
    254 }
    255 if (isset( $_GET['KB_RESET_CAPS'] ))
    256     add_action( 'init', kb_reset_caps );
Note: See TracChangeset for help on using the changeset viewer.