内核更新,多余文件清理

This commit is contained in:
2016-07-01 18:04:46 +08:00
parent a595f3a13e
commit 0398e5bd80
31 changed files with 426 additions and 551 deletions

View File

@@ -9,7 +9,7 @@
// | Author: yangweijie <yangweijiester@gmail.com>
// +----------------------------------------------------------------------
namespace think\debug\trace;
namespace think\debug;
use think\Cache;
use think\Config;
@@ -34,7 +34,7 @@ class Console
}
/**
* 日志写入接口
* 调试输出接口
* @access public
* @param array $log 日志信息
* @return bool
@@ -42,9 +42,9 @@ class Console
public function output(array $log = [])
{
// 获取基本信息
$runtime = number_format(microtime(true), 8, '.', '') - START_TIME;
$runtime = number_format(microtime(true), 8, '.', '') - THINK_START_TIME;
$reqs = number_format(1 / $runtime, 2);
$mem = number_format((memory_get_usage() - START_MEM) / 1024, 2);
$mem = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
if (isset($_SERVER['HTTP_HOST'])) {
$uri = $_SERVER['SERVER_PROTOCOL'] . ' ' . $_SERVER['REQUEST_METHOD'] . ' : ' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
@@ -126,12 +126,12 @@ JS;
}
break;
case '错误':
$msg = str_replace(PHP_EOL, '\n', $m);
$msg = str_replace("\n", '\n', $m);
$style = 'color:#F4006B;font-size:14px;';
$line[] = "console.error(\"%c{$msg}\", \"{$style}\");";
break;
case 'sql':
$msg = str_replace(PHP_EOL, '\n', $m);
$msg = str_replace("\n", '\n', $m);
$style = "color:#009bb4;";
$line[] = "console.log(\"%c{$msg}\", \"{$style}\");";
break;

View File

@@ -9,7 +9,7 @@
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\debug\trace;
namespace think\debug;
use think\Cache;
use think\Config;
@@ -34,7 +34,7 @@ class Html
}
/**
* 日志写入接口
* 调试输出接口
* @access public
* @param array $log 日志信息
* @return bool
@@ -43,9 +43,9 @@ class Html
{
// 获取基本信息
$runtime = number_format(microtime(true), 8, '.', '') - START_TIME;
$runtime = number_format(microtime(true), 8, '.', '') - THINK_START_TIME;
$reqs = number_format(1 / $runtime, 2);
$mem = number_format((memory_get_usage() - START_MEM) / 1024, 2);
$mem = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
// 页面Trace信息
if (isset($_SERVER['HTTP_HOST'])) {

View File

@@ -0,0 +1,250 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: luofei614 <weibo.com/luofei614>
// +----------------------------------------------------------------------
namespace think\debug;
/**
* github: https://github.com/luofei614/SocketLog
* @author luofei614<weibo.com/luofei614>
*/
class Socket
{
public $port = 1116; //SocketLog 服务的http的端口号
protected $config = [
'enable' => true, //是否记录日志的开关
'host' => 'localhost',
//是否显示利于优化的参数,如果允许时间,消耗内存等
'optimize' => false,
'show_included_files' => false,
'error_handler' => false,
//日志强制记录到配置的client_id
'force_client_ids' => [],
//限制允许读取日志的client_id
'allow_client_ids' => [],
];
protected $css = [
'sql' => 'color:#009bb4;',
'sql_warn' => 'color:#009bb4;font-size:14px;',
'error_handler' => 'color:#f4006b;font-size:14px;',
'page' => 'color:#40e2ff;background:#171717;',
'big' => 'font-size:20px;color:red;',
];
protected $allowForceClientIds = []; //配置强制推送且被授权的client_id
/**
* 架构函数
* @param array $config 缓存参数
* @access public
*/
public function __construct(array $config = [])
{
if (!empty($config)) {
$this->config = array_merge($this->config, $config);
}
}
/**
* 调试输出接口
* @access public
* @param array $logs 日志信息
* @return bool
*/
public function output(array $logs = [])
{
if (!$this->check()) {
return false;
}
$runtime = number_format(microtime(true), 8, '.', '') - THINK_START_TIME;
$reqs = number_format(1 / number_format($runtime, 8), 2);
$time_str = " [运行时间:{$runtime}s][吞吐率:{$reqs}req/s]";
$memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);
$memory_str = " [内存消耗:{$memory_use}kb]";
$file_load = " [文件加载:" . count(get_included_files()) . "]";
if (isset($_SERVER['HTTP_HOST'])) {
$current_uri = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
} else {
$current_uri = "cmd:" . implode(' ', $_SERVER['argv']);
}
// 基本信息
$trace[] = [
'type' => 'group',
'msg' => $current_uri . $time_str . $memory_str . $file_load,
'css' => $this->css['page'],
];
foreach ($logs as $type => $val) {
$trace[] = [
'type' => 'groupCollapsed',
'msg' => '[ ' . $type . ' ]',
'css' => isset($this->css[$type]) ? $this->css[$type] : '',
];
foreach ($val as $msg) {
if (!is_string($msg)) {
$msg = var_export($msg, true);
}
$trace[] = [
'type' => 'log',
'msg' => $msg,
'css' => '',
];
}
$trace[] = [
'type' => 'groupEnd',
'msg' => '',
'css' => '',
];
}
if ($this->config['show_included_files']) {
$trace[] = [
'type' => 'groupCollapsed',
'msg' => 'included_files',
'css' => '',
];
$trace[] = [
'type' => 'log',
'msg' => implode("\n", get_included_files()),
'css' => '',
];
$trace[] = [
'type' => 'groupEnd',
'msg' => '',
'css' => '',
];
}
$trace[] = [
'type' => 'groupEnd',
'msg' => '',
'css' => '',
];
$tabid = $this->getClientArg('tabid');
if (!$client_id = $this->getClientArg('client_id')) {
$client_id = '';
}
if (!empty($this->allowForceClientIds)) {
//强制推送到多个client_id
foreach ($this->allowForceClientIds as $force_client_id) {
$client_id = $force_client_id;
$this->sendToClient($tabid, $client_id, $trace, $force_client_id);
}
} else {
$this->sendToClient($tabid, $client_id, $trace, '');
}
return true;
}
/**
* 发送给指定客户端
* @author Zjmainstay
* @param $tabid
* @param $client_id
* @param $logs
* @param $force_client_id
*/
protected function sendToClient($tabid, $client_id, $logs, $force_client_id)
{
$logs = [
'tabid' => $tabid,
'client_id' => $client_id,
'logs' => $logs,
'force_client_id' => $force_client_id,
];
$msg = @json_encode($logs);
$address = '/' . $client_id; //将client_id作为地址 server端通过地址判断将日志发布给谁
$this->send($this->config['host'], $msg, $address);
}
protected function check()
{
if (!$this->config['enable']) {
return false;
}
$tabid = $this->getClientArg('tabid');
//是否记录日志的检查
if (!$tabid && !$this->config['force_client_ids']) {
return false;
}
//用户认证
$allow_client_ids = $this->config['allow_client_ids'];
if (!empty($allow_client_ids)) {
//通过数组交集得出授权强制推送的client_id
$this->allowForceClientIds = array_intersect($allow_client_ids, $this->config['force_client_ids']);
if (!$tabid && count($this->allowForceClientIds)) {
return true;
}
$client_id = $this->getClientArg('client_id');
if (!in_array($client_id, $allow_client_ids)) {
return false;
}
} else {
$this->allowForceClientIds = $this->config['force_client_ids'];
}
return true;
}
protected function getClientArg($name)
{
static $args = [];
$key = 'HTTP_USER_AGENT';
if (isset($_SERVER['HTTP_SOCKETLOG'])) {
$key = 'HTTP_SOCKETLOG';
}
if (!isset($_SERVER[$key])) {
return null;
}
if (empty($args)) {
if (!preg_match('/SocketLog\((.*?)\)/', $_SERVER[$key], $match)) {
$args = ['tabid' => null];
return null;
}
parse_str($match[1], $args);
}
if (isset($args[$name])) {
return $args[$name];
}
return null;
}
/**
* @param null $host - $host of socket server
* @param string $message - 发送的消息
* @param string $address - 地址
* @return bool
*/
protected function send($host, $message = '', $address = '/')
{
$url = 'http://' . $host . ':' . $this->port . $address;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$headers = [
"Content-Type: application/json;charset=UTF-8",
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置header
return curl_exec($ch);
}
}

