Introduce optional logging

This commit is contained in:
Danilo Bargen 2015-12-30 08:49:37 +01:00
commit c645b93899
4 changed files with 90 additions and 0 deletions

55
Cargo.lock generated
View file

@ -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"

View file

@ -3,5 +3,10 @@ name = "tldr"
version = "0.1.0"
authors = ["Danilo Bargen <mail@dbrgn.ch>"]
[features]
logging = ["env_logger"]
[dependencies]
ansi_term = "^0.7"
log = "^0.3"
env_logger = { version = "^0.3", optional = true }

View file

@ -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

View file

@ -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<BufReader<File>, 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 {