From 001e69daef223679ad2331e61e78f45aec590f0a Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Tue, 20 Jan 2015 21:46:23 +0100 Subject: One class per file: framework/Util --- framework/Util/TBrowserLogRoute.php | 179 ++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 framework/Util/TBrowserLogRoute.php (limited to 'framework/Util/TBrowserLogRoute.php') diff --git a/framework/Util/TBrowserLogRoute.php b/framework/Util/TBrowserLogRoute.php new file mode 100644 index 00000000..047b393f --- /dev/null +++ b/framework/Util/TBrowserLogRoute.php @@ -0,0 +1,179 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005-2014 PradoSoft + * @license http://www.pradosoft.com/license/ + * @package System.Util + */ + +/** + * TBrowserLogRoute class. + * + * TBrowserLogRoute prints selected log messages in the response. + * + * @author Xiang Wei Zhuo + * @package System.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 = << + + + Application Log + + +   + CategoryMessageTime Spent (s)Cumulated Time Spent (s) + +EOD; + } + else + { + $string = << + + + Application Log + + +   + CategoryMessageTime Spent (s)Cumulated Time Spent (s) + +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 = << +   + {$log[2]} + {$msg} + {$delta} + {$total} + +EOD; + } + else + { + $bgcolor = $info['even'] ? "#fff" : "#eee"; + $color = $this->getColorLevel($log[1]); + $string = << +   + {$log[2]} + {$msg} + {$delta} + {$total} + +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 .= ''; + foreach(self::$_levelValues as $name => $level) + { + $string .= ''.strtoupper($name).""; + } + } + else + { + $string .= ""; + foreach(self::$_levelValues as $name => $level) + { + $string .= "getColorLevel($level); + $string .= ";margin: 0.5em; padding:0.01em;\">".strtoupper($name).""; + } + } + $string .= ''; + return $string; + } +} \ No newline at end of file -- cgit v1.2.3