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