Skip to main content

lyquor_cli/
lib.rs

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.
9pub mod script;
10
11#[macro_export]
12macro_rules! build_version {
13    () => {
14        env!("LYQUOR_BUILD_VERSION")
15    };
16}
17
18/// Install the process-wide tracing subscriber from Lyquor logging environment variables.
19pub fn setup_tracing() -> anyhow::Result<()> {
20    use tracing_subscriber::prelude::*;
21
22    let env_filter = tracing_subscriber::EnvFilter::builder()
23        .with_default_directive("info".parse().unwrap())
24        .with_env_var("LYQUOR_LOG")
25        .from_env_lossy()
26        .add_directive("foundry_compilers=warn".parse().unwrap())
27        .add_directive("cranelift=info".parse().unwrap())
28        .add_directive("wasmtime=info".parse().unwrap());
29
30    let span_events = {
31        use tracing_subscriber::fmt::format::FmtSpan;
32
33        let mut span_events = FmtSpan::NONE;
34
35        let s = std::env::var("LYQUOR_LOG_SPAN_EVENTS")
36            .unwrap_or_else(|_| "new,close".into())
37            .split(',')
38            .map(|s| s.trim().to_lowercase())
39            .collect::<Vec<_>>();
40        for fmt_span in s {
41            match fmt_span.as_str() {
42                "new" => span_events |= FmtSpan::NEW,
43                "close" => span_events |= FmtSpan::CLOSE,
44                "enter" => span_events |= FmtSpan::ENTER,
45                "exit" => span_events |= FmtSpan::EXIT,
46                "active" => span_events |= FmtSpan::ACTIVE,
47                "full" => span_events |= FmtSpan::FULL,
48                _ => (),
49            }
50        }
51        span_events
52    };
53
54    let fmt_layer = tracing_subscriber::fmt::layer()
55        .with_thread_ids(true)
56        .with_writer(std::io::stderr)
57        .with_span_events(span_events);
58
59    let registry = tracing_subscriber::registry();
60
61    #[cfg(feature = "tokio-console")]
62    let registry = registry.with(console_subscriber::spawn());
63
64    match std::env::var("LYQUOR_LOG_FORMAT")
65        .unwrap_or_else(|_| "full".into())
66        .to_lowercase()
67        .as_str()
68    {
69        "compact" => registry.with(fmt_layer.compact().with_filter(env_filter)).init(),
70        "pretty" => registry.with(fmt_layer.pretty().with_filter(env_filter)).init(),
71        _ => registry.with(fmt_layer.with_filter(env_filter)).init(),
72    };
73
74    Ok(())
75}
76
77/// Render the startup banner using the supplied build version string.
78pub fn format_logo_banner(version: &str) -> String {
79    const LOGO: &str = r"
80     __    _  _   __   _  _   __  ____    _o/_
81    (..)  (.\/.) /  \ / )( \ /  \(  _ \   \##/
82    /.(_/\ )../ (  O )) \/ ((  O ))   /    ||
83    \..../(../te \__\)\____/ \__/(__\_)um _||_";
84
85    format!(
86        "{LOGO}         
87
88    Version: {version:>33}
89    =========================================\n",
90    )
91}