Skip to content

Commit

Permalink
Add a last chance write log line to STDERR if our stream has closed u…
Browse files Browse the repository at this point in the history
…nexpectedly
  • Loading branch information
lucasnetau committed Nov 27, 2024
1 parent bcc6a7c commit e916459
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/JsonRpcLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use function is_resource;
use function is_scalar;
use function is_string;
use function is_writable;
use function json_encode;
use function method_exists;
use function sprintf;
Expand Down Expand Up @@ -113,8 +114,13 @@ private function write(JsonRpcNotification $message) {
$this->stream->write($message);
} elseif ($this->stream instanceof WritableStreamInterface) {
$this->stream->write(json_encode($message) . "\n");
} elseif(is_resource($this->stream)) {
$bytes = @fwrite($this->stream, json_encode($message) . "\n");
if ($bytes === false) {
fwrite(STDERR, json_encode($message) . "\n");
}
} else {
fwrite($this->stream, json_encode($message) . "\n");
fwrite(STDERR, json_encode($message) . "\n");
}
}

Expand Down

0 comments on commit e916459

Please sign in to comment.