PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/external/tracy/src/Tracy/FireLogger.php

https://bitbucket.org/navigatecms/navigatecms
PHP | 178 lines | 123 code | 32 blank | 23 comment | 34 complexity | 34b0e072369af0d30afd4f0792f31f25 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-2.1, BSD-3-Clause, AGPL-3.0, Apache-2.0
  1. <?php
  2. /**
  3. * This file is part of the Tracy (https://tracy.nette.org)
  4. * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  5. */
  6. namespace Tracy;
  7. use Tracy;
  8. /**
  9. * FireLogger console logger.
  10. *
  11. * @see http://firelogger.binaryage.com
  12. */
  13. class FireLogger implements ILogger
  14. {
  15. /** @var int */
  16. public $maxDepth = 3;
  17. /** @var int */
  18. public $maxLength = 150;
  19. /** @var array */
  20. private $payload = array('logs' => array());
  21. /**
  22. * Sends message to FireLogger console.
  23. * @param mixed
  24. * @return bool was successful?
  25. */
  26. public function log($message, $priority = self::DEBUG)
  27. {
  28. if (!isset($_SERVER['HTTP_X_FIRELOGGER']) || headers_sent()) {
  29. return FALSE;
  30. }
  31. $item = array(
  32. 'name' => 'PHP',
  33. 'level' => $priority,
  34. 'order' => count($this->payload['logs']),
  35. 'time' => str_pad(number_format((microtime(TRUE) - Debugger::$time) * 1000, 1, '.', ' '), 8, '0', STR_PAD_LEFT) . ' ms',
  36. 'template' => '',
  37. 'message' => '',
  38. 'style' => 'background:#767ab6',
  39. );
  40. $args = func_get_args();
  41. if (isset($args[0]) && is_string($args[0])) {
  42. $item['template'] = array_shift($args);
  43. }
  44. if (isset($args[0]) && ($args[0] instanceof \Exception || $args[0] instanceof \Throwable)) {
  45. $e = array_shift($args);
  46. $trace = $e->getTrace();
  47. if (isset($trace[0]['class']) && $trace[0]['class'] === 'Tracy\Debugger'
  48. && ($trace[0]['function'] === 'shutdownHandler' || $trace[0]['function'] === 'errorHandler')
  49. ) {
  50. unset($trace[0]);
  51. }
  52. $file = str_replace(dirname(dirname(dirname($e->getFile()))), "\xE2\x80\xA6", $e->getFile());
  53. $item['template'] = ($e instanceof \ErrorException ? '' : Helpers::getClass($e) . ': ')
  54. . $e->getMessage() . ($e->getCode() ? ' #' . $e->getCode() : '') . ' in ' . $file . ':' . $e->getLine();
  55. $item['pathname'] = $e->getFile();
  56. $item['lineno'] = $e->getLine();
  57. } else {
  58. $trace = debug_backtrace();
  59. if (isset($trace[1]['class']) && $trace[1]['class'] === 'Tracy\Debugger'
  60. && ($trace[1]['function'] === 'fireLog')
  61. ) {
  62. unset($trace[0]);
  63. }
  64. foreach ($trace as $frame) {
  65. if (isset($frame['file']) && is_file($frame['file'])) {
  66. $item['pathname'] = $frame['file'];
  67. $item['lineno'] = $frame['line'];
  68. break;
  69. }
  70. }
  71. }
  72. $item['exc_info'] = array('', '', array());
  73. $item['exc_frames'] = array();
  74. foreach ($trace as $frame) {
  75. $frame += array('file' => NULL, 'line' => NULL, 'class' => NULL, 'type' => NULL, 'function' => NULL, 'object' => NULL, 'args' => NULL);
  76. $item['exc_info'][2][] = array($frame['file'], $frame['line'], "$frame[class]$frame[type]$frame[function]", $frame['object']);
  77. $item['exc_frames'][] = $frame['args'];
  78. }
  79. if (isset($args[0]) && in_array($args[0], array(self::DEBUG, self::INFO, self::WARNING, self::ERROR, self::CRITICAL), TRUE)) {
  80. $item['level'] = array_shift($args);
  81. }
  82. $item['args'] = $args;
  83. $this->payload['logs'][] = $this->jsonDump($item, -1);
  84. foreach (str_split(base64_encode(json_encode($this->payload)), 4990) as $k => $v) {
  85. header("FireLogger-de11e-$k:$v");
  86. }
  87. return TRUE;
  88. }
  89. /**
  90. * Dump implementation for JSON.
  91. * @param mixed variable to dump
  92. * @param int current recursion level
  93. * @return string
  94. */
  95. private function jsonDump(&$var, $level = 0)
  96. {
  97. if (is_bool($var) || is_null($var) || is_int($var) || is_float($var)) {
  98. return $var;
  99. } elseif (is_string($var)) {
  100. return Dumper::encodeString($var, $this->maxLength);
  101. } elseif (is_array($var)) {
  102. static $marker;
  103. if ($marker === NULL) {
  104. $marker = uniqid("\x00", TRUE);
  105. }
  106. if (isset($var[$marker])) {
  107. return "\xE2\x80\xA6RECURSION\xE2\x80\xA6";
  108. } elseif ($level < $this->maxDepth || !$this->maxDepth) {
  109. $var[$marker] = TRUE;
  110. $res = array();
  111. foreach ($var as $k => &$v) {
  112. if ($k !== $marker) {
  113. $res[$this->jsonDump($k)] = $this->jsonDump($v, $level + 1);
  114. }
  115. }
  116. unset($var[$marker]);
  117. return $res;
  118. } else {
  119. return " \xE2\x80\xA6 ";
  120. }
  121. } elseif (is_object($var)) {
  122. $arr = (array) $var;
  123. static $list = array();
  124. if (in_array($var, $list, TRUE)) {
  125. return "\xE2\x80\xA6RECURSION\xE2\x80\xA6";
  126. } elseif ($level < $this->maxDepth || !$this->maxDepth) {
  127. $list[] = $var;
  128. $res = array("\x00" => '(object) ' . Helpers::getClass($var));
  129. foreach ($arr as $k => &$v) {
  130. if (isset($k[0]) && $k[0] === "\x00") {
  131. $k = substr($k, strrpos($k, "\x00") + 1);
  132. }
  133. $res[$this->jsonDump($k)] = $this->jsonDump($v, $level + 1);
  134. }
  135. array_pop($list);
  136. return $res;
  137. } else {
  138. return " \xE2\x80\xA6 ";
  139. }
  140. } elseif (is_resource($var)) {
  141. return 'resource ' . get_resource_type($var);
  142. } else {
  143. return 'unknown type';
  144. }
  145. }
  146. }