<?php
/*
Plugin Name: Debug Display
Plugin URI: http://www.santosj.name
Description: Display log messages
Author: Jacob Santos
Version: 1.0
Author URI: http://www.santosj.name
*/

add_action('shutdown', 'wp_log_shutdown');

/**
 * wp_log_shutdown() - Display the log messages to the user
 *
 * Will display the messages in the theme footer ('wp_footer').
 *
 * @since 2.5
 */
function wp_log_shutdown() {
	// If WP_DEBUG is not defined, then don't process log message
	if( !defined('WP_DEBUG') )
		return;

	global $wp_log_messages, $wp_plugin_log_messages;

	if( !is_array($wp_log_messages) || empty($wp_log_messages) )
		return;

	$log_content = '';

	uasort($wp_log_messages, 'strnatcasecmp');

	$log_content .= apply_filters('wp_log_display_wordpress', '<h1>WordPress Log Messages</h1>');

	$log_content .= apply_filters('wp_log_display_start', '<div align="left" style="font-size: 10px;">');
	$log_content .= wp_log_display( $wp_log_messages );
	$log_content .= apply_filters('wp_log_display_end', '</div>');

	$log_content .= apply_filters('wp_log_display_plugins', '<h1>Plugin Log Messages</h1>');

	$log_content .= apply_filters('wp_log_display_start', '<div align="left" style="font-size: 10px;">');
	$log_content .= wp_log_display( $wp_plugin_log_messages );
	$log_content .= apply_filters('wp_log_display_end', '</div>');

	echo $log_content;
}

/**
 * wp_log_display() - Converts log message array to display form
 *
 * @since 2.5
 * @param array $log_messages
 * @return string
 */
function wp_log_display($log_messages) {
	$log_content = '';

	foreach( $log_messages as $type => $log_type_array ) {
		$log_type = apply_filters('wp_log_type', true, $type);
		$log_type_with_type = apply_filters("wp_log_{$type}_type", true);

		// Catch types that shouldn't have been logged.
		if( false === $log_type || false === $log_type_with_type )
			continue;

		$log_content .= apply_filters('wp_log_display_type_start', "<h2>{$type}</h2><ul>", $type);

		uksort($log_type_array, 'strnatcasecmp');

		foreach( $log_type_array as $severity => $log_messages ) {

			// Catch severity items that shouldn't be displayed
			if ( false === apply_filters('wp_log_severity', true, $severity) )
				continue;

			$severity_display = wp_log_severity_display_name($severity);
			$log_content .= apply_filters('wp_log_display_severity_start', '<li><h3>' . $severity_display . '</h3><br /><br />', $severity, $severity_display);

			$log_content .= "<ol>\n";

			foreach( $log_messages as $log_message ) {
				$log_content .= "<li>\n";
				// See if we need to add the backtrace.
				$log_content .= '<strong>'. $log_message[0] .'</strong><br /><br />';
				$backtrace = apply_filters('wp_log_backtrace', $log_message[1]);
				
				// Format the backtrace display
				$log_content .= "<table style=\"font-size: 10px;\">\n<tr><th>file</th><th>line</th><th>function</th><th>args</th></tr>\n";

				for($i=0; $i < count($backtrace); ++$i) {
					if( 'wp_log' == $backtrace_message['function'] )
						continue;

					$log_content .= "<tr". ( ($i%2) == 0 ? ' style="background-color:#ffffff;"' : '') .">\n";
					$log_content .= "<td>". $backtrace[$i]['file'] ."</td>\n";
					$log_content .= "<td>". $backtrace[$i]['line'] ."</td>\n";
					$log_content .= "<td>". $backtrace[$i]['function'] ."</td>\n";
					$log_content .= "<td>". implode("<br />\n", $backtrace[$i]['args']) ."</td>\n";
					$log_content .= "</tr>";
				}

				$log_content .= "</table><br /><br />\n";
				$log_content .= "</li>\n";
			}
			$log_content .= "</ol>\n";

			$log_content .= apply_filters('wp_log_display_severity_end', '</li>');
		}

		$log_content .= apply_filters('wp_log_display_type_end', "</ul>\n\n");
	}

	return $log_content;
}

