summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndrew Gallant <jamslam@gmail.com>2018-02-03 21:31:28 -0500
committerAndrew Gallant <jamslam@gmail.com>2018-02-04 10:40:20 -0500
commit35350470942920574e58170c1ab9d419f68af6da (patch)
tree2a791159591e4ff98463213604f4f08121ff4acb /src
parentfe0025549418822c1711886bf136660845e8f433 (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/args.rs13
-rw-r--r--src/logger.rs57
-rw-r--r--src/main.rs2
3 files changed, 64 insertions, 8 deletions
diff --git a/src/args.rs b/src/args.rs
index a0c1d7db..f544db49 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,14 +91,13 @@ impl Args {
pub fn parse() -> Result<Args> {
let matches = app::app().get_matches();
- let mut logb = env_logger::LogBuilder::new();
+ if let Err(err) = Logger::init() {
+ errored!("failed to initialize logger: {}", err);
+ }
if matches.is_present("debug") {
- logb.filter(None, log::LogLevelFilter::Debug);
+ log::set_max_level(log::LevelFilter::Debug);
} else {
- logb.filter(None, log::LogLevelFilter::Warn);
- }
- if let Err(err) = logb.init() {
- errored!("failed to initialize logger: {}", err);
+ 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 00000000..8bd7e09c
--- /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 fea89e44..d6373505 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;