|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TLogRouter, TLogRoute, TFileLogRoute, TEmailLogRoute class file |
|
4
|
|
|
* |
|
5
|
|
|
* @author Qiang Xue <[email protected]> |
|
6
|
|
|
* @link https://github.com/pradosoft/prado |
|
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Prado\Util; |
|
11
|
|
|
|
|
12
|
|
|
use Prado\Web\Javascripts\TJavaScript; |
|
13
|
|
|
use Prado\Web\UI\ActiveControls\TActivePageAdapter; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* TFirebugLogRoute class. |
|
17
|
|
|
* |
|
18
|
|
|
* TFirebugLogRoute prints selected log messages in the firebug log console. |
|
19
|
|
|
* |
|
20
|
|
|
* {@link http://www.getfirebug.com/ FireBug Website} |
|
21
|
|
|
* |
|
22
|
|
|
* @author Enrico Stahn <[email protected]>, Christophe Boulain <[email protected]> |
|
23
|
|
|
* @since 3.1.2 |
|
24
|
|
|
* @method \Prado\Web\Services\TPageService getService() |
|
25
|
|
|
*/ |
|
26
|
|
|
class TFirebugLogRoute extends TBrowserLogRoute |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Logs via Firebug. |
|
30
|
|
|
* @param array $logs list of log messages |
|
31
|
|
|
* @param bool $final is the final flush |
|
32
|
|
|
* @param array $meta the meta data for the logs. |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function processLogs(array $logs, bool $final, array $meta) |
|
35
|
|
|
{ |
|
36
|
|
|
$page = $this->getService()->getRequestedPage(); |
|
37
|
|
|
if (empty($logs) || $this->getApplication()->getMode() === \Prado\TApplicationMode::Performance) { |
|
38
|
|
|
return; |
|
39
|
|
|
} |
|
40
|
|
|
$even = false; |
|
41
|
|
|
|
|
42
|
|
|
$blocks = [['info', 'Tot Time', 'Time ', '[Level] [Category] [Message]']]; |
|
43
|
|
|
for ($i = 0, $n = count($logs); $i < $n; ++$i) { |
|
44
|
|
|
$logs[$i]['even'] = ($even = !$even); |
|
|
|
|
|
|
45
|
|
|
$blocks[] = $this->renderMessageCallback($logs[$i]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
try { |
|
49
|
|
|
$blocks = TJavaScript::jsonEncode($blocks); |
|
50
|
|
|
} catch (\Exception $e) { |
|
51
|
|
|
// strip everythin not 7bit ascii |
|
52
|
|
|
$blocks = preg_replace('/[^(\x20-\x7F)]*/', '', serialize($blocks)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// the response has already been flushed |
|
56
|
|
|
$response = $this->getApplication()->getResponse(); |
|
57
|
|
|
if ($page->getIsCallback()) { |
|
58
|
|
|
$content = $response->createHtmlWriter(); |
|
59
|
|
|
$content->getWriter()->setBoundary(TActivePageAdapter::CALLBACK_DEBUG_HEADER); |
|
60
|
|
|
$content->write($blocks); |
|
61
|
|
|
$response->write($content->flush()); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function renderHeader() |
|
66
|
|
|
{ |
|
67
|
|
|
$page = $this->getService()->getRequestedPage(); |
|
68
|
|
|
if ($page->getIsCallback()) { |
|
69
|
|
|
return |
|
70
|
|
|
<<<EOD |
|
71
|
|
|
|
|
72
|
|
|
<script> |
|
73
|
|
|
/*<![CDATA[*/ |
|
74
|
|
|
if (typeof(console) == 'object') |
|
75
|
|
|
{ |
|
76
|
|
|
var groupFunc = blocks.length < 10 ? 'group': 'groupCollapsed'; |
|
77
|
|
|
if(typeof log[groupFunc] === "function") |
|
78
|
|
|
log[groupFunc]("Callback logs ("+blocks.length+" entries)"); |
|
79
|
|
|
|
|
80
|
|
|
console.log ("[Tot Time] [Time ] [Level] [Category] [Message]"); |
|
81
|
|
|
|
|
82
|
|
|
EOD; |
|
83
|
|
|
} |
|
84
|
|
|
return ''; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function renderMessage($log, $meta) |
|
88
|
|
|
{ |
|
89
|
|
|
$logfunc = 'console.' . $this->getFirebugLoggingFunction($log[TLogger::LOG_LEVEL]); |
|
90
|
|
|
$total = sprintf('%0.6f', $log['total']); |
|
91
|
|
|
$delta = sprintf('%0.6f', $log['delta']); |
|
92
|
|
|
$msg = trim($this->formatLogMessage($log)); |
|
93
|
|
|
$msg = preg_replace('/\(line[^\)]+\)$/', '', $msg); //remove line number info |
|
94
|
|
|
$msg = "[{$total}] [{$delta}] " . $msg; // Add time spent and cumulated time spent |
|
95
|
|
|
$string = $logfunc . '(\'' . addslashes($msg) . '\');' . "\n"; |
|
96
|
|
|
|
|
97
|
|
|
return $string; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function renderMessageCallback($log) |
|
101
|
|
|
{ |
|
102
|
|
|
$logfunc = $this->getFirebugLoggingFunction($log[TLogger::LOG_LEVEL]); |
|
103
|
|
|
$total = sprintf('%0.6f', $log['total']); |
|
104
|
|
|
$delta = sprintf('%0.6f', $log['delta']); |
|
105
|
|
|
$msg = trim($this->formatLogMessage($log)); |
|
106
|
|
|
$msg = preg_replace('/\(line[^\)]+\)$/', '', $msg); //remove line number info |
|
107
|
|
|
|
|
108
|
|
|
return [$logfunc, $total, $delta, $msg]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
protected function renderFooter() |
|
112
|
|
|
{ |
|
113
|
|
|
$string = |
|
114
|
|
|
<<<EOD |
|
115
|
|
|
if(typeof console.groupEnd === "function") |
|
116
|
|
|
console.groupEnd(); |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
</script> |
|
120
|
|
|
|
|
121
|
|
|
EOD; |
|
122
|
|
|
|
|
123
|
|
|
return $string; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
protected function getFirebugLoggingFunction($level) |
|
127
|
|
|
{ |
|
128
|
|
|
switch ($level) { |
|
129
|
|
|
case TLogger::PROFILE: |
|
130
|
|
|
case TLogger::PROFILE_BEGIN: |
|
131
|
|
|
case TLogger::PROFILE_END: |
|
132
|
|
|
case TLogger::DEBUG: |
|
133
|
|
|
case TLogger::INFO: |
|
134
|
|
|
case TLogger::NOTICE: |
|
135
|
|
|
return 'info'; |
|
136
|
|
|
case TLogger::WARNING: |
|
137
|
|
|
return 'warn'; |
|
138
|
|
|
case TLogger::ERROR: |
|
139
|
|
|
case TLogger::ALERT: |
|
140
|
|
|
case TLogger::FATAL: |
|
141
|
|
|
return 'error'; |
|
142
|
|
|
default: |
|
143
|
|
|
return 'log'; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|