/home/runner/work/lyquor/lyquor/toolchain/cli/src/lib.rs
Line | Count | Source |
1 | | //! Shared command-line support for Lyquor binaries. |
2 | | //! |
3 | | //! `lyquor-cli` keeps cross-binary concerns out of the node and tooling crates. It owns tracing |
4 | | //! initialization, environment-driven log filtering, build-version display, and Cargo build-script |
5 | | //! helpers used by binaries that otherwise have separate command surfaces. Command-specific parsing |
6 | | //! and behavior remain in the crates that expose those binaries. |
7 | | |
8 | | /// Cargo build-script helpers shared by Lyquor binaries. |
9 | | pub mod script; |
10 | | |
11 | | #[macro_export] |
12 | | macro_rules! build_version { |
13 | | () => { |
14 | | env!("LYQUOR_BUILD_VERSION") |
15 | | }; |
16 | | } |
17 | | |
18 | | /// Install the process-wide tracing subscriber from Lyquor logging environment variables. |
19 | 438 | pub fn setup_tracing() -> anyhow::Result<()> { |
20 | | use tracing_subscriber::prelude::*; |
21 | | |
22 | 438 | let env_filter = tracing_subscriber::EnvFilter::builder() |
23 | 438 | .with_default_directive("info".parse().unwrap()) |
24 | 438 | .with_env_var("LYQUOR_LOG") |
25 | 438 | .from_env_lossy() |
26 | 438 | .add_directive("foundry_compilers=warn".parse().unwrap()) |
27 | 438 | .add_directive("cranelift=info".parse().unwrap()) |
28 | 438 | .add_directive("wasmtime=info".parse().unwrap()); |
29 | | |
30 | 438 | let span_events = { |
31 | | use tracing_subscriber::fmt::format::FmtSpan; |
32 | | |
33 | 438 | let mut span_events = FmtSpan::NONE; |
34 | | |
35 | 438 | let s = std::env::var("LYQUOR_LOG_SPAN_EVENTS") |
36 | 438 | .unwrap_or_else(|_| "new,close".into()) |
37 | 438 | .split(',') |
38 | 876 | .map438 (|s| s.trim().to_lowercase()) |
39 | 438 | .collect::<Vec<_>>(); |
40 | 876 | for fmt_span in s438 { |
41 | 876 | match fmt_span.as_str() { |
42 | 876 | "new" => span_events |= FmtSpan::NEW438 , |
43 | 438 | "close" => span_events |= FmtSpan::CLOSE, |
44 | 0 | "enter" => span_events |= FmtSpan::ENTER, |
45 | 0 | "exit" => span_events |= FmtSpan::EXIT, |
46 | 0 | "active" => span_events |= FmtSpan::ACTIVE, |
47 | 0 | "full" => span_events |= FmtSpan::FULL, |
48 | 0 | _ => (), |
49 | | } |
50 | | } |
51 | 438 | span_events |
52 | | }; |
53 | | |
54 | 438 | let fmt_layer = tracing_subscriber::fmt::layer() |
55 | 438 | .with_thread_ids(true) |
56 | 438 | .with_writer(std::io::stderr) |
57 | 438 | .with_span_events(span_events); |
58 | | |
59 | 438 | let registry = tracing_subscriber::registry(); |
60 | | |
61 | | #[cfg(feature = "tokio-console")] |
62 | | let registry = registry.with(console_subscriber::spawn()); |
63 | | |
64 | 438 | match std::env::var("LYQUOR_LOG_FORMAT") |
65 | 438 | .unwrap_or_else(|_| "full".into()) |
66 | 438 | .to_lowercase() |
67 | 438 | .as_str() |
68 | | { |
69 | 438 | "compact" => registry0 .with0 (fmt_layer0 .compact0 ().with_filter0 (env_filter0 )).init0 (), |
70 | 438 | "pretty" => registry0 .with0 (fmt_layer0 .pretty0 ().with_filter0 (env_filter0 )).init0 (), |
71 | 438 | _ => registry.with(fmt_layer.with_filter(env_filter)).init(), |
72 | | }; |
73 | | |
74 | 438 | Ok(()) |
75 | 438 | } |
76 | | |
77 | | /// Render the startup banner using the supplied build version string. |
78 | 0 | pub fn format_logo_banner(version: &str) -> String { |
79 | | const LOGO: &str = r" |
80 | | __ _ _ __ _ _ __ ____ _o/_ |
81 | | (..) (.\/.) / \ / )( \ / \( _ \ \##/ |
82 | | /.(_/\ )../ ( O )) \/ (( O )) / || |
83 | | \..../(../te \__\)\____/ \__/(__\_)um _||_"; |
84 | | |
85 | 0 | format!( |
86 | | "{LOGO} |
87 | | |
88 | | Version: {version:>33} |
89 | | =========================================\n", |
90 | | ) |
91 | 0 | } |