-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #616 from stof/location_aware_logger
Add an API for location-aware loggers
- Loading branch information
Showing
11 changed files
with
341 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/** | ||
* SCSSPHP | ||
* | ||
* @copyright 2012-2020 Leaf Corcoran | ||
* | ||
* @license http://opensource.org/licenses/MIT MIT | ||
* | ||
* @link http://scssphp.github.io/scssphp | ||
*/ | ||
|
||
namespace ScssPhp\ScssPhp\Logger; | ||
|
||
use ScssPhp\ScssPhp\SourceSpan\FileSpan; | ||
use ScssPhp\ScssPhp\StackTrace\Trace; | ||
use ScssPhp\ScssPhp\Util; | ||
use ScssPhp\ScssPhp\Util\Path; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class AdaptingLogger implements LocationAwareLoggerInterface | ||
{ | ||
/** | ||
* @var LoggerInterface | ||
* @readonly | ||
*/ | ||
private $logger; | ||
|
||
private function __construct(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
public static function adaptLogger(LoggerInterface $logger): LocationAwareLoggerInterface | ||
{ | ||
if ($logger instanceof LocationAwareLoggerInterface) { | ||
return $logger; | ||
} | ||
|
||
return new self($logger); | ||
} | ||
|
||
public function warn(string $message, bool $deprecation = false, ?FileSpan $span = null, ?Trace $trace = null) | ||
{ | ||
if ($span === null) { | ||
$formattedMessage = $message; | ||
} elseif ($trace !== null) { | ||
// If there's a span and a trace, the span's location information is | ||
// probably duplicated in the trace, so we just use it for highlighting. | ||
$formattedMessage = $message; | ||
// TODO implement the highlight of a span | ||
} else { | ||
$formattedMessage = ' on ' . $span->message("\n" . $message); | ||
} | ||
|
||
if ($trace !== null) { | ||
$formattedMessage .= "\n" . Util::indent(rtrim($trace->getFormattedTrace()), 4); | ||
} | ||
|
||
$this->logger->warn($formattedMessage, $deprecation); | ||
} | ||
|
||
public function debug(string $message, FileSpan $span = null) | ||
{ | ||
$location = ''; | ||
if ($span !== null) { | ||
$url = $span->getStart()->getSourceUrl() === null ? '-' : Path::prettyUri($span->getStart()->getSourceUrl()); | ||
$line = $span->getStart()->getLine() + 1; | ||
$location = "$url:$line "; | ||
} | ||
|
||
$this->logger->debug(sprintf("%sDEBUG: %s", $location, $message)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/** | ||
* SCSSPHP | ||
* | ||
* @copyright 2012-2020 Leaf Corcoran | ||
* | ||
* @license http://opensource.org/licenses/MIT MIT | ||
* | ||
* @link http://scssphp.github.io/scssphp | ||
*/ | ||
|
||
namespace ScssPhp\ScssPhp\Logger; | ||
|
||
use ScssPhp\ScssPhp\SourceSpan\FileSpan; | ||
use ScssPhp\ScssPhp\StackTrace\Trace; | ||
|
||
/** | ||
* Interface implemented by loggers for warnings and debug messages. | ||
* | ||
* The official Sass implementation recommends that loggers report the | ||
* messages immediately rather than waiting for the end of the | ||
* compilation, to provide a better debugging experience when the | ||
* compilation does not end (error or infinite loop after the warning | ||
* for instance). | ||
*/ | ||
interface LocationAwareLoggerInterface extends LoggerInterface | ||
{ | ||
/** | ||
* Emits a warning with the given message. | ||
* | ||
* If $span is passed, it's the location in the Sass source that generated | ||
* the warning. If $trace is passed, it's the Sass stack trace when the | ||
* warning was issued. | ||
* If $deprecation is true, it indicates that this is a deprecation | ||
* warning. Implementations should surface all this information to | ||
* the end user. | ||
* | ||
* @return void | ||
*/ | ||
public function warn(string $message, bool $deprecation = false, ?FileSpan $span = null, ?Trace $trace = null); | ||
|
||
/** | ||
* Emits a debugging message associated with the given span. | ||
* | ||
* @return void | ||
*/ | ||
public function debug(string $message, FileSpan $span = null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
/** | ||
* SCSSPHP | ||
* | ||
* @copyright 2012-2020 Leaf Corcoran | ||
* | ||
* @license http://opensource.org/licenses/MIT MIT | ||
* | ||
* @link http://scssphp.github.io/scssphp | ||
*/ | ||
|
||
namespace ScssPhp\ScssPhp\StackTrace; | ||
|
||
use ScssPhp\ScssPhp\Util\Path; | ||
|
||
/** | ||
* A single stack frame. Each frame points to a precise location in Sass code. | ||
*/ | ||
final class Frame | ||
{ | ||
/** | ||
* The URI of the file in which the code is located. | ||
* | ||
* @var string | ||
* @readonly | ||
*/ | ||
private $url; | ||
|
||
/** | ||
* The line number on which the code location is located. | ||
* | ||
* This can be null, indicating that the line number is unknown or | ||
* unimportant. | ||
* | ||
* @var int|null | ||
* @readonly | ||
*/ | ||
private $line; | ||
|
||
/** | ||
* The column number of the code location. | ||
* | ||
* This can be null, indicating that the column number is unknown or | ||
* unimportant. | ||
* | ||
* @var int|null | ||
* @readonly | ||
*/ | ||
private $column; | ||
|
||
/** | ||
* The name of the member in which the code location occurs. | ||
* | ||
* @var string|null | ||
* @readonly | ||
*/ | ||
private $member; | ||
|
||
public function __construct(string $url, ?int $line, ?int $column, ?string $member) | ||
{ | ||
$this->url = $url; | ||
$this->line = $line; | ||
$this->column = $column; | ||
$this->member = $member; | ||
} | ||
|
||
/** | ||
* The URI of the file in which the code is located. | ||
*/ | ||
public function getUrl(): string | ||
{ | ||
return $this->url; | ||
} | ||
|
||
/** | ||
* The line number on which the code location is located. | ||
* | ||
* This can be null, indicating that the line number is unknown or | ||
* unimportant. | ||
*/ | ||
public function getLine(): ?int | ||
{ | ||
return $this->line; | ||
} | ||
|
||
/** | ||
* The column number of the code location. | ||
* | ||
* This can be null, indicating that the column number is unknown or | ||
* unimportant. | ||
*/ | ||
public function getColumn(): ?int | ||
{ | ||
return $this->column; | ||
} | ||
|
||
/** | ||
* The name of the member in which the code location occurs. | ||
*/ | ||
public function getMember(): ?string | ||
{ | ||
return $this->member; | ||
} | ||
|
||
/** | ||
* A human-friendly description of the code location. | ||
*/ | ||
public function getLocation(): string | ||
{ | ||
$library = Path::prettyUri($this->url); | ||
|
||
if ($this->line === null) { | ||
return $library; | ||
} | ||
|
||
if ($this->column === null) { | ||
return $library . ' ' . $this->line; | ||
} | ||
|
||
return $library . ' ' . $this->line . ':' . $this->column; | ||
} | ||
} |
Oops, something went wrong.