PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Nette/Diagnostics/FireLogger.php

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