From 35350470942920574e58170c1ab9d419f68af6da Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Sat, 3 Feb 2018 21:31:28 -0500 Subject: [PATCH] logger: drop env_logger This commit updates the `log` crate to 0.4 and drops the dependency on env_logger. In particular, the latest version of env_logger brings in additional non-optional dependencies such as chrono that I don't think is worth including into ripgrep. It turns out ripgrep doesn't need any fancy logging. We just need a concept of log levels and the ability to print to stderr. Therefore, we just roll our own super simple logger. This update is motivated by the persistent configuration task. In particular, we need the ability to toggle the global log level more than once, and this doesn't appear to be possible with older versions of the log crate. --- Cargo.lock | 31 +++++-------------------- Cargo.toml | 3 +-- globset/Cargo.toml | 2 +- grep/Cargo.toml | 2 +- ignore/Cargo.toml | 2 +- src/args.rs | 15 ++++++------ src/logger.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 2 +- 8 files changed, 75 insertions(+), 39 deletions(-) create mode 100644 src/logger.rs diff --git a/Cargo.lock b/Cargo.lock index a5bb387..d9d5d3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,14 +67,6 @@ dependencies = [ "simd 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "env_logger" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "fnv" version = "1.0.6" @@ -106,7 +98,7 @@ dependencies = [ "aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -115,7 +107,7 @@ dependencies = [ name = "grep" version = "0.1.7" dependencies = [ - "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "regex-syntax 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -128,7 +120,7 @@ dependencies = [ "crossbeam 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "globset 0.2.1", "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "same-file 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -150,15 +142,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "log" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "log 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "log" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -237,13 +221,12 @@ dependencies = [ "bytecount 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.29.0 (registry+https://github.com/rust-lang/crates.io-index)", "encoding_rs 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "globset 0.2.1", "grep 0.1.7", "ignore 0.3.1", "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -386,15 +369,13 @@ dependencies = [ "checksum clap 2.29.0 (registry+https://github.com/rust-lang/crates.io-index)" = "110d43e343eb29f4f51c1db31beb879d546db27998577e5715270a54bcf41d3f" "checksum crossbeam 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "24ce9782d4d5c53674646a6a4c1863a21a8fc0cb649b3c94dfc16e45071dea19" "checksum encoding_rs 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f5215aabf22b83153be3ee44dfe3f940214541b2ce13d419c55e7a115c8c51a9" -"checksum env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3ddf21e73e016298f5cb37d6ef8e8da8e39f91f9ec8b0df44b7deb16a9f8cd5b" "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" "checksum fuchsia-zircon 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bd510087c325af53ba24f3be8f1c081b0982319adcb8b03cad764512923ccc19" "checksum fuchsia-zircon-sys 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "08b3a6f13ad6b96572b53ce7af74543132f1a7055ccceb6d073dd36c54481859" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" "checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d" "checksum libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)" = "36fbc8a8929c632868295d0178dd8f63fc423fd7537ad0738372bd010b3ac9b0" -"checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" -"checksum log 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b3a89a0c46ba789b8a247d4c567aed4d7c68e624672d238b45cc3ec20dc9f940" +"checksum log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "89f010e843f2b1a31dbd316b3b8d443758bc634bed37aabade59c686d644e0a2" "checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d" "checksum memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e2ffa2c986de11a9df78620c01eeaaf27d94d3ff02bf81bfcca953102dd0c6ff" "checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30" diff --git a/Cargo.toml b/Cargo.toml index e6f73ef..61f0b73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,12 +37,11 @@ atty = "0.2.2" bytecount = "0.3.1" clap = "2.26" encoding_rs = "0.7" -env_logger = { version = "0.4", default-features = false } grep = { version = "0.1.7", path = "grep" } ignore = { version = "0.3.1", path = "ignore" } lazy_static = "1" libc = "0.2" -log = "0.3" +log = "0.4" memchr = "2" memmap = "0.6" num_cpus = "1" diff --git a/globset/Cargo.toml b/globset/Cargo.toml index 96d47e3..145166d 100644 --- a/globset/Cargo.toml +++ b/globset/Cargo.toml @@ -21,7 +21,7 @@ bench = false [dependencies] aho-corasick = "0.6.0" fnv = "1.0" -log = "0.3" +log = "0.4" memchr = "2" regex = "0.2.1" diff --git a/grep/Cargo.toml b/grep/Cargo.toml index ddce71a..34f96bc 100644 --- a/grep/Cargo.toml +++ b/grep/Cargo.toml @@ -13,7 +13,7 @@ keywords = ["regex", "grep", "egrep", "search", "pattern"] license = "Unlicense/MIT" [dependencies] -log = "0.3" +log = "0.4" memchr = "2" regex = "0.2.1" regex-syntax = "0.4.0" diff --git a/ignore/Cargo.toml b/ignore/Cargo.toml index 89d8672..7948cd7 100644 --- a/ignore/Cargo.toml +++ b/ignore/Cargo.toml @@ -21,7 +21,7 @@ bench = false crossbeam = "0.3" globset = { version = "0.2.1", path = "../globset" } lazy_static = "1" -log = "0.3" +log = "0.4" memchr = "2" regex = "0.2.1" same-file = "1" diff --git a/src/args.rs b/src/args.rs index a0c1d7d..f544db4 100644 --- a/src/args.rs +++ b/src/args.rs @@ -10,7 +10,6 @@ use std::sync::atomic::{AtomicBool, Ordering}; use clap; use encoding_rs::Encoding; -use env_logger; use grep::{Grep, GrepBuilder, Error as GrepError}; use log; use num_cpus; @@ -27,6 +26,7 @@ use printer::{ColorSpecs, Printer}; use unescape::unescape; use worker::{Worker, WorkerBuilder}; +use logger::Logger; use Result; /// `Args` are transformed/normalized from `ArgMatches`. @@ -91,15 +91,14 @@ impl Args { pub fn parse() -> Result { let matches = app::app().get_matches(); - let mut logb = env_logger::LogBuilder::new(); - if matches.is_present("debug") { - logb.filter(None, log::LogLevelFilter::Debug); - } else { - logb.filter(None, log::LogLevelFilter::Warn); - } - if let Err(err) = logb.init() { + if let Err(err) = Logger::init() { errored!("failed to initialize logger: {}", err); } + if matches.is_present("debug") { + log::set_max_level(log::LevelFilter::Debug); + } else { + log::set_max_level(log::LevelFilter::Warn); + } ArgMatches(matches).to_args() } diff --git a/src/logger.rs b/src/logger.rs new file mode 100644 index 0000000..8bd7e09 --- /dev/null +++ b/src/logger.rs @@ -0,0 +1,57 @@ +// This module defines a super simple logger that works with the `log` crate. +// We don't need anything fancy; just basic log levels and the ability to +// print to stderr. We therefore avoid bringing in extra dependencies just +// for this functionality. + +use log::{self, Log}; + +/// The simplest possible logger that logs to stderr. +/// +/// This logger does no filtering. Instead, it relies on the `log` crates +/// filtering via its global max_level setting. +#[derive(Debug)] +pub struct Logger(()); + +const LOGGER: &'static Logger = &Logger(()); + +impl Logger { + /// Create a new logger that logs to stderr and initialize it as the + /// global logger. If there was a problem setting the logger, then an + /// error is returned. + pub fn init() -> Result<(), log::SetLoggerError> { + log::set_logger(LOGGER) + } +} + +impl Log for Logger { + fn enabled(&self, _: &log::Metadata) -> bool { + // We set the log level via log::set_max_level, so we don't need to + // implement filtering here. + true + } + + fn log(&self, record: &log::Record) { + match (record.file(), record.line()) { + (Some(file), Some(line)) => { + eprintln!( + "{}/{}/{}:{}: {}", + record.level(), record.target(), + file, line, record.args()); + } + (Some(file), None) => { + eprintln!( + "{}/{}/{}: {}", + record.level(), record.target(), file, record.args()); + } + _ => { + eprintln!( + "{}/{}: {}", + record.level(), record.target(), record.args()); + } + } + } + + fn flush(&self) { + // We use eprintln! which is flushed on every call. + } +} diff --git a/src/main.rs b/src/main.rs index fea89e4..d637350 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ extern crate bytecount; #[macro_use] extern crate clap; extern crate encoding_rs; -extern crate env_logger; extern crate globset; extern crate grep; extern crate ignore; @@ -42,6 +41,7 @@ mod app; mod args; mod decoder; mod decompressor; +mod logger; mod pathutil; mod printer; mod search_buffer;