forked from FuelLabs/sway
-
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(forc): extract tracing utils from
forc-util
into dedicated…
… `forc-tracing` (FuelLabs#3108) closes FuelLabs#3107 For the motivation for this PR, refer to the issue linked above. This PR extracts all `tracing` related util functions from `forc-util` and puts them into a dedicated `forc-tracing` crate. There is a minor code change [here](https://github.com/FuelLabs/sway/pull/3108/files#diff-fb650e8292a5d6bc84d8a261393ff2cabe324b5162c40ee5cf23af57ff4dcf58R161-R164) because it is the only place that `forc-explore` uses `forc-util` (so we can eliminate the dependency entirely), but other than that this PR is mostly just moving code around and updating deps as required for crates dependent on the moved `tracing` utils. Co-authored-by: Joshua Batty <[email protected]>
- Loading branch information
1 parent
58db549
commit 642c4b0
Showing
31 changed files
with
209 additions
and
161 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
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
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,14 @@ | ||
[package] | ||
name = "forc-tracing" | ||
version = "0.27.0" | ||
authors = ["Fuel Labs <[email protected]>"] | ||
edition = "2021" | ||
homepage = "https://fuel.network/" | ||
license = "Apache-2.0" | ||
repository = "https://github.com/FuelLabs/sway" | ||
description = "Tracing utility shared between forc crates." | ||
|
||
[dependencies] | ||
ansi_term = "0.12" | ||
tracing = "0.1" | ||
tracing-subscriber = { version = "0.3", features = ["ansi", "env-filter", "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,140 @@ | ||
//! Utility items shared between forc crates. | ||
use ansi_term::Colour; | ||
use std::str; | ||
use std::{env, io}; | ||
use tracing::{Level, Metadata}; | ||
use tracing_subscriber::{ | ||
filter::{EnvFilter, LevelFilter}, | ||
fmt::MakeWriter, | ||
}; | ||
|
||
pub fn println_red(txt: &str) { | ||
println_std_out(txt, Colour::Red); | ||
} | ||
|
||
pub fn println_green(txt: &str) { | ||
println_std_out(txt, Colour::Green); | ||
} | ||
|
||
pub fn println_yellow_err(txt: &str) { | ||
println_std_err(txt, Colour::Yellow); | ||
} | ||
|
||
pub fn println_red_err(txt: &str) { | ||
println_std_err(txt, Colour::Red); | ||
} | ||
|
||
pub fn println_green_err(txt: &str) { | ||
println_std_err(txt, Colour::Green); | ||
} | ||
|
||
fn println_std_out(txt: &str, color: Colour) { | ||
tracing::info!("{}", color.paint(txt)); | ||
} | ||
|
||
fn println_std_err(txt: &str, color: Colour) { | ||
tracing::error!("{}", color.paint(txt)); | ||
} | ||
|
||
const LOG_FILTER: &str = "RUST_LOG"; | ||
|
||
// This allows us to write ERROR and WARN level logs to stderr and everything else to stdout. | ||
// https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/trait.MakeWriter.html | ||
struct StdioTracingWriter { | ||
writer_mode: TracingWriterMode, | ||
} | ||
|
||
impl<'a> MakeWriter<'a> for StdioTracingWriter { | ||
type Writer = Box<dyn io::Write>; | ||
|
||
fn make_writer(&'a self) -> Self::Writer { | ||
if self.writer_mode == TracingWriterMode::Stderr { | ||
Box::new(io::stderr()) | ||
} else { | ||
// We must have an implementation of `make_writer` that makes | ||
// a "default" writer without any configuring metadata. Let's | ||
// just return stdout in that case. | ||
Box::new(io::stdout()) | ||
} | ||
} | ||
|
||
fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { | ||
// Here's where we can implement our special behavior. We'll | ||
// check if the metadata's verbosity level is WARN or ERROR, | ||
// and return stderr in that case. | ||
if self.writer_mode == TracingWriterMode::Stderr | ||
|| (self.writer_mode == TracingWriterMode::Stdio && meta.level() <= &Level::WARN) | ||
{ | ||
return Box::new(io::stderr()); | ||
} | ||
|
||
// Otherwise, we'll return stdout. | ||
Box::new(io::stdout()) | ||
} | ||
} | ||
|
||
#[derive(PartialEq, Eq)] | ||
pub enum TracingWriterMode { | ||
/// Write ERROR and WARN to stderr and everything else to stdout. | ||
Stdio, | ||
/// Write everything to stdout. | ||
Stdout, | ||
/// Write everything to stderr. | ||
Stderr, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct TracingSubscriberOptions { | ||
pub verbosity: Option<u8>, | ||
pub silent: Option<bool>, | ||
pub log_level: Option<LevelFilter>, | ||
pub writer_mode: Option<TracingWriterMode>, | ||
} | ||
|
||
/// A subscriber built from default `tracing_subscriber::fmt::SubscriberBuilder` such that it would match directly using `println!` throughout the repo. | ||
/// | ||
/// `RUST_LOG` environment variable can be used to set different minimum level for the subscriber, default is `INFO`. | ||
pub fn init_tracing_subscriber(options: TracingSubscriberOptions) { | ||
let env_filter = match env::var_os(LOG_FILTER) { | ||
Some(_) => EnvFilter::try_from_default_env().expect("Invalid `RUST_LOG` provided"), | ||
None => EnvFilter::new("info"), | ||
}; | ||
|
||
let level_filter = options | ||
.log_level | ||
.or_else(|| { | ||
options.verbosity.and_then(|verbosity| { | ||
match verbosity { | ||
1 => Some(LevelFilter::DEBUG), // matches --verbose or -v | ||
2 => Some(LevelFilter::TRACE), // matches -vv | ||
_ => None, | ||
} | ||
}) | ||
}) | ||
.or_else(|| { | ||
options.silent.and_then(|silent| match silent { | ||
true => Some(LevelFilter::OFF), | ||
_ => None, | ||
}) | ||
}); | ||
|
||
let builder = tracing_subscriber::fmt::Subscriber::builder() | ||
.with_env_filter(env_filter) | ||
.with_ansi(true) | ||
.with_level(false) | ||
.with_file(false) | ||
.with_line_number(false) | ||
.without_time() | ||
.with_target(false) | ||
.with_writer(StdioTracingWriter { | ||
writer_mode: options.writer_mode.unwrap_or(TracingWriterMode::Stdio), | ||
}); | ||
|
||
// If log level, verbosity, or silent mode is set, it overrides the RUST_LOG setting | ||
if let Some(level_filter) = level_filter { | ||
builder.with_max_level(level_filter).init(); | ||
} else { | ||
builder.init(); | ||
} | ||
} |
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
Oops, something went wrong.