From dcdc0ef297cd884fd97393f7ca0c6859ec0efcb9 Mon Sep 17 00:00:00 2001 From: Peter Molnar Date: Fri, 5 Feb 2016 13:34:27 +0000 Subject: [PATCH] extending debug function --- wp-featured2rss.php | 51 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/wp-featured2rss.php b/wp-featured2rss.php index 3600df9..f9d995c 100644 --- a/wp-featured2rss.php +++ b/wp-featured2rss.php @@ -143,23 +143,54 @@ class WP_FEATURED2RSS { * * @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( '

Error:

' . '

' . $message . '

' ); - 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; + } } - error_log( __CLASS__ . " => " . $message ); + // ERR, CRIT, ALERT and EMERG + if ( 3 >= $level_ ) { + wp_die( '

Error:

' . '

' . $message . '

' ); + exit; + } + + $trace = debug_backtrace(); + $caller = $trace[1]; + $parent = $caller['function']; + + if (isset($caller['class'])) + $parent = $caller['class'] . '::' . $parent; + + return error_log( "{$parent}: {$message}" ); } /**