Skip to content

Commit

Permalink
Draft remove all init_logger part to use a common way for all crates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Douceur committed Nov 22, 2024
1 parent 95bfe78 commit 7bb1fa2
Show file tree
Hide file tree
Showing 23 changed files with 82 additions and 138 deletions.
17 changes: 13 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions crates/subspace-farmer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ subspace-erasure-coding = { version = "0.1.0", path = "../subspace-erasure-codin
subspace-farmer-components = { version = "0.1.0", path = "../subspace-farmer-components" }
subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primitives" }
subspace-kzg = { version = "0.1.0", path = "../../shared/subspace-kzg" }
subspace-logging = { version = "0.0.1", path = "../../shared/subspace-logging" }
subspace-metrics = { version = "0.1.0", path = "../../shared/subspace-metrics", optional = true }
subspace-networking = { version = "0.1.0", path = "../subspace-networking" }
subspace-proof-of-space = { version = "0.1.0", path = "../subspace-proof-of-space" }
Expand All @@ -69,7 +70,6 @@ thread-priority = "1.1.0"
tokio = { version = "1.40.0", features = ["macros", "parking_lot", "rt-multi-thread", "signal", "sync", "time"] }
tokio-stream = { version = "0.1.16", features = ["sync"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"], optional = true }
ulid = { version = "1.1.3", features = ["serde"] }
zeroize = "1.8.1"

Expand Down Expand Up @@ -97,5 +97,4 @@ binary = [
"dep:mimalloc",
"dep:subspace-metrics",
"dep:supports-color",
"dep:tracing-subscriber",
]
22 changes: 2 additions & 20 deletions crates/subspace-farmer/src/bin/subspace-farmer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ use std::path::PathBuf;
use std::process::exit;
use std::{fs, panic};
use subspace_farmer::single_disk_farm::{ScrubTarget, SingleDiskFarm};
use subspace_logging::init_logger;
use subspace_proof_of_space::chia::ChiaTable;
use tracing::info;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};

#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
Expand Down Expand Up @@ -77,23 +75,7 @@ async fn main() -> anyhow::Result<()> {
exit(1);
}));

tracing_subscriber::registry()
.with(
fmt::layer()
// TODO: Workaround for https://github.com/tokio-rs/tracing/issues/2214, also on
// Windows terminal doesn't support the same colors as bash does
.with_ansi(if cfg!(windows) {
false
} else {
supports_color::on(supports_color::Stream::Stderr).is_some()
})
.with_filter(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy(),
),
)
.init();
init_logger();
utils::raise_fd_limit();

let command = Command::parse();
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ subspace-data-retrieval = { version = "0.1.0", path = "../../shared/subspace-dat
subspace-erasure-coding = { version = "0.1.0", path = "../subspace-erasure-coding" }
subspace-gateway-rpc = { version = "0.1.0", path = "../subspace-gateway-rpc" }
subspace-kzg = { version = "0.1.0", path = "../../shared/subspace-kzg" }
subspace-logging = { version = "0.0.1", path = "../../shared/subspace-logging" }
subspace-networking = { version = "0.1.0", path = "../subspace-networking" }
subspace-rpc-primitives = { version = "0.1.0", path = "../subspace-rpc-primitives" }
subspace-verification = { version = "0.1.0", path = "../subspace-verification", default-features = false }
supports-color = "3.0.1"
thiserror = "2.0.0"
tokio = { version = "1.40.0", features = ["rt-multi-thread", "signal", "macros"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
23 changes: 0 additions & 23 deletions crates/subspace-gateway/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ use clap::Parser;
use std::panic;
use std::process::exit;
use tokio::signal;
use tracing::level_filters::LevelFilter;
use tracing::{debug, warn};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{fmt, EnvFilter, Layer};

/// Commands for working with a gateway.
#[derive(Debug, Parser)]
Expand All @@ -32,25 +28,6 @@ pub(crate) fn set_exit_on_panic() {
}));
}

pub(crate) fn init_logger() {
// TODO: Workaround for https://github.com/tokio-rs/tracing/issues/2214, also on
// Windows terminal doesn't support the same colors as bash does
let enable_color = if cfg!(windows) {
false
} else {
supports_color::on(supports_color::Stream::Stderr).is_some()
};
tracing_subscriber::registry()
.with(
fmt::layer().with_ansi(enable_color).with_filter(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy(),
),
)
.init();
}