View File

@@ -1,63 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\debug;
use think\Config;
use think\exception\ClassNotFoundException;
use think\Log;
use think\Request;
use think\Response;
use think\response\Redirect;
class Trace
{
public static function inject(Response $response)
{
$config = Config::get('trace');
$type = isset($config['type']) ? $config['type'] : 'Html';
if ($type !== false) {
$request = Request::instance();
$accept = $request->header('accept');
$contentType = $response->getHeader('Content-Type');
$class = false !== strpos($type, '\\') ? $type : '\\think\\debug\\trace\\' . ucwords($type);
unset($config['type']);
if(class_exists($class)) {
$trace = new $class($config);
} else {
throw new ClassNotFoundException('class not exists:' . $class, $class);
}
if ($response instanceof Redirect) {
//TODO 记录
} elseif (strpos($accept, 'application/json') === 0 || $request->isAjax()) {
//TODO 记录
} elseif (!empty($contentType) && strpos($contentType, 'html') === false) {
//TODO 记录
} else {
$output = $trace->output(Log::getLog());
$content = $response->getContent();
$pos = strripos($content, '</body>');
if (false !== $pos) {
$content = substr($content, 0, $pos) . $output . substr($content, $pos);
} else {
$content = $content . $output;
}
$response->content($content);
}
}
}
}