feat: allow YAZI_LOG to control the log level of Überzug++ (#2183)

This commit is contained in:
三咲雅 · Misaki Masa 2025-01-09 15:58:38 +08:00 committed by GitHub
parent 6bc4a0295d
commit 1e0c5ff61c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 60 additions and 12 deletions

View file

@ -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<Child> {
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())

View file

@ -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<WorkerGuard> = 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();

View file

@ -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()?;

View file

@ -1,3 +1,7 @@
use std::fmt::{Display, Formatter};
pub static LOG_LEVEL: crate::SyncCell<LogLevel> = 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<String> 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<str> 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()) }
}

View file

@ -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);