From f01b2cd8b77f325ce89ca70887605355e0c57464 Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Wed, 29 Jun 2016 17:35:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Btrace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base.php | 7 +- convention.php | 7 ++ library/think/App.php | 18 ++++-- library/think/Hook.php | 5 +- library/think/Log.php | 50 ++++++--------- library/think/Response.php | 64 +++++++++++++++---- library/think/debug/Trace.php | 47 ++++++++++++++ .../{log/driver => debug/trace}/Browser.php | 22 +++---- .../driver/Trace.php => debug/trace/Html.php} | 18 ++---- library/think/log/alarm/Email.php | 41 ------------ library/think/log/driver/File.php | 5 +- library/think/log/driver/Sae.php | 5 +- library/think/log/driver/Socket.php | 3 +- 13 files changed, 163 insertions(+), 129 deletions(-) create mode 100644 library/think/debug/Trace.php rename library/think/{log/driver => debug/trace}/Browser.php (89%) rename library/think/{log/driver/Trace.php => debug/trace/Html.php} (86%) delete mode 100644 library/think/log/alarm/Email.php diff --git a/base.php b/base.php index 7ab5a9ab..9bbf8ff2 100644 --- a/base.php +++ b/base.php @@ -8,9 +8,14 @@ // +---------------------------------------------------------------------- // | Author: liu21st // +---------------------------------------------------------------------- +function microtime_float() +{ + list ($usec, $sec) = explode(" ", microtime()); + return (( float )$usec + ( float )$sec); +} define('THINK_VERSION', '5.0.0 RC3'); -define('START_TIME', microtime(true)); +define('START_TIME', microtime_float()); define('START_MEM', memory_get_usage()); define('EXT', '.php'); define('DS', DIRECTORY_SEPARATOR); diff --git a/convention.php b/convention.php index eca9e883..ac6a2ce1 100644 --- a/convention.php +++ b/convention.php @@ -146,6 +146,13 @@ return [ 'path' => LOG_PATH, ], + // +---------------------------------------------------------------------- + // | Trace设置 + // +---------------------------------------------------------------------- + 'trace' => [ + 'type' => 'Html' + ], + // +---------------------------------------------------------------------- // | 缓存设置 // +---------------------------------------------------------------------- diff --git a/library/think/App.php b/library/think/App.php index f03c31f8..53118d69 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -12,6 +12,7 @@ namespace think; use think\Config; +use think\debug\Trace; use think\Exception; use think\exception\HttpException; use think\exception\HttpResponseException; @@ -70,7 +71,7 @@ class App * 执行应用程序 * @access public * @param Request $request Request对象 - * @return mixed + * @return Response * @throws Exception */ public static function run(Request $request = null) @@ -136,22 +137,27 @@ class App $data = $exception->getResponse(); } - // 监听app_end - Hook::listen('app_end', $data); // 清空类的实例化 Loader::clearInstance(); // 输出数据到客户端 if ($data instanceof Response) { - return $data; + $response = $data; } elseif (!is_null($data)) { // 默认自动识别响应输出类型 $isAjax = $request->isAjax(); $type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type'); - return Response::create($data, $type); + $response = Response::create($data, $type); } else { - return Response::create(); + $response = Response::create(); } + + self::$debug && Trace::inject($response); + + // 监听app_end + Hook::listen('app_end', $response); + + return $response; } /** diff --git a/library/think/Hook.php b/library/think/Hook.php index e9127bdd..4f863d63 100644 --- a/library/think/Hook.php +++ b/library/think/Hook.php @@ -43,9 +43,8 @@ class Hook /** * 批量导入插件 - * @param array $data 插件信息 - * @param boolean $recursive 是否递归合并 - * @return void + * @param array $tags 插件信息 + * @param boolean $recursive 是否递归合并 */ public static function import($tags, $recursive = true) { diff --git a/library/think/Log.php b/library/think/Log.php index 263dc7db..36388424 100644 --- a/library/think/Log.php +++ b/library/think/Log.php @@ -11,8 +11,17 @@ namespace think; -use think\App; - +/** + * Class Log + * @package think + * + * @method void log($msg) static + * @method void error($msg) static + * @method void info($msg) static + * @method void sql($msg) static + * @method void notice($msg) static + * @method void alert($msg) static + */ class Log { const LOG = 'log'; @@ -30,16 +39,15 @@ class Log protected static $type = ['log', 'error', 'info', 'sql', 'notice', 'alert']; // 日志写入驱动 protected static $driver; - // 通知发送驱动 - protected static $alarm; + // 当前日志授权key protected static $key; /** * 日志初始化 - * @return void + * @param array $config */ - public static function init($config = []) + public static function init($config = []) { $type = isset($config['type']) ? $config['type'] : 'File'; $class = false !== strpos($type, '\\') ? $type : '\\think\\log\\driver\\' . ucwords($type); @@ -49,21 +57,7 @@ class Log // 记录初始化信息 App::$debug && Log::record('[ LOG ] INIT ' . $type . ': ' . var_export($config, true), 'info'); } - - /** - * 通知初始化 - * @return void - */ - public static function alarm($config = []) - { - $type = isset($config['type']) ? $config['type'] : 'Email'; - $class = false !== strpos($type, '\\') ? $type : '\\think\\log\\alarm\\' . ucwords($type); - unset($config['type']); - self::$alarm = new $class($config['alarm']); - // 记录初始化信息 - App::$debug && Log::record('[ CACHE ] ALARM ' . $type . ': ' . var_export($config, true), 'info'); - } - + /** * 获取日志信息 * @param string $type 信息类型 @@ -164,19 +158,11 @@ class Log return self::$driver->save($log); } - /** - * 发送预警通知 - * @param mixed $msg 调试信息 - * @return void - */ - public static function send($msg) - { - self::$alarm && self::$alarm->send($msg); - } - /** * 静态调用 - * @return void + * @param $method + * @param $args + * @return mixed */ public static function __callStatic($method, $args) { diff --git a/library/think/Response.php b/library/think/Response.php index 2ba83741..74c5a6c9 100644 --- a/library/think/Response.php +++ b/library/think/Response.php @@ -93,9 +93,6 @@ class Response // 处理输出数据 $data = $this->getContent(); - // 监听response_data - Hook::listen('response_data', $data, $this); - if (!headers_sent() && !empty($this->header)) { // 发送状态码 http_response_code($this->code); @@ -165,6 +162,26 @@ class Response return $this; } + /** + * 设置页面输出内容 + * @param $content + * @return $this + */ + public function content($content) + { + if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([ + $content, + '__toString', + ]) + ) { + throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content))); + } + + $this->content = (string)$content; + + return $this; + } + /** * 发送HTTP状态 * @param integer $code 状态码 @@ -228,10 +245,30 @@ class Response */ public function contentType($contentType, $charset = 'utf-8') { + $this->contentType = $contentType; + $this->charset = $charset; + $this->header['Content-Type'] = $contentType . '; charset=' . $charset; return $this; } + /** + * 获取页面输出类型 + * @return string + */ + public function getContentType() + { + return $this->contentType; + } + + /** + * 获取页面输出字符集 + */ + public function getCharset() + { + return $this->charset; + } + /** * 获取头部信息 * @param string $name 头部名称 @@ -257,17 +294,20 @@ class Response */ public function getContent() { - $content = $this->output($this->data); + if ($this->content == null) { + $content = $this->output($this->data); - if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([ - $content, - '__toString', - ]) - ) { - throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content))); + if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([ + $content, + '__toString', + ]) + ) { + throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content))); + } + + $this->content = (string)$content; } - - return (string) $content; + return $this->content; } /** diff --git a/library/think/debug/Trace.php b/library/think/debug/Trace.php new file mode 100644 index 00000000..163f62ab --- /dev/null +++ b/library/think/debug/Trace.php @@ -0,0 +1,47 @@ + +// +---------------------------------------------------------------------- +namespace think\debug; + +use think\Config; +use think\Log; +use think\Request; +use think\Response; + +class Trace +{ + public static function inject(Response $response) + { + $config = Config::get('trace'); + + $type = isset($config['type']) ? $config['type'] : 'Html'; + + if ($type !== false && !Request::instance()->isAjax() && $response->getContentType() == 'text/html') { + + $class = false !== strpos($type, '\\') ? $type : '\\think\\debug\\trace\\' . ucwords($type); + + unset($config['type']); + $trace = new $class($config); + + $output = $trace->output(Log::getLog()); + + $content = $response->getContent(); + + $pos = strripos($content, ''); + if (false !== $pos) { + $content = substr($content, 0, $pos) . $output . substr($content, $pos); + } else { + $content = $content . $output; + } + + $response->content($content); + } + } +} \ No newline at end of file diff --git a/library/think/log/driver/Browser.php b/library/think/debug/trace/Browser.php similarity index 89% rename from library/think/log/driver/Browser.php rename to library/think/debug/trace/Browser.php index 12a428a0..7e373154 100644 --- a/library/think/log/driver/Browser.php +++ b/library/think/debug/trace/Browser.php @@ -9,7 +9,7 @@ // | Author: yangweijie // +---------------------------------------------------------------------- -namespace think\log\driver; +namespace think\debug\trace; use think\Cache; use think\Config; @@ -40,16 +40,11 @@ class Browser * @param array $log 日志信息 * @return bool */ - public function save(array $log = []) + public function output(array $log = []) { - if (IS_CLI || IS_API || Request::instance()->isAjax() || (defined('RESPONSE_TYPE') && !in_array(RESPONSE_TYPE, ['html', 'view']))) { - // ajax cli api方式下不输出 - return false; - } // 获取基本信息 - $runtime = microtime(true) - START_TIME; - $reqs = number_format(1 / number_format($runtime, 8), 2); - $runtime = number_format($runtime, 6); + $runtime = microtime_float() - START_TIME; + $reqs = number_format(1 / $runtime, 2); $mem = number_format((memory_get_usage() - START_MEM) / 1024, 2); // 页面Trace信息 @@ -96,19 +91,18 @@ class Browser //输出到控制台 $lines = ''; foreach ($trace as $type => $msg) { - $lines .= $this->output($type, $msg); + $lines .= $this->console($type, $msg); } $js = << + JS; - echo $js; - return true; + return $js; } - public function output($type, $msg) + protected function console($type, $msg) { $type = strtolower($type); $trace_tabs = array_values($this->config['trace_tabs']); diff --git a/library/think/log/driver/Trace.php b/library/think/debug/trace/Html.php similarity index 86% rename from library/think/log/driver/Trace.php rename to library/think/debug/trace/Html.php index 8b121e76..518f0bc5 100644 --- a/library/think/log/driver/Trace.php +++ b/library/think/debug/trace/Html.php @@ -9,7 +9,7 @@ // | Author: liu21st // +---------------------------------------------------------------------- -namespace think\log\driver; +namespace think\debug\trace; use think\Cache; use think\Config; @@ -20,7 +20,7 @@ use think\Request; /** * 页面Trace调试 */ -class Trace +class Html { protected $config = [ 'trace_file' => '', @@ -40,17 +40,12 @@ class Trace * @param array $log 日志信息 * @return bool */ - public function save(array $log = []) + public function output(array $log = []) { - if (IS_CLI || IS_API || Request::instance()->isAjax() || (defined('RESPONSE_TYPE') && !in_array(RESPONSE_TYPE, ['html', 'view']))) { - // ajax cli api方式下不输出 - return false; - } // 获取基本信息 - $runtime = microtime(true) - START_TIME; - $reqs = number_format(1 / number_format($runtime, 8), 2); - $runtime = number_format($runtime, 6); + $runtime = microtime_float() - START_TIME; + $reqs = number_format(1 / $runtime, 2); $mem = number_format((memory_get_usage() - START_MEM) / 1024, 2); // 页面Trace信息 @@ -96,8 +91,7 @@ class Trace // 调用Trace页面模板 ob_start(); include $this->config['trace_file']; - echo ob_get_clean(); - return true; + return ob_get_clean(); } } diff --git a/library/think/log/alarm/Email.php b/library/think/log/alarm/Email.php deleted file mode 100644 index 0a1ac7dd..00000000 --- a/library/think/log/alarm/Email.php +++ /dev/null @@ -1,41 +0,0 @@ - -// +---------------------------------------------------------------------- - -namespace think\log\alarm; - -/** - * 邮件通知驱动 - */ -class Email -{ - - protected $config = [ - 'address' => '', - ]; - - // 实例化并传入参数 - public function __construct($config = []) - { - $this->config = array_merge($this->config, $config); - } - - /** - * 通知发送接口 - * @access public - * @param string $msg 日志信息 - * @return bool - */ - public function send($msg = '') - { - return error_log($msg, 1, $this->config['address']); - } - -} diff --git a/library/think/log/driver/File.php b/library/think/log/driver/File.php index 81053045..78d5dc30 100644 --- a/library/think/log/driver/File.php +++ b/library/think/log/driver/File.php @@ -54,9 +54,8 @@ class File } else { $current_uri = "cmd:" . implode(' ', $_SERVER['argv']); } - $runtime = microtime(true) - START_TIME; - $reqs = number_format(1 / number_format($runtime, 8), 2); - $runtime = number_format($runtime, 6); + $runtime = microtime_float() - START_TIME; + $reqs = number_format(1 / $runtime, 2); $time_str = " [运行时间:{$runtime}s] [吞吐率:{$reqs}req/s]"; $memory_use = number_format((memory_get_usage() - START_MEM) / 1024, 2); $memory_str = " [内存消耗:{$memory_use}kb]"; diff --git a/library/think/log/driver/Sae.php b/library/think/log/driver/Sae.php index d9ca44fe..17a41594 100644 --- a/library/think/log/driver/Sae.php +++ b/library/think/log/driver/Sae.php @@ -33,9 +33,8 @@ class Sae } else { $current_uri = "cmd:" . implode(' ', $_SERVER['argv']); } - $runtime = microtime(true) - START_TIME; - $reqs = number_format(1 / number_format($runtime, 8), 2); - $runtime = number_format($runtime, 6); + $runtime = microtime_float() - START_TIME; + $reqs = number_format(1 / $runtime, 2); $time_str = " [运行时间:{$runtime}s] [吞吐率:{$reqs}req/s]"; $memory_use = number_format((memory_get_usage() - START_MEM) / 1024, 2); $memory_str = " [内存消耗:{$memory_use}kb]"; diff --git a/library/think/log/driver/Socket.php b/library/think/log/driver/Socket.php index a6a666f4..5336853d 100644 --- a/library/think/log/driver/Socket.php +++ b/library/think/log/driver/Socket.php @@ -65,9 +65,8 @@ class Socket if (!$this->check()) { return false; } - $runtime = microtime(true) - START_TIME; + $runtime = microtime_float() - START_TIME; $reqs = number_format(1 / number_format($runtime, 8), 2); - $runtime = number_format($runtime, 6); $time_str = " [运行时间:{$runtime}s][吞吐率:{$reqs}req/s]"; $memory_use = number_format((memory_get_usage() - START_MEM) / 1024, 2); $memory_str = " [内存消耗:{$memory_use}kb]";