lesavka/client/src/main.rs

71 lines
2.0 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// client/src/main.rs
#![forbid(unsafe_code)]
use anyhow::Result;
use std::{env, fs::OpenOptions, path::Path};
use tracing_appender::non_blocking;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::{filter::EnvFilter, fmt, prelude::*};
use lesavka_client::LesavkaClientApp;
#[tokio::main]
async fn main() -> Result<()> {
/*------------- common filter & stderr layer ------------------------*/
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
EnvFilter::new(
"lesavka_client=trace,\
lesavka_server=trace,\
tonic=debug,\
h2=debug,\
tower=debug",
)
});
let stderr_layer = fmt::layer()
.with_target(true)
.with_thread_ids(true)
.with_file(true);
let dev_mode = env::var("NAVKA_DEV_MODE").is_ok();
let mut _guard: Option<WorkerGuard> = None; // keep guard alive
/*------------- subscriber setup -----------------------------------*/
if dev_mode {
let log_path = Path::new("/tmp").join("lesavka-client.log");
// file → nonblocking writer (+ guard)
let file = OpenOptions::new()
.create(true)
.write(true)
// .truncate(true)
.open(&log_path)?;
let (file_writer, guard) = non_blocking(file);
_guard = Some(guard);
let file_layer = fmt::layer()
.with_writer(file_writer)
.with_ansi(false)
.with_target(true)
.with_level(true);
tracing_subscriber::registry()
.with(env_filter)
.with(stderr_layer)
.with(file_layer)
.init();
tracing::info!("lesavka-client running in DEV mode → {}", log_path.display());
} else {
tracing_subscriber::registry()
.with(env_filter)
.with(stderr_layer)
.init();
}
/*------------- run the actual application -------------------------*/
let mut app = LesavkaClientApp::new()?;
app.run().await
}