Skip to content

Commit

Permalink
add coWrite(); use swoft/swoole-ide-helper (#83)
Browse files Browse the repository at this point in the history
* use swoft/swoole-ide-helper

* add coWrite()

* update
  • Loading branch information
daydaygo authored and huangzhhui committed May 29, 2018
1 parent da53c72 commit 01ef3d0
Show file tree
Hide file tree
Showing 20 changed files with 30 additions and 52 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"text/template": "~2.3"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7",
"friendsofphp/php-cs-fixer": "^2.10"
},
Expand Down
2 changes: 1 addition & 1 deletion src/console/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
}
],
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/db/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
]
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7",
"friendsofphp/php-cs-fixer": "^2.10"
},
Expand Down
2 changes: 1 addition & 1 deletion src/framework/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
]
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7",
"friendsofphp/php-cs-fixer": "^2.10"
},
Expand Down
44 changes: 11 additions & 33 deletions src/framework/src/Log/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Monolog\Handler\AbstractProcessingHandler;
use Swoft\App;
use Swoole\Coroutine;

/**
* 日志文件输出器
Expand Down Expand Up @@ -55,50 +56,27 @@ protected function write(array $records)
{
// 参数
$this->createDir();
$isTask = App::isWorkerStatus();
$logFile = App::getAlias($this->logFile);
$messageText = implode("\n", $records) . "\n";

// 同步写
if ($isTask === false) {
return $this->syncWrite($logFile, $messageText);
}
// 异步写
$this->aysncWrite($logFile, $messageText);
}

/**
* 同步写文件
*
* @param string $logFile 日志路径
* @param string $messageText 文本信息
*/
private function syncWrite(string $logFile, string $messageText)
{
$fp = fopen($logFile, 'a');
if ($fp === false) {
throw new \InvalidArgumentException("Unable to append to log file: {$this->logFile}");
}
flock($fp, LOCK_EX);
fwrite($fp, $messageText);
flock($fp, LOCK_UN);
fclose($fp);

// 协程写
$this->coWrite($logFile, $messageText);
}

/**
* 异步写文件
* 协程写文件
*
* @param string $logFile 日志路径
* @param string $messageText 文本信息
*/
private function aysncWrite(string $logFile, string $messageText)
private function coWrite(string $logFile, string $messageText)
{
while (true) {
$result = \Swoole\Async::writeFile($logFile, $messageText, null, FILE_APPEND);
if ($result == true) {
break;
go(function () use ($logFile, $messageText) {
$res = Coroutine::writeFile($logFile, $messageText, FILE_APPEND);
if ($res === false) {
throw new \InvalidArgumentException("Unable to append to log file: {$this->logFile}");
}
}
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/http-client/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"swoft/framework": "^1.0"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/http-message/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
}
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/http-server/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
}
],
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
}
],
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/memory/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7",
"friendsofphp/php-cs-fixer": "^2.10"
},
Expand Down
2 changes: 1 addition & 1 deletion src/process/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
}
],
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/queue/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/redis/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
],
"require-dev": {
"swoft/service-governance": "^1.0",
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/rpc-client/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"swoft/framework": "^1.0"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/rpc-server/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"swoft/framework": "^1.0"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/service-governance/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"swoft/framework": "^1.0"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/task/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"require-dev": {
"swoft/process": "^1.0",
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion src/trace/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7",
"friendsofphp/php-cs-fixer": "^2.10"
},
Expand Down
2 changes: 1 addition & 1 deletion src/view/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
],
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
"swoft/swoole-ide-helper": "dev-master",
"phpunit/phpunit": "^5.7"
},
"scripts": {
Expand Down

0 comments on commit 01ef3d0

Please sign in to comment.