Passed
Push — master ( d979ac...b22592 )
by Fabio
05:10
created

TFirePhpLogRoute::processLogs()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 23
nc 4
nop 3
dl 0
loc 36
ccs 0
cts 31
cp 0
crap 30
rs 9.2408
c 1
b 0
f 0
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
/**
13
 * TFirePhpLogRoute class.
14
 *
15
 * TFirePhpLogRoute prints log messages in the firebug log console via firephp.
16
 *
17
 * {@link http://www.getfirebug.com/ FireBug Website}
18
 * {@link http://www.firephp.org/ FirePHP Website}
19
 *
20
 * @author Yves Berkholz <godzilla80[at]gmx[dot]net>
21
 * @since 3.1.5
22
 */
23
class TFirePhpLogRoute extends TLogRoute implements IOutputLogRoute
24
{
25
	/**
26
	 * Default group label
27
	 */
28
	public const DEFAULT_LABEL = TLogRouter::class . '(TFirePhpLogRoute)';
29
30
	private $_groupLabel;
31
32
	/**
33
	 * Logs via FirePhp.
34
	 * @param array $logs list of log messages
35
	 * @param bool $final is the final flush
36
	 * @param array $meta the meta data for the logs.
37
	 */
38
	protected function processLogs(array $logs, bool $final, array $meta)
39
	{
40
		if (empty($logs) || $this->getApplication()->getMode() === 'Performance') {
41
			return;
42
		}
43
44
		if (headers_sent()) {
45
			echo '
46
				<div style="width:100%; background-color:darkred; color:#FFF; padding:2px">
47
					TFirePhpLogRoute.GroupLabel "<i>' . $this -> getGroupLabel() . '</i>" -
48
					Routing to FirePHP impossible, because headers already sent!
49
				</div>
50
			';
51
			$fallback = new TBrowserLogRoute();
52
			$fallback->processLogs($logs, $final, $meta);
53
			return;
54
		}
55
56
		$firephp = \FirePHP::getInstance(true);
57
		$firephp->setOptions(['useNativeJsonEncode' => false]);
58
		$firephp->group($this->getGroupLabel(), ['Collapsed' => true]);
59
		$firephp->log('Time,  Message');
60
61
		$startTime = $_SERVER["REQUEST_TIME_FLOAT"];
0 ignored issues
show
Unused Code introduced by
The assignment to $startTime is dead and can be removed.
Loading history...
62
		$c = count($logs);
63
		for ($i = 0, $n = $c; $i < $n; ++$i) {
64
			$message = $logs[$i][TLogger::LOG_MESSAGE];
65
			$level = $logs[$i][TLogger::LOG_LEVEL];
66
			$category = $logs[$i][TLogger::LOG_CATEGORY];
67
			$delta = $logs[$i]['delta'];
68
69
			$message = sPrintF('+%0.6f: %s', $delta, preg_replace('/\(line[^\)]+\)$/', '', $message));
70
			$firephp->fb($message, $category, self::translateLogLevel($level));
71
		}
72
		$firephp->log(sPrintF('%0.6f', $meta['total']), 'Cumulated Time');
73
		$firephp->groupEnd();
74
	}
75
76
	/**
77
	 * Translates a PRADO log level attribute into one understood by FirePHP for correct visualization
78
	 * @param string $level prado log level
79
	 * @return string FirePHP log level
80
	 */
81
	protected static function translateLogLevel($level)
82
	{
83
		switch ($level) {
84
			case TLogger::INFO:
85
				return \FirePHP::INFO;
86
			case TLogger::PROFILE:
87
			case TLogger::PROFILE_BEGIN:
88
			case TLogger::PROFILE_END:
89
			case TLogger::DEBUG:
90
			case TLogger::NOTICE:
91
				return \FirePHP::LOG;
92
			case TLogger::WARNING:
93
				return \FirePHP::WARN;
94
			case TLogger::ERROR:
95
			case TLogger::ALERT:
96
			case TLogger::FATAL:
97
				return \FirePHP::ERROR;
98
			default:
99
				return \FirePHP::LOG;
100
		}
101
	}
102
103
	/**
104
	 * @return string group label. Defaults to TFirePhpLogRoute::DEFAULT_LABEL
105
	 */
106
	public function getGroupLabel()
107
	{
108
		if ($this->_groupLabel === null) {
109
			$this->_groupLabel = self::DEFAULT_LABEL;
110
		}
111
112
		return $this->_groupLabel;
113
	}
114
115
	/**
116
	 * @param string $value group label.
117
	 * @return static The current object.
118
	 */
119
	public function setGroupLabel($value): static
120
	{
121
		$this->_groupLabel = $value;
122
		return $this;
123
	}
124
}
125