pub(crate) fn raise_fd_limit() {
match fdlimit::raise_fd_limit() {
Ok(fdlimit::Outcome::LimitRaised { from, to }) => {
Expand Down
3 changes: 2 additions & 1 deletion crates/subspace-gateway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ mod node_client;
mod piece_getter;
mod piece_validator;

use crate::commands::{init_logger, raise_fd_limit, set_exit_on_panic, Command};
use crate::commands::{raise_fd_limit, set_exit_on_panic, Command};
use clap::Parser;
use subspace_logging::init_logger;

#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
Expand Down
3 changes: 2 additions & 1 deletion crates/subspace-networking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ thiserror = "2.0.0"
tokio = { version = "1.40.0", features = ["macros", "parking_lot", "rt-multi-thread", "signal", "sync", "time"] }
tokio-stream = "0.1.16"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
unsigned-varint = { version = "0.8.0", features = ["futures", "asynchronous_codec"] }
void = "1.0.2"
subspace-logging = { version = "0.0.1", path = "../../shared/subspace-logging" }


[dependencies.libp2p]
# TODO: Replace with upstream once https://github.com/libp2p/rust-libp2p/issues/5626 and
Expand Down
18 changes: 2 additions & 16 deletions crates/subspace-networking/examples/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use subspace_core_primitives::pieces::{Piece, PieceIndex};
use subspace_logging::init_logger;
use subspace_networking::protocols::request_response::handlers::piece_by_index::PieceByIndexRequestHandler;
use subspace_networking::utils::piece_provider::{NoPieceValidator, PieceProvider, PieceValidator};
use subspace_networking::{Config, Node};
use tokio::sync::Semaphore;
use tracing::{debug, error, info, trace, warn, Level};
use tracing_subscriber::fmt::Subscriber;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;

/// Defines initial duration between get_piece calls.
const GET_PIECE_INITIAL_INTERVAL: Duration = Duration::from_secs(5);
Expand Down Expand Up @@ -128,8 +126,7 @@ enum Command {

#[tokio::main]
async fn main() {
init_logging();

init_logger();
let args: Args = Args::parse();

info!(?args, "Benchmark started.");
Expand Down Expand Up @@ -395,14 +392,3 @@ pub async fn configure_dsn(

node
}

fn init_logging() {
// set default log to info if the RUST_LOG is not set.
let env_filter = EnvFilter::builder()
.with_default_directive(Level::INFO.into())
.from_env_lossy();

let builder = Subscriber::builder().with_env_filter(env_filter).finish();

builder.init()
}
3 changes: 2 additions & 1 deletion crates/subspace-networking/examples/get-peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use libp2p::multiaddr::Protocol;
use parking_lot::Mutex;
use std::sync::Arc;
use std::time::Duration;
use subspace_logging::init_logger;
use subspace_networking::Config;

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
init_logger();

let config_1 = Config {
listen_on: vec!["/ip4/0.0.0.0/tcp/0".parse().unwrap()],
Expand Down
3 changes: 2 additions & 1 deletion crates/subspace-networking/examples/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use parking_lot::Mutex;
use prometheus_client::registry::Registry;
use std::sync::Arc;
use std::time::Duration;
use subspace_logging::init_logger;
use subspace_metrics::{start_prometheus_metrics_server, RegistryAdapter};
use subspace_networking::{Config, Node};
use tokio::signal;
Expand All @@ -15,7 +16,7 @@ use tracing::{error, info};

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
init_logger();
let mut metric_registry = Registry::default();
let metrics = Metrics::new(&mut metric_registry);

Expand Down
3 changes: 2 additions & 1 deletion crates/subspace-networking/examples/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ use libp2p::multiaddr::Protocol;
use parking_lot::Mutex;
use std::sync::Arc;
use std::time::Duration;
use subspace_logging::init_logger;
use subspace_networking::Config;

const TOPIC: &str = "Foo";

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
init_logger();

let config_1 = Config {
listen_on: vec!["/ip4/0.0.0.0/tcp/0".parse().unwrap()],
Expand Down
17 changes: 2 additions & 15 deletions crates/subspace-networking/examples/random-walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use subspace_core_primitives::pieces::PieceIndex;
use subspace_logging::init_logger;
use subspace_networking::protocols::request_response::handlers::piece_by_index::{
PieceByIndexRequest, PieceByIndexRequestHandler, PieceByIndexResponse,
};
use subspace_networking::{Config, Multihash, Node, PeerDiscovered, SendRequestError};
use tracing::{debug, error, info, warn, Level};
use tracing_subscriber::fmt::Subscriber;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;

#[derive(Debug, Parser)]
struct Args {
Expand Down Expand Up @@ -47,7 +45,7 @@ struct Args {

#[tokio::main]
async fn main() {
init_logging();
init_logger();

let args: Args = Args::parse();

Expand Down Expand Up @@ -416,14 +414,3 @@ async fn configure_dsn(

node
}

fn init_logging() {
// set default log to info if the RUST_LOG is not set.
let env_filter = EnvFilter::builder()
.with_default_directive(Level::INFO.into())
.from_env_lossy();

let builder = Subscriber::builder().with_env_filter(env_filter).finish();

builder.init()
}
3 changes: 2 additions & 1 deletion crates/subspace-networking/examples/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use parity_scale_codec::{Decode, Encode};
use parking_lot::Mutex;
use std::sync::Arc;
use std::time::Duration;
use subspace_logging::init_logger;
use subspace_networking::protocols::request_response::handlers::generic_request_handler::{
GenericRequest, GenericRequestHandler,
};
Expand All @@ -24,7 +25,7 @@ struct ExampleResponse;

#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
init_logger();

let config_1 = Config {
listen_on: vec!["/ip4/0.0.0.0/tcp/0".parse().unwrap()],
Expand Down
20 changes: 3 additions & 17 deletions crates/subspace-networking/src/bin/subspace-bootstrap-node/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::panic;
use std::process::exit;
use std::sync::Arc;
use subspace_logging::init_logger;
use subspace_metrics::{start_prometheus_metrics_server, RegistryAdapter};
use subspace_networking::libp2p::multiaddr::Protocol;
use subspace_networking::{peer_id, Config, KademliaMode};
use tracing::{debug, info, Level};
use tracing_subscriber::fmt::Subscriber;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;
use tracing::{debug, info};

/// Size of the LRU cache for peers.
pub const KNOWN_PEERS_CACHE_SIZE: u32 = 10000;
Expand Down Expand Up @@ -117,22 +115,10 @@ fn set_exit_on_panic() {
}));
}

fn init_logging() {
// set default log to info if the RUST_LOG is not set.
let env_filter = EnvFilter::builder()
.with_default_directive(Level::INFO.into())
.from_env_lossy();

let builder = Subscriber::builder().with_env_filter(env_filter).finish();

builder.init()
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
set_exit_on_panic();
init_logging();

init_logger();
let command: Command = Command::parse();

match command {
Expand Down
Loading

0 comments on commit 7bb1fa2

Please sign in to comment.