summaryrefslogtreecommitdiff
path: root/framework/Util/TBrowserLogRoute.php
blob: 154cab640882ec74fbf698d3cc183ed14c67649c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
 * TLogRouter, TLogRoute, TFileLogRoute, TEmailLogRoute class file
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2014 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @package Prado\Util
 */

namespace Prado\Util;
use Prado\TPropertyValue;
use Prado\Web\THttpUtility;

/**
 * TBrowserLogRoute class.
 *
 * TBrowserLogRoute prints selected log messages in the response.
 *
 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @package Prado\Util
 * @since 3.0
 */
class TBrowserLogRoute extends TLogRoute
{
	/**
	 * @var string css class for indentifying the table structure in the dom tree
	 */
	private $_cssClass=null;

	public function processLogs($logs)
	{
		if(empty($logs) || $this->getApplication()->getMode()==='Performance') return;
		$first = $logs[0][3];
		$even = true;
		$response = $this->getApplication()->getResponse();
		$response->write($this->renderHeader());
		for($i=0,$n=count($logs);$i<$n;++$i)
		{
			if ($i<$n-1)
			{
				$timing['delta'] = $logs[$i+1][3] - $logs[$i][3];
				$timing['total'] = $logs[$i+1][3] - $first;
			}
			else
			{
				$timing['delta'] = '?';
				$timing['total'] = $logs[$i][3] - $first;
			}
			$timing['even'] = !($even = !$even);
			$response->write($this->renderMessage($logs[$i],$timing));
		}
		$response->write($this->renderFooter());
	}

	/**
	 * @param string the css class of the control
	 */
	public function setCssClass($value)
	{
		$this->_cssClass = TPropertyValue::ensureString($value);
	}

	/**
	 * @return string the css class of the control
	 */
	public function getCssClass()
	{
		return TPropertyValue::ensureString($this->_cssClass);
	}

	protected function renderHeader()
	{
		$string = '';
		if($className=$this->getCssClass())
		{
			$string = <<<EOD
<table class="$className">
	<tr class="header">
		<th colspan="5">
			Application Log
		</th>
	</tr><tr class="description">
	    <th>&nbsp;</th>
		<th>Category</th><th>Message</th><th>Time Spent (s)</th><th>Cumulated Time Spent (s)</th>
	</tr>
EOD;
		}
		else
		{
			$string = <<<EOD
<table cellspacing="0" cellpadding="2" border="0" width="100%" style="table-layout:auto">
	<tr>
		<th style="background-color: black; color:white;" colspan="5">
			Application Log
		</th>
	</tr><tr style="background-color: #ccc; color:black">
	    <th style="width: 15px">&nbsp;</th>
		<th style="width: auto">Category</th><th style="width: auto">Message</th><th style="width: 120px">Time Spent (s)</th><th style="width: 150px">Cumulated Time Spent (s)</th>
	</tr>
EOD;
		}
		return $string;
	}

	protected function renderMessage($log, $info)
	{
		$string = '';
		$total = sprintf('%0.6f', $info['total']);
		$delta = sprintf('%0.6f', $info['delta']);
		$msg = preg_replace('/\(line[^\)]+\)$/','',$log[0]); //remove line number info
		$msg = THttpUtility::htmlEncode($msg);
		if($this->getCssClass())
		{
			$colorCssClass = $log[1];
			$messageCssClass = $info['even'] ? 'even' : 'odd';
			$string = <<<EOD
	<tr class="message level$colorCssClass $messageCssClass">
		<td class="code">&nbsp;</td>
		<td class="category">{$log[2]}</td>
		<td class="message">{$msg}</td>
		<td class="time">{$delta}</td>
		<td class="cumulatedtime">{$total}</td>
	</tr>
EOD;
		}
		else
		{
			$bgcolor = $info['even'] ? "#fff" : "#eee";
			$color = $this->getColorLevel($log[1]);
			$string = <<<EOD
	<tr style="background-color: {$bgcolor}; color:#000">
		<td style="border:1px solid silver;background-color: $color;">&nbsp;</td>
		<td>{$log[2]}</td>
		<td>{$msg}</td>
		<td style="text-align:center">{$delta}</td>
		<td style="text-align:center">{$total}</td>
	</tr>
EOD;
		}
		return $string;
	}

	protected function getColorLevel($level)
	{
		switch($level)
		{
			case TLogger::DEBUG: return 'green';
			case TLogger::INFO: return 'black';
			case TLogger::NOTICE: return '#3333FF';
			case TLogger::WARNING: return '#33FFFF';
			case TLogger::ERROR: return '#ff9933';
			case TLogger::ALERT: return '#ff00ff';
			case TLogger::FATAL: return 'red';
		}
		return '';
	}

	protected function renderFooter()
	{
		$string = '';
		if($this->getCssClass())
		{
			$string .= '<tr class="footer"><td colspan="5">';
			foreach(self::$_levelValues as $name => $level)
			{
				$string .= '<span class="level'.$level.'">'.strtoupper($name)."</span>";
			}
		}
		else
		{
			$string .= "<tr><td colspan=\"5\" style=\"text-align:center; background-color:black; border-top: 1px solid #ccc; padding:0.2em;\">";
			foreach(self::$_levelValues as $name => $level)
			{
				$string .= "<span style=\"color:white; border:1px solid white; background-color:".$this->getColorLevel($level);
				$string .= ";margin: 0.5em; padding:0.01em;\">".strtoupper($name)."</span>";
			}
		}
		$string .= '</td></tr></table>';
		return $string;
	}
}