From 04e2d4c69295f6990023955d0e930e702e691553 Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Sat, 10 Oct 2020 02:01:12 +0100 Subject: [PATCH] Just straight-up roll our own logger This commit removes the env_logger dependency, replacing it with a simple implementation. Doing so removes like ten other transitive dependencies that no longer need to be included in the build. It also gains the ability to enable trace-level logging. The users crate, which contains such logging statements as of the version I published a few days ago, has been upgraded to celebrate. Also, change the log imports to globs. I'm only interested that a file doing logging, not what level it's logging at. --- Cargo.lock | 123 +----------------------------------------- Cargo.toml | 3 +- devtools/dev-bash.sh | 22 +++++--- src/fs/dir.rs | 2 +- src/fs/feature/git.rs | 2 +- src/fs/file.rs | 2 +- src/logger.rs | 66 +++++++++++++++++++++++ src/main.rs | 33 ++---------- src/options/style.rs | 2 +- src/output/details.rs | 2 +- src/output/table.rs | 2 +- 11 files changed, 93 insertions(+), 166 deletions(-) create mode 100644 src/logger.rs diff --git a/Cargo.lock b/Cargo.lock index 5337a2b..2d97cb2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,14 +1,5 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -[[package]] -name = "aho-corasick" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36b7aa1ccb7d7ea3f437cf025a2ab1c47cc6c1bc9fc84918ff449def12f5e282" -dependencies = [ - "memchr", -] - [[package]] name = "ansi_term" version = "0.12.0" @@ -18,16 +9,6 @@ dependencies = [ "winapi 0.3.8", ] -[[package]] -name = "atty" -version = "0.2.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90" -dependencies = [ - "libc", - "winapi 0.3.8", -] - [[package]] name = "autocfg" version = "0.1.5" @@ -73,26 +54,12 @@ dependencies = [ "winapi 0.3.8", ] -[[package]] -name = "env_logger" -version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - [[package]] name = "exa" version = "0.9.0" dependencies = [ "ansi_term", "datetime", - "env_logger", "git2", "glob", "lazy_static", @@ -130,15 +97,6 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" -[[package]] -name = "humantime" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114" -dependencies = [ - "quick-error", -] - [[package]] name = "idna" version = "0.1.5" @@ -319,40 +277,12 @@ version = "0.3.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" -[[package]] -name = "quick-error" -version = "1.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" - [[package]] name = "redox_syscall" version = "0.1.56" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" -[[package]] -name = "regex" -version = "1.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9d8297cc20bbb6184f8b45ff61c8ee6a9ac56c156cec8e38c3e5084773c44ad" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", - "thread_local", - "utf8-ranges", -] - -[[package]] -name = "regex-syntax" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b01330cce219c1c6b2e209e5ed64ccd587ae5c67bed91c0b49eecf02ae40e21" -dependencies = [ - "ucd-util", -] - [[package]] name = "scoped_threadpool" version = "0.1.9" @@ -385,30 +315,6 @@ dependencies = [ "winapi 0.2.8", ] -[[package]] -name = "termcolor" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96d6098003bde162e4277c70665bd87c326f5a0c3f3fbfb285787fa482d54e6e" -dependencies = [ - "wincolor", -] - -[[package]] -name = "thread_local" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" -dependencies = [ - "lazy_static", -] - -[[package]] -name = "ucd-util" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" - [[package]] name = "unicode-bidi" version = "0.3.4" @@ -446,20 +352,14 @@ dependencies = [ [[package]] name = "users" -version = "0.10.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa4227e95324a443c9fcb06e03d4d85e91aabe9a5a02aa818688b6918b6af486" +checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032" dependencies = [ "libc", "log", ] -[[package]] -name = "utf8-ranges" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d50aa7650df78abf942826607c62468ce18d9019673d4a2ebe1865dbb96ffde" - [[package]] name = "vcpkg" version = "0.2.7" @@ -500,31 +400,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9" -dependencies = [ - "winapi 0.3.8", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "wincolor" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "561ed901ae465d6185fa7864d63fbd5720d0ef718366c9a4dc83cf6170d7e9ba" -dependencies = [ - "winapi 0.3.8", - "winapi-util", -] - [[package]] name = "zoneinfo_compiled" version = "0.5.0" diff --git a/Cargo.toml b/Cargo.toml index d3862c3..fcb94d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,6 @@ name = "exa" [dependencies] ansi_term = "0.12.0" datetime = "0.5" -env_logger = "0.6.1" glob = "0.3.0" lazy_static = "1.3.0" libc = "0.2.51" @@ -37,7 +36,7 @@ scoped_threadpool = "0.1.9" term_grid = "0.1.7" term_size = "0.3.1" unicode-width = "0.1.5" -users = "0.10" +users = "0.11" zoneinfo_compiled = "0.5" [dependencies.git2] diff --git a/devtools/dev-bash.sh b/devtools/dev-bash.sh index e869ea2..95a48a4 100644 --- a/devtools/dev-bash.sh +++ b/devtools/dev-bash.sh @@ -12,7 +12,7 @@ bash /vagrant/devtools/dev-versions.sh # The Cool Prompt tells you whether you’re in debug or strict mode, whether # you have colours configured, and whether your last command failed. function nonzero_return() { RETVAL=$?; [ $RETVAL -ne 0 ] && echo "$RETVAL "; } -function debug_mode() { [ -n "$EXA_DEBUG" ] && echo "debug "; } +function debug_mode() { [ "$EXA_DEBUG" == "trace" ] && echo -n "trace-"; [ -n "$EXA_DEBUG" ] && echo "debug "; } function strict_mode() { [ -n "$EXA_STRICT" ] && echo "strict "; } function lsc_mode() { [ -n "$LS_COLORS" ] && echo "lsc "; } function exac_mode() { [ -n "$EXA_COLORS" ] && echo "exac "; } @@ -22,10 +22,14 @@ export PS1="\[\e[1;36m\]\h \[\e[32m\]\w \[\e[31m\]\`nonzero_return\`\[\e[35m\]\` # The ‘debug’ function lets you switch debug mode on and off. # Turn it on if you need to see exa’s debugging logs. function debug () { - case "$1" in "on") export EXA_DEBUG=1 ;; - "off") export EXA_DEBUG= ;; - "") [ -n "$EXA_DEBUG" ] && echo "debug on" || echo "debug off" ;; - *) echo "Usage: debug on|off"; return 1 ;; esac; } + case "$1" in + ""|"on") export EXA_DEBUG=1 ;; + "off") export EXA_DEBUG= ;; + "trace") export EXA_DEBUG=trace ;; + "status") [ -n "$EXA_DEBUG" ] && echo "debug on" || echo "debug off" ;; + *) echo "Usage: debug on|off|trace|status"; return 1 ;; + esac; +} # The ‘strict’ function lets you switch strict mode on and off. # Turn it on if you’d like exa’s command-line arguments checked. @@ -33,7 +37,9 @@ function strict () { case "$1" in "on") export EXA_STRICT=1 ;; "off") export EXA_STRICT= ;; "") [ -n "$EXA_STRICT" ] && echo "strict on" || echo "strict off" ;; - *) echo "Usage: strict on|off"; return 1 ;; esac; } + *) echo "Usage: strict on|off"; return 1 ;; + esac; +} # The ‘colors’ function sets or unsets the ‘LS_COLORS’ and ‘EXA_COLORS’ # environment variables. There’s also a ‘hacker’ theme which turns everything @@ -53,4 +59,6 @@ function colors () { "") [ -n "$LS_COLORS" ] && echo "LS_COLORS=$LS_COLORS" || echo "ls-colors off" [ -n "$EXA_COLORS" ] && echo "EXA_COLORS=$EXA_COLORS" || echo "exa-colors off" ;; - *) echo "Usage: ls-colors ls|hacker|off"; return 1 ;; esac; } + *) echo "Usage: ls-colors ls|hacker|off"; return 1 ;; + esac; +} diff --git a/src/fs/dir.rs b/src/fs/dir.rs index 2d81397..9b87ff5 100644 --- a/src/fs/dir.rs +++ b/src/fs/dir.rs @@ -5,7 +5,7 @@ use std::fs; use std::path::{Path, PathBuf}; use std::slice::Iter as SliceIter; -use log::info; +use log::*; use crate::fs::File; diff --git a/src/fs/feature/git.rs b/src/fs/feature/git.rs index df4044f..7cf68a8 100644 --- a/src/fs/feature/git.rs +++ b/src/fs/feature/git.rs @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf}; use std::sync::Mutex; -use log::{debug, error, info, warn}; +use log::*; use crate::fs::fields as f; diff --git a/src/fs/file.rs b/src/fs/file.rs index 234df9b..60ac398 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -6,7 +6,7 @@ use std::os::unix::fs::{MetadataExt, PermissionsExt, FileTypeExt}; use std::path::{Path, PathBuf}; use std::time::{Duration, SystemTime, UNIX_EPOCH}; -use log::{debug, error}; +use log::*; use crate::fs::dir::Dir; use crate::fs::fields as f; diff --git a/src/logger.rs b/src/logger.rs new file mode 100644 index 0000000..4f89eb1 --- /dev/null +++ b/src/logger.rs @@ -0,0 +1,66 @@ +//! Debug error logging. + +use std::ffi::OsStr; + +use ansi_term::{Colour, ANSIString}; + + +/// Sets the internal logger, changing the log level based on the value of an +/// environment variable. +pub fn configure>(ev: Option) { + let ev = match ev { + Some(v) => v, + None => return, + }; + + let env_var = ev.as_ref(); + if env_var.is_empty() { + return; + } + + if env_var == "trace" { + log::set_max_level(log::LevelFilter::Trace); + } + else { + log::set_max_level(log::LevelFilter::Debug); + } + + let result = log::set_logger(GLOBAL_LOGGER); + if let Err(e) = result { + eprintln!("Failed to initialise logger: {}", e); + } +} + + +#[derive(Debug)] +struct Logger; + +const GLOBAL_LOGGER: &Logger = &Logger; + +impl log::Log for Logger { + fn enabled(&self, _: &log::Metadata<'_>) -> bool { + true // no need to filter after using ‘set_max_level’. + } + + fn log(&self, record: &log::Record<'_>) { + let open = Colour::Fixed(243).paint("["); + let level = level(record.level()); + let close = Colour::Fixed(243).paint("]"); + + eprintln!("{}{} {}{} {}", open, level, record.target(), close, record.args()); + } + + fn flush(&self) { + // no need to flush with ‘eprintln!’. + } +} + +fn level(level: log::Level) -> ANSIString<'static> { + match level { + log::Level::Error => Colour::Red.paint("ERROR"), + log::Level::Warn => Colour::Yellow.paint("WARN"), + log::Level::Info => Colour::Cyan.paint("INFO"), + log::Level::Debug => Colour::Blue.paint("DEBUG"), + log::Level::Trace => Colour::Fixed(245).paint("TRACE"), + } +} diff --git a/src/main.rs b/src/main.rs index 6806b81..5724285 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use std::path::{Component, PathBuf}; use ansi_term::{ANSIStrings, Style}; -use log::debug; +use log::*; use crate::fs::{Dir, File}; use crate::fs::filter::GitIgnore; @@ -20,6 +20,7 @@ use crate::output::{escape, lines, grid, grid_details, details, View, Mode}; mod fs; mod info; +mod logger; mod options; mod output; mod style; @@ -28,7 +29,7 @@ mod style; fn main() { use std::process::exit; - configure_logger(); + logger::configure(env::var_os(vars::EXA_DEBUG)); let args: Vec = env::args_os().skip(1).collect(); match Exa::from_args(args.iter(), &mut stdout()) { @@ -66,34 +67,6 @@ fn main() { } -/// Sets up a global logger if one is asked for. -/// The ‘EXA_DEBUG’ environment variable controls whether log messages are -/// displayed or not. Currently there are just two settings (on and off). -/// -/// This can’t be done in exa’s own option parsing because that part of it -/// logs as well, so by the time execution gets there, the logger needs to -/// have already been set up. -pub fn configure_logger() { - extern crate env_logger; - extern crate log; - - let present = match env::var_os(vars::EXA_DEBUG) { - Some(debug) => debug.len() > 0, - None => false, - }; - - let mut logs = env_logger::Builder::new(); - if present { - logs.filter(None, log::LevelFilter::Debug); - } - else { - logs.filter(None, log::LevelFilter::Off); - } - - logs.init() -} - - /// The main program wrapper. pub struct Exa<'args, 'w, W: Write + 'w> { diff --git a/src/options/style.rs b/src/options/style.rs index 177cb12..133e0df 100644 --- a/src/options/style.rs +++ b/src/options/style.rs @@ -122,7 +122,7 @@ impl Styles { /// Also returns if the EXA_COLORS variable should reset the existing file /// type mappings or not. The `reset` code needs to be the first one. fn parse_color_vars(vars: &V, colours: &mut Colours) -> (ExtensionMappings, bool) { - use log::warn; + use log::*; use crate::options::vars; use crate::style::LSColors; diff --git a/src/output/details.rs b/src/output/details.rs index db607ed..6b79177 100644 --- a/src/output/details.rs +++ b/src/output/details.rs @@ -194,7 +194,7 @@ impl<'a> Render<'a> { /// parallelisable, and uses a pool of threads. fn add_files_to_table<'dir, 'ig>(&self, pool: &mut Pool, table: &mut Option>, rows: &mut Vec, src: &[File<'dir>], git: Option<&'ig GitCache>, depth: TreeDepth) { use std::sync::{Arc, Mutex}; - use log::error; + use log::*; use crate::fs::feature::xattr; let mut file_eggs = (0..src.len()).map(|_| MaybeUninit::uninit()).collect::>(); diff --git a/src/output/table.rs b/src/output/table.rs index 490e712..984c6a2 100644 --- a/src/output/table.rs +++ b/src/output/table.rs @@ -7,7 +7,7 @@ use std::sync::{Mutex, MutexGuard}; use datetime::TimeZone; use zoneinfo_compiled::{CompiledData, Result as TZResult}; -use log::debug; +use log::*; use users::UsersCache;