Issues (1474)

framework/Util/TFirePhpLogRoute.php (1 issue)

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