-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructure logger to enable
logger_srcfile
(#104)
The overall goal of this change is to support a "sub-trait" of `logger` that adds some src-file logging methods associated against a given logging. This will be used to trace issues with src files while e.g. parsing or interpreting the structure of source files. As part of this change, the `Sync` requirement of logger had to be dropped. This is because the src file informatio nwe get from `swc` is necessarially non-`Sync`/`Send`. --------- Co-authored-by: Max Huang-Hobbs <[email protected]>
- Loading branch information
1 parent
e3a2a58
commit 29cc6f5
Showing
8 changed files
with
129 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
change/@good-fences-api-6ffba5db-a75b-4958-8cba-3e2dea22fa8b.json
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,7 @@ | ||
{ | ||
"type": "none", | ||
"comment": "internal refactoring of loggers", | ||
"packageName": "@good-fences/api", | ||
"email": "[email protected]", | ||
"dependentChangeType": "none" | ||
} |
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,12 @@ | ||
[package] | ||
name = "logger_srcfile" | ||
version = "0.2.0" | ||
authors = ["Maxwell Huang-Hobbs <[email protected]>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
crate-type = ["lib"] | ||
|
||
[dependencies] | ||
logger = { path = "../logger" } | ||
swc_common.workspace = true |
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,81 @@ | ||
use logger::Logger; | ||
use swc_common::{Loc, SourceMap, Span}; | ||
|
||
pub trait SrcLogger: Logger { | ||
fn src_warn(&self, loc: &Loc, message: impl Into<String>) { | ||
self.warn(format!( | ||
"{}:{}:{} :: {}", | ||
loc.file.name, | ||
loc.line, | ||
loc.col_display, | ||
message.into() | ||
)); | ||
} | ||
fn src_error(&self, loc: &Loc, message: impl Into<String>) { | ||
self.error(format!( | ||
"{}:{}:{} :: {}", | ||
loc.file.name, | ||
loc.line, | ||
loc.col_display, | ||
message.into() | ||
)); | ||
} | ||
} | ||
|
||
pub trait HasSourceMap { | ||
fn source_map(&self) -> &SourceMap; | ||
} | ||
|
||
pub trait SrcFileLogger: Logger + HasSourceMap { | ||
fn src_warn(&self, location: &Span, message: impl Into<String>) { | ||
let loc = self.source_map().lookup_char_pos(location.lo); | ||
self.warn(format!( | ||
"{}:{}:{} :: {}", | ||
loc.file.name, | ||
loc.line, | ||
loc.col_display, | ||
message.into() | ||
)); | ||
} | ||
fn src_error(&self, location: &Span, message: impl Into<String>) { | ||
let loc = self.source_map().lookup_char_pos(location.lo); | ||
self.error(format!( | ||
"{}:{}:{} :: {}", | ||
loc.file.name, | ||
loc.line, | ||
loc.col_display, | ||
message.into() | ||
)); | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct WrapFileLogger<'a, TLogger: Logger> { | ||
source_map: &'a SourceMap, | ||
inner_logger: TLogger, | ||
} | ||
impl<'a, TLogger: Logger> WrapFileLogger<'a, TLogger> { | ||
pub fn new(source_map: &'a SourceMap, inner_logger: TLogger) -> Self { | ||
Self { | ||
source_map, | ||
inner_logger, | ||
} | ||
} | ||
} | ||
impl<TLogger: Logger> Logger for WrapFileLogger<'_, TLogger> { | ||
fn log(&self, message: impl Into<String>) { | ||
self.inner_logger.log(message); | ||
} | ||
fn error(&self, message: impl Into<String>) { | ||
self.inner_logger.error(message); | ||
} | ||
fn warn(&self, message: impl Into<String>) { | ||
self.inner_logger.warn(message); | ||
} | ||
} | ||
impl<TLogger: Logger> HasSourceMap for WrapFileLogger<'_, TLogger> { | ||
fn source_map(&self) -> &SourceMap { | ||
self.source_map | ||
} | ||
} | ||
impl<TLogger: Logger> SrcFileLogger for WrapFileLogger<'_, TLogger> {} |
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