diff --git a/README.md b/README.md index 3ca210e..b844e57 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,16 @@ Allows users with admin access to view and purge the `debug.log` file kept insid * allows for loading the debug file in a new browser window * allows for purging the debug file +# Roadmap +* fancier formatting for log file +* collapse / expand stack traces + #### [Pull requests](https://github.com/norcross/debug-quick-look/pulls) are very much welcome and encouraged. ## Changelog +#### Version 0.0.2 - 2017/07/12 +* Changed method for pulling and display log file to check for memory overload. Props [@rarst](https://github.com/Rarst) + #### Version 0.0.1 - 2017/07/12 * Initial release! diff --git a/debug-quick-look.php b/debug-quick-look.php index 36a246b..aa40b2c 100644 --- a/debug-quick-look.php +++ b/debug-quick-look.php @@ -5,7 +5,7 @@ * Description: Creates an admin bar link to view or purge the debug log file * Author: Andrew Norcross * Author URI: http://andrewnorcross.com/ - * Version: 0.0.1 + * Version: 0.0.2 * Text Domain: debug-quick-look * Requires WP: 4.4 * Domain Path: languages @@ -37,6 +37,11 @@ * THE SOFTWARE. */ +// Define our version. +if ( ! defined( 'DEBUG_QUICK_LOOK_VERS' ) ) { + define( 'DEBUG_QUICK_LOOK_VERS', '0.0.2' ); +} + /** * Call our class. */ @@ -138,7 +143,7 @@ public function process_debug_type() { p.returnlink { text-align: center; font-size: 14px; line-height: 22px; } p.nofile { text-align: center; font-size: 14px; line-height: 22px; font-style: italic; } p.codeblock { background-color: #fff; color: #000; font-size: 14px; line-height: 22px; padding: 5px 15px; } - p.codeblock br { height: 5px; display: block; margin: 0; padding: 0; } + p.codeblock .empty-space { display: block; width: 100%; height: 0; margin: 10px 0 -10px; padding: 0; border-top: 1px dotted #ccc; } p strong { font-weight: bold; } p em { font-style: italic; } code, pre { white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word; } code pre, span.prewrap { color: #ff0000; } @@ -161,30 +166,17 @@ public function process_debug_type() { // We have a file. So start the additional checks. if ( false !== $exists = $this->check_file_data() ) { + // Set our file. + $file = WP_CONTENT_DIR . '/debug.log'; + // We requested a viewing. if ( 'view' === sanitize_key( $_GET['quickaction'] ) ) { - - // Parse out the data. - $data = file_get_contents( WP_CONTENT_DIR . '/debug.log' ); - - // Trim and break it up. - $data = nl2br( trim( $data ) ); - - // Convert my line breaks. - $data = str_replace( array( '
', '' ), array( '', '' ), $data ); - - // Generate the actual output. - $build .= '
' . $data . '
' . esc_html__( 'The log file has been purged.', 'debug-quick-look' ) . '
'; + $build .= $this->purge_log( $file ); } } @@ -200,6 +192,125 @@ public function process_debug_type() { die(); } + /** + * Our abstracted function for viewing the log file. + * + * @param string $file The filepath we are working with. + * + * @return string + */ + public function view_log( $file = '' ) { + + // Parse out the data. + $data = $this->parse_log( $file ); + + // Trim and break it up. + $data = nl2br( trim( $data ) ); + + // Now convert the line break markup to an empty div. + $data = str_replace( array( '', '' ), array( '', '' ), $data ); + + // Generate and return the actual output. + return '
' . $data . '
' . esc_html__( 'The log file has been purged.', 'debug-quick-look' ) . '
'; + } + + /** + * Parse my log file from the end in case it's too big. + * + * @link http://stackoverflow.com/questions/6451232/php-reading-large-files-from-end/6451391#6451391 + * + * @param string $file The filepath we are working with. + * @param integer $count Our line count that we're working with. + * @param integer $size How many bytes we safely wanna check. + * + * @return string + */ + public function parse_log( $file = '', $count = 100, $size = 512 ) { + + // Set my empty. + $lines = array(); + + // We will always have a fragment of a non-complete line, so keep this in here till we have our next entire line. + $left = ''; + + // Open our file. + $readf = fopen( $file, 'r' ); + + // Go to the end of the file. + fseek( $readf, 0, SEEK_END ); + + do { + + // Confirm we can actually go back $size bytes + $check = $size; + + if ( ftell( $readf ) <= $size ) { + $check = ftell( $readf ); + } + + // Bail on an empty file. + if ( empty( $check ) ) { + break; + } + + // Go back as many bytes as we can and read them to $data, + // and then move the file pointer back to where we were. + fseek( $readf, - $check, SEEK_CUR ); + + // Set the data. + $data = fread( $readf, $check ); + + // Include the "leftovers". + $data .= $left; + + // Seek back into it. + fseek( $readf, - $check, SEEK_CUR ); + + // Split lines by \n. Then reverse them, now the last line is most likely + // not a complete line which is why we do not directly add it, but + // append it to the data read the next time. + $split = array_reverse( explode( "\n", $data ) ); + $newls = array_slice( $split, 0, - 1 ); + $lines = array_merge( $lines, $newls ); + $left = $split[ count( $split ) - 1 ]; + + } while ( count( $lines ) < $count && ftell( $readf ) != 0 ); + + // Check and add the extra line. + if ( ftell( $readf ) == 0 ) { + $lines[] = $left; + } + + // Close the file we just dealt with. + fclose( $readf ); + + // Usually, we will read too many lines, correct that here. + $array = array_slice( $lines, 0, $count ); + $array = array_reverse( array_filter( $array, 'strlen' ) ); + + // Convert my array to a large string. + return implode( "\n", $array ); + } + /** * Add the links for the debug log file. *