From 196872b1091adf843bda7bd5397a1cf3bc4fd903 Mon Sep 17 00:00:00 2001 From: zjp Date: Fri, 12 May 2023 17:01:49 +0800 Subject: introduce use env_logger related to https://github.com/denisidoro/navi/issues/576 For the following config cheats: paths: - C:\\Users\\Administrator\\AppData\\Roaming\\navi\\cheat - C:\\Users\\Administrator\\AppData\\Roaming\\navi\\cheat navi now gets incorrect paths on Windows, since the seperator `:` for path join is a valid component. [2023-05-12T08:58:26Z DEBUG navi::commands::core] Filesystem( Some( "C:\\\\Users\\\\Administrator\\\\AppData\\\\Roaming\\\\navi\\\\cheat:C:\\\\Users\\\\Administrator\\\\AppData\\\\Roaming\\\\navi\\\\cheat", ), ) [2023-05-12T08:58:28Z DEBUG navi::filesystem] filesystem::Fetcher = Fetcher { path: Some( "C:\\\\Users\\\\Administrator\\\\AppData\\\\Roaming\\\\navi\\\\cheat:C:\\\\Users\\\\Administrator\\\\AppData\\\\Roaming\\\\navi\\\\cheat", ), files: RefCell { value: [], }, } --- .gitignore | 1 + Cargo.lock | 22 ++++++++++++++++++++-- Cargo.toml | 2 ++ src/bin/main.rs | 16 ++++++++++++++-- src/commands/core/mod.rs | 4 +++- src/commands/mod.rs | 1 + src/config/cli.rs | 1 + src/config/env.rs | 1 + src/config/mod.rs | 1 + src/config/yaml.rs | 16 ++++++++-------- src/filesystem.rs | 2 ++ src/lib.rs | 2 -- 12 files changed, 54 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 53eaa21..3f2d71b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target **/*.rs.bk +navi.log diff --git a/Cargo.lock b/Cargo.lock index bbdffee..58753ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -260,6 +260,16 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +[[package]] +name = "env_logger" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" +dependencies = [ + "humantime", + "log", +] + [[package]] name = "errno" version = "0.3.0" @@ -344,6 +354,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "indexmap" version = "1.9.3" @@ -412,9 +428,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.14" +version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ "cfg-if", ] @@ -456,8 +472,10 @@ dependencies = [ "dns_common", "dns_common_derive", "edit", + "env_logger", "etcetera", "lazy_static", + "log", "regex", "remove_dir_all 0.8.2", "serde", diff --git a/Cargo.toml b/Cargo.toml index a9dc3d1..0115aca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,8 @@ serde_yaml = "0.9.21" dns_common_derive = { version = "0.2.1" } dns_common = { version = "0.2.1", default-features = false, features = ["yaml", "json"] } unicode-width = "0.1.10" +log = "0.4" +env_logger = { version = "0.10", default_features = false, features = ["humantime"] } [lib] name = "navi" diff --git a/src/bin/main.rs b/src/bin/main.rs index ad67e71..48c2fa0 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,6 +1,5 @@ extern crate navi; -use std::fmt::Debug; use thiserror::Error; #[derive(Error, Debug)] @@ -25,5 +24,18 @@ impl FileAnIssue { } fn main() -> Result<(), anyhow::Error> { - navi::handle().map_err(|e| FileAnIssue::new(e).into()) + init_logger()?; + navi::handle().map_err(|e| { + log::error!("{e:?}"); + FileAnIssue::new(e).into() + }) +} + +fn init_logger() -> anyhow::Result<()> { + let file = std::fs::File::create("navi.log")?; + env_logger::builder() + .target(env_logger::Target::Pipe(Box::new(file))) + .init(); + + Ok(()) } diff --git a/src/commands/core/mod.rs b/src/commands/core/mod.rs index ded24f1..2a3d334 100644 --- a/src/commands/core/mod.rs +++ b/src/commands/core/mod.rs @@ -44,7 +44,9 @@ pub fn init(fetcher: Box) -> Result<()> { } pub fn get_fetcher() -> Result> { - match CONFIG.source() { + let source = CONFIG.source(); + log::debug!("{source:#?}"); + match source { Source::Cheats(query) => { let lines = cheatsh::call(&query)?; let fetcher = Box::new(StaticFetcher::new(lines)); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 783cbad..257df1c 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -12,6 +12,7 @@ use crate::prelude::*; pub fn handle() -> Result<()> { use crate::config::Command::*; + log::debug!("CONFIG = {:#?}", &*CONFIG); match CONFIG.cmd() { None => commands::core::main(), diff --git a/src/config/cli.rs b/src/config/cli.rs index 7e2ec20..c6f86f9 100644 --- a/src/config/cli.rs +++ b/src/config/cli.rs @@ -117,6 +117,7 @@ pub enum Command { Info(commands::info::Input), } +#[derive(Debug)] pub enum Source { Filesystem(Option), Tldr(String), diff --git a/src/config/env.rs b/src/config/env.rs index 7afd27e..ba428fc 100644 --- a/src/config/env.rs +++ b/src/config/env.rs @@ -2,6 +2,7 @@ use crate::env_var; use crate::finder::FinderChoice; use crate::prelude::*; +#[derive(Debug)] pub struct EnvConfig { pub config_yaml: Option, pub config_path: Option, diff --git a/src/config/mod.rs b/src/config/mod.rs index 774f930..68638cf 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -12,6 +12,7 @@ use yaml::YamlConfig; lazy_static! { pub static ref CONFIG: Config = Config::new(); } +#[derive(Debug)] pub struct Config { yaml: YamlConfig, clap: ClapConfig, diff --git a/src/config/yaml.rs b/src/config/yaml.rs index 8c4a693..70a5a17 100644 --- a/src/config/yaml.rs +++ b/src/config/yaml.rs @@ -6,7 +6,7 @@ use crate::prelude::*; use crossterm::style::Color as TerminalColor; use serde::de; -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] pub struct Color(#[serde(deserialize_with = "color_deserialize")] TerminalColor); impl Color { @@ -24,7 +24,7 @@ where .map_err(|_| de::Error::custom(format!("Failed to deserialize color: {s}"))) } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] #[serde(default)] pub struct ColorWidth { pub color: Color, @@ -32,7 +32,7 @@ pub struct ColorWidth { pub min_width: u16, } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] #[serde(default)] pub struct Style { pub tag: ColorWidth, @@ -40,7 +40,7 @@ pub struct Style { pub snippet: ColorWidth, } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] #[serde(default)] pub struct Finder { #[serde(deserialize_with = "finder_deserialize")] @@ -58,27 +58,27 @@ where .map_err(|_| de::Error::custom(format!("Failed to deserialize finder: {s}"))) } -#[derive(Deserialize, Default)] +#[derive(Deserialize, Default, Debug)] #[serde(default)] pub struct Cheats { pub path: Option, pub paths: Vec, } -#[derive(Deserialize, Default)] +#[derive(Deserialize, Default, Debug)] #[serde(default)] pub struct Search { pub tags: Option, } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] #[serde(default)] pub struct Shell { pub command: String, pub finder_command: Option, } -#[derive(Deserialize, Default)] +#[derive(Deserialize, Default, Debug)] #[serde(default)] pub struct YamlConfig { pub style: Style, diff --git a/src/filesystem.rs b/src/filesystem.rs index 6340f82..ed0f32e 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -125,6 +125,7 @@ fn interpolate_paths(paths: String) -> String { newtext } +#[derive(Debug)] pub struct Fetcher { path: Option, files: RefCell>, @@ -180,6 +181,7 @@ impl fetcher::Fetcher for Fetcher { } } + log::debug!("filesystem::Fetcher = {self:#?}"); Ok(found_something) } diff --git a/src/lib.rs b/src/lib.rs index cbae52d..f4a434f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,5 @@ #[macro_use] extern crate lazy_static; -// #[macro_use] -// extern crate anyhow; mod clients; mod commands; -- cgit v1.2.3