diff --git a/yazi-adapter/src/drivers/ueberzug.rs b/yazi-adapter/src/drivers/ueberzug.rs index 1fa60ed7..addb5a95 100644 --- a/yazi-adapter/src/drivers/ueberzug.rs +++ b/yazi-adapter/src/drivers/ueberzug.rs @@ -6,7 +6,7 @@ use ratatui::layout::Rect; use tokio::{io::AsyncWriteExt, process::{Child, Command}, sync::mpsc::{self, UnboundedSender}}; use tracing::{debug, warn}; use yazi_config::PREVIEW; -use yazi_shared::{RoCell, env_exists}; +use yazi_shared::{LOG_LEVEL, RoCell, env_exists}; use crate::{Adapter, Dimension}; @@ -87,7 +87,7 @@ impl Ueberzug { fn create_demon(adapter: Adapter) -> Result { let result = Command::new("ueberzugpp") .args(["layer", "-so", &adapter.to_string()]) - .env("SPDLOG_LEVEL", if cfg!(debug_assertions) { "debug" } else { "" }) + .env("SPDLOG_LEVEL", if LOG_LEVEL.get().is_none() { "" } else { "debug" }) .kill_on_drop(true) .stdin(Stdio::piped()) .stdout(Stdio::null()) diff --git a/yazi-fm/src/logs.rs b/yazi-fm/src/logs.rs index bc540aae..7cd497a6 100644 --- a/yazi-fm/src/logs.rs +++ b/yazi-fm/src/logs.rs @@ -1,10 +1,10 @@ -use std::{env, fs::File}; +use std::fs::File; use anyhow::Context; use tracing_appender::non_blocking::WorkerGuard; use tracing_subscriber::EnvFilter; use yazi_fs::Xdg; -use yazi_shared::RoCell; +use yazi_shared::{LOG_LEVEL, RoCell}; static _GUARD: RoCell = RoCell::new(); @@ -12,9 +12,8 @@ pub(super) struct Logs; impl Logs { pub(super) fn start() -> anyhow::Result<()> { - let mut level = env::var("YAZI_LOG").unwrap_or_default(); - level.make_ascii_uppercase(); - if !matches!(level.as_str(), "ERROR" | "WARN" | "INFO" | "DEBUG") { + let level = LOG_LEVEL.get(); + if LOG_LEVEL.get().is_none() { return Ok(()); } @@ -29,7 +28,7 @@ impl Logs { let (non_blocking, guard) = tracing_appender::non_blocking(log_file); tracing_subscriber::fmt() .pretty() - .with_env_filter(EnvFilter::new(&level)) + .with_env_filter(EnvFilter::new(level)) .with_writer(non_blocking) .with_ansi(cfg!(debug_assertions)) .init(); diff --git a/yazi-fm/src/main.rs b/yazi-fm/src/main.rs index 7d8c4e42..96d8bff2 100644 --- a/yazi-fm/src/main.rs +++ b/yazi-fm/src/main.rs @@ -12,12 +12,11 @@ yazi_macro::mod_flat!(context executor logs panic root router signals term); #[tokio::main] async fn main() -> anyhow::Result<()> { Panic::install(); - Logs::start()?; - - _ = fdlimit::raise_fd_limit(); - yazi_shared::init(); + Logs::start()?; + _ = fdlimit::raise_fd_limit(); + yazi_fs::init(); yazi_config::init()?; diff --git a/yazi-shared/src/env.rs b/yazi-shared/src/env.rs index 29e2c6e2..77f9498d 100644 --- a/yazi-shared/src/env.rs +++ b/yazi-shared/src/env.rs @@ -1,3 +1,7 @@ +use std::fmt::{Display, Formatter}; + +pub static LOG_LEVEL: crate::SyncCell = crate::SyncCell::new(LogLevel::None); + #[inline] pub fn env_exists(name: &str) -> bool { std::env::var_os(name).is_some_and(|s| !s.is_empty()) } @@ -17,3 +21,47 @@ pub fn in_wsl() -> bool { pub fn in_ssh_connection() -> bool { env_exists("SSH_CLIENT") || env_exists("SSH_TTY") || env_exists("SSH_CONNECTION") } + +// LogLevel +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum LogLevel { + None, + Error, + Warn, + Info, + Debug, +} + +impl LogLevel { + #[inline] + pub fn is_none(self) -> bool { self == Self::None } +} + +impl From for LogLevel { + fn from(mut s: String) -> Self { + s.make_ascii_uppercase(); + match s.as_str() { + "ERROR" => Self::Error, + "WARN" => Self::Warn, + "INFO" => Self::Info, + "DEBUG" => Self::Debug, + _ => Self::None, + } + } +} + +impl AsRef for LogLevel { + fn as_ref(&self) -> &str { + match self { + Self::None => "NONE", + Self::Error => "ERROR", + Self::Warn => "WARN", + Self::Info => "INFO", + Self::Debug => "DEBUG", + } + } +} + +impl Display for LogLevel { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.as_ref()) } +} diff --git a/yazi-shared/src/lib.rs b/yazi-shared/src/lib.rs index 2260781c..7588f037 100644 --- a/yazi-shared/src/lib.rs +++ b/yazi-shared/src/lib.rs @@ -5,6 +5,8 @@ yazi_macro::mod_pub!(errors event shell theme translit url); yazi_macro::mod_flat!(chars condition debounce either env id layer natsort number os rand ro_cell sync_cell terminal throttle time); pub fn init() { + LOG_LEVEL.replace(<_>::from(std::env::var("YAZI_LOG").unwrap_or_default())); + #[cfg(unix)] USERS_CACHE.with(<_>::default);