FS_Logger

The NextGEN Gallery FS Logger class.

Defined (1)

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

/freemius/includes/class-fs-logger.php  
  1. class FS_Logger { 
  2. private $_id; 
  3. private $_on = false; 
  4. private $_echo = false; 
  5. private $_file_start = 0; 
  6.  
  7. private static $LOGGERS = array(); 
  8. private static $LOG = array(); 
  9. private static $CNT = 0; 
  10. private static $_HOOKED_FOOTER = false; 
  11.  
  12. private function __construct( $id, $on = false, $echo = false ) { 
  13. $this->_id = $id; 
  14.  
  15. $bt = debug_backtrace(); 
  16. $caller = $bt[2]; 
  17.  
  18. $this->_file_start = strpos( $caller['file'], 'plugins' ) + strlen( 'plugins/' ); 
  19.  
  20. if ( $on ) { 
  21. $this->on(); 
  22. if ( $echo ) { 
  23. $this->echo_on(); 
  24.  
  25. /** 
  26. * @param string $id 
  27. * @param bool $on 
  28. * @param bool $echo 
  29. * @return FS_Logger 
  30. */ 
  31. public static function get_logger( $id, $on = false, $echo = false ) { 
  32. $id = strtolower( $id ); 
  33.  
  34. if ( ! isset( self::$LOGGERS[ $id ] ) ) { 
  35. self::$LOGGERS[ $id ] = new FS_Logger( $id, $on, $echo ); 
  36.  
  37. return self::$LOGGERS[ $id ]; 
  38.  
  39. private static function _hook_footer() { 
  40. if ( self::$_HOOKED_FOOTER ) { 
  41. return; 
  42.  
  43. if ( is_admin() ) { 
  44. add_action( 'admin_footer', 'FS_Logger::dump', 100 ); 
  45. } else { 
  46. add_action( 'wp_footer', 'FS_Logger::dump', 100 ); 
  47.  
  48. function is_on() { 
  49. return $this->_on; 
  50.  
  51. function on() { 
  52. $this->_on = true; 
  53.  
  54. self::_hook_footer(); 
  55.  
  56. function echo_on() { 
  57. $this->on(); 
  58.  
  59. $this->_echo = true; 
  60.  
  61. function is_echo_on() { 
  62. return $this->_echo; 
  63.  
  64. function get_id() { 
  65. return $this->_id; 
  66.  
  67. function get_file() { 
  68. return $this->_file_start; 
  69.  
  70. private function _log( &$message, $type = 'log', $wrapper ) { 
  71. if ( ! $this->is_on() ) { 
  72. return; 
  73.  
  74. $bt = debug_backtrace(); 
  75. $depth = $wrapper ? 3 : 2; 
  76. while ( $depth < count( $bt ) - 1 && 'eval' === $bt[ $depth ]['function'] ) { 
  77. $depth ++; 
  78.  
  79. $caller = $bt[ $depth ]; 
  80.  
  81. $log = array_merge( $caller, array( 
  82. 'cnt' => self::$CNT ++,  
  83. 'logger' => $this,  
  84. 'timestamp' => microtime(true),  
  85. 'type' => $type,  
  86. 'msg' => $message,  
  87. ) ); 
  88.  
  89. self::$LOG[] = $log; 
  90.  
  91. if ( $this->is_echo_on() ) { 
  92. echo self::format_html( $log ) . "\n"; 
  93.  
  94. function log( $message, $wrapper = false ) { 
  95. $this->_log( $message, 'log', $wrapper ); 
  96.  
  97. function info( $message, $wrapper = false ) { 
  98. $this->_log( $message, 'info', $wrapper ); 
  99.  
  100. function warn( $message, $wrapper = false ) { 
  101. $this->_log( $message, 'warn', $wrapper ); 
  102.  
  103. function error( $message, $wrapper = false ) { 
  104. $this->_log( $message, 'error', $wrapper ); 
  105.  
  106. /** 
  107. * Log API error. 
  108. * @author Vova Feldman (@svovaf) 
  109. * @since 1.2.1.5 
  110. * @param mixed $api_result 
  111. * @param bool $wrapper 
  112. */ 
  113. function api_error( $api_result, $wrapper = false ) { 
  114. $message = ''; 
  115. if ( is_object( $api_result ) && isset( $api_result->error ) ) { 
  116. $message = $api_result->error->message; 
  117. } else if ( is_object( $api_result ) ) { 
  118. $message = var_export( $api_result, true ); 
  119. } else if ( is_string( $api_result ) ) { 
  120. $message = $api_result; 
  121. } else if ( empty( $api_result ) ) { 
  122. $message = 'Empty API result.'; 
  123.  
  124. $message = 'API Error: ' . $message; 
  125.  
  126. $this->_log( $message, 'error', $wrapper ); 
  127.  
  128. function entrance( $message = '', $wrapper = false ) { 
  129. $msg = 'Entrance' . ( empty( $message ) ? '' : ' > ' ) . $message; 
  130.  
  131. $this->_log( $msg, 'log', $wrapper ); 
  132.  
  133. function departure( $message = '', $wrapper = false ) { 
  134. $msg = 'Departure' . ( empty( $message ) ? '' : ' > ' ) . $message; 
  135.  
  136. $this->_log( $msg, 'log', $wrapper ); 
  137.  
  138. private static function format( $log, $show_type = true ) { 
  139. return '[' . str_pad( $log['cnt'], strlen( self::$CNT ), '0', STR_PAD_LEFT ) . '] [' . $log['logger']->_id . '] ' . ( $show_type ? '[' . $log['type'] . ']' : '' ) . $log['function'] . ' >> ' . $log['msg'] . ( isset( $log['file'] ) ? ' (' . substr( $log['file'], $log['logger']->_file_start ) . ' ' . $log['line'] . ') ' : '' ) . ' [' . $log['timestamp'] . ']'; 
  140.  
  141. private static function format_html( $log ) { 
  142. return '<div style="font-size: 13px; font-family: monospace; color: #7da767; padding: 8px 3px; background: #000; border-bottom: 1px solid #555;">[' . $log['cnt'] . '] [' . $log['logger']->_id . '] [' . $log['type'] . '] <b><code style="color: #c4b1e0;">' . $log['function'] . '</code> >> <b style="color: #f59330;">' . esc_html($log['msg']) . '</b></b>' . ( isset( $log['file'] ) ? ' (' . substr( $log['file'], $log['logger']->_file_start ) . ' ' . $log['line'] . ')' : '' ) . ' [' . $log['timestamp'] . ']</div>'; 
  143.  
  144. static function dump() { 
  145. ?> 
  146. <!-- BEGIN: Freemius PHP Console Log --> 
  147. <script type="text/javascript"> 
  148. <?php 
  149. foreach (self::$LOG as $log) 
  150. echo 'console.' . $log['type'] . '(' . json_encode(self::format($log, false)) . ')' . "\n"; 
  151. ?> 
  152. </script> 
  153. <!-- END: Freemius PHP Console Log --> 
  154. <?php 
  155.  
  156. static function get_log() { 
  157. return self::$LOG;