-
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.
Refactor
unused_finder/logger
into standalone package (#100)
This is so it can be compiled separately. In the future, it may be referenced from other packages that want to do logging without creating dependency cyles. --------- Co-authored-by: Max Huang-Hobbs <[email protected]>
- Loading branch information
1 parent
9b4dcff
commit 8e5bf47
Showing
17 changed files
with
115 additions
and
65 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.
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
7 changes: 7 additions & 0 deletions
7
change/@good-fences-api-a7359cc9-6b5b-4355-a1c7-448c715e2197.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": "refactor logger from unused_finder into own package", | ||
"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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "logger" | ||
version = "0.2.0" | ||
authors = ["Maxwell Huang-Hobbs <[email protected]>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
crate-type = ["lib"] | ||
|
||
[dependencies] | ||
anyhow.workspace = true |
2 changes: 2 additions & 0 deletions
2
crates/unused_finder/src/logger.rs → crates/logger/src/lib.rs
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,13 @@ | ||
|
||
[package] | ||
name = "logger_console" | ||
version = "0.2.0" | ||
authors = ["Maxwell Huang-Hobbs <[email protected]>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
crate-type = ["lib"] | ||
|
||
[dependencies] | ||
logger = { version = "0.2.0", path = "../logger" } | ||
napi.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,49 @@ | ||
use std::sync::Arc; | ||
|
||
use napi::{ | ||
threadsafe_function::{ | ||
ErrorStrategy::{self}, | ||
ThreadSafeCallContext, ThreadsafeFunction, ThreadsafeFunctionCallMode, | ||
}, | ||
JsFunction, JsObject, Result, Status, | ||
}; | ||
|
||
use logger::Logger; | ||
|
||
#[derive(Clone)] | ||
pub struct ConsoleLogger { | ||
logfn: Arc<ThreadsafeFunction<String, ErrorStrategy::CalleeHandled>>, | ||
} | ||
|
||
impl ConsoleLogger { | ||
pub fn new(console: JsObject) -> Result<Self> { | ||
let logfn = console.get_named_property::<JsFunction>("log")?; | ||
Ok(Self { | ||
logfn: Arc::new(logfn.create_threadsafe_function( | ||
// allow queueing console responses? | ||
100, | ||
|ctx: ThreadSafeCallContext<String>| { | ||
let js_str = ctx.env.create_string(&ctx.value)?; | ||
// return as an argv array | ||
Ok(vec![js_str]) | ||
}, | ||
)?), | ||
}) | ||
} | ||
} | ||
|
||
impl Logger for ConsoleLogger { | ||
fn log(&self, message: impl Into<String>) { | ||
let message_string: String = message.into(); | ||
let status = self | ||
.logfn | ||
.call(Ok(message_string), ThreadsafeFunctionCallMode::Blocking); | ||
match status { | ||
Status::Ok => {} | ||
_ => { | ||
eprintln!(); | ||
panic!("Error calling console.log from Rust. Unexpected threadsafe function call mode {}", status); | ||
} | ||
} | ||
} | ||
} |
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
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