Skip to content

Commit

Permalink
Merge pull request eqlabs#1227 from eqlabs/color_logs
Browse files Browse the repository at this point in the history
feat: suppport color output configuration
  • Loading branch information
Mirko-von-Leipzig authored Jul 20, 2023
2 parents 4cb2423 + 43c907b commit 31fd448
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- control log color output via `--color auto|always|never`

### Fixed

- system contract updates are not correctly stored
Expand Down
7 changes: 6 additions & 1 deletion crates/gateway-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ primitive-types = "0.12.1"
rand = { version = "0.8.5", optional = true }
reqwest = "0.11.13"
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["arbitrary_precision", "raw_value"] }
serde_json = { workspace = true, features = [
"arbitrary_precision",
"raw_value",
] }
serde_with = { workspace = true }
sha3 = "0.10"
stark_hash = { path = "../stark_hash" }
Expand All @@ -30,5 +33,7 @@ tokio = { workspace = true }
[dev-dependencies]
assert_matches = { workspace = true }
# Due to pathfinder_common::starkhash!() usage
fake = { version = "2.5.0", features = ["derive"] }
rand = { version = "0.8.5" }
starknet-gateway-test-fixtures = { path = "../gateway-test-fixtures" }
tokio = { workspace = true, features = ["macros", "test-util"] }
4 changes: 2 additions & 2 deletions crates/pathfinder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "pathfinder"
version = "0.6.7"
edition = "2021"
license = "MIT OR Apache-2.0"
rust-version = "1.65"
rust-version = "1.70"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
Expand Down Expand Up @@ -51,7 +51,7 @@ time = { version = "0.3.20", features = ["macros"] }
tokio = { workspace = true, features = ["process"] }
toml = "0.5.9"
tracing = { workspace = true }
tracing-subscriber = { version = "0.3.16", features = ["env-filter", "time"] }
tracing-subscriber = { version = "0.3.16", features = ["env-filter", "time", "ansi"] }
url = "2.3.1"
warp = "0.3.3"

Expand Down
31 changes: 31 additions & 0 deletions crates/pathfinder/src/bin/pathfinder/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,35 @@ Examples:
env = "PATHFINDER_HEAD_POLL_INTERVAL_SECONDS"
)]
poll_interval: std::num::NonZeroU64,

#[arg(
long = "color",
long_help = "This flag controls when to use colors in the output logs.",
default_value = "auto",
env = "PATHFINDER_COLOR",
value_name = "WHEN"
)]
color: Color,
}

#[derive(clap::ValueEnum, Debug, Clone, Copy, PartialEq)]
pub enum Color {
Auto,
Never,
Always,
}

impl Color {
/// Returns true if color should be enabled, either because the setting is [Color::Always],
/// or because it is [Color::Auto] and stdout is targetting a terminal.
pub fn is_color_enabled(&self) -> bool {
use std::io::IsTerminal;
match self {
Color::Auto => std::io::stdout().is_terminal(),
Color::Never => false,
Color::Always => true,
}
}
}

#[derive(clap::Args)]
Expand Down Expand Up @@ -288,6 +317,7 @@ pub struct Config {
pub sqlite_wal: JournalMode,
pub max_rpc_connections: std::num::NonZeroU32,
pub poll_interval: std::time::Duration,
pub color: Color,
}

pub struct WebSocket {
Expand Down Expand Up @@ -381,6 +411,7 @@ impl Config {
},
max_rpc_connections: cli.max_rpc_connections,
poll_interval: std::time::Duration::from_secs(cli.poll_interval.get()),
color: cli.color,
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions crates/pathfinder/src/bin/pathfinder/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ async fn main() -> anyhow::Result<()> {
std::env::set_var("RUST_LOG", "pathfinder=info");
}

setup_tracing();

let config = config::Config::parse();

setup_tracing(config.color);

info!(
// this is expected to be $(last_git_tag)-$(commits_since)-$(commit_hash)
version = VERGEN_GIT_DESCRIBE,
Expand Down Expand Up @@ -232,13 +232,14 @@ Hint: This is usually caused by exceeding the file descriptor limit of your syse
}

#[cfg(feature = "tokio-console")]
fn setup_tracing() {
fn setup_tracing(color: config::Color) {
use tracing_subscriber::prelude::*;

// EnvFilter isn't really a Filter, so this we need this ugly workaround for filtering with it.
// See https://github.com/tokio-rs/tracing/issues/1868 for more details.
let env_filter = Arc::new(tracing_subscriber::EnvFilter::from_default_env());
let fmt_layer = tracing_subscriber::fmt::layer()
.with_ansi(color.is_color_enabled())
.with_target(false)
.compact()
.with_filter(tracing_subscriber::filter::dynamic_filter_fn(
Expand All @@ -252,7 +253,7 @@ fn setup_tracing() {
}

#[cfg(not(feature = "tokio-console"))]
fn setup_tracing() {
fn setup_tracing(color: config::Color) {
use time::macros::format_description;

let time_fmt = format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]");
Expand All @@ -262,6 +263,7 @@ fn setup_tracing() {
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.with_target(false)
.with_timer(time_fmt)
.with_ansi(color.is_color_enabled())
.compact()
.init();
}
Expand Down

0 comments on commit 31fd448

Please sign in to comment.