entities
10 years ago
managers
10 years ago
sdk
10 years ago
class-freemius-abstract.php
10 years ago
class-freemius.php
10 years ago
class-fs-api.php
10 years ago
class-fs-logger.php
10 years ago
class-fs-plugin-updater.php
10 years ago
class-fs-security.php
10 years ago
fs-core-functions.php
10 years ago
fs-plugin-functions.php
10 years ago
i18n.php
10 years ago
class-fs-logger.php
168 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package Freemius |
| 4 | * @copyright Copyright (c) 2015, Freemius, Inc. |
| 5 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 6 | * @since 1.0.3 |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | class FS_Logger { |
| 14 | private $_id; |
| 15 | private $_on = false; |
| 16 | private $_echo = false; |
| 17 | private $_file_start = 0; |
| 18 | |
| 19 | private static $LOGGERS = array(); |
| 20 | private static $LOG = array(); |
| 21 | private static $CNT = 0; |
| 22 | private static $_HOOKED_FOOTER = false; |
| 23 | |
| 24 | private function __construct( $id, $on = false, $echo = false ) { |
| 25 | $this->_id = $id; |
| 26 | |
| 27 | $bt = debug_backtrace(); |
| 28 | $caller = $bt[2]; |
| 29 | |
| 30 | $this->_file_start = strpos( $caller['file'], 'plugins' ) + strlen( 'plugins/' ); |
| 31 | |
| 32 | if ( $on ) { |
| 33 | $this->on(); |
| 34 | } |
| 35 | if ( $echo ) { |
| 36 | $this->echo_on(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param string $id |
| 42 | * @param bool $on |
| 43 | * @param bool $echo |
| 44 | * |
| 45 | * @return FS_Logger |
| 46 | */ |
| 47 | public static function get_logger( $id, $on = false, $echo = false ) { |
| 48 | $id = strtolower( $id ); |
| 49 | |
| 50 | if ( ! isset( self::$LOGGERS[ $id ] ) ) { |
| 51 | self::$LOGGERS[ $id ] = new FS_Logger( $id, $on, $echo ); |
| 52 | } |
| 53 | |
| 54 | return self::$LOGGERS[ $id ]; |
| 55 | } |
| 56 | |
| 57 | private static function _hook_footer() { |
| 58 | if ( self::$_HOOKED_FOOTER ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | if ( is_admin() ) { |
| 63 | add_action( 'admin_footer', 'FS_Logger::dump', 100 ); |
| 64 | } else { |
| 65 | add_action( 'wp_footer', 'FS_Logger::dump', 100 ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | function is_on() { |
| 70 | return $this->_on; |
| 71 | } |
| 72 | |
| 73 | function on() { |
| 74 | $this->_on = true; |
| 75 | |
| 76 | self::_hook_footer(); |
| 77 | } |
| 78 | |
| 79 | function echo_on() { |
| 80 | $this->_on; |
| 81 | |
| 82 | $this->_echo = true; |
| 83 | } |
| 84 | |
| 85 | function is_echo_on() { |
| 86 | return $this->_echo; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | private function _log( &$message, $type = 'log', $wrapper ) { |
| 91 | if ( ! $this->is_on() ) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | $bt = debug_backtrace(); |
| 96 | $depth = $wrapper ? 3 : 2; |
| 97 | while ( $depth < count( $bt ) - 1 && 'eval' === $bt[ $depth ]['function'] ) { |
| 98 | $depth ++; |
| 99 | } |
| 100 | |
| 101 | $caller = $bt[ $depth ]; |
| 102 | |
| 103 | $log = array_merge( $caller, array( |
| 104 | 'cnt' => self::$CNT ++, |
| 105 | 'logger' => $this, |
| 106 | 'timestamp' => date( WP_FS__LOG_DATETIME_FORMAT . ':u' ), |
| 107 | 'type' => $type, |
| 108 | 'msg' => $message, |
| 109 | ) ); |
| 110 | |
| 111 | self::$LOG[] = $log; |
| 112 | |
| 113 | if ( $this->is_echo_on() ) { |
| 114 | echo self::_format_html( $log ) . "\n"; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | function log( $message, $wrapper = false ) { |
| 119 | $this->_log( $message, 'log', $wrapper ); |
| 120 | } |
| 121 | |
| 122 | function info( $message, $wrapper = false ) { |
| 123 | $this->_log( $message, 'info', $wrapper ); |
| 124 | } |
| 125 | |
| 126 | function warn( $message, $wrapper = false ) { |
| 127 | $this->_log( $message, 'warn', $wrapper ); |
| 128 | } |
| 129 | |
| 130 | function error( $message, $wrapper = false ) { |
| 131 | $this->_log( $message, 'error', $wrapper ); |
| 132 | } |
| 133 | |
| 134 | function entrance( $message = '', $wrapper = false ) { |
| 135 | $msg = 'Entrance' . ( empty( $message ) ? '' : ' > ' ) . $message; |
| 136 | |
| 137 | $this->_log( $msg, 'log', $wrapper ); |
| 138 | } |
| 139 | |
| 140 | function departure( $message = '', $wrapper = false ) { |
| 141 | $msg = 'Departure' . ( empty( $message ) ? '' : ' > ' ) . $message; |
| 142 | |
| 143 | $this->_log( $msg, 'log', $wrapper ); |
| 144 | } |
| 145 | |
| 146 | private static function _format( $log, $show_type = true ) { |
| 147 | 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'] . ']'; |
| 148 | } |
| 149 | |
| 150 | private static function _format_html( $log ) { |
| 151 | return '<div style="font-size: 11px; padding: 3px; background: #ccc; margin-bottom: 3px;">[' . $log['cnt'] . '] [' . $log['logger']->_id . '] [' . $log['type'] . '] <b><code style="color: blue;">' . $log['function'] . '</code> >> <b style="color: darkorange;">' . $log['msg'] . '</b></b>' . ( isset( $log['file'] ) ? ' (' . substr( $log['file'], $log['logger']->_file_start ) . ' ' . $log['line'] . ')' : '' ) . ' [' . $log['timestamp'] . ']</div>'; |
| 152 | } |
| 153 | |
| 154 | static function dump() { |
| 155 | ?> |
| 156 | <!-- BEGIN: Freemius PHP Console Log --> |
| 157 | <script type="text/javascript"> |
| 158 | <?php |
| 159 | foreach (self::$LOG as $log) |
| 160 | { |
| 161 | echo 'console.' . $log['type'] . '(' . json_encode(self::_format($log, false)) . ')' . "\n"; |
| 162 | } |
| 163 | ?> |
| 164 | </script> |
| 165 | <!-- END: Freemius PHP Console Log --> |
| 166 | <?php |
| 167 | } |
| 168 | } |