| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright 2010-2013 Craig Campbell |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
namespace wpCloud\StatelessMedia { |
| 18 |
|
| 19 |
if( !class_exists( 'wpCloud\StatelessMedia\Logger' ) ) { |
| 20 |
|
| 21 |
/** |
| 22 |
* Server Side Chrome PHP debugger class |
| 23 |
* |
| 24 |
* @package ChromePhp |
| 25 |
* @author Craig Campbell <iamcraigcampbell@gmail.com> |
| 26 |
*/ |
| 27 |
class Logger { |
| 28 |
/** |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
const VERSION = '4.1.0'; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
const HEADER_NAME = 'X-ChromeLogger-Data'; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
const BACKTRACE_LEVEL = 'backtrace_level'; |
| 42 |
|
| 43 |
/** |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
const LOG = 'log'; |
| 47 |
|
| 48 |
/** |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
const WARN = 'warn'; |
| 52 |
|
| 53 |
/** |
| 54 |
* @var string |
| 55 |
*/ |
| 56 |
const ERROR = 'error'; |
| 57 |
|
| 58 |
/** |
| 59 |
* @var string |
| 60 |
*/ |
| 61 |
const GROUP = 'group'; |
| 62 |
|
| 63 |
/** |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
const INFO = 'info'; |
| 67 |
|
| 68 |
/** |
| 69 |
* @var string |
| 70 |
*/ |
| 71 |
const GROUP_END = 'groupEnd'; |
| 72 |
|
| 73 |
/** |
| 74 |
* @var string |
| 75 |
*/ |
| 76 |
const GROUP_COLLAPSED = 'groupCollapsed'; |
| 77 |
|
| 78 |
/** |
| 79 |
* @var string |
| 80 |
*/ |
| 81 |
const TABLE = 'table'; |
| 82 |
|
| 83 |
/** |
| 84 |
* @var string |
| 85 |
*/ |
| 86 |
protected $_php_version; |
| 87 |
|
| 88 |
/** |
| 89 |
* @var int |
| 90 |
*/ |
| 91 |
protected $_timestamp; |
| 92 |
|
| 93 |
/** |
| 94 |
* @var array |
| 95 |
*/ |
| 96 |
protected $_json = array( |
| 97 |
'version' => self::VERSION, |
| 98 |
'columns' => array( 'log', 'backtrace', 'type' ), |
| 99 |
'rows' => array() |
| 100 |
); |
| 101 |
|
| 102 |
/** |
| 103 |
* @var array |
| 104 |
*/ |
| 105 |
protected $_backtraces = array(); |
| 106 |
|
| 107 |
/** |
| 108 |
* @var bool |
| 109 |
*/ |
| 110 |
protected $_error_triggered = false; |
| 111 |
|
| 112 |
/** |
| 113 |
* @var array |
| 114 |
*/ |
| 115 |
protected $_settings = array( |
| 116 |
self::BACKTRACE_LEVEL => 1 |
| 117 |
); |
| 118 |
|
| 119 |
/** |
| 120 |
* @var ChromePhp |
| 121 |
*/ |
| 122 |
protected static $_instance; |
| 123 |
|
| 124 |
/** |
| 125 |
* Prevent recursion when working with objects referring to each other |
| 126 |
* |
| 127 |
* @var array |
| 128 |
*/ |
| 129 |
protected $_processed = array(); |
| 130 |
|
| 131 |
/** |
| 132 |
* constructor |
| 133 |
*/ |
| 134 |
private function __construct() { |
| 135 |
$this->_php_version = phpversion(); |
| 136 |
$this->_timestamp = $this->_php_version >= 5.1 ? $_SERVER[ 'REQUEST_TIME' ] : time(); |
| 137 |
$this->_json[ 'request_uri' ] = $_SERVER[ 'REQUEST_URI' ]; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* gets instance of this class |
| 142 |
* |
| 143 |
* @return ChromePhp |
| 144 |
*/ |
| 145 |
public static function getInstance() { |
| 146 |
if( self::$_instance === null ) { |
| 147 |
self::$_instance = new self(); |
| 148 |
} |
| 149 |
return self::$_instance; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* logs a variable to the console |
| 154 |
* |
| 155 |
* @param mixed $data,... unlimited OPTIONAL number of additional logs [...] |
| 156 |
* @return void |
| 157 |
*/ |
| 158 |
public static function log() { |
| 159 |
$args = func_get_args(); |
| 160 |
return self::_log( '', $args ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* logs a warning to the console |
| 165 |
* |
| 166 |
* @param mixed $data,... unlimited OPTIONAL number of additional logs [...] |
| 167 |
* @return void |
| 168 |
*/ |
| 169 |
public static function warn() { |
| 170 |
$args = func_get_args(); |
| 171 |
return self::_log( self::WARN, $args ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* logs an error to the console |
| 176 |
* |
| 177 |
* @param mixed $data,... unlimited OPTIONAL number of additional logs [...] |
| 178 |
* @return void |
| 179 |
*/ |
| 180 |
public static function error() { |
| 181 |
$args = func_get_args(); |
| 182 |
return self::_log( self::ERROR, $args ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* sends a group log |
| 187 |
* |
| 188 |
* @param string value |
| 189 |
*/ |
| 190 |
public static function group() { |
| 191 |
$args = func_get_args(); |
| 192 |
return self::_log( self::GROUP, $args ); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* sends an info log |
| 197 |
* |
| 198 |
* @param mixed $data,... unlimited OPTIONAL number of additional logs [...] |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
public static function info() { |
| 202 |
$args = func_get_args(); |
| 203 |
return self::_log( self::INFO, $args ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* sends a collapsed group log |
| 208 |
* |
| 209 |
* @param string value |
| 210 |
*/ |
| 211 |
public static function groupCollapsed() { |
| 212 |
$args = func_get_args(); |
| 213 |
return self::_log( self::GROUP_COLLAPSED, $args ); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* ends a group log |
| 218 |
* |
| 219 |
* @param string value |
| 220 |
*/ |
| 221 |
public static function groupEnd() { |
| 222 |
$args = func_get_args(); |
| 223 |
return self::_log( self::GROUP_END, $args ); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* sends a table log |
| 228 |
* |
| 229 |
* @param string value |
| 230 |
*/ |
| 231 |
public static function table() { |
| 232 |
$args = func_get_args(); |
| 233 |
return self::_log( self::TABLE, $args ); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* internal logging call |
| 238 |
* |
| 239 |
* @param string $type |
| 240 |
* @return void |
| 241 |
*/ |
| 242 |
protected static function _log( $type, array $args ) { |
| 243 |
// nothing passed in, don't do anything |
| 244 |
if( count( $args ) == 0 && $type != self::GROUP_END ) { |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
$logger = self::getInstance(); |
| 249 |
|
| 250 |
$logger->_processed = array(); |
| 251 |
|
| 252 |
$logs = array(); |
| 253 |
foreach( $args as $arg ) { |
| 254 |
$logs[] = $logger->_convert( $arg ); |
| 255 |
} |
| 256 |
|
| 257 |
$backtrace = debug_backtrace( false ); |
| 258 |
$level = $logger->getSetting( self::BACKTRACE_LEVEL ); |
| 259 |
|
| 260 |
$backtrace_message = 'unknown'; |
| 261 |
if( isset( $backtrace[ $level ][ 'file' ] ) && isset( $backtrace[ $level ][ 'line' ] ) ) { |
| 262 |
$backtrace_message = $backtrace[ $level ][ 'file' ] . ' : ' . $backtrace[ $level ][ 'line' ]; |
| 263 |
} |
| 264 |
|
| 265 |
$logger->_addRow( $logs, $backtrace_message, $type ); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* converts an object to a better format for logging |
| 270 |
* |
| 271 |
* @param Object |
| 272 |
* @return array |
| 273 |
*/ |
| 274 |
protected function _convert( $object ) { |
| 275 |
// if this isn't an object then just return it |
| 276 |
if( !is_object( $object ) ) { |
| 277 |
return $object; |
| 278 |
} |
| 279 |
|
| 280 |
//Mark this object as processed so we don't convert it twice and it |
| 281 |
//Also avoid recursion when objects refer to each other |
| 282 |
$this->_processed[] = $object; |
| 283 |
|
| 284 |
$object_as_array = array(); |
| 285 |
|
| 286 |
// first add the class name |
| 287 |
$object_as_array[ '___class_name' ] = get_class( $object ); |
| 288 |
|
| 289 |
// loop through object vars |
| 290 |
$object_vars = get_object_vars( $object ); |
| 291 |
foreach( $object_vars as $key => $value ) { |
| 292 |
|
| 293 |
// same instance as parent object |
| 294 |
if( $value === $object || in_array( $value, $this->_processed, true ) ) { |
| 295 |
$value = 'recursion - parent object [' . get_class( $value ) . ']'; |
| 296 |
} |
| 297 |
$object_as_array[ $key ] = $this->_convert( $value ); |
| 298 |
} |
| 299 |
|
| 300 |
$reflection = new ReflectionClass( $object ); |
| 301 |
|
| 302 |
// loop through the properties and add those |
| 303 |
foreach( $reflection->getProperties() as $property ) { |
| 304 |
|
| 305 |
// if one of these properties was already added above then ignore it |
| 306 |
if( array_key_exists( $property->getName(), $object_vars ) ) { |
| 307 |
continue; |
| 308 |
} |
| 309 |
$type = $this->_getPropertyKey( $property ); |
| 310 |
|
| 311 |
if( $this->_php_version >= 5.3 ) { |
| 312 |
$property->setAccessible( true ); |
| 313 |
} |
| 314 |
|
| 315 |
try { |
| 316 |
$value = $property->getValue( $object ); |
| 317 |
} catch ( ReflectionException $e ) { |
| 318 |
$value = 'only PHP 5.3 can access private/protected properties'; |
| 319 |
} |
| 320 |
|
| 321 |
// same instance as parent object |
| 322 |
if( $value === $object || in_array( $value, $this->_processed, true ) ) { |
| 323 |
$value = 'recursion - parent object [' . get_class( $value ) . ']'; |
| 324 |
} |
| 325 |
|
| 326 |
$object_as_array[ $type ] = $this->_convert( $value ); |
| 327 |
} |
| 328 |
return $object_as_array; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* takes a reflection property and returns a nicely formatted key of the property name |
| 333 |
* |
| 334 |
* @param ReflectionProperty |
| 335 |
* @return string |
| 336 |
*/ |
| 337 |
protected function _getPropertyKey( ReflectionProperty $property ) { |
| 338 |
$static = $property->isStatic() ? ' static' : ''; |
| 339 |
if( $property->isPublic() ) { |
| 340 |
return 'public' . $static . ' ' . $property->getName(); |
| 341 |
} |
| 342 |
|
| 343 |
if( $property->isProtected() ) { |
| 344 |
return 'protected' . $static . ' ' . $property->getName(); |
| 345 |
} |
| 346 |
|
| 347 |
if( $property->isPrivate() ) { |
| 348 |
return 'private' . $static . ' ' . $property->getName(); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* adds a value to the data array |
| 354 |
* |
| 355 |
* @var mixed |
| 356 |
* @return void |
| 357 |
*/ |
| 358 |
protected function _addRow( array $logs, $backtrace, $type ) { |
| 359 |
// if this is logged on the same line for example in a loop, set it to null to save space |
| 360 |
if( in_array( $backtrace, $this->_backtraces ) ) { |
| 361 |
$backtrace = null; |
| 362 |
} |
| 363 |
|
| 364 |
// for group, groupEnd, and groupCollapsed |
| 365 |
// take out the backtrace since it is not useful |
| 366 |
if( $type == self::GROUP || $type == self::GROUP_END || $type == self::GROUP_COLLAPSED ) { |
| 367 |
$backtrace = null; |
| 368 |
} |
| 369 |
|
| 370 |
if( $backtrace !== null ) { |
| 371 |
$this->_backtraces[] = $backtrace; |
| 372 |
} |
| 373 |
|
| 374 |
$row = array( $logs, $backtrace, $type ); |
| 375 |
|
| 376 |
$this->_json[ 'rows' ][] = $row; |
| 377 |
$this->_writeHeader( $this->_json ); |
| 378 |
} |
| 379 |
|
| 380 |
protected function _writeHeader( $data ) { |
| 381 |
header( self::HEADER_NAME . ': ' . $this->_encode( $data ) ); |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* encodes the data to be sent along with the request |
| 386 |
* |
| 387 |
* @param array $data |
| 388 |
* @return string |
| 389 |
*/ |
| 390 |
protected function _encode( $data ) { |
| 391 |
return base64_encode( utf8_encode( json_encode( $data ) ) ); |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* adds a setting |
| 396 |
* |
| 397 |
* @param string key |
| 398 |
* @param mixed value |
| 399 |
* @return void |
| 400 |
*/ |
| 401 |
public function addSetting( $key, $value ) { |
| 402 |
$this->_settings[ $key ] = $value; |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* add ability to set multiple settings in one call |
| 407 |
* |
| 408 |
* @param array $settings |
| 409 |
* @return void |
| 410 |
*/ |
| 411 |
public function addSettings( array $settings ) { |
| 412 |
foreach( $settings as $key => $value ) { |
| 413 |
$this->addSetting( $key, $value ); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
/** |
| 418 |
* gets a setting |
| 419 |
* |
| 420 |
* @param string key |
| 421 |
* @return mixed |
| 422 |
*/ |
| 423 |
public function getSetting( $key ) { |
| 424 |
if( !isset( $this->_settings[ $key ] ) ) { |
| 425 |
return null; |
| 426 |
} |
| 427 |
return $this->_settings[ $key ]; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
} |
| 432 |
|
| 433 |
} |
| 434 |
|