Skip to content

Commit

Permalink
feat: allow YAZI_LOG to control the log level of Überzug++ (#2183)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored Jan 9, 2025
1 parent 6bc4a02 commit 1e0c5ff
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
4 changes: 2 additions & 2 deletions yazi-adapter/src/drivers/ueberzug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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())
Expand Down
11 changes: 5 additions & 6 deletions yazi-fm/src/logs.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
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();

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(());
}

Expand All @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions yazi-fm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
yazi_shared::init();

Logs::start()?;
_ = fdlimit::raise_fd_limit();

yazi_shared::init();

yazi_fs::init();

yazi_config::init()?;
Expand Down
48 changes: 48 additions & 0 deletions yazi-shared/src/env.rs
Original file line number Diff line number Diff line change
@@ -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()) }

Expand All @@ -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()) }
}
2 changes: 2 additions & 0 deletions yazi-shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 1e0c5ff

Please sign in to comment.