зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1451825 - Update to env_logger 0.5. r=ted
MozReview-Commit-ID: CqKdONY1NMT --HG-- extra : rebase_source : 29f9c7a40cf71847a6f5e5e3f33e8695e9bd0b64
This commit is contained in:
Родитель
3319ebe86f
Коммит
4aa522320e
|
@ -545,14 +545,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 = "env_logger"
|
||||
version = "0.5.6"
|
||||
|
@ -868,11 +860,11 @@ version = "0.1.4"
|
|||
dependencies = [
|
||||
"bindgen 0.33.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"cmake 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"env_logger 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.39 (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)",
|
||||
"mozjs_sys 0.0.0",
|
||||
"num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
@ -1917,7 +1909,7 @@ dependencies = [
|
|||
"core-foundation-sys 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libudev 0.2.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)",
|
||||
"rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"runloop 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
@ -2288,7 +2280,6 @@ dependencies = [
|
|||
"checksum either 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18785c1ba806c258137c937e44ada9ee7e69a37e3c72077542cd2f069d78562a"
|
||||
"checksum encoding_c 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "93ec52324ca72f423237a413ca0e1c60654c8b3d0934fcd5fd888508dfcc4ba7"
|
||||
"checksum encoding_rs 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "98fd0f24d1fb71a4a6b9330c8ca04cbd4e7cc5d846b54ca74ff376bc7c9f798d"
|
||||
"checksum env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3ddf21e73e016298f5cb37d6ef8e8da8e39f91f9ec8b0df44b7deb16a9f8cd5b"
|
||||
"checksum env_logger 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0561146661ae44c579e993456bc76d11ce1e0c7d745e57b2fa7146b6e49fa2ad"
|
||||
"checksum error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff511d5dc435d703f4971bc399647c9bc38e20cb41452e3b9feb4765419ed3f3"
|
||||
"checksum euclid 0.17.2 (registry+https://github.com/rust-lang/crates.io-index)" = "adfe67a9343519c1449d208da5998c6de582de698f7a39c4ac82ffba23d131a5"
|
||||
|
|
|
@ -14,7 +14,7 @@ winapi = "0.2.8"
|
|||
|
||||
[dependencies]
|
||||
rand = "0.3"
|
||||
log = "0.3"
|
||||
log = "0.4"
|
||||
libc = "^0.2"
|
||||
boxfnonce = "0.0.3"
|
||||
runloop = "0.1.0"
|
||||
|
@ -23,4 +23,4 @@ bitflags = "1.0"
|
|||
[dev-dependencies]
|
||||
rust-crypto = "^0.2"
|
||||
base64 = "^0.4"
|
||||
env_logger = "0.4.1"
|
||||
env_logger = "0.5"
|
||||
|
|
|
@ -40,7 +40,7 @@ fn u2f_get_key_handle_from_register_response(register_response: &Vec<u8>) -> io:
|
|||
}
|
||||
|
||||
fn main() {
|
||||
env_logger::init().expect("Cannot start logger");
|
||||
env_logger::init();
|
||||
|
||||
println!("Asking a security key to register now...");
|
||||
let challenge_str = format!("{}{}",
|
||||
|
|
|
@ -10,7 +10,7 @@ use util::io_err;
|
|||
use log;
|
||||
|
||||
fn trace_hex(data: &[u8]) {
|
||||
if log_enabled!(log::LogLevel::Trace) {
|
||||
if log_enabled!(log::Level::Trace) {
|
||||
let parts: Vec<String> = data.iter().map(|byte| format!("{:02x}", byte)).collect();
|
||||
trace!("USB send: {}", parts.join(""));
|
||||
}
|
||||
|
|
|
@ -6,8 +6,7 @@ build = "build.rs"
|
|||
license = "MPL-2.0"
|
||||
|
||||
[build-dependencies]
|
||||
env_logger = {version = "0.4", default-features = false} # disable `regex` to reduce code size
|
||||
log = "0.3"
|
||||
env_logger = {version = "0.5", default-features = false} # disable `regex` to reduce code size
|
||||
bindgen = {version = "0.33.1", default-features = false} # disable `logging` to reduce code size
|
||||
cmake = "0.1"
|
||||
glob = "0.2.11"
|
||||
|
@ -45,5 +44,5 @@ path = "../src"
|
|||
[dependencies]
|
||||
lazy_static = "1.0"
|
||||
libc = "0.2"
|
||||
log = "0.3"
|
||||
log = "0.4"
|
||||
num-traits = "0.1.32"
|
||||
|
|
|
@ -4,22 +4,14 @@
|
|||
|
||||
extern crate bindgen;
|
||||
extern crate cmake;
|
||||
extern crate glob;
|
||||
|
||||
extern crate log;
|
||||
extern crate env_logger;
|
||||
extern crate glob;
|
||||
|
||||
use std::env;
|
||||
use std::path;
|
||||
|
||||
fn main() {
|
||||
log::set_logger(|max_log_level| {
|
||||
use env_logger::Logger;
|
||||
let env_logger = Logger::new();
|
||||
max_log_level.set(env_logger.filter());
|
||||
Box::new(env_logger)
|
||||
}).expect("Failed to set logger.");
|
||||
|
||||
env_logger::init();
|
||||
build_jsapi_bindings();
|
||||
build_jsglue_cpp();
|
||||
}
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
{"files":{"Cargo.toml":"3f4cf313ce063b0d3888a3acdc3a31ed99432415aa4204b143fd39de0d037996","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"6485b8ed310d3f0340bf1ad1f47645069ce4069dcc6bb46c7d5c6faf41de1fdb","src/lib.rs":"5fac719110645152e649993bfd7edb8ca755155844867cbfa8077b486ddde9e1","src/regex.rs":"d8e2a6958d4ed8084867063aae4b5c77ffc5d271dc2e17909d56c5a5e1552034","src/string.rs":"26ede9ab41a2673c3ad6001bc1802c005ce9a4f190f55860a24aa66b6b71bbc7","tests/regexp_filter.rs":"a3f9c01623e90e54b247a62c53b25caf5f502d054f28c0bdf92abbea486a95b5"},"package":"3ddf21e73e016298f5cb37d6ef8e8da8e39f91f9ec8b0df44b7deb16a9f8cd5b"}
|
|
@ -1,35 +0,0 @@
|
|||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||
#
|
||||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g. crates.io) dependencies
|
||||
#
|
||||
# If you believe there's an error in this file please file an
|
||||
# issue against the rust-lang/cargo repository. If you're
|
||||
# editing this file be aware that the upstream Cargo.toml
|
||||
# will likely look very different (and much more reasonable)
|
||||
|
||||
[package]
|
||||
name = "env_logger"
|
||||
version = "0.4.3"
|
||||
authors = ["The Rust Project Developers"]
|
||||
description = "A logging implementation for `log` which is configured via an environment\nvariable.\n"
|
||||
homepage = "https://github.com/rust-lang/log"
|
||||
documentation = "http://doc.rust-lang.org/log/env_logger"
|
||||
categories = ["development-tools::debugging"]
|
||||
license = "MIT/Apache-2.0"
|
||||
repository = "https://github.com/rust-lang/log"
|
||||
|
||||
[[test]]
|
||||
name = "regexp_filter"
|
||||
harness = false
|
||||
[dependencies.regex]
|
||||
version = "0.2"
|
||||
optional = true
|
||||
|
||||
[dependencies.log]
|
||||
version = "0.3"
|
||||
|
||||
[features]
|
||||
default = ["regex"]
|
|
@ -1,201 +0,0 @@
|
|||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
|
@ -1,25 +0,0 @@
|
|||
Copyright (c) 2014 The Rust Project Developers
|
||||
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without
|
||||
limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
|
@ -1,652 +0,0 @@
|
|||
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
//! A logger configured via an environment variable which writes to standard
|
||||
//! error.
|
||||
//!
|
||||
//! ## Example
|
||||
//!
|
||||
//! ```
|
||||
//! #[macro_use] extern crate log;
|
||||
//! extern crate env_logger;
|
||||
//!
|
||||
//! use log::LogLevel;
|
||||
//!
|
||||
//! fn main() {
|
||||
//! env_logger::init().unwrap();
|
||||
//!
|
||||
//! debug!("this is a debug {}", "message");
|
||||
//! error!("this is printed by default");
|
||||
//!
|
||||
//! if log_enabled!(LogLevel::Info) {
|
||||
//! let x = 3 * 4; // expensive computation
|
||||
//! info!("the answer was: {}", x);
|
||||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! Assumes the binary is `main`:
|
||||
//!
|
||||
//! ```{.bash}
|
||||
//! $ RUST_LOG=error ./main
|
||||
//! ERROR:main: this is printed by default
|
||||
//! ```
|
||||
//!
|
||||
//! ```{.bash}
|
||||
//! $ RUST_LOG=info ./main
|
||||
//! ERROR:main: this is printed by default
|
||||
//! INFO:main: the answer was: 12
|
||||
//! ```
|
||||
//!
|
||||
//! ```{.bash}
|
||||
//! $ RUST_LOG=debug ./main
|
||||
//! DEBUG:main: this is a debug message
|
||||
//! ERROR:main: this is printed by default
|
||||
//! INFO:main: the answer was: 12
|
||||
//! ```
|
||||
//!
|
||||
//! You can also set the log level on a per module basis:
|
||||
//!
|
||||
//! ```{.bash}
|
||||
//! $ RUST_LOG=main=info ./main
|
||||
//! ERROR:main: this is printed by default
|
||||
//! INFO:main: the answer was: 12
|
||||
//! ```
|
||||
//!
|
||||
//! And enable all logging:
|
||||
//!
|
||||
//! ```{.bash}
|
||||
//! $ RUST_LOG=main ./main
|
||||
//! DEBUG:main: this is a debug message
|
||||
//! ERROR:main: this is printed by default
|
||||
//! INFO:main: the answer was: 12
|
||||
//! ```
|
||||
//!
|
||||
//! See the documentation for the log crate for more information about its API.
|
||||
//!
|
||||
//! ## Enabling logging
|
||||
//!
|
||||
//! Log levels are controlled on a per-module basis, and by default all logging
|
||||
//! is disabled except for `error!`. Logging is controlled via the `RUST_LOG`
|
||||
//! environment variable. The value of this environment variable is a
|
||||
//! comma-separated list of logging directives. A logging directive is of the
|
||||
//! form:
|
||||
//!
|
||||
//! ```text
|
||||
//! path::to::module=log_level
|
||||
//! ```
|
||||
//!
|
||||
//! The path to the module is rooted in the name of the crate it was compiled
|
||||
//! for, so if your program is contained in a file `hello.rs`, for example, to
|
||||
//! turn on logging for this file you would use a value of `RUST_LOG=hello`.
|
||||
//! Furthermore, this path is a prefix-search, so all modules nested in the
|
||||
//! specified module will also have logging enabled.
|
||||
//!
|
||||
//! The actual `log_level` is optional to specify. If omitted, all logging will
|
||||
//! be enabled. If specified, it must be one of the strings `debug`, `error`,
|
||||
//! `info`, `warn`, or `trace`.
|
||||
//!
|
||||
//! As the log level for a module is optional, the module to enable logging for
|
||||
//! is also optional. If only a `log_level` is provided, then the global log
|
||||
//! level for all modules is set to this value.
|
||||
//!
|
||||
//! Some examples of valid values of `RUST_LOG` are:
|
||||
//!
|
||||
//! * `hello` turns on all logging for the 'hello' module
|
||||
//! * `info` turns on all info logging
|
||||
//! * `hello=debug` turns on debug logging for 'hello'
|
||||
//! * `hello,std::option` turns on hello, and std's option logging
|
||||
//! * `error,hello=warn` turn on global error logging and also warn for hello
|
||||
//!
|
||||
//! ## Filtering results
|
||||
//!
|
||||
//! A RUST_LOG directive may include a regex filter. The syntax is to append `/`
|
||||
//! followed by a regex. Each message is checked against the regex, and is only
|
||||
//! logged if it matches. Note that the matching is done after formatting the
|
||||
//! log string but before adding any logging meta-data. There is a single filter
|
||||
//! for all modules.
|
||||
//!
|
||||
//! Some examples:
|
||||
//!
|
||||
//! * `hello/foo` turns on all logging for the 'hello' module where the log
|
||||
//! message includes 'foo'.
|
||||
//! * `info/f.o` turns on all info logging where the log message includes 'foo',
|
||||
//! 'f1o', 'fao', etc.
|
||||
//! * `hello=debug/foo*foo` turns on debug logging for 'hello' where the log
|
||||
//! message includes 'foofoo' or 'fofoo' or 'fooooooofoo', etc.
|
||||
//! * `error,hello=warn/[0-9] scopes` turn on global error logging and also
|
||||
//! warn for hello. In both cases the log message must include a single digit
|
||||
//! number followed by 'scopes'.
|
||||
|
||||
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
||||
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
|
||||
html_root_url = "http://doc.rust-lang.org/env_logger/")]
|
||||
#![cfg_attr(test, deny(warnings))]
|
||||
|
||||
// When compiled for the rustc compiler itself we want to make sure that this is
|
||||
// an unstable crate
|
||||
#![cfg_attr(rustbuild, feature(staged_api, rustc_private))]
|
||||
#![cfg_attr(rustbuild, unstable(feature = "rustc_private", issue = "27812"))]
|
||||
|
||||
extern crate log;
|
||||
|
||||
use std::env;
|
||||
use std::io::prelude::*;
|
||||
use std::io;
|
||||
use std::mem;
|
||||
|
||||
use log::{Log, LogLevel, LogLevelFilter, LogRecord, SetLoggerError, LogMetadata};
|
||||
|
||||
#[cfg(feature = "regex")]
|
||||
#[path = "regex.rs"]
|
||||
mod filter;
|
||||
|
||||
#[cfg(not(feature = "regex"))]
|
||||
#[path = "string.rs"]
|
||||
mod filter;
|
||||
|
||||
/// Log target, either stdout or stderr.
|
||||
#[derive(Debug)]
|
||||
pub enum LogTarget {
|
||||
Stdout,
|
||||
Stderr,
|
||||
}
|
||||
|
||||
/// The logger.
|
||||
pub struct Logger {
|
||||
directives: Vec<LogDirective>,
|
||||
filter: Option<filter::Filter>,
|
||||
format: Box<Fn(&LogRecord) -> String + Sync + Send>,
|
||||
target: LogTarget,
|
||||
}
|
||||
|
||||
/// LogBuilder acts as builder for initializing the Logger.
|
||||
/// It can be used to customize the log format, change the enviromental variable used
|
||||
/// to provide the logging directives and also set the default log level filter.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```
|
||||
/// #[macro_use]
|
||||
/// extern crate log;
|
||||
/// extern crate env_logger;
|
||||
///
|
||||
/// use std::env;
|
||||
/// use log::{LogRecord, LogLevelFilter};
|
||||
/// use env_logger::LogBuilder;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let format = |record: &LogRecord| {
|
||||
/// format!("{} - {}", record.level(), record.args())
|
||||
/// };
|
||||
///
|
||||
/// let mut builder = LogBuilder::new();
|
||||
/// builder.format(format).filter(None, LogLevelFilter::Info);
|
||||
///
|
||||
/// if env::var("RUST_LOG").is_ok() {
|
||||
/// builder.parse(&env::var("RUST_LOG").unwrap());
|
||||
/// }
|
||||
///
|
||||
/// builder.init().unwrap();
|
||||
///
|
||||
/// error!("error message");
|
||||
/// info!("info message");
|
||||
/// }
|
||||
/// ```
|
||||
pub struct LogBuilder {
|
||||
directives: Vec<LogDirective>,
|
||||
filter: Option<filter::Filter>,
|
||||
format: Box<Fn(&LogRecord) -> String + Sync + Send>,
|
||||
target: LogTarget,
|
||||
}
|
||||
|
||||
impl LogBuilder {
|
||||
/// Initializes the log builder with defaults
|
||||
pub fn new() -> LogBuilder {
|
||||
LogBuilder {
|
||||
directives: Vec::new(),
|
||||
filter: None,
|
||||
format: Box::new(|record: &LogRecord| {
|
||||
format!("{}:{}: {}", record.level(),
|
||||
record.location().module_path(), record.args())
|
||||
}),
|
||||
target: LogTarget::Stderr,
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds filters to the logger
|
||||
///
|
||||
/// The given module (if any) will log at most the specified level provided.
|
||||
/// If no module is provided then the filter will apply to all log messages.
|
||||
pub fn filter(&mut self,
|
||||
module: Option<&str>,
|
||||
level: LogLevelFilter) -> &mut Self {
|
||||
self.directives.push(LogDirective {
|
||||
name: module.map(|s| s.to_string()),
|
||||
level: level,
|
||||
});
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the format function for formatting the log output.
|
||||
///
|
||||
/// This function is called on each record logged to produce a string which
|
||||
/// is actually printed out.
|
||||
pub fn format<F: 'static>(&mut self, format: F) -> &mut Self
|
||||
where F: Fn(&LogRecord) -> String + Sync + Send
|
||||
{
|
||||
self.format = Box::new(format);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the target for the log output.
|
||||
///
|
||||
/// Env logger can log to either stdout or stderr. The default is stderr.
|
||||
pub fn target(&mut self, target: LogTarget) -> &mut Self {
|
||||
self.target = target;
|
||||
self
|
||||
}
|
||||
|
||||
/// Parses the directives string in the same form as the RUST_LOG
|
||||
/// environment variable.
|
||||
///
|
||||
/// See the module documentation for more details.
|
||||
pub fn parse(&mut self, filters: &str) -> &mut Self {
|
||||
let (directives, filter) = parse_logging_spec(filters);
|
||||
|
||||
self.filter = filter;
|
||||
|
||||
for directive in directives {
|
||||
self.directives.push(directive);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Initializes the global logger with an env logger.
|
||||
///
|
||||
/// This should be called early in the execution of a Rust program, and the
|
||||
/// global logger may only be initialized once. Future initialization
|
||||
/// attempts will return an error.
|
||||
pub fn init(&mut self) -> Result<(), SetLoggerError> {
|
||||
log::set_logger(|max_level| {
|
||||
let logger = self.build();
|
||||
max_level.set(logger.filter());
|
||||
Box::new(logger)
|
||||
})
|
||||
}
|
||||
|
||||
/// Build an env logger.
|
||||
pub fn build(&mut self) -> Logger {
|
||||
if self.directives.is_empty() {
|
||||
// Adds the default filter if none exist
|
||||
self.directives.push(LogDirective {
|
||||
name: None,
|
||||
level: LogLevelFilter::Error,
|
||||
});
|
||||
} else {
|
||||
// Sort the directives by length of their name, this allows a
|
||||
// little more efficient lookup at runtime.
|
||||
self.directives.sort_by(|a, b| {
|
||||
let alen = a.name.as_ref().map(|a| a.len()).unwrap_or(0);
|
||||
let blen = b.name.as_ref().map(|b| b.len()).unwrap_or(0);
|
||||
alen.cmp(&blen)
|
||||
});
|
||||
}
|
||||
|
||||
Logger {
|
||||
directives: mem::replace(&mut self.directives, Vec::new()),
|
||||
filter: mem::replace(&mut self.filter, None),
|
||||
format: mem::replace(&mut self.format, Box::new(|_| String::new())),
|
||||
target: mem::replace(&mut self.target, LogTarget::Stderr),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Logger {
|
||||
pub fn new() -> Logger {
|
||||
let mut builder = LogBuilder::new();
|
||||
|
||||
if let Ok(s) = env::var("RUST_LOG") {
|
||||
builder.parse(&s);
|
||||
}
|
||||
|
||||
builder.build()
|
||||
}
|
||||
|
||||
pub fn filter(&self) -> LogLevelFilter {
|
||||
self.directives.iter()
|
||||
.map(|d| d.level).max()
|
||||
.unwrap_or(LogLevelFilter::Off)
|
||||
}
|
||||
|
||||
fn enabled(&self, level: LogLevel, target: &str) -> bool {
|
||||
// Search for the longest match, the vector is assumed to be pre-sorted.
|
||||
for directive in self.directives.iter().rev() {
|
||||
match directive.name {
|
||||
Some(ref name) if !target.starts_with(&**name) => {},
|
||||
Some(..) | None => {
|
||||
return level <= directive.level
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl Log for Logger {
|
||||
fn enabled(&self, metadata: &LogMetadata) -> bool {
|
||||
self.enabled(metadata.level(), metadata.target())
|
||||
}
|
||||
|
||||
fn log(&self, record: &LogRecord) {
|
||||
if !Log::enabled(self, record.metadata()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(filter) = self.filter.as_ref() {
|
||||
if !filter.is_match(&*record.args().to_string()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
match self.target {
|
||||
LogTarget::Stdout => println!("{}", (self.format)(record)),
|
||||
LogTarget::Stderr => {
|
||||
let _ = writeln!(&mut io::stderr(), "{}", (self.format)(record));
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
struct LogDirective {
|
||||
name: Option<String>,
|
||||
level: LogLevelFilter,
|
||||
}
|
||||
|
||||
/// Initializes the global logger with an env logger.
|
||||
///
|
||||
/// This should be called early in the execution of a Rust program, and the
|
||||
/// global logger may only be initialized once. Future initialization attempts
|
||||
/// will return an error.
|
||||
pub fn init() -> Result<(), SetLoggerError> {
|
||||
let mut builder = LogBuilder::new();
|
||||
|
||||
if let Ok(s) = env::var("RUST_LOG") {
|
||||
builder.parse(&s);
|
||||
}
|
||||
|
||||
builder.init()
|
||||
}
|
||||
|
||||
/// Parse a logging specification string (e.g: "crate1,crate2::mod3,crate3::x=error/foo")
|
||||
/// and return a vector with log directives.
|
||||
fn parse_logging_spec(spec: &str) -> (Vec<LogDirective>, Option<filter::Filter>) {
|
||||
let mut dirs = Vec::new();
|
||||
|
||||
let mut parts = spec.split('/');
|
||||
let mods = parts.next();
|
||||
let filter = parts.next();
|
||||
if parts.next().is_some() {
|
||||
println!("warning: invalid logging spec '{}', \
|
||||
ignoring it (too many '/'s)", spec);
|
||||
return (dirs, None);
|
||||
}
|
||||
mods.map(|m| { for s in m.split(',') {
|
||||
if s.len() == 0 { continue }
|
||||
let mut parts = s.split('=');
|
||||
let (log_level, name) = match (parts.next(), parts.next().map(|s| s.trim()), parts.next()) {
|
||||
(Some(part0), None, None) => {
|
||||
// if the single argument is a log-level string or number,
|
||||
// treat that as a global fallback
|
||||
match part0.parse() {
|
||||
Ok(num) => (num, None),
|
||||
Err(_) => (LogLevelFilter::max(), Some(part0)),
|
||||
}
|
||||
}
|
||||
(Some(part0), Some(""), None) => (LogLevelFilter::max(), Some(part0)),
|
||||
(Some(part0), Some(part1), None) => {
|
||||
match part1.parse() {
|
||||
Ok(num) => (num, Some(part0)),
|
||||
_ => {
|
||||
println!("warning: invalid logging spec '{}', \
|
||||
ignoring it", part1);
|
||||
continue
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
println!("warning: invalid logging spec '{}', \
|
||||
ignoring it", s);
|
||||
continue
|
||||
}
|
||||
};
|
||||
dirs.push(LogDirective {
|
||||
name: name.map(|s| s.to_string()),
|
||||
level: log_level,
|
||||
});
|
||||
}});
|
||||
|
||||
let filter = filter.map_or(None, |filter| {
|
||||
match filter::Filter::new(filter) {
|
||||
Ok(re) => Some(re),
|
||||
Err(e) => {
|
||||
println!("warning: invalid regex filter - {}", e);
|
||||
None
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return (dirs, filter);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use log::{LogLevel, LogLevelFilter};
|
||||
|
||||
use super::{LogBuilder, Logger, LogDirective, parse_logging_spec};
|
||||
|
||||
fn make_logger(dirs: Vec<LogDirective>) -> Logger {
|
||||
let mut logger = LogBuilder::new().build();
|
||||
logger.directives = dirs;
|
||||
logger
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filter_info() {
|
||||
let logger = LogBuilder::new().filter(None, LogLevelFilter::Info).build();
|
||||
assert!(logger.enabled(LogLevel::Info, "crate1"));
|
||||
assert!(!logger.enabled(LogLevel::Debug, "crate1"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filter_beginning_longest_match() {
|
||||
let logger = LogBuilder::new()
|
||||
.filter(Some("crate2"), LogLevelFilter::Info)
|
||||
.filter(Some("crate2::mod"), LogLevelFilter::Debug)
|
||||
.filter(Some("crate1::mod1"), LogLevelFilter::Warn)
|
||||
.build();
|
||||
assert!(logger.enabled(LogLevel::Debug, "crate2::mod1"));
|
||||
assert!(!logger.enabled(LogLevel::Debug, "crate2"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_default() {
|
||||
let logger = LogBuilder::new().parse("info,crate1::mod1=warn").build();
|
||||
assert!(logger.enabled(LogLevel::Warn, "crate1::mod1"));
|
||||
assert!(logger.enabled(LogLevel::Info, "crate2::mod2"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn match_full_path() {
|
||||
let logger = make_logger(vec![
|
||||
LogDirective {
|
||||
name: Some("crate2".to_string()),
|
||||
level: LogLevelFilter::Info
|
||||
},
|
||||
LogDirective {
|
||||
name: Some("crate1::mod1".to_string()),
|
||||
level: LogLevelFilter::Warn
|
||||
}
|
||||
]);
|
||||
assert!(logger.enabled(LogLevel::Warn, "crate1::mod1"));
|
||||
assert!(!logger.enabled(LogLevel::Info, "crate1::mod1"));
|
||||
assert!(logger.enabled(LogLevel::Info, "crate2"));
|
||||
assert!(!logger.enabled(LogLevel::Debug, "crate2"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_match() {
|
||||
let logger = make_logger(vec![
|
||||
LogDirective { name: Some("crate2".to_string()), level: LogLevelFilter::Info },
|
||||
LogDirective { name: Some("crate1::mod1".to_string()), level: LogLevelFilter::Warn }
|
||||
]);
|
||||
assert!(!logger.enabled(LogLevel::Warn, "crate3"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn match_beginning() {
|
||||
let logger = make_logger(vec![
|
||||
LogDirective { name: Some("crate2".to_string()), level: LogLevelFilter::Info },
|
||||
LogDirective { name: Some("crate1::mod1".to_string()), level: LogLevelFilter::Warn }
|
||||
]);
|
||||
assert!(logger.enabled(LogLevel::Info, "crate2::mod1"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn match_beginning_longest_match() {
|
||||
let logger = make_logger(vec![
|
||||
LogDirective { name: Some("crate2".to_string()), level: LogLevelFilter::Info },
|
||||
LogDirective { name: Some("crate2::mod".to_string()), level: LogLevelFilter::Debug },
|
||||
LogDirective { name: Some("crate1::mod1".to_string()), level: LogLevelFilter::Warn }
|
||||
]);
|
||||
assert!(logger.enabled(LogLevel::Debug, "crate2::mod1"));
|
||||
assert!(!logger.enabled(LogLevel::Debug, "crate2"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn match_default() {
|
||||
let logger = make_logger(vec![
|
||||
LogDirective { name: None, level: LogLevelFilter::Info },
|
||||
LogDirective { name: Some("crate1::mod1".to_string()), level: LogLevelFilter::Warn }
|
||||
]);
|
||||
assert!(logger.enabled(LogLevel::Warn, "crate1::mod1"));
|
||||
assert!(logger.enabled(LogLevel::Info, "crate2::mod2"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_level() {
|
||||
let logger = make_logger(vec![
|
||||
LogDirective { name: None, level: LogLevelFilter::Info },
|
||||
LogDirective { name: Some("crate1::mod1".to_string()), level: LogLevelFilter::Off }
|
||||
]);
|
||||
assert!(!logger.enabled(LogLevel::Error, "crate1::mod1"));
|
||||
assert!(logger.enabled(LogLevel::Info, "crate2::mod2"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_logging_spec_valid() {
|
||||
let (dirs, filter) = parse_logging_spec("crate1::mod1=error,crate1::mod2,crate2=debug");
|
||||
assert_eq!(dirs.len(), 3);
|
||||
assert_eq!(dirs[0].name, Some("crate1::mod1".to_string()));
|
||||
assert_eq!(dirs[0].level, LogLevelFilter::Error);
|
||||
|
||||
assert_eq!(dirs[1].name, Some("crate1::mod2".to_string()));
|
||||
assert_eq!(dirs[1].level, LogLevelFilter::max());
|
||||
|
||||
assert_eq!(dirs[2].name, Some("crate2".to_string()));
|
||||
assert_eq!(dirs[2].level, LogLevelFilter::Debug);
|
||||
assert!(filter.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_logging_spec_invalid_crate() {
|
||||
// test parse_logging_spec with multiple = in specification
|
||||
let (dirs, filter) = parse_logging_spec("crate1::mod1=warn=info,crate2=debug");
|
||||
assert_eq!(dirs.len(), 1);
|
||||
assert_eq!(dirs[0].name, Some("crate2".to_string()));
|
||||
assert_eq!(dirs[0].level, LogLevelFilter::Debug);
|
||||
assert!(filter.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_logging_spec_invalid_log_level() {
|
||||
// test parse_logging_spec with 'noNumber' as log level
|
||||
let (dirs, filter) = parse_logging_spec("crate1::mod1=noNumber,crate2=debug");
|
||||
assert_eq!(dirs.len(), 1);
|
||||
assert_eq!(dirs[0].name, Some("crate2".to_string()));
|
||||
assert_eq!(dirs[0].level, LogLevelFilter::Debug);
|
||||
assert!(filter.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_logging_spec_string_log_level() {
|
||||
// test parse_logging_spec with 'warn' as log level
|
||||
let (dirs, filter) = parse_logging_spec("crate1::mod1=wrong,crate2=warn");
|
||||
assert_eq!(dirs.len(), 1);
|
||||
assert_eq!(dirs[0].name, Some("crate2".to_string()));
|
||||
assert_eq!(dirs[0].level, LogLevelFilter::Warn);
|
||||
assert!(filter.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_logging_spec_empty_log_level() {
|
||||
// test parse_logging_spec with '' as log level
|
||||
let (dirs, filter) = parse_logging_spec("crate1::mod1=wrong,crate2=");
|
||||
assert_eq!(dirs.len(), 1);
|
||||
assert_eq!(dirs[0].name, Some("crate2".to_string()));
|
||||
assert_eq!(dirs[0].level, LogLevelFilter::max());
|
||||
assert!(filter.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_logging_spec_global() {
|
||||
// test parse_logging_spec with no crate
|
||||
let (dirs, filter) = parse_logging_spec("warn,crate2=debug");
|
||||
assert_eq!(dirs.len(), 2);
|
||||
assert_eq!(dirs[0].name, None);
|
||||
assert_eq!(dirs[0].level, LogLevelFilter::Warn);
|
||||
assert_eq!(dirs[1].name, Some("crate2".to_string()));
|
||||
assert_eq!(dirs[1].level, LogLevelFilter::Debug);
|
||||
assert!(filter.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_logging_spec_valid_filter() {
|
||||
let (dirs, filter) = parse_logging_spec("crate1::mod1=error,crate1::mod2,crate2=debug/abc");
|
||||
assert_eq!(dirs.len(), 3);
|
||||
assert_eq!(dirs[0].name, Some("crate1::mod1".to_string()));
|
||||
assert_eq!(dirs[0].level, LogLevelFilter::Error);
|
||||
|
||||
assert_eq!(dirs[1].name, Some("crate1::mod2".to_string()));
|
||||
assert_eq!(dirs[1].level, LogLevelFilter::max());
|
||||
|
||||
assert_eq!(dirs[2].name, Some("crate2".to_string()));
|
||||
assert_eq!(dirs[2].level, LogLevelFilter::Debug);
|
||||
assert!(filter.is_some() && filter.unwrap().to_string() == "abc");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_logging_spec_invalid_crate_filter() {
|
||||
let (dirs, filter) = parse_logging_spec("crate1::mod1=error=warn,crate2=debug/a.c");
|
||||
assert_eq!(dirs.len(), 1);
|
||||
assert_eq!(dirs[0].name, Some("crate2".to_string()));
|
||||
assert_eq!(dirs[0].level, LogLevelFilter::Debug);
|
||||
assert!(filter.is_some() && filter.unwrap().to_string() == "a.c");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_logging_spec_empty_with_filter() {
|
||||
let (dirs, filter) = parse_logging_spec("crate1/a*c");
|
||||
assert_eq!(dirs.len(), 1);
|
||||
assert_eq!(dirs[0].name, Some("crate1".to_string()));
|
||||
assert_eq!(dirs[0].level, LogLevelFilter::max());
|
||||
assert!(filter.is_some() && filter.unwrap().to_string() == "a*c");
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
extern crate regex;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use self::regex::Regex;
|
||||
|
||||
pub struct Filter {
|
||||
inner: Regex,
|
||||
}
|
||||
|
||||
impl Filter {
|
||||
pub fn new(spec: &str) -> Result<Filter, String> {
|
||||
match Regex::new(spec){
|
||||
Ok(r) => Ok(Filter { inner: r }),
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_match(&self, s: &str) -> bool {
|
||||
self.inner.is_match(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Filter {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.inner.fmt(f)
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
use std::fmt;
|
||||
|
||||
pub struct Filter {
|
||||
inner: String,
|
||||
}
|
||||
|
||||
impl Filter {
|
||||
pub fn new(spec: &str) -> Result<Filter, String> {
|
||||
Ok(Filter { inner: spec.to_string() })
|
||||
}
|
||||
|
||||
pub fn is_match(&self, s: &str) -> bool {
|
||||
s.contains(&self.inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Filter {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.inner.fmt(f)
|
||||
}
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
#[macro_use] extern crate log;
|
||||
extern crate env_logger;
|
||||
|
||||
use std::process;
|
||||
use std::env;
|
||||
use std::str;
|
||||
|
||||
fn main() {
|
||||
if env::var("LOG_REGEXP_TEST").ok() == Some(String::from("1")) {
|
||||
child_main();
|
||||
} else {
|
||||
parent_main()
|
||||
}
|
||||
}
|
||||
|
||||
fn child_main() {
|
||||
env_logger::init().unwrap();
|
||||
info!("XYZ Message");
|
||||
}
|
||||
|
||||
fn run_child(rust_log: String) -> bool {
|
||||
let exe = env::current_exe().unwrap();
|
||||
let out = process::Command::new(exe)
|
||||
.env("LOG_REGEXP_TEST", "1")
|
||||
.env("RUST_LOG", rust_log)
|
||||
.output()
|
||||
.unwrap_or_else(|e| panic!("Unable to start child process: {}", e));
|
||||
str::from_utf8(out.stderr.as_ref()).unwrap().contains("XYZ Message")
|
||||
}
|
||||
|
||||
fn assert_message_printed(rust_log: &str) {
|
||||
if !run_child(rust_log.to_string()) {
|
||||
panic!("RUST_LOG={} should allow the test log message", rust_log)
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_message_not_printed(rust_log: &str) {
|
||||
if run_child(rust_log.to_string()) {
|
||||
panic!("RUST_LOG={} should not allow the test log message", rust_log)
|
||||
}
|
||||
}
|
||||
|
||||
fn parent_main() {
|
||||
// test normal log severity levels
|
||||
assert_message_printed("info");
|
||||
assert_message_not_printed("warn");
|
||||
|
||||
// test of regular expression filters
|
||||
assert_message_printed("info/XYZ");
|
||||
assert_message_not_printed("info/XXX");
|
||||
}
|
Загрузка…
Ссылка в новой задаче