feat: new log system (#1945)

This commit is contained in:
三咲雅 · Misaki Masa 2024-11-24 17:47:32 +08:00 committed by GitHub
parent 805597268a
commit 1277b01758
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 61 additions and 74 deletions

40
Cargo.lock generated
View file

@ -992,8 +992,8 @@ dependencies = [
"aho-corasick",
"bstr",
"log",
"regex-automata",
"regex-syntax",
"regex-automata 0.4.9",
"regex-syntax 0.8.5",
]
[[package]]
@ -1486,6 +1486,15 @@ dependencies = [
"which",
]
[[package]]
name = "matchers"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
dependencies = [
"regex-automata 0.1.10",
]
[[package]]
name = "maybe-rayon"
version = "0.1.1"
@ -2160,8 +2169,17 @@ checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
"regex-automata 0.4.9",
"regex-syntax 0.8.5",
]
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
dependencies = [
"regex-syntax 0.6.29",
]
[[package]]
@ -2172,9 +2190,15 @@ checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
"regex-syntax 0.8.5",
]
[[package]]
name = "regex-syntax"
version = "0.6.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
[[package]]
name = "regex-syntax"
version = "0.8.5"
@ -2499,7 +2523,7 @@ dependencies = [
"once_cell",
"onig",
"plist",
"regex-syntax",
"regex-syntax 0.8.5",
"serde",
"serde_derive",
"serde_json",
@ -2768,10 +2792,14 @@ version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b"
dependencies = [
"matchers",
"nu-ansi-term",
"once_cell",
"regex",
"sharded-slab",
"smallvec",
"thread_local",
"tracing",
"tracing-core",
"tracing-log",
]

View file

@ -32,6 +32,6 @@ shell-words = "1.1.0"
tokio = { version = "1.41.1", features = [ "full" ] }
tokio-stream = "0.1.16"
tokio-util = "0.7.12"
tracing = { version = "0.1.40", features = [ "max_level_debug", "release_max_level_warn" ] }
tracing = { version = "0.1.40", features = [ "max_level_debug", "release_max_level_debug" ] }
unicode-width = "0.2.0"
uzers = "0.12.1"

View file

@ -2,7 +2,6 @@ use std::{env, ffi::OsStr, fmt::Write};
use regex::Regex;
use yazi_adapter::Mux;
use yazi_shared::Xdg;
use super::Actions;
@ -98,13 +97,6 @@ impl Actions {
writeln!(s, " xclip : {}", Self::process_output("xclip", "-version"))?;
writeln!(s, " xsel : {}", Self::process_output("xsel", "--version"))?;
writeln!(s, "\n\n--------------------------------------------------")?;
writeln!(
s,
"When reporting a bug, please also upload the `yazi.log` log file - only upload the most recent content by time."
)?;
writeln!(s, "You can find it in the {:?} directory.", Xdg::state_dir())?;
Ok(s)
}

View file

@ -214,6 +214,3 @@ sort_by = "none"
sort_sensitive = false
sort_reverse = false
sort_translit = false
[log]
enabled = false

View file

@ -1,6 +1,6 @@
#![allow(clippy::module_inception)]
yazi_macro::mod_pub!(keymap log manager open plugin popup preview tasks theme which);
yazi_macro::mod_pub!(keymap manager open plugin popup preview tasks theme which);
yazi_macro::mod_flat!(layout pattern preset priority);
@ -9,7 +9,6 @@ use std::str::FromStr;
use yazi_shared::{RoCell, SyncCell, Xdg};
pub static KEYMAP: RoCell<keymap::Keymap> = RoCell::new();
pub static LOG: RoCell<log::Log> = RoCell::new();
pub static MANAGER: RoCell<manager::Manager> = RoCell::new();
pub static OPEN: RoCell<open::Open> = RoCell::new();
pub static PLUGIN: RoCell<plugin::Plugin> = RoCell::new();
@ -89,7 +88,6 @@ fn try_init(merge: bool) -> anyhow::Result<()> {
};
let keymap = <_>::from_str(&keymap_toml)?;
let log = <_>::from_str(&yazi_toml)?;
let manager = <_>::from_str(&yazi_toml)?;
let open = <_>::from_str(&yazi_toml)?;
let plugin = <_>::from_str(&yazi_toml)?;
@ -102,7 +100,6 @@ fn try_init(merge: bool) -> anyhow::Result<()> {
let which = <_>::from_str(&yazi_toml)?;
KEYMAP.init(keymap);
LOG.init(log);
MANAGER.init(manager);
OPEN.init(open);
PLUGIN.init(plugin);

View file

@ -1,37 +0,0 @@
use std::str::FromStr;
use anyhow::Context;
use serde::{Deserialize, Deserializer};
#[derive(Debug)]
pub struct Log {
pub enabled: bool,
}
impl FromStr for Log {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
toml::from_str(s).context("Failed to parse the [log] section in your yazi.toml")
}
}
impl<'de> Deserialize<'de> for Log {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Outer {
log: Shadow,
}
#[derive(Deserialize)]
struct Shadow {
enabled: bool,
}
let outer = Outer::deserialize(deserializer)?;
Ok(Self { enabled: outer.log.enabled })
}
}

View file

@ -1 +0,0 @@
yazi_macro::mod_flat!(log);

View file

@ -1,9 +1,9 @@
use std::{borrow::Cow, path::PathBuf, str::FromStr, time::{SystemTime, UNIX_EPOCH}};
use std::{borrow::Cow, path::PathBuf, str::FromStr};
use anyhow::Context;
use serde::{Deserialize, Deserializer, Serialize};
use validator::Validate;
use yazi_shared::fs::expand_path;
use yazi_shared::{fs::expand_path, timestamp_us};
use super::PreviewWrap;
use crate::Xdg;
@ -32,8 +32,7 @@ pub struct Preview {
impl Preview {
#[inline]
pub fn tmpfile(&self, prefix: &str) -> PathBuf {
let time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards");
self.cache_dir.join(format!("{prefix}-{}", time.as_nanos() / 1000))
self.cache_dir.join(format!("{prefix}-{}", timestamp_us()))
}
#[inline]

View file

@ -41,7 +41,7 @@ tokio-stream = { workspace = true }
# Logging
tracing = { workspace = true }
tracing-appender = "0.2.3"
tracing-subscriber = "0.3.18"
tracing-subscriber = { version = "0.3.18", features = [ "env-filter" ] }
[target."cfg(unix)".dependencies]
libc = { workspace = true }

View file

@ -1,6 +1,8 @@
use std::{env, fs::File};
use anyhow::Context;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::{Registry, fmt, prelude::__tracing_subscriber_SubscriberExt};
use tracing_subscriber::EnvFilter;
use yazi_shared::{RoCell, Xdg};
static _GUARD: RoCell<WorkerGuard> = RoCell::new();
@ -9,21 +11,31 @@ 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") {
return Ok(());
}
let state_dir = Xdg::state_dir();
std::fs::create_dir_all(&state_dir)
.with_context(|| format!("failed to create state directory: {state_dir:?}"))?;
let appender = tracing_appender::rolling::never(state_dir, "yazi.log");
let (handle, guard) = tracing_appender::non_blocking(appender);
let log_path = state_dir.join("yazi.log");
let log_file = File::create(&log_path)
.with_context(|| format!("failed to create log file: {log_path:?}"))?;
// let filter = EnvFilter::from_default_env();
let subscriber = Registry::default()
.with(fmt::layer().pretty().with_writer(handle).with_ansi(cfg!(debug_assertions)));
tracing::subscriber::set_global_default(subscriber)
.context("setting default subscriber failed")?;
let (non_blocking, guard) = tracing_appender::non_blocking(log_file);
tracing_subscriber::fmt()
.pretty()
.with_env_filter(EnvFilter::new(&level))
.with_writer(non_blocking)
.with_ansi(cfg!(debug_assertions))
.init();
_GUARD.init(guard);
eprintln!("Running with log level `{level}`, logs are written to {log_path:?}");
Ok(())
}
}