| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfWebDebugLogger extends sfLogger |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$context = null, |
|---|
| 23 |
$dispatcher = null, |
|---|
| 24 |
$webDebug = null, |
|---|
| 25 |
$xdebugLogging = false; |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
* Initializes this logger. |
|---|
| 29 |
* |
|---|
| 30 |
* Available options: |
|---|
| 31 |
* |
|---|
| 32 |
* - web_debug_class: The web debug class (sfWebDebug by default). |
|---|
| 33 |
* - xdebugLogging: Whether to add xdebug trace to the logs (false by default). |
|---|
| 34 |
* |
|---|
| 35 |
* @param sfEventDispatcher $dispatcher A sfEventDispatcher instance |
|---|
| 36 |
* @param array $options An array of options. |
|---|
| 37 |
* |
|---|
| 38 |
* @return Boolean true, if initialization completes successfully, otherwise false. |
|---|
| 39 |
*/ |
|---|
| 40 |
public function initialize(sfEventDispatcher $dispatcher, $options = array()) |
|---|
| 41 |
{ |
|---|
| 42 |
$this->context = sfContext::getInstance(); |
|---|
| 43 |
$this->dispatcher = $dispatcher; |
|---|
| 44 |
|
|---|
| 45 |
$class = isset($options['web_debug_class']) ? $options['web_debug_class'] : 'sfWebDebug'; |
|---|
| 46 |
$this->webDebug = new $class($dispatcher); |
|---|
| 47 |
|
|---|
| 48 |
$dispatcher->connect('response.filter_content', array($this, 'filterResponseContent')); |
|---|
| 49 |
|
|---|
| 50 |
if (isset($options['xdebug_logging'])) |
|---|
| 51 |
{ |
|---|
| 52 |
$this->xdebugLogging = $options['xdebug_logging']; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
if (isset($_GET['XDEBUG_SESSION_START']) || isset($_COOKIE['XDEBUG_SESSION'])) |
|---|
| 57 |
{ |
|---|
| 58 |
$this->xdebugLogging = false; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
return parent::initialize($dispatcher, $options); |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
* Listens to the response.filter_content event. |
|---|
| 66 |
* |
|---|
| 67 |
* @param sfEvent $event The sfEvent instance |
|---|
| 68 |
* @param string $context The response content |
|---|
| 69 |
* |
|---|
| 70 |
* @return string The filtered response content |
|---|
| 71 |
*/ |
|---|
| 72 |
public function filterResponseContent(sfEvent $event, $content) |
|---|
| 73 |
{ |
|---|
| 74 |
if (!sfConfig::get('sf_web_debug')) |
|---|
| 75 |
{ |
|---|
| 76 |
return $content; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
$messages = array(); |
|---|
| 81 |
foreach (sfTimerManager::getTimers() as $name => $timer) |
|---|
| 82 |
{ |
|---|
| 83 |
$messages[] = sprintf('%s %.2f ms (%d)', $name, $timer->getElapsedTime() * 1000, $timer->getCalls()); |
|---|
| 84 |
} |
|---|
| 85 |
$this->dispatcher->notify(new sfEvent($this, 'application.log', $messages)); |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
// * for XHR requests |
|---|
| 89 |
// * if 304 |
|---|
| 90 |
// * if not rendering to the client |
|---|
| 91 |
// * if HTTP headers only |
|---|
| 92 |
$response = $event->getSubject(); |
|---|
| 93 |
if (!$this->context->has('request') || !$this->context->has('response') || !$this->context->has('controller') || |
|---|
| 94 |
$this->context->getRequest()->isXmlHttpRequest() || |
|---|
| 95 |
strpos($response->getContentType(), 'html') === false || |
|---|
| 96 |
'3' == substr($response->getStatusCode(), 0, 1) || |
|---|
| 97 |
$this->context->getController()->getRenderMode() != sfView::RENDER_CLIENT || |
|---|
| 98 |
$response->isHeaderOnly() |
|---|
| 99 |
) |
|---|
| 100 |
{ |
|---|
| 101 |
return $content; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
$root = $this->context->getRequest()->getRelativeUrlRoot(); |
|---|
| 106 |
$assets = sprintf(' |
|---|
| 107 |
<script type="text/javascript" src="%s"></script> |
|---|
| 108 |
<link rel="stylesheet" type="text/css" media="screen" href="%s" />', |
|---|
| 109 |
$root.sfConfig::get('sf_web_debug_web_dir').'/js/main.js', |
|---|
| 110 |
$root.sfConfig::get('sf_web_debug_web_dir').'/css/main.css' |
|---|
| 111 |
); |
|---|
| 112 |
$content = str_ireplace('</head>', $assets.'</head>', $content); |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
$webDebugContent = $this->webDebug->getResults(); |
|---|
| 116 |
$count = 0; |
|---|
| 117 |
$content = str_ireplace('</body>', $webDebugContent.'</body>', $content, $count); |
|---|
| 118 |
if (!$count) |
|---|
| 119 |
{ |
|---|
| 120 |
$content .= $webDebugContent; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
return $content; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
* Logs a message. |
|---|
| 128 |
* |
|---|
| 129 |
* @param string $message Message |
|---|
| 130 |
* @param string $priority Message priority |
|---|
| 131 |
*/ |
|---|
| 132 |
protected function doLog($message, $priority) |
|---|
| 133 |
{ |
|---|
| 134 |
|
|---|
| 135 |
$debugStack = array(); |
|---|
| 136 |
if ($this->xdebugLogging && function_exists('xdebug_get_function_stack')) |
|---|
| 137 |
{ |
|---|
| 138 |
foreach (xdebug_get_function_stack() as $i => $stack) |
|---|
| 139 |
{ |
|---|
| 140 |
if ( |
|---|
| 141 |
(isset($stack['function']) && !in_array($stack['function'], array('emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug', 'log'))) |
|---|
| 142 |
|| !isset($stack['function']) |
|---|
| 143 |
) |
|---|
| 144 |
{ |
|---|
| 145 |
$tmp = ''; |
|---|
| 146 |
if (isset($stack['function'])) |
|---|
| 147 |
{ |
|---|
| 148 |
$tmp .= sprintf('in "%s" ', $stack['function']); |
|---|
| 149 |
} |
|---|
| 150 |
$tmp .= sprintf('from "%s" line %s', $stack['file'], $stack['line']); |
|---|
| 151 |
$debugStack[] = $tmp; |
|---|
| 152 |
} |
|---|
| 153 |
} |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
$type = 'sfOther'; |
|---|
| 158 |
if (preg_match('/^\s*{([^}]+)}\s*(.+?)$/s', $message, $matches)) |
|---|
| 159 |
{ |
|---|
| 160 |
$type = $matches[1]; |
|---|
| 161 |
$message = $matches[2]; |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
$this->webDebug->log(array( |
|---|
| 166 |
'priority' => $priority, |
|---|
| 167 |
'time' => time(), |
|---|
| 168 |
'message' => $message, |
|---|
| 169 |
'type' => $type, |
|---|
| 170 |
'debugStack' => $debugStack, |
|---|
| 171 |
)); |
|---|
| 172 |
} |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|