MS_Helper_Debug

This Helper creates utility functions for debugging.

Defined (1)

The class is defined in the following location(s).

/app/helper/class-ms-helper-debug.php  
  1. class MS_Helper_Debug extends MS_Helper { 
  2. /** 
  3. * Logs errors to WordPress debug log. 
  4. * The following constants ned to be set in wp-config.php 
  5. * or elsewhere where turning on and off debugging makes sense. 
  6. * // Essential 
  7. * define('WP_DEBUG', true); 
  8. * // Enables logging to /wp-content/debug.log 
  9. * define('WP_DEBUG_LOG', true); 
  10. * // Force debug messages in WordPress to be turned off (using logs instead) 
  11. * define('WP_DEBUG_DISPLAY', false); 
  12. * @since 1.0.0 
  13. * @param mixed $message Array, object or text to output to log. 
  14. */ 
  15. public static function log( $message, $echo_file = false ) { 
  16. if ( ! WP_DEBUG && ! WDEV_DEBUG ) { return; } 
  17.  
  18. if ( defined( 'DEBUG_BACKTRACE_IGNORE_ARGS' ) ) { 
  19. $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); 
  20. } else { 
  21. $trace = debug_backtrace(); 
  22.  
  23. $exception = new Exception(); 
  24. $debug = array_shift( $trace ); 
  25. $caller = array_shift( $trace ); 
  26. $exception = $exception->getTrace(); 
  27. $callee = array_shift( $exception ); 
  28.  
  29. $msg = isset( $caller['class'] ) ? $caller['class'] . '[' . $callee['line'] . ']: ' : ''; 
  30.  
  31. if ( is_array( $message ) || is_object( $message ) ) { 
  32. $msg .= print_r( $message, true ); 
  33. } else { 
  34. $msg .= $message; 
  35.  
  36. if ( $echo_file ) { 
  37. $msg .= "\nIn " . $callee['file'] . ' on line ' . $callee['line']; 
  38.  
  39. error_log( $msg ); 
  40.  
  41. public static function debug_trace( $return = false ) { 
  42. if ( ! WP_DEBUG && ! WDEV_DEBUG ) { return; } 
  43.  
  44. $traces = debug_backtrace(); 
  45. $fields = array( 
  46. 'file',  
  47. 'line',  
  48. 'function',  
  49. 'class',  
  50. ); 
  51. $log = array( '---------------------------- Trace start ----------------------------' ); 
  52.  
  53. foreach ( $traces as $i => $trace ) { 
  54. $line = array(); 
  55. foreach ( $fields as $field ) { 
  56. if ( ! empty( $trace[ $field ] ) ) { 
  57. $line[] = "$field: {$trace[ $field ]}"; 
  58. $log[] = " [$i]". implode( '; ', $line ); 
  59.  
  60. if ( $return ) { 
  61. return implode( "\n", $log ); 
  62. } else { 
  63. error_log( implode( "\n", $log ) ); 
  64.