From c645b93899f74f809e0cf0d15bcc60412ec14d09 Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Wed, 30 Dec 2015 08:49:37 +0100 Subject: [PATCH] Introduce optional logging --- Cargo.lock | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 5 +++++ README.md | 14 ++++++++++++++ src/main.rs | 16 ++++++++++++++++ 4 files changed, 90 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 1411234..1e5b92e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,6 +3,16 @@ name = "tldr" version = "0.1.0" dependencies = [ "ansi_term 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "aho-corasick" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -10,3 +20,48 @@ name = "ansi_term" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "env_logger" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "log" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "memchr" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + diff --git a/Cargo.toml b/Cargo.toml index fa72330..bd61a84 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,5 +3,10 @@ name = "tldr" version = "0.1.0" authors = ["Danilo Bargen "] +[features] +logging = ["env_logger"] + [dependencies] ansi_term = "^0.7" +log = "^0.3" +env_logger = { version = "^0.3", optional = true } diff --git a/README.md b/README.md index f6f94cb..a0c1eeb 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,20 @@ An implementation of [tldr](https://github.com/tldr-pages/tldr) in Rust. +## Building + +Debug build with logging enabled: + + $ cargo build --features logging + +Release build without logging: + + $ cargo build --release + +To enable the log output, set the `RUST_LOG` env variable: + + $ export RUST_LOG=tldr=debug + ## License MIT diff --git a/src/main.rs b/src/main.rs index 187b90c..adb794a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,7 @@ +//! An implementation of [tldr](https://github.com/tldr-pages/tldr) in Rust. + +#[macro_use] extern crate log; +#[cfg(feature = "logging")]extern crate env_logger; extern crate ansi_term; use std::io::BufReader; @@ -20,8 +24,20 @@ fn get_file_reader(filepath: &str) -> Result, String> { } +#[cfg(feature = "logging")] +fn init_log() { + env_logger::init().unwrap(); +} + +#[cfg(not(feature = "logging"))] +fn init_log() { } + + fn main() { + // Initialize logger + init_log(); + // Parse arguments let args: Vec<_> = env::args().collect(); if args.len() != 2 {