This commit is contained in:
nora 2022-03-04 22:15:19 +01:00
parent 93ce632b5d
commit 4346db648f
24 changed files with 224 additions and 209 deletions

View file

@ -1,26 +1,29 @@
#![warn(rust_2018_idioms)]
use anyhow::Result;
use std::env;
use clap::Parser;
use tracing::{info, info_span, Instrument};
/// An AMQP 0-9-1 broker implementation.
#[derive(Parser)]
struct Args {
/// Whether to serve the dashboard on localhost. Port defaults to 3000.
#[clap(short, long)]
dashboard: bool,
/// The log level of the application. Overwrites the `RUST_LOG` env var.
#[clap(long)]
log_level: Option<String>,
}
#[tokio::main]
async fn main() -> Result<()> {
let mut dashboard = false;
let args = Args::parse();
for arg in env::args().skip(1) {
match arg.as_str() {
"--dashboard" => dashboard = true,
"ignore-this-clippy" => eprintln!("yes please"),
_ => {}
}
}
setup_tracing();
setup_tracing(&args);
let global_data = amqp_core::GlobalData::default();
if dashboard {
if args.dashboard {
let dashboard_span = info_span!("dashboard");
tokio::spawn(amqp_dashboard::dashboard(global_data.clone()).instrument(dashboard_span));
}
@ -28,21 +31,22 @@ async fn main() -> Result<()> {
amqp_transport::do_thing_i_guess(global_data).await
}
fn setup_tracing() {
fn setup_tracing(args: &Args) {
const DEFAULT_LOG: &str = "hyper=info,debug";
let rust_log = std::env::var("RUST_LOG");
let log_filter = args
.log_level
.clone()
.or_else(|| std::env::var("RUST_LOG").ok())
.unwrap_or_else(|| DEFAULT_LOG.to_owned());
tracing_subscriber::fmt()
.with_level(true)
.with_timer(tracing_subscriber::fmt::time::time())
.with_ansi(true)
.with_thread_names(true)
.with_env_filter(rust_log.clone().unwrap_or_else(|_| DEFAULT_LOG.to_string()))
.with_env_filter(&log_filter)
.init();
if let Ok(rust_log) = rust_log {
info!(%rust_log, "Using custom log level");
} else {
info!(%DEFAULT_LOG, "Using default log level");
}
info!(%log_filter, "Using log filter level");
}