better debugging
Peter Molnar hello@petermolnar.eu
Tue, 08 Mar 2016 13:46:35 +0000
2 files changed,
58 insertions(+),
22 deletions(-)
M
readme.txt
→
readme.txt
@@ -3,8 +3,8 @@ Contributors: cadeyrn
Donate link: https://paypal.me/petermolnar/3 Tags: backup, YAML, flat files Requires at least: 3.0 -Tested up to: 4.4 -Stable tag: 0.1 +Tested up to: 4.4.2 +Stable tag: 0.1.1 License: GPLv3 License URI: http://www.gnu.org/licenses/gpl-3.0.html Required minimum PHP version: 5.3@@ -38,6 +38,11 @@
* every A. indicates BIG changes. * every .B version indicates new features. * every ..C indicates bugfixes for A.B version. + += 0.1.1 = +*2016-03-08* + +* better debugging = 0.1 = *2015-12-10*
M
wp-flatbackups.php
→
wp-flatbackups.php
@@ -3,7 +3,7 @@ /*
Plugin Name: wp-flatbackups Plugin URI: https://github.com/petermolnar/wp-flatbackups Description: auto-export WordPress content to flat YAML + Markdown files -Version: 0.1 +Version: 0.1.1 Author: Peter Molnar <hello@petermolnar.eu> Author URI: http://petermolnar.eu/ License: GPLv3@@ -54,7 +54,7 @@ */
public static function export_yaml () { if (!function_exists('yaml_emit')) { - static::debug('`yaml_emit` function missing. Please install the YAML extension; otherwise this plugin will not work'); + static::debug('`yaml_emit` function missing. Please install the YAML extension; otherwise this plugin will not work', 4); return false; }@@ -83,7 +83,7 @@ $mkdir = array ( $flatroot, $flatdir );
foreach ( $mkdir as $dir ) { if ( !is_dir($dir)) { if (!mkdir( $dir )) { - static::debug_log('Failed to create ' . $dir . ', exiting YAML creation'); + static::debug_log('Failed to create ' . $dir . ', exiting YAML creation', 4); return false; } }@@ -108,12 +108,12 @@ foreach ( $attachments as $aid => $attachment ) {
$attachment_path = get_attached_file( $aid ); $attachment_file = basename( $attachment_path); $target_file = $flatdir . DIRECTORY_SEPARATOR . $attachment_file; - //static::debug ('should ' . $post->post_name . ' have this attachment?: ' . $attachment_file ); + static::debug ('should ' . $post->post_name . ' have this attachment?: ' . $attachment_file, 7 ); if ( !is_file($target_file)) { if (!link( $attachment_path, $target_file )) { - static::debug("could not hardlink '$attachment_path' to '$target_file'; trying to copy"); + static::debug("could not hardlink '$attachment_path' to '$target_file'; trying to copy", 5); if (!copy($attachment_path, $target_file )) { - static::debug("could not copy '$attachment_path' to '$target_file'; saving attachment failed!"); + static::debug("could not copy '$attachment_path' to '$target_file'; saving attachment failed!", 4); } } }@@ -161,7 +161,7 @@
$cout = yaml_emit($c, YAML_UTF8_ENCODING ); $cout .= "---\n" . $comment->comment_content; - //static::debug ('Exporting comment #' . $comment->comment_ID. ' to ' . $cfile ); + static::debug ('Exporting comment #' . $comment->comment_ID. ' to ' . $cfile, 6 ); file_put_contents ($cfile, $cout); touch ( $cfile, $c_timestamp ); }@@ -174,7 +174,7 @@
$out = static::yaml(); // write log - //static::debug ('Exporting #' . $post->ID . ', ' . $post->post_name . ' to ' . $flatfile ); + static::debug ('Exporting #' . $post->ID . ', ' . $post->post_name . ' to ' . $flatfile, 6 ); file_put_contents ($flatfile, $out); touch ( $flatfile, $post_timestamp ); return true;@@ -188,7 +188,7 @@ */
public static function yaml ( $postid = false ) { if (!function_exists('yaml_emit')) { - static::debug('`yaml_emit` function missing. Please install the YAML extension; otherwise this plugin will not work'); + static::debug('`yaml_emit` function missing. Please install the YAML extension; otherwise this plugin will not work', 4); return false; }@@ -382,23 +382,54 @@ * or if the level is LOG_ERR, but that will kill the process
* * @param string $message * @param int $level + * + * @output log to syslog | wp_die on high level + * @return false on not taking action, true on log sent */ - static function debug( $message, $level = LOG_NOTICE ) { + public static function debug( $message, $level = LOG_NOTICE ) { + if ( empty( $message ) ) + return false; + if ( @is_array( $message ) || @is_object ( $message ) ) $message = json_encode($message); + $levels = array ( + LOG_EMERG => 0, // system is unusable + LOG_ALERT => 1, // Alert action must be taken immediately + LOG_CRIT => 2, // Critical critical conditions + LOG_ERR => 3, // Error error conditions + LOG_WARNING => 4, // Warning warning conditions + LOG_NOTICE => 5, // Notice normal but significant condition + LOG_INFO => 6, // Informational informational messages + LOG_DEBUG => 7, // Debug debug-level messages + ); - switch ( $level ) { - case LOG_ERR : - wp_die( '<h1>Error:</h1>' . '<p>' . $message . '</p>' ); - exit; - default: - if ( !defined( 'WP_DEBUG' ) || WP_DEBUG != true ) - return; - break; + // number for number based comparison + // should work with the defines only, this is just a make-it-sure step + $level_ = $levels [ $level ]; + + // in case WordPress debug log has a minimum level + if ( defined ( 'WP_DEBUG_LEVEL' ) ) { + $wp_level = $levels [ WP_DEBUG_LEVEL ]; + if ( $level_ > $wp_level ) { + return false; + } + } + + // ERR, CRIT, ALERT and EMERG + if ( 3 >= $level_ ) { + wp_die( '<h1>Error:</h1>' . '<p>' . $message . '</p>' ); + exit; } - error_log( __CLASS__ . " => " . $message ); + $trace = debug_backtrace(); + $caller = $trace[1]; + $parent = $caller['function']; + + if (isset($caller['class'])) + $parent = $caller['class'] . '::' . $parent; + + return error_log( "{$parent}: {$message}" ); } /**@@ -415,4 +446,4 @@ }
$WP_FLATBACKUPS = new WP_FLATBACKUPS(); -endif;+endif;