Bug 1657466 - Update to Neqo 0.4.9 r=dragana,necko-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D86087
This commit is contained in:
Andy Grover 2020-08-06 04:54:05 +00:00
parent d25129dd6f
commit dce9a42b82
134 changed files with 10557 additions and 1163 deletions

View File

@ -10,7 +10,7 @@ replace-with = "vendored-sources"
[source."https://github.com/mozilla/neqo"]
git = "https://github.com/mozilla/neqo"
replace-with = "vendored-sources"
tag = "v0.4.8"
tag = "v0.4.9"
[source."https://github.com/mozilla/mp4parse-rust"]
git = "https://github.com/mozilla/mp4parse-rust"

73
Cargo.lock generated
View File

@ -21,6 +21,15 @@ dependencies = [
"memchr",
]
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi 0.3.7",
]
[[package]]
name = "anyhow"
version = "1.0.30"
@ -215,7 +224,7 @@ dependencies = [
"bindgen",
"cranelift-codegen",
"cranelift-wasm",
"env_logger",
"env_logger 0.6.2",
"log",
"smallvec",
]
@ -275,14 +284,18 @@ dependencies = [
"cexpr",
"cfg-if",
"clang-sys",
"clap",
"env_logger 0.7.1",
"lazy_static",
"lazycell",
"log",
"peeking_take_while",
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"shlex",
"which",
]
[[package]]
@ -589,11 +602,14 @@ version = "2.31.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0f16b89cbb9ee36d87483dc939fe9f1e13c05898d56d7b230a0d4dff033a536"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"term_size",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
@ -1280,6 +1296,19 @@ dependencies = [
"termcolor",
]
[[package]]
name = "env_logger"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "error-chain"
version = "0.11.0"
@ -1680,7 +1709,7 @@ name = "gecko_logger"
version = "0.1.0"
dependencies = [
"app_services_logger",
"env_logger",
"env_logger 0.6.2",
"lazy_static",
"log",
]
@ -2381,7 +2410,7 @@ version = "0.1.4"
dependencies = [
"bindgen",
"cmake",
"env_logger",
"env_logger 0.6.2",
"glob",
"lazy_static",
"libc",
@ -3129,11 +3158,11 @@ dependencies = [
[[package]]
name = "neqo-common"
version = "0.4.8"
source = "git+https://github.com/mozilla/neqo?tag=v0.4.8#a5abafdacb88e331913376943ad1768720caed9e"
version = "0.4.9"
source = "git+https://github.com/mozilla/neqo?tag=v0.4.9#422197c2bc2010647cd9c156deea18fb543079c6"
dependencies = [
"chrono",
"env_logger",
"env_logger 0.6.2",
"lazy_static",
"log",
"num-traits",
@ -3142,8 +3171,8 @@ dependencies = [
[[package]]
name = "neqo-crypto"
version = "0.4.8"
source = "git+https://github.com/mozilla/neqo?tag=v0.4.8#a5abafdacb88e331913376943ad1768720caed9e"
version = "0.4.9"
source = "git+https://github.com/mozilla/neqo?tag=v0.4.9#422197c2bc2010647cd9c156deea18fb543079c6"
dependencies = [
"bindgen",
"log",
@ -3155,8 +3184,8 @@ dependencies = [
[[package]]
name = "neqo-http3"
version = "0.4.8"
source = "git+https://github.com/mozilla/neqo?tag=v0.4.8#a5abafdacb88e331913376943ad1768720caed9e"
version = "0.4.9"
source = "git+https://github.com/mozilla/neqo?tag=v0.4.9#422197c2bc2010647cd9c156deea18fb543079c6"
dependencies = [
"log",
"neqo-common",
@ -3170,8 +3199,8 @@ dependencies = [
[[package]]
name = "neqo-qpack"
version = "0.4.8"
source = "git+https://github.com/mozilla/neqo?tag=v0.4.8#a5abafdacb88e331913376943ad1768720caed9e"
version = "0.4.9"
source = "git+https://github.com/mozilla/neqo?tag=v0.4.9#422197c2bc2010647cd9c156deea18fb543079c6"
dependencies = [
"lazy_static",
"log",
@ -3185,9 +3214,10 @@ dependencies = [
[[package]]
name = "neqo-transport"
version = "0.4.8"
source = "git+https://github.com/mozilla/neqo?tag=v0.4.8#a5abafdacb88e331913376943ad1768720caed9e"
version = "0.4.9"
source = "git+https://github.com/mozilla/neqo?tag=v0.4.9#422197c2bc2010647cd9c156deea18fb543079c6"
dependencies = [
"indexmap",
"lazy_static",
"log",
"neqo-common",
@ -3446,7 +3476,7 @@ version = "0.1.4"
dependencies = [
"byteorder",
"core-foundation",
"env_logger",
"env_logger 0.6.2",
"lazy_static",
"libloading 0.5.2",
"log",
@ -4502,7 +4532,7 @@ name = "smoosh"
version = "0.1.0"
dependencies = [
"bumpalo",
"env_logger",
"env_logger 0.6.2",
"jsparagus",
"log",
]
@ -4702,7 +4732,7 @@ dependencies = [
"atomic_refcell",
"cssparser",
"cstr",
"env_logger",
"env_logger 0.6.2",
"geckoservo",
"libc",
"log",
@ -5681,6 +5711,15 @@ dependencies = [
"wgpu-types",
]
[[package]]
name = "which"
version = "3.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724"
dependencies = [
"libc",
]
[[package]]
name = "winapi"
version = "0.2.8"

View File

@ -8,17 +8,17 @@ edition = "2018"
name = "neqo_glue"
[dependencies]
neqo-http3 = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" }
neqo-transport = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" }
neqo-common = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" }
neqo-qpack = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" }
neqo-http3 = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" }
neqo-transport = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" }
neqo-common = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" }
neqo-qpack = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" }
nserror = { path = "../../../xpcom/rust/nserror" }
nsstring = { path = "../../../xpcom/rust/nsstring" }
xpcom = { path = "../../../xpcom/rust/xpcom" }
thin-vec = { version = "0.1.0", features = ["gecko-ffi"] }
[dependencies.neqo-crypto]
tag = "v0.4.8"
tag = "v0.4.9"
git = "https://github.com/mozilla/neqo"
default-features = false
features = ["gecko"]

View File

@ -5,16 +5,16 @@ authors = ["Dragana Damjanovic <dragana.damjano@gmail.com>"]
edition = "2018"
[dependencies]
neqo-transport = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" }
neqo-common = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" }
neqo-http3 = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" }
neqo-qpack = { tag = "v0.4.8", git = "https://github.com/mozilla/neqo" }
neqo-transport = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" }
neqo-common = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" }
neqo-http3 = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" }
neqo-qpack = { tag = "v0.4.9", git = "https://github.com/mozilla/neqo" }
mio = "0.6.17"
mio-extras = "2.0.5"
log = "0.4.0"
[dependencies.neqo-crypto]
tag = "v0.4.8"
tag = "v0.4.9"
git = "https://github.com/mozilla/neqo"
default-features = false
features = ["gecko"]

View File

@ -0,0 +1 @@
{"files":{"Cargo.toml":"48df4570f3bbfba5b3c19731a54abf70ac8608e4dab6f4dae6a694b18d6ad102","LICENCE":"a24742368cf773bbb8b6f0fcbe86ca4b802c2b7c081bc8bebf14ac38618e7c63","README.md":"779b02ebacd6f4d08e01ef289bd7976a4467054f40355593817fd6df7e8c9dd4","examples/colours.rs":"e4870671adb9574607e37a0e4145643f9047c881c310113de114ec20d76aaf4b","src/ansi.rs":"b8f5de966e7ec2fba7a4d5a373d0aceafe19ea6e20a3f4daaf448e119c989ae7","src/debug.rs":"0ab28b65c39538825707d8b7e81c6f91c78310856c936bba0ee609e06d138543","src/difference.rs":"da68156310cbaf57a3619160d0fb966f496f970c32a2e57601127cc8f54a2fbf","src/display.rs":"a43f19b7cf4d95e90e4f3954399405d8350523d423f0beed9f01399e17527b17","src/lib.rs":"b6df00ab61ca0d82c9f7b3798d516384dd2617fe73e8981f37125ecccc970dd7","src/style.rs":"7c5c2524428f0dfbe3b8d5876ddb81c47d8635704ee31ef63ddeb2429a67f457","src/windows.rs":"3b52469eed89fdc258139e4fd978f0e30c72168863966247df781da8715f0841","src/write.rs":"247c518f8b0c103c970bbe7bc70caba3ee961ab0d37095e2da5c69db98d2fc24"},"package":"ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"}

27
third_party/rust/ansi_term/Cargo.toml vendored Normal file
View File

@ -0,0 +1,27 @@
# 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 = "ansi_term"
version = "0.11.0"
authors = ["ogham@bsago.me", "Ryan Scheel (Havvy) <ryan.havvy@gmail.com>", "Josh Triplett <josh@joshtriplett.org>"]
description = "Library for ANSI terminal colours and styles (bold, underline)"
homepage = "https://github.com/ogham/rust-ansi-term"
documentation = "https://docs.rs/ansi_term"
readme = "README.md"
license = "MIT"
[lib]
name = "ansi_term"
[target."cfg(target_os=\"windows\")".dependencies.winapi]
version = "0.3.4"
features = ["errhandlingapi", "consoleapi", "processenv"]

21
third_party/rust/ansi_term/LICENCE vendored Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014 Benjamin Sago
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.

174
third_party/rust/ansi_term/README.md vendored Normal file
View File

@ -0,0 +1,174 @@
# rust-ansi-term [![ansi-term on crates.io](http://meritbadge.herokuapp.com/ansi-term)](https://crates.io/crates/ansi_term) [![Build status](https://travis-ci.org/ogham/rust-ansi-term.svg?branch=master)](https://travis-ci.org/ogham/rust-ansi-term) [![Coverage status](https://coveralls.io/repos/ogham/rust-ansi-term/badge.svg?branch=master&service=github)](https://coveralls.io/github/ogham/rust-ansi-term?branch=master)
This is a library for controlling colours and formatting, such as red bold text or blue underlined text, on ANSI terminals.
### [View the Rustdoc](https://docs.rs/ansi_term/0.9.0/ansi_term/)
# Installation
This crate works with [Cargo](http://crates.io). Add the following to your `Cargo.toml` dependencies section:
```toml
[dependencies]
ansi_term = "0.9"
```
## Basic usage
There are two main data structures in this crate that you need to be concerned with: `ANSIString` and `Style`.
A `Style` holds stylistic information: colours, whether the text should be bold, or blinking, or whatever.
There are also `Colour` variants that represent simple foreground colour styles.
An `ANSIString` is a string paired with a `Style`.
(Yes, its British English, but you wont have to write “colour” very often. `Style` is used the majority of the time.)
To format a string, call the `paint` method on a `Style` or a `Colour`, passing in the string you want to format as the argument.
For example, heres how to get some red text:
```rust
use ansi_term::Colour::Red;
println!("This is in red: {}", Red.paint("a red string"));
```
Its important to note that the `paint` method does *not* actually return a string with the ANSI control characters surrounding it.
Instead, it returns an `ANSIString` value that has a `Display` implementation that, when formatted, returns the characters.
This allows strings to be printed with a minimum of `String` allocations being performed behind the scenes.
If you *do* want to get at the escape codes, then you can convert the `ANSIString` to a string as you would any other `Display` value:
```rust
use ansi_term::Colour::Red;
use std::string::ToString;
let red_string = Red.paint("a red string").to_string();
```
**Note for Windows 10 users:** On Windows 10, the application must enable ANSI support first:
```rust
let enabled = ansi_term::enable_ansi_support();
```
## Bold, underline, background, and other styles
For anything more complex than plain foreground colour changes, you need to construct `Style` objects themselves, rather than beginning with a `Colour`.
You can do this by chaining methods based on a new `Style`, created with `Style::new()`.
Each method creates a new style that has that specific property set.
For example:
```rust
use ansi_term::Style;
println!("How about some {} and {}?",
Style::new().bold().paint("bold"),
Style::new().underline().paint("underline"));
```
For brevity, these methods have also been implemented for `Colour` values, so you can give your styles a foreground colour without having to begin with an empty `Style` value:
```rust
use ansi_term::Colour::{Blue, Yellow};
println!("Demonstrating {} and {}!",
Blue.bold().paint("blue bold"),
Yellow.underline().paint("yellow underline"));
println!("Yellow on blue: {}", Yellow.on(Blue).paint("wow!"));
```
The complete list of styles you can use are:
`bold`, `dimmed`, `italic`, `underline`, `blink`, `reverse`, `hidden`, and `on` for background colours.
In some cases, you may find it easier to change the foreground on an existing `Style` rather than starting from the appropriate `Colour`.
You can do this using the `fg` method:
```rust
use ansi_term::Style;
use ansi_term::Colour::{Blue, Cyan, Yellow};
println!("Yellow on blue: {}", Style::new().on(Blue).fg(Yellow).paint("yow!"));
println!("Also yellow on blue: {}", Cyan.on(Blue).fg(Yellow).paint("zow!"));
```
Finally, you can turn a `Colour` into a `Style` with the `normal` method.
This will produce the exact same `ANSIString` as if you just used the `paint` method on the `Colour` directly, but its useful in certain cases: for example, you may have a method that returns `Styles`, and need to represent both the “red bold” and “red, but not bold” styles with values of the same type. The `Style` struct also has a `Default` implementation if you want to have a style with *nothing* set.
```rust
use ansi_term::Style;
use ansi_term::Colour::Red;
Red.normal().paint("yet another red string");
Style::default().paint("a completely regular string");
```
## Extended colours
You can access the extended range of 256 colours by using the `Fixed` colour variant, which takes an argument of the colour number to use.
This can be included wherever you would use a `Colour`:
```rust
use ansi_term::Colour::Fixed;
Fixed(134).paint("A sort of light purple");
Fixed(221).on(Fixed(124)).paint("Mustard in the ketchup");
```
The first sixteen of these values are the same as the normal and bold standard colour variants.
Theres nothing stopping you from using these as `Fixed` colours instead, but theres nothing to be gained by doing so either.
You can also access full 24-bit color by using the `RGB` colour variant, which takes separate `u8` arguments for red, green, and blue:
```rust
use ansi_term::Colour::RGB;
RGB(70, 130, 180).paint("Steel blue");
```
## Combining successive coloured strings
The benefit of writing ANSI escape codes to the terminal is that they *stack*: you do not need to end every coloured string with a reset code if the text that follows it is of a similar style.
For example, if you want to have some blue text followed by some blue bold text, its possible to send the ANSI code for blue, followed by the ANSI code for bold, and finishing with a reset code without having to have an extra one between the two strings.
This crate can optimise the ANSI codes that get printed in situations like this, making life easier for your terminal renderer.
The `ANSIStrings` struct takes a slice of several `ANSIString` values, and will iterate over each of them, printing only the codes for the styles that need to be updated as part of its formatting routine.
The following code snippet uses this to enclose a binary number displayed in red bold text inside some red, but not bold, brackets:
```rust
use ansi_term::Colour::Red;
use ansi_term::{ANSIString, ANSIStrings};
let some_value = format!("{:b}", 42);
let strings: &[ANSIString<'static>] = &[
Red.paint("["),
Red.bold().paint(some_value),
Red.paint("]"),
];
println!("Value: {}", ANSIStrings(strings));
```
There are several things to note here.
Firstly, the `paint` method can take *either* an owned `String` or a borrowed `&str`.
Internally, an `ANSIString` holds a copy-on-write (`Cow`) string value to deal with both owned and borrowed strings at the same time.
This is used here to display a `String`, the result of the `format!` call, using the same mechanism as some statically-available `&str` slices.
Secondly, that the `ANSIStrings` value works in the same way as its singular counterpart, with a `Display` implementation that only performs the formatting when required.
## Byte strings
This library also supports formatting `[u8]` byte strings; this supports
applications working with text in an unknown encoding. `Style` and
`Color` support painting `[u8]` values, resulting in an `ANSIByteString`.
This type does not implement `Display`, as it may not contain UTF-8, but
it does provide a method `write_to` to write the result to any
`io::Write`:
```rust
use ansi_term::Colour::Green;
Green.paint("user data".as_bytes()).write_to(&mut std::io::stdout()).unwrap();
```
Similarly, the type `ANSIByteStrings` supports writing a list of
`ANSIByteString` values with minimal escape sequences:
```rust
use ansi_term::Colour::Green;
use ansi_term::ANSIByteStrings;
ANSIByteStrings(&[
Green.paint("user data 1\n".as_bytes()),
Green.bold().paint("user data 2\n".as_bytes()),
]).write_to(&mut std::io::stdout()).unwrap();
```

View File

@ -0,0 +1,13 @@
extern crate ansi_term;
use ansi_term::Colour::*;
fn main() {
println!("{}", Black.paint("Black"));
println!("{}", Red.paint("Red"));
println!("{}", Green.paint("Green"));
println!("{}", Yellow.paint("Yellow"));
println!("{}", Blue.paint("Blue"));
println!("{}", Purple.paint("Purple"));
println!("{}", Cyan.paint("Cyan"));
println!("{}", White.paint("White"));
}

258
third_party/rust/ansi_term/src/ansi.rs vendored Normal file
View File

@ -0,0 +1,258 @@
use style::{Colour, Style};
use std::fmt;
use write::AnyWrite;
// ---- generating ANSI codes ----
impl Style {
/// Write any ANSI codes that go *before* a piece of text. These should be
/// the codes to set the terminal to a different colour or font style.
fn write_prefix<W: AnyWrite + ?Sized>(&self, f: &mut W) -> Result<(), W::Error> {
// If there are actually no styles here, then dont write *any* codes
// as the prefix. An empty ANSI code may not affect the terminal
// output at all, but a user may just want a code-free string.
if self.is_plain() {
return Ok(());
}
// Write the codes prefix, then write numbers, separated by
// semicolons, for each text style we want to apply.
write!(f, "\x1B[")?;
let mut written_anything = false;
{
let mut write_char = |c| {
if written_anything { write!(f, ";")?; }
written_anything = true;
write!(f, "{}", c)?;
Ok(())
};
if self.is_bold { write_char('1')? }
if self.is_dimmed { write_char('2')? }
if self.is_italic { write_char('3')? }
if self.is_underline { write_char('4')? }
if self.is_blink { write_char('5')? }
if self.is_reverse { write_char('7')? }
if self.is_hidden { write_char('8')? }
if self.is_strikethrough { write_char('9')? }
}
// The foreground and background colours, if specified, need to be
// handled specially because the number codes are more complicated.
// (see `write_background_code` and `write_foreground_code`)
if let Some(bg) = self.background {
if written_anything { write!(f, ";")?; }
written_anything = true;
bg.write_background_code(f)?;
}
if let Some(fg) = self.foreground {
if written_anything { write!(f, ";")?; }
fg.write_foreground_code(f)?;
}
// All the codes end with an `m`, because reasons.
write!(f, "m")?;
Ok(())
}
/// Write any ANSI codes that go *after* a piece of text. These should be
/// the codes to *reset* the terminal back to its normal colour and style.
fn write_suffix<W: AnyWrite + ?Sized>(&self, f: &mut W) -> Result<(), W::Error> {
if self.is_plain() {
Ok(())
}
else {
write!(f, "{}", RESET)
}
}
}
/// The code to send to reset all styles and return to `Style::default()`.
pub static RESET: &str = "\x1B[0m";
impl Colour {
fn write_foreground_code<W: AnyWrite + ?Sized>(&self, f: &mut W) -> Result<(), W::Error> {
match *self {
Colour::Black => write!(f, "30"),
Colour::Red => write!(f, "31"),
Colour::Green => write!(f, "32"),
Colour::Yellow => write!(f, "33"),
Colour::Blue => write!(f, "34"),
Colour::Purple => write!(f, "35"),
Colour::Cyan => write!(f, "36"),
Colour::White => write!(f, "37"),
Colour::Fixed(num) => write!(f, "38;5;{}", &num),
Colour::RGB(r,g,b) => write!(f, "38;2;{};{};{}", &r, &g, &b),
}
}
fn write_background_code<W: AnyWrite + ?Sized>(&self, f: &mut W) -> Result<(), W::Error> {
match *self {
Colour::Black => write!(f, "40"),
Colour::Red => write!(f, "41"),
Colour::Green => write!(f, "42"),
Colour::Yellow => write!(f, "43"),
Colour::Blue => write!(f, "44"),
Colour::Purple => write!(f, "45"),
Colour::Cyan => write!(f, "46"),
Colour::White => write!(f, "47"),
Colour::Fixed(num) => write!(f, "48;5;{}", &num),
Colour::RGB(r,g,b) => write!(f, "48;2;{};{};{}", &r, &g, &b),
}
}
}
/// Like `ANSIString`, but only displays the style prefix.
#[derive(Clone, Copy, Debug)]
pub struct Prefix(Style);
/// Like `ANSIString`, but only displays the difference between two
/// styles.
#[derive(Clone, Copy, Debug)]
pub struct Infix(Style, Style);
/// Like `ANSIString`, but only displays the style suffix.
#[derive(Clone, Copy, Debug)]
pub struct Suffix(Style);
impl Style {
/// The prefix for this style.
pub fn prefix(self) -> Prefix {
Prefix(self)
}
/// The infix between this style and another.
pub fn infix(self, other: Style) -> Infix {
Infix(self, other)
}
/// The suffix for this style.
pub fn suffix(self) -> Suffix {
Suffix(self)
}
}
impl Colour {
/// The prefix for this colour.
pub fn prefix(self) -> Prefix {
Prefix(self.normal())
}
/// The infix between this colour and another.
pub fn infix(self, other: Colour) -> Infix {
Infix(self.normal(), other.normal())
}
/// The suffix for this colour.
pub fn suffix(self) -> Suffix {
Suffix(self.normal())
}
}
impl fmt::Display for Prefix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let f: &mut fmt::Write = f;
self.0.write_prefix(f)
}
}
impl fmt::Display for Infix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use difference::Difference;
match Difference::between(&self.0, &self.1) {
Difference::ExtraStyles(style) => {
let f: &mut fmt::Write = f;
style.write_prefix(f)
},
Difference::Reset => {
let f: &mut fmt::Write = f;
write!(f, "{}{}", RESET, self.0.prefix())
},
Difference::NoDifference => {
Ok(()) // nothing to write
},
}
}
}
impl fmt::Display for Suffix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let f: &mut fmt::Write = f;
self.0.write_suffix(f)
}
}
#[cfg(test)]
mod test {
use style::Style;
use style::Colour::*;
macro_rules! test {
($name: ident: $style: expr; $input: expr => $result: expr) => {
#[test]
fn $name() {
assert_eq!($style.paint($input).to_string(), $result.to_string());
let mut v = Vec::new();
$style.paint($input.as_bytes()).write_to(&mut v).unwrap();
assert_eq!(v.as_slice(), $result.as_bytes());
}
};
}
test!(plain: Style::default(); "text/plain" => "text/plain");
test!(red: Red; "hi" => "\x1B[31mhi\x1B[0m");
test!(black: Black.normal(); "hi" => "\x1B[30mhi\x1B[0m");
test!(yellow_bold: Yellow.bold(); "hi" => "\x1B[1;33mhi\x1B[0m");
test!(yellow_bold_2: Yellow.normal().bold(); "hi" => "\x1B[1;33mhi\x1B[0m");
test!(blue_underline: Blue.underline(); "hi" => "\x1B[4;34mhi\x1B[0m");
test!(green_bold_ul: Green.bold().underline(); "hi" => "\x1B[1;4;32mhi\x1B[0m");
test!(green_bold_ul_2: Green.underline().bold(); "hi" => "\x1B[1;4;32mhi\x1B[0m");
test!(purple_on_white: Purple.on(White); "hi" => "\x1B[47;35mhi\x1B[0m");
test!(purple_on_white_2: Purple.normal().on(White); "hi" => "\x1B[47;35mhi\x1B[0m");
test!(yellow_on_blue: Style::new().on(Blue).fg(Yellow); "hi" => "\x1B[44;33mhi\x1B[0m");
test!(yellow_on_blue_2: Cyan.on(Blue).fg(Yellow); "hi" => "\x1B[44;33mhi\x1B[0m");
test!(cyan_bold_on_white: Cyan.bold().on(White); "hi" => "\x1B[1;47;36mhi\x1B[0m");
test!(cyan_ul_on_white: Cyan.underline().on(White); "hi" => "\x1B[4;47;36mhi\x1B[0m");
test!(cyan_bold_ul_on_white: Cyan.bold().underline().on(White); "hi" => "\x1B[1;4;47;36mhi\x1B[0m");
test!(cyan_ul_bold_on_white: Cyan.underline().bold().on(White); "hi" => "\x1B[1;4;47;36mhi\x1B[0m");
test!(fixed: Fixed(100); "hi" => "\x1B[38;5;100mhi\x1B[0m");
test!(fixed_on_purple: Fixed(100).on(Purple); "hi" => "\x1B[45;38;5;100mhi\x1B[0m");
test!(fixed_on_fixed: Fixed(100).on(Fixed(200)); "hi" => "\x1B[48;5;200;38;5;100mhi\x1B[0m");
test!(rgb: RGB(70,130,180); "hi" => "\x1B[38;2;70;130;180mhi\x1B[0m");
test!(rgb_on_blue: RGB(70,130,180).on(Blue); "hi" => "\x1B[44;38;2;70;130;180mhi\x1B[0m");
test!(blue_on_rgb: Blue.on(RGB(70,130,180)); "hi" => "\x1B[48;2;70;130;180;34mhi\x1B[0m");
test!(rgb_on_rgb: RGB(70,130,180).on(RGB(5,10,15)); "hi" => "\x1B[48;2;5;10;15;38;2;70;130;180mhi\x1B[0m");
test!(bold: Style::new().bold(); "hi" => "\x1B[1mhi\x1B[0m");
test!(underline: Style::new().underline(); "hi" => "\x1B[4mhi\x1B[0m");
test!(bunderline: Style::new().bold().underline(); "hi" => "\x1B[1;4mhi\x1B[0m");
test!(dimmed: Style::new().dimmed(); "hi" => "\x1B[2mhi\x1B[0m");
test!(italic: Style::new().italic(); "hi" => "\x1B[3mhi\x1B[0m");
test!(blink: Style::new().blink(); "hi" => "\x1B[5mhi\x1B[0m");
test!(reverse: Style::new().reverse(); "hi" => "\x1B[7mhi\x1B[0m");
test!(hidden: Style::new().hidden(); "hi" => "\x1B[8mhi\x1B[0m");
test!(stricken: Style::new().strikethrough(); "hi" => "\x1B[9mhi\x1B[0m");
}

122
third_party/rust/ansi_term/src/debug.rs vendored Normal file
View File

@ -0,0 +1,122 @@
use std::fmt;
use style::Style;
/// Styles have a special `Debug` implementation that only shows the fields that
/// are set. Fields that havent been touched arent included in the output.
///
/// This behaviour gets bypassed when using the alternate formatting mode
/// `format!("{:#?}")`.
///
/// use ansi_term::Colour::{Red, Blue};
/// assert_eq!("Style { fg(Red), on(Blue), bold, italic }",
/// format!("{:?}", Red.on(Blue).bold().italic()));
impl fmt::Debug for Style {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
if fmt.alternate() {
fmt.debug_struct("Style")
.field("foreground", &self.foreground)
.field("background", &self.background)
.field("blink", &self.is_blink)
.field("bold", &self.is_bold)
.field("dimmed", &self.is_dimmed)
.field("hidden", &self.is_hidden)
.field("italic", &self.is_italic)
.field("reverse", &self.is_reverse)
.field("strikethrough", &self.is_strikethrough)
.field("underline", &self.is_underline)
.finish()
}
else if self.is_plain() {
fmt.write_str("Style {}")
}
else {
fmt.write_str("Style { ")?;
let mut written_anything = false;
if let Some(fg) = self.foreground {
if written_anything { fmt.write_str(", ")? }
written_anything = true;
write!(fmt, "fg({:?})", fg)?
}
if let Some(bg) = self.background {
if written_anything { fmt.write_str(", ")? }
written_anything = true;
write!(fmt, "on({:?})", bg)?
}
{
let mut write_flag = |name| {
if written_anything { fmt.write_str(", ")? }
written_anything = true;
fmt.write_str(name)
};
if self.is_blink { write_flag("blink")? }
if self.is_bold { write_flag("bold")? }
if self.is_dimmed { write_flag("dimmed")? }
if self.is_hidden { write_flag("hidden")? }
if self.is_italic { write_flag("italic")? }
if self.is_reverse { write_flag("reverse")? }
if self.is_strikethrough { write_flag("strikethrough")? }
if self.is_underline { write_flag("underline")? }
}
write!(fmt, " }}")
}
}
}
#[cfg(test)]
mod test {
use style::Colour::*;
use style::Style;
fn style() -> Style {
Style::new()
}
macro_rules! test {
($name: ident: $obj: expr => $result: expr) => {
#[test]
fn $name() {
assert_eq!($result, format!("{:?}", $obj));
}
};
}
test!(empty: style() => "Style {}");
test!(bold: style().bold() => "Style { bold }");
test!(italic: style().italic() => "Style { italic }");
test!(both: style().bold().italic() => "Style { bold, italic }");
test!(red: Red.normal() => "Style { fg(Red) }");
test!(redblue: Red.normal().on(RGB(3, 2, 4)) => "Style { fg(Red), on(RGB(3, 2, 4)) }");
test!(everything:
Red.on(Blue).blink().bold().dimmed().hidden().italic().reverse().strikethrough().underline() =>
"Style { fg(Red), on(Blue), blink, bold, dimmed, hidden, italic, reverse, strikethrough, underline }");
#[test]
fn long_and_detailed() {
let debug = r##"Style {
foreground: Some(
Blue
),
background: None,
blink: false,
bold: true,
dimmed: false,
hidden: false,
italic: false,
reverse: false,
strikethrough: false,
underline: false
}"##;
assert_eq!(debug, format!("{:#?}", Blue.bold()));
}
}

View File

@ -0,0 +1,179 @@
use super::Style;
/// When printing out one coloured string followed by another, use one of
/// these rules to figure out which *extra* control codes need to be sent.
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum Difference {
/// Print out the control codes specified by this style to end up looking
/// like the second string's styles.
ExtraStyles(Style),
/// Converting between these two is impossible, so just send a reset
/// command and then the second string's styles.
Reset,
/// The before style is exactly the same as the after style, so no further
/// control codes need to be printed.
NoDifference,
}
impl Difference {
/// Compute the 'style difference' required to turn an existing style into
/// the given, second style.
///
/// For example, to turn green text into green bold text, it's redundant
/// to write a reset command then a second green+bold command, instead of
/// just writing one bold command. This method should see that both styles
/// use the foreground colour green, and reduce it to a single command.
///
/// This method returns an enum value because it's not actually always
/// possible to turn one style into another: for example, text could be
/// made bold and underlined, but you can't remove the bold property
/// without also removing the underline property. So when this has to
/// happen, this function returns None, meaning that the entire set of
/// styles should be reset and begun again.
pub fn between(first: &Style, next: &Style) -> Difference {
use self::Difference::*;
// XXX(Havvy): This algorithm is kind of hard to replicate without
// having the Plain/Foreground enum variants, so I'm just leaving
// it commented out for now, and defaulting to Reset.
if first == next {
return NoDifference;
}
// Cannot un-bold, so must Reset.
if first.is_bold && !next.is_bold {
return Reset;
}
if first.is_dimmed && !next.is_dimmed {
return Reset;
}
if first.is_italic && !next.is_italic {
return Reset;
}
// Cannot un-underline, so must Reset.
if first.is_underline && !next.is_underline {
return Reset;
}
if first.is_blink && !next.is_blink {
return Reset;
}
if first.is_reverse && !next.is_reverse {
return Reset;
}
if first.is_hidden && !next.is_hidden {
return Reset;
}
if first.is_strikethrough && !next.is_strikethrough {
return Reset;
}
// Cannot go from foreground to no foreground, so must Reset.
if first.foreground.is_some() && next.foreground.is_none() {
return Reset;
}
// Cannot go from background to no background, so must Reset.
if first.background.is_some() && next.background.is_none() {
return Reset;
}
let mut extra_styles = Style::default();
if first.is_bold != next.is_bold {
extra_styles.is_bold = true;
}
if first.is_dimmed != next.is_dimmed {
extra_styles.is_dimmed = true;
}
if first.is_italic != next.is_italic {
extra_styles.is_italic = true;
}
if first.is_underline != next.is_underline {
extra_styles.is_underline = true;
}
if first.is_blink != next.is_blink {
extra_styles.is_blink = true;
}
if first.is_reverse != next.is_reverse {
extra_styles.is_reverse = true;
}
if first.is_hidden != next.is_hidden {
extra_styles.is_hidden = true;
}
if first.is_strikethrough != next.is_strikethrough {
extra_styles.is_strikethrough = true;
}
if first.foreground != next.foreground {
extra_styles.foreground = next.foreground;
}
if first.background != next.background {
extra_styles.background = next.background;
}
ExtraStyles(extra_styles)
}
}
#[cfg(test)]
mod test {
use super::*;
use super::Difference::*;
use style::Colour::*;
use style::Style;
fn style() -> Style {
Style::new()
}
macro_rules! test {
($name: ident: $first: expr; $next: expr => $result: expr) => {
#[test]
fn $name() {
assert_eq!($result, Difference::between(&$first, &$next));
}
};
}
test!(nothing: Green.normal(); Green.normal() => NoDifference);
test!(uppercase: Green.normal(); Green.bold() => ExtraStyles(style().bold()));
test!(lowercase: Green.bold(); Green.normal() => Reset);
test!(nothing2: Green.bold(); Green.bold() => NoDifference);
test!(colour_change: Red.normal(); Blue.normal() => ExtraStyles(Blue.normal()));
test!(addition_of_blink: style(); style().blink() => ExtraStyles(style().blink()));
test!(addition_of_dimmed: style(); style().dimmed() => ExtraStyles(style().dimmed()));
test!(addition_of_hidden: style(); style().hidden() => ExtraStyles(style().hidden()));
test!(addition_of_reverse: style(); style().reverse() => ExtraStyles(style().reverse()));
test!(addition_of_strikethrough: style(); style().strikethrough() => ExtraStyles(style().strikethrough()));
test!(removal_of_strikethrough: style().strikethrough(); style() => Reset);
test!(removal_of_reverse: style().reverse(); style() => Reset);
test!(removal_of_hidden: style().hidden(); style() => Reset);
test!(removal_of_dimmed: style().dimmed(); style() => Reset);
test!(removal_of_blink: style().blink(); style() => Reset);
}

View File

@ -0,0 +1,279 @@
use std::borrow::Cow;
use std::fmt;
use std::io;
use std::ops::Deref;
use ansi::RESET;
use difference::Difference;
use style::{Style, Colour};
use write::AnyWrite;
/// An `ANSIGenericString` includes a generic string type and a `Style` to
/// display that string. `ANSIString` and `ANSIByteString` are aliases for
/// this type on `str` and `[u8]`, respectively.
#[derive(PartialEq, Debug)]
pub struct ANSIGenericString<'a, S: 'a + ToOwned + ?Sized>
where <S as ToOwned>::Owned: fmt::Debug {
style: Style,
string: Cow<'a, S>,
}
/// Cloning an `ANSIGenericString` will clone its underlying string.
///
/// ### Examples
///
/// ```
/// use ansi_term::ANSIString;
///
/// let plain_string = ANSIString::from("a plain string");
/// let clone_string = plain_string.clone();
/// assert_eq!(clone_string, plain_string);
/// ```
impl<'a, S: 'a + ToOwned + ?Sized> Clone for ANSIGenericString<'a, S>
where <S as ToOwned>::Owned: fmt::Debug {
fn clone(&self) -> ANSIGenericString<'a, S> {
ANSIGenericString {
style: self.style,
string: self.string.clone(),
}
}
}
// You might think that the hand-written Clone impl above is the same as the
// one that gets generated with #[derive]. But its not *quite* the same!
//
// `str` is not Clone, and the derived Clone implementation puts a Clone
// constraint on the S type parameter (generated using --pretty=expanded):
//
// ↓_________________↓
// impl <'a, S: ::std::clone::Clone + 'a + ToOwned + ?Sized> ::std::clone::Clone
// for ANSIGenericString<'a, S> where
// <S as ToOwned>::Owned: fmt::Debug { ... }
//
// This resulted in compile errors when you tried to derive Clone on a type
// that used it:
//
// #[derive(PartialEq, Debug, Clone, Default)]
// pub struct TextCellContents(Vec<ANSIString<'static>>);
// ^^^^^^^^^^^^^^^^^^^^^^^^^
// error[E0277]: the trait `std::clone::Clone` is not implemented for `str`
//
// The hand-written impl above can ignore that constraint and still compile.
/// An ANSI String is a string coupled with the `Style` to display it
/// in a terminal.
///
/// Although not technically a string itself, it can be turned into
/// one with the `to_string` method.
///
/// ### Examples
///
/// ```no_run
/// use ansi_term::ANSIString;
/// use ansi_term::Colour::Red;
///
/// let red_string = Red.paint("a red string");
/// println!("{}", red_string);
/// ```
///
/// ```
/// use ansi_term::ANSIString;
///
/// let plain_string = ANSIString::from("a plain string");
/// assert_eq!(&*plain_string, "a plain string");
/// ```
pub type ANSIString<'a> = ANSIGenericString<'a, str>;
/// An `ANSIByteString` represents a formatted series of bytes. Use
/// `ANSIByteString` when styling text with an unknown encoding.
pub type ANSIByteString<'a> = ANSIGenericString<'a, [u8]>;
impl<'a, I, S: 'a + ToOwned + ?Sized> From<I> for ANSIGenericString<'a, S>
where I: Into<Cow<'a, S>>,
<S as ToOwned>::Owned: fmt::Debug {
fn from(input: I) -> ANSIGenericString<'a, S> {
ANSIGenericString {
string: input.into(),
style: Style::default(),
}
}
}
impl<'a, S: 'a + ToOwned + ?Sized> Deref for ANSIGenericString<'a, S>
where <S as ToOwned>::Owned: fmt::Debug {
type Target = S;
fn deref(&self) -> &S {
self.string.deref()
}
}
/// A set of `ANSIGenericString`s collected together, in order to be
/// written with a minimum of control characters.
pub struct ANSIGenericStrings<'a, S: 'a + ToOwned + ?Sized>
(pub &'a [ANSIGenericString<'a, S>])
where <S as ToOwned>::Owned: fmt::Debug;
/// A set of `ANSIString`s collected together, in order to be written with a
/// minimum of control characters.
pub type ANSIStrings<'a> = ANSIGenericStrings<'a, str>;
/// A function to construct an `ANSIStrings` instance.
#[allow(non_snake_case)]
pub fn ANSIStrings<'a>(arg: &'a [ANSIString<'a>]) -> ANSIStrings<'a> {
ANSIGenericStrings(arg)
}
/// A set of `ANSIByteString`s collected together, in order to be
/// written with a minimum of control characters.
pub type ANSIByteStrings<'a> = ANSIGenericStrings<'a, [u8]>;
/// A function to construct an `ANSIByteStrings` instance.
#[allow(non_snake_case)]
pub fn ANSIByteStrings<'a>(arg: &'a [ANSIByteString<'a>]) -> ANSIByteStrings<'a> {
ANSIGenericStrings(arg)
}
// ---- paint functions ----
impl Style {
/// Paints the given text with this colour, returning an ANSI string.
pub fn paint<'a, I, S: 'a + ToOwned + ?Sized>(self, input: I) -> ANSIGenericString<'a, S>
where I: Into<Cow<'a, S>>,
<S as ToOwned>::Owned: fmt::Debug {
ANSIGenericString {
string: input.into(),
style: self,
}
}
}
impl Colour {
/// Paints the given text with this colour, returning an ANSI string.
/// This is a short-cut so you dont have to use `Blue.normal()` just
/// to get blue text.
///
/// ```
/// use ansi_term::Colour::Blue;
/// println!("{}", Blue.paint("da ba dee"));
/// ```
pub fn paint<'a, I, S: 'a + ToOwned + ?Sized>(self, input: I) -> ANSIGenericString<'a, S>
where I: Into<Cow<'a, S>>,
<S as ToOwned>::Owned: fmt::Debug {
ANSIGenericString {
string: input.into(),
style: self.normal(),
}
}
}
// ---- writers for individual ANSI strings ----
impl<'a> fmt::Display for ANSIString<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let w: &mut fmt::Write = f;
self.write_to_any(w)
}
}
impl<'a> ANSIByteString<'a> {
/// Write an `ANSIByteString` to an `io::Write`. This writes the escape
/// sequences for the associated `Style` around the bytes.
pub fn write_to<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
let w: &mut io::Write = w;
self.write_to_any(w)
}
}
impl<'a, S: 'a + ToOwned + ?Sized> ANSIGenericString<'a, S>
where <S as ToOwned>::Owned: fmt::Debug, &'a S: AsRef<[u8]> {
fn write_to_any<W: AnyWrite<wstr=S> + ?Sized>(&self, w: &mut W) -> Result<(), W::Error> {
write!(w, "{}", self.style.prefix())?;
w.write_str(self.string.as_ref())?;
write!(w, "{}", self.style.suffix())
}
}
// ---- writers for combined ANSI strings ----
impl<'a> fmt::Display for ANSIStrings<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let f: &mut fmt::Write = f;
self.write_to_any(f)
}
}
impl<'a> ANSIByteStrings<'a> {
/// Write `ANSIByteStrings` to an `io::Write`. This writes the minimal
/// escape sequences for the associated `Style`s around each set of
/// bytes.
pub fn write_to<W: io::Write>(&self, w: &mut W) -> io::Result<()> {
let w: &mut io::Write = w;
self.write_to_any(w)
}
}
impl<'a, S: 'a + ToOwned + ?Sized> ANSIGenericStrings<'a, S>
where <S as ToOwned>::Owned: fmt::Debug, &'a S: AsRef<[u8]> {
fn write_to_any<W: AnyWrite<wstr=S> + ?Sized>(&self, w: &mut W) -> Result<(), W::Error> {
use self::Difference::*;
let first = match self.0.first() {
None => return Ok(()),
Some(f) => f,
};
write!(w, "{}", first.style.prefix())?;
w.write_str(first.string.as_ref())?;
for window in self.0.windows(2) {
match Difference::between(&window[0].style, &window[1].style) {
ExtraStyles(style) => write!(w, "{}", style.prefix())?,
Reset => write!(w, "{}{}", RESET, window[1].style.prefix())?,
NoDifference => {/* Do nothing! */},
}
w.write_str(&window[1].string)?;
}
// Write the final reset string after all of the ANSIStrings have been
// written, *except* if the last one has no styles, because it would
// have already been written by this point.
if let Some(last) = self.0.last() {
if !last.style.is_plain() {
write!(w, "{}", RESET)?;
}
}
Ok(())
}
}
// ---- tests ----
#[cfg(test)]
mod tests {
pub use super::super::ANSIStrings;
pub use style::Style;
pub use style::Colour::*;
#[test]
fn no_control_codes_for_plain() {
let one = Style::default().paint("one");
let two = Style::default().paint("two");
let output = format!("{}", ANSIStrings( &[ one, two ] ));
assert_eq!(&*output, "onetwo");
}
}

205
third_party/rust/ansi_term/src/lib.rs vendored Normal file
View File

@ -0,0 +1,205 @@
//! This is a library for controlling colours and formatting, such as
//! red bold text or blue underlined text, on ANSI terminals.
//!
//!
//! ## Basic usage
//!
//! There are two main data structures in this crate that you need to be
//! concerned with: `ANSIString` and `Style`. A `Style` holds stylistic
//! information: colours, whether the text should be bold, or blinking, or
//! whatever. There are also `Colour` variants that represent simple foreground
//! colour styles. An `ANSIString` is a string paired with a `Style`.
//!
//! (Yes, its British English, but you wont have to write “colour” very often.
//! `Style` is used the majority of the time.)
//!
//! To format a string, call the `paint` method on a `Style` or a `Colour`,
//! passing in the string you want to format as the argument. For example,
//! heres how to get some red text:
//!
//! use ansi_term::Colour::Red;
//! println!("This is in red: {}", Red.paint("a red string"));
//!
//! Its important to note that the `paint` method does *not* actually return a
//! string with the ANSI control characters surrounding it. Instead, it returns
//! an `ANSIString` value that has a `Display` implementation that, when
//! formatted, returns the characters. This allows strings to be printed with a
//! minimum of `String` allocations being performed behind the scenes.
//!
//! If you *do* want to get at the escape codes, then you can convert the
//! `ANSIString` to a string as you would any other `Display` value:
//!
//! use ansi_term::Colour::Red;
//! use std::string::ToString;
//! let red_string = Red.paint("a red string").to_string();
//!
//!
//! ## Bold, underline, background, and other styles
//!
//! For anything more complex than plain foreground colour changes, you need to
//! construct `Style` objects themselves, rather than beginning with a `Colour`.
//! You can do this by chaining methods based on a new `Style`, created with
//! `Style::new()`. Each method creates a new style that has that specific
//! property set. For example:
//!
//! use ansi_term::Style;
//! println!("How about some {} and {}?",
//! Style::new().bold().paint("bold"),
//! Style::new().underline().paint("underline"));
//!
//! For brevity, these methods have also been implemented for `Colour` values,
//! so you can give your styles a foreground colour without having to begin with
//! an empty `Style` value:
//!
//! use ansi_term::Colour::{Blue, Yellow};
//! println!("Demonstrating {} and {}!",
//! Blue.bold().paint("blue bold"),
//! Yellow.underline().paint("yellow underline"));
//! println!("Yellow on blue: {}", Yellow.on(Blue).paint("wow!"));
//!
//! The complete list of styles you can use are: `bold`, `dimmed`, `italic`,
//! `underline`, `blink`, `reverse`, `hidden`, `strikethrough`, and `on` for
//! background colours.
//!
//! In some cases, you may find it easier to change the foreground on an
//! existing `Style` rather than starting from the appropriate `Colour`.
//! You can do this using the `fg` method:
//!
//! use ansi_term::Style;
//! use ansi_term::Colour::{Blue, Cyan, Yellow};
//! println!("Yellow on blue: {}", Style::new().on(Blue).fg(Yellow).paint("yow!"));
//! println!("Also yellow on blue: {}", Cyan.on(Blue).fg(Yellow).paint("zow!"));
//!
//! Finally, you can turn a `Colour` into a `Style` with the `normal` method.
//! This will produce the exact same `ANSIString` as if you just used the
//! `paint` method on the `Colour` directly, but its useful in certain cases:
//! for example, you may have a method that returns `Styles`, and need to
//! represent both the “red bold” and “red, but not bold” styles with values of
//! the same type. The `Style` struct also has a `Default` implementation if you
//! want to have a style with *nothing* set.
//!
//! use ansi_term::Style;
//! use ansi_term::Colour::Red;
//! Red.normal().paint("yet another red string");
//! Style::default().paint("a completely regular string");
//!
//!
//! ## Extended colours
//!
//! You can access the extended range of 256 colours by using the `Fixed` colour
//! variant, which takes an argument of the colour number to use. This can be
//! included wherever you would use a `Colour`:
//!
//! use ansi_term::Colour::Fixed;
//! Fixed(134).paint("A sort of light purple");
//! Fixed(221).on(Fixed(124)).paint("Mustard in the ketchup");
//!
//! The first sixteen of these values are the same as the normal and bold
//! standard colour variants. Theres nothing stopping you from using these as
//! `Fixed` colours instead, but theres nothing to be gained by doing so
//! either.
//!
//! You can also access full 24-bit color by using the `RGB` colour variant,
//! which takes separate `u8` arguments for red, green, and blue:
//!
//! use ansi_term::Colour::RGB;
//! RGB(70, 130, 180).paint("Steel blue");
//!
//! ## Combining successive coloured strings
//!
//! The benefit of writing ANSI escape codes to the terminal is that they
//! *stack*: you do not need to end every coloured string with a reset code if
//! the text that follows it is of a similar style. For example, if you want to
//! have some blue text followed by some blue bold text, its possible to send
//! the ANSI code for blue, followed by the ANSI code for bold, and finishing
//! with a reset code without having to have an extra one between the two
//! strings.
//!
//! This crate can optimise the ANSI codes that get printed in situations like
//! this, making life easier for your terminal renderer. The `ANSIStrings`
//! struct takes a slice of several `ANSIString` values, and will iterate over
//! each of them, printing only the codes for the styles that need to be updated
//! as part of its formatting routine.
//!
//! The following code snippet uses this to enclose a binary number displayed in
//! red bold text inside some red, but not bold, brackets:
//!
//! use ansi_term::Colour::Red;
//! use ansi_term::{ANSIString, ANSIStrings};
//! let some_value = format!("{:b}", 42);
//! let strings: &[ANSIString<'static>] = &[
//! Red.paint("["),
//! Red.bold().paint(some_value),
//! Red.paint("]"),
//! ];
//! println!("Value: {}", ANSIStrings(strings));
//!
//! There are several things to note here. Firstly, the `paint` method can take
//! *either* an owned `String` or a borrowed `&str`. Internally, an `ANSIString`
//! holds a copy-on-write (`Cow`) string value to deal with both owned and
//! borrowed strings at the same time. This is used here to display a `String`,
//! the result of the `format!` call, using the same mechanism as some
//! statically-available `&str` slices. Secondly, that the `ANSIStrings` value
//! works in the same way as its singular counterpart, with a `Display`
//! implementation that only performs the formatting when required.
//!
//! ## Byte strings
//!
//! This library also supports formatting `[u8]` byte strings; this supports
//! applications working with text in an unknown encoding. `Style` and
//! `Color` support painting `[u8]` values, resulting in an `ANSIByteString`.
//! This type does not implement `Display`, as it may not contain UTF-8, but
//! it does provide a method `write_to` to write the result to any
//! `io::Write`:
//!
//! use ansi_term::Colour::Green;
//! Green.paint("user data".as_bytes()).write_to(&mut std::io::stdout()).unwrap();
//!
//! Similarly, the type `ANSIByteStrings` supports writing a list of
//! `ANSIByteString` values with minimal escape sequences:
//!
//! use ansi_term::Colour::Green;
//! use ansi_term::ANSIByteStrings;
//! ANSIByteStrings(&[
//! Green.paint("user data 1\n".as_bytes()),
//! Green.bold().paint("user data 2\n".as_bytes()),
//! ]).write_to(&mut std::io::stdout()).unwrap();
#![crate_name = "ansi_term"]
#![crate_type = "rlib"]
#![crate_type = "dylib"]
#![warn(missing_copy_implementations)]
#![warn(missing_docs)]
#![warn(trivial_casts, trivial_numeric_casts)]
#![warn(unused_extern_crates, unused_qualifications)]
#[cfg(target_os="windows")]
extern crate winapi;
mod ansi;
pub use ansi::{Prefix, Infix, Suffix};
mod style;
pub use style::{Colour, Style};
/// Color is a type alias for Colour for those who can't be bothered.
pub use Colour as Color;
// I'm not beyond calling Colour Colour, rather than Color, but I did
// purposefully name this crate 'ansi-term' so people wouldn't get
// confused when they tried to install it.
//
// Only *after* they'd installed it.
mod difference;
mod display;
pub use display::*;
mod write;
mod windows;
pub use windows::*;
mod debug;

259
third_party/rust/ansi_term/src/style.rs vendored Normal file
View File

@ -0,0 +1,259 @@
/// A style is a collection of properties that can format a string
/// using ANSI escape codes.
#[derive(PartialEq, Clone, Copy)]
pub struct Style {
/// The style's foreground colour, if it has one.
pub foreground: Option<Colour>,
/// The style's background colour, if it has one.
pub background: Option<Colour>,
/// Whether this style is bold.
pub is_bold: bool,
/// Whether this style is dimmed.
pub is_dimmed: bool,
/// Whether this style is italic.
pub is_italic: bool,
/// Whether this style is underlined.
pub is_underline: bool,
/// Whether this style is blinking.
pub is_blink: bool,
/// Whether this style has reverse colours.
pub is_reverse: bool,
/// Whether this style is hidden.
pub is_hidden: bool,
/// Whether this style is struckthrough.
pub is_strikethrough: bool
}
impl Style {
/// Creates a new Style with no differences.
pub fn new() -> Style {
Style::default()
}
/// Returns a `Style` with the bold property set.
pub fn bold(&self) -> Style {
Style { is_bold: true, .. *self }
}
/// Returns a `Style` with the dimmed property set.
pub fn dimmed(&self) -> Style {
Style { is_dimmed: true, .. *self }
}
/// Returns a `Style` with the italic property set.
pub fn italic(&self) -> Style {
Style { is_italic: true, .. *self }
}
/// Returns a `Style` with the underline property set.
pub fn underline(&self) -> Style {
Style { is_underline: true, .. *self }
}
/// Returns a `Style` with the blink property set.
pub fn blink(&self) -> Style {
Style { is_blink: true, .. *self }
}
/// Returns a `Style` with the reverse property set.
pub fn reverse(&self) -> Style {
Style { is_reverse: true, .. *self }
}
/// Returns a `Style` with the hidden property set.
pub fn hidden(&self) -> Style {
Style { is_hidden: true, .. *self }
}
/// Returns a `Style` with the hidden property set.
pub fn strikethrough(&self) -> Style {
Style { is_strikethrough: true, .. *self }
}
/// Returns a `Style` with the foreground colour property set.
pub fn fg(&self, foreground: Colour) -> Style {
Style { foreground: Some(foreground), .. *self }
}
/// Returns a `Style` with the background colour property set.
pub fn on(&self, background: Colour) -> Style {
Style { background: Some(background), .. *self }
}
/// Return true if this `Style` has no actual styles, and can be written
/// without any control characters.
pub fn is_plain(self) -> bool {
self == Style::default()
}
}
impl Default for Style {
/// Returns a style with *no* properties set. Formatting text using this
/// style returns the exact same text.
///
/// ```
/// use ansi_term::Style;
/// assert_eq!(None, Style::default().foreground);
/// assert_eq!(None, Style::default().background);
/// assert_eq!(false, Style::default().is_bold);
/// assert_eq!("txt", Style::default().paint("txt").to_string());
/// ```
fn default() -> Style {
Style {
foreground: None,
background: None,
is_bold: false,
is_dimmed: false,
is_italic: false,
is_underline: false,
is_blink: false,
is_reverse: false,
is_hidden: false,
is_strikethrough: false,
}
}
}
// ---- colours ----
/// A colour is one specific type of ANSI escape code, and can refer
/// to either the foreground or background colour.
///
/// These use the standard numeric sequences.
/// See <http://invisible-island.net/xterm/ctlseqs/ctlseqs.html>
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum Colour {
/// Colour #0 (foreground code `30`, background code `40`).
///
/// This is not necessarily the background colour, and using it as one may
/// render the text hard to read on terminals with dark backgrounds.
Black,
/// Colour #1 (foreground code `31`, background code `41`).
Red,
/// Colour #2 (foreground code `32`, background code `42`).
Green,
/// Colour #3 (foreground code `33`, background code `43`).
Yellow,
/// Colour #4 (foreground code `34`, background code `44`).
Blue,
/// Colour #5 (foreground code `35`, background code `45`).
Purple,
/// Colour #6 (foreground code `36`, background code `46`).
Cyan,
/// Colour #7 (foreground code `37`, background code `47`).
///
/// As above, this is not necessarily the foreground colour, and may be
/// hard to read on terminals with light backgrounds.
White,
/// A colour number from 0 to 255, for use in 256-colour terminal
/// environments.
///
/// - Colours 0 to 7 are the `Black` to `White` variants respectively.
/// These colours can usually be changed in the terminal emulator.
/// - Colours 8 to 15 are brighter versions of the eight colours above.
/// These can also usually be changed in the terminal emulator, or it
/// could be configured to use the original colours and show the text in
/// bold instead. It varies depending on the program.
/// - Colours 16 to 231 contain several palettes of bright colours,
/// arranged in six squares measuring six by six each.
/// - Colours 232 to 255 are shades of grey from black to white.
///
/// It might make more sense to look at a [colour chart][cc].
///
/// [cc]: https://upload.wikimedia.org/wikipedia/en/1/15/Xterm_256color_chart.svg
Fixed(u8),
/// A 24-bit RGB color, as specified by ISO-8613-3.
RGB(u8, u8, u8),
}
impl Colour {
/// Return a `Style` with the foreground colour set to this colour.
pub fn normal(self) -> Style {
Style { foreground: Some(self), .. Style::default() }
}
/// Returns a `Style` with the bold property set.
pub fn bold(self) -> Style {
Style { foreground: Some(self), is_bold: true, .. Style::default() }
}
/// Returns a `Style` with the dimmed property set.
pub fn dimmed(self) -> Style {
Style { foreground: Some(self), is_dimmed: true, .. Style::default() }
}
/// Returns a `Style` with the italic property set.
pub fn italic(self) -> Style {
Style { foreground: Some(self), is_italic: true, .. Style::default() }
}
/// Returns a `Style` with the underline property set.
pub fn underline(self) -> Style {
Style { foreground: Some(self), is_underline: true, .. Style::default() }
}
/// Returns a `Style` with the blink property set.
pub fn blink(self) -> Style {
Style { foreground: Some(self), is_blink: true, .. Style::default() }
}
/// Returns a `Style` with the reverse property set.
pub fn reverse(self) -> Style {
Style { foreground: Some(self), is_reverse: true, .. Style::default() }
}
/// Returns a `Style` with the hidden property set.
pub fn hidden(self) -> Style {
Style { foreground: Some(self), is_hidden: true, .. Style::default() }
}
/// Returns a `Style` with the strikethrough property set.
pub fn strikethrough(self) -> Style {
Style { foreground: Some(self), is_strikethrough: true, .. Style::default() }
}
/// Returns a `Style` with the background colour property set.
pub fn on(self, background: Colour) -> Style {
Style { foreground: Some(self), background: Some(background), .. Style::default() }
}
}
impl From<Colour> for Style {
/// You can turn a `Colour` into a `Style` with the foreground colour set
/// with the `From` trait.
///
/// ```
/// use ansi_term::{Style, Colour};
/// let green_foreground = Style::default().fg(Colour::Green);
/// assert_eq!(green_foreground, Colour::Green.normal());
/// assert_eq!(green_foreground, Colour::Green.into());
/// assert_eq!(green_foreground, Style::from(Colour::Green));
/// ```
fn from(colour: Colour) -> Style {
colour.normal()
}
}

View File

@ -0,0 +1,40 @@
/// Enables ANSI code support on Windows 10.
///
/// This uses Windows API calls to alter the properties of the console that
/// the program is running in.
///
/// https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx
///
/// Returns a `Result` with the Windows error code if unsuccessful.
#[cfg(windows)]
pub fn enable_ansi_support() -> Result<(), u32> {
use winapi::um::processenv::GetStdHandle;
use winapi::um::errhandlingapi::GetLastError;
use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode};
const STD_OUT_HANDLE: u32 = -11i32 as u32;
const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x0004;
unsafe {
// https://docs.microsoft.com/en-us/windows/console/getstdhandle
let std_out_handle = GetStdHandle(STD_OUT_HANDLE);
let error_code = GetLastError();
if error_code != 0 { return Err(error_code); }
// https://docs.microsoft.com/en-us/windows/console/getconsolemode
let mut console_mode: u32 = 0;
GetConsoleMode(std_out_handle, &mut console_mode);
let error_code = GetLastError();
if error_code != 0 { return Err(error_code); }
// VT processing not already enabled?
if console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0 {
// https://docs.microsoft.com/en-us/windows/console/setconsolemode
SetConsoleMode(std_out_handle, console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
let error_code = GetLastError();
if error_code != 0 { return Err(error_code); }
}
}
return Ok(());
}

40
third_party/rust/ansi_term/src/write.rs vendored Normal file
View File

@ -0,0 +1,40 @@
use std::fmt;
use std::io;
pub trait AnyWrite {
type wstr: ?Sized;
type Error;
fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<(), Self::Error>;
fn write_str(&mut self, s: &Self::wstr) -> Result<(), Self::Error>;
}
impl<'a> AnyWrite for fmt::Write + 'a {
type wstr = str;
type Error = fmt::Error;
fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<(), Self::Error> {
fmt::Write::write_fmt(self, fmt)
}
fn write_str(&mut self, s: &Self::wstr) -> Result<(), Self::Error> {
fmt::Write::write_str(self, s)
}
}
impl<'a> AnyWrite for io::Write + 'a {
type wstr = [u8];
type Error = io::Error;
fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<(), Self::Error> {
io::Write::write_fmt(self, fmt)
}
fn write_str(&mut self, s: &Self::wstr) -> Result<(), Self::Error> {
io::Write::write_all(self, s)
}
}

View File

@ -0,0 +1 @@
{"files":{"CHANGELOG.md":"7c044d74477515ab39287a4caff27eb96daebaed8b9f9b6a1d1c081a7b42d4a7","Cargo.lock":"132c1f881b80a79314567a6993141c6204495fec144cdcec1729f2a3e0fec18b","Cargo.toml":"b60137f1fd54001ca4d8be1d0bbec154225a44c8f4fa3576078bdad55216d357","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"6485b8ed310d3f0340bf1ad1f47645069ce4069dcc6bb46c7d5c6faf41de1fdb","README.md":"0e231c1c4ad51ff0239062297bdaa69aeb34a8692e3f814188ce1e0ade8583d5","examples/custom_default_format.rs":"799c439f61cb711078f8aa584db537a5758c25b90d44767849dae2ad3822885c","examples/custom_format.rs":"ac8323e2febf8b8ff7238bd254fbbbfb3183da5af84f7f3a261fd9ad892c9ab6","examples/custom_logger.rs":"99fb3c9761ad4c5fe73f4ec2a2bd44b4acf6d1f7b7cfaa16bf0373665d3e2a4b","examples/default.rs":"ac96427611784d310704f738c7a29ebddd7930c8a70ad3c464c4d3eae4cf74a3","examples/direct_logger.rs":"549f6a10e0903d06aca2cc7ba82415b07a23392676101c9bc7aa72b4a9b0b9e2","examples/filters_from_code.rs":"84bd82803683d19ae96f85edcf4ee38cda028c2dbde923dddecc8563453b18e2","src/filter/mod.rs":"de471579c5db400c5ed11b9d7c9fc62686068b42798c58f7165806319ab7ec09","src/filter/regex.rs":"5fff47d1d4d0aa3f2bab90636127d3e72aebf800c3b78faba99637220ffdf865","src/filter/string.rs":"52bbd047c31a1afdb3cd1c11629b956f21b3f47bf22e06421baf3d693a045e59","src/fmt/humantime/extern_impl.rs":"cd2538e7a03fd3ad6c843af3c3d4016ca96cadaefee32cf9b37329c4787e6552","src/fmt/humantime/mod.rs":"408496eb21344c654b9e06da2a2df86de56e427147bb7f7b47851e0da976c003","src/fmt/humantime/shim_impl.rs":"7c2fdf4031f5568b716df14842b0d32bc03ff398763f4849960df7f9632a5bb2","src/fmt/mod.rs":"5104dad2fd14bc18ab6ab46e7c2bc5752b509d9fc934fb99f0ebc126728f8f04","src/fmt/writer/atty.rs":"3e9fd61d291d0919f7aa7119a26dd15d920df8783b4ae57bcf2c3cb6f3ff06b5","src/fmt/writer/mod.rs":"583f6616e0cf21955a530baa332fb7a99bf4fcd418a2367bbd1e733a06a22318","src/fmt/writer/termcolor/extern_impl.rs":"15e048be128568abcdd0ce99dafffe296df26131d4aa05921585761d31c11db5","src/fmt/writer/termcolor/mod.rs":"a3cf956aec030e0f940e4eaefe58d7703857eb900022286e328e05e5f61de183","src/fmt/writer/termcolor/shim_impl.rs":"bdd479c4e933b14ba02a3c1a9fe30eb51bcdf600e23cebd044d68683fdaad037","src/lib.rs":"2c5ab92ee141022f3e657b0f81e84e5ee4e7fad9fb648204e00ed4fb03d4166f","tests/init-twice-retains-filter.rs":"00524ce0f6779981b695bad1fdd244f87b76c126aeccd8b4ff77ef9e6325478b","tests/log-in-log.rs":"41126910998adfbac771c2a1237fecbc5437344f8e4dfc2f93235bab764a087e","tests/regexp_filter.rs":"44aa6c39de894be090e37083601e501cfffb15e3c0cd36209c48abdf3e2cb120"},"package":"aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3"}

View File

@ -0,0 +1,3 @@
Changes to this crate are tracked via [GitHub Releases][releases].
[releases]: https://github.com/sebasmagri/env_logger/releases

212
third_party/rust/env_logger-0.6.2/Cargo.lock generated vendored Normal file
View File

@ -0,0 +1,212 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "aho-corasick"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "atty"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cfg-if"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "env_logger"
version = "0.6.2"
dependencies = [
"atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "humantime"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "log"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "memchr"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quick-error"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "redox_syscall"
version = "0.1.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "redox_termios"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "termcolor"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"wincolor 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "termion"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ucd-util"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "utf8-ranges"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-util"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "wincolor"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e"
"checksum atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "af80143d6f7608d746df1520709e5d141c96f240b0e62b0aa41bdfb53374d9d4"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114"
"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d"
"checksum libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd41f331ac7c5b8ac259b8bf82c75c0fb2e469bbf37d2becbba9a6a2221965b"
"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6"
"checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16"
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
"checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "37e7cbbd370869ce2e8dff25c7018702d10b21a20ef7135316f8daecd6c25b7f"
"checksum regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8c2f35eedad5295fdf00a63d7d4b238135723f92b434ec06774dad15c7ab0861"
"checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d"
"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737"
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
"checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
"checksum wincolor 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "561ed901ae465d6185fa7864d63fbd5720d0ef718366c9a4dc83cf6170d7e9ba"

View File

@ -0,0 +1,57 @@
# 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.6.2"
authors = ["The Rust Project Developers"]
description = "A logging implementation for `log` which is configured via an environment\nvariable.\n"
documentation = "https://docs.rs/env_logger"
readme = "README.md"
keywords = ["logging", "log", "logger"]
categories = ["development-tools::debugging"]
license = "MIT/Apache-2.0"
repository = "https://github.com/sebasmagri/env_logger/"
[[test]]
name = "regexp_filter"
harness = false
[[test]]
name = "log-in-log"
harness = false
[[test]]
name = "init-twice-retains-filter"
harness = false
[dependencies.atty]
version = "0.2.5"
optional = true
[dependencies.humantime]
version = "1.1"
optional = true
[dependencies.log]
version = "0.4"
features = ["std"]
[dependencies.regex]
version = "1.0.3"
optional = true
[dependencies.termcolor]
version = "1.0.2"
optional = true
[features]
default = ["termcolor", "atty", "humantime", "regex"]

View File

@ -0,0 +1,201 @@
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.

View File

@ -0,0 +1,25 @@
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.

View File

@ -0,0 +1,152 @@
env_logger [![Build Status](https://travis-ci.org/sebasmagri/env_logger.svg?branch=master)](https://travis-ci.org/sebasmagri/env_logger) [![Maintenance](https://img.shields.io/badge/maintenance-actively%20maintained-brightgreen.svg)](https://github.com/sebasmagri/env_logger) [![crates.io](https://img.shields.io/crates/v/env_logger.svg)](https://crates.io/crates/env_logger) [![Documentation](https://img.shields.io/badge/docs-current-blue.svg)](https://docs.rs/env_logger)
==========
Implements a logger that can be configured via environment variables.
## Usage
### In libraries
`env_logger` makes sense when used in executables (binary projects). Libraries should use the [`log`](https://doc.rust-lang.org/log) crate instead.
### In executables
It must be added along with `log` to the project dependencies:
```toml
[dependencies]
log = "0.4.0"
env_logger = "0.6.2"
```
`env_logger` must be initialized as early as possible in the project. After it's initialized, you can use the `log` macros to do actual logging.
```rust
#[macro_use]
extern crate log;
extern crate env_logger;
fn main() {
env_logger::init();
info!("starting up");
// ...
}
```
Then when running the executable, specify a value for the `RUST_LOG`
environment variable that corresponds with the log messages you want to show.
```bash
$ RUST_LOG=info ./main
[2018-11-03T06:09:06Z INFO default] starting up
```
`env_logger` can be configured in other ways besides an environment variable. See [the examples](https://github.com/sebasmagri/env_logger/tree/master/examples) for more approaches.
### In tests
Tests can use the `env_logger` crate to see log messages generated during that test:
```toml
[dependencies]
log = "0.4.0"
[dev-dependencies]
env_logger = "0.6.2"
```
```rust
#[macro_use]
extern crate log;
fn add_one(num: i32) -> i32 {
info!("add_one called with {}", num);
num + 1
}
#[cfg(test)]
mod tests {
use super::*;
extern crate env_logger;
fn init() {
let _ = env_logger::builder().is_test(true).try_init();
}
#[test]
fn it_adds_one() {
init();
info!("can log from the test too");
assert_eq!(3, add_one(2));
}
#[test]
fn it_handles_negative_numbers() {
init();
info!("logging from another test");
assert_eq!(-7, add_one(-8));
}
}
```
Assuming the module under test is called `my_lib`, running the tests with the
`RUST_LOG` filtering to info messages from this module looks like:
```bash
$ RUST_LOG=my_lib=info cargo test
Running target/debug/my_lib-...
running 2 tests
[INFO my_lib::tests] logging from another test
[INFO my_lib] add_one called with -8
test tests::it_handles_negative_numbers ... ok
[INFO my_lib::tests] can log from the test too
[INFO my_lib] add_one called with 2
test tests::it_adds_one ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured
```
Note that `env_logger::try_init()` needs to be called in each test in which you
want to enable logging. Additionally, the default behavior of tests to
run in parallel means that logging output may be interleaved with test output.
Either run tests in a single thread by specifying `RUST_TEST_THREADS=1` or by
running one test by specifying its name as an argument to the test binaries as
directed by the `cargo test` help docs:
```bash
$ RUST_LOG=my_lib=info cargo test it_adds_one
Running target/debug/my_lib-...
running 1 test
[INFO my_lib::tests] can log from the test too
[INFO my_lib] add_one called with 2
test tests::it_adds_one ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
```
## Configuring log target
By default, `env_logger` logs to stderr. If you want to log to stdout instead,
you can use the `Builder` to change the log target:
```rust
use std::env;
use env_logger::{Builder, Target};
let mut builder = Builder::from_default_env();
builder.target(Target::Stdout);
builder.init();
```
## Stability of the default format
The default format won't optimise for long-term stability, and explicitly makes no guarantees about the stability of its output across major, minor or patch version bumps during `0.x`.
If you want to capture or interpret the output of `env_logger` programmatically then you should use a custom format.

View File

@ -0,0 +1,44 @@
/*!
Disabling parts of the default format.
Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`:
```no_run,shell
$ export MY_LOG_LEVEL='info'
```
Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors
or `auto` to enable them:
```no_run,shell
$ export MY_LOG_STYLE=never
```
If you want to control the logging output completely, see the `custom_logger` example.
*/
#[macro_use]
extern crate log;
extern crate env_logger;
use env_logger::{Env, Builder};
fn init_logger() {
let env = Env::default()
.filter("MY_LOG_LEVEL")
.write_style("MY_LOG_STYLE");
let mut builder = Builder::from_env(env);
builder
.default_format_level(false)
.default_format_timestamp_nanos(true);
builder.init();
}
fn main() {
init_logger();
info!("a log from `MyLogger`");
}

View File

@ -0,0 +1,54 @@
/*!
Changing the default logging format.
Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`:
```no_run,shell
$ export MY_LOG_LEVEL='info'
```
Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors
or `auto` to enable them:
```no_run,shell
$ export MY_LOG_STYLE=never
```
If you want to control the logging output completely, see the `custom_logger` example.
*/
#[macro_use]
extern crate log;
extern crate env_logger;
use std::io::Write;
use env_logger::{Env, Builder, fmt};
fn init_logger() {
let env = Env::default()
.filter("MY_LOG_LEVEL")
.write_style("MY_LOG_STYLE");
let mut builder = Builder::from_env(env);
// Use a different format for writing log records
// The colors are only available when the `termcolor` dependency is (which it is by default)
#[cfg(feature = "termcolor")]
builder.format(|buf, record| {
let mut style = buf.style();
style.set_bg(fmt::Color::Yellow).set_bold(true);
let timestamp = buf.timestamp();
writeln!(buf, "My formatted log ({}): {}", timestamp, style.value(record.args()))
});
builder.init();
}
fn main() {
init_logger();
info!("a log from `MyLogger`");
}

View File

@ -0,0 +1,60 @@
/*!
Using `env_logger` to drive a custom logger.
Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`:
```no_run,shell
$ export MY_LOG_LEVEL='info'
```
If you only want to change the way logs are formatted, look at the `custom_format` example.
*/
#[macro_use]
extern crate log;
extern crate env_logger;
use env_logger::filter::Filter;
use log::{Log, Metadata, Record, SetLoggerError};
struct MyLogger {
inner: Filter
}
impl MyLogger {
fn new() -> MyLogger {
use env_logger::filter::Builder;
let mut builder = Builder::from_env("MY_LOG_LEVEL");
MyLogger {
inner: builder.build()
}
}
fn init() -> Result<(), SetLoggerError> {
let logger = Self::new();
log::set_max_level(logger.inner.filter());
log::set_boxed_logger(Box::new(logger))
}
}
impl Log for MyLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
self.inner.enabled(metadata)
}
fn log(&self, record: &Record) {
// Check if the record is matched by the logger before logging
if self.inner.matches(record) {
println!("{} - {}", record.level(), record.args());
}
}
fn flush(&self) { }
}
fn main() {
MyLogger::init().unwrap();
info!("a log from `MyLogger`");
}

View File

@ -0,0 +1,39 @@
/*!
Using `env_logger`.
Before running this example, try setting the `MY_LOG_LEVEL` environment variable to `info`:
```no_run,shell
$ export MY_LOG_LEVEL='info'
```
Also try setting the `MY_LOG_STYLE` environment variable to `never` to disable colors
or `auto` to enable them:
```no_run,shell
$ export MY_LOG_STYLE=never
```
*/
#[macro_use]
extern crate log;
extern crate env_logger;
use env_logger::Env;
fn main() {
// The `Env` lets us tweak what the environment
// variables to read are and what the default
// value is if they're missing
let env = Env::default()
.filter_or("MY_LOG_LEVEL", "trace")
.write_style_or("MY_LOG_STYLE", "always");
env_logger::init_from_env(env);
trace!("some trace log");
debug!("some debug log");
info!("some information log");
warn!("some warning log");
error!("some error log");
}

View File

@ -0,0 +1,40 @@
/*!
Using `env_logger::Logger` and the `log::Log` trait directly.
This example doesn't rely on environment variables, or having a static logger installed.
*/
extern crate log;
extern crate env_logger;
fn record() -> log::Record<'static> {
let error_metadata = log::MetadataBuilder::new()
.target("myApp")
.level(log::Level::Error)
.build();
log::Record::builder()
.metadata(error_metadata)
.args(format_args!("Error!"))
.line(Some(433))
.file(Some("app.rs"))
.module_path(Some("server"))
.build()
}
fn main() {
use log::Log;
let stylish_logger = env_logger::Builder::new()
.filter(None, log::LevelFilter::Error)
.write_style(env_logger::WriteStyle::Always)
.build();
let unstylish_logger = env_logger::Builder::new()
.filter(None, log::LevelFilter::Error)
.write_style(env_logger::WriteStyle::Never)
.build();
stylish_logger.log(&record());
unstylish_logger.log(&record());
}

View File

@ -0,0 +1,19 @@
/*!
Specify logging filters in code instead of using an environment variable.
*/
#[macro_use]
extern crate log;
extern crate env_logger;
fn main() {
env_logger::builder()
.filter_level(log::LevelFilter::Trace)
.init();
trace!("some trace log");
debug!("some debug log");
info!("some information log");
warn!("some warning log");
error!("some error log");
}

View File

@ -0,0 +1,579 @@
//! Filtering for log records.
//!
//! This module contains the log filtering used by `env_logger` to match records.
//! You can use the `Filter` type in your own logger implementation to use the same
//! filter parsing and matching as `env_logger`. For more details about the format
//! for directive strings see [Enabling Logging].
//!
//! ## Using `env_logger` in your own logger
//!
//! You can use `env_logger`'s filtering functionality with your own logger.
//! Call [`Builder::parse`] to parse directives from a string when constructing
//! your logger. Call [`Filter::matches`] to check whether a record should be
//! logged based on the parsed filters when log records are received.
//!
//! ```
//! extern crate log;
//! extern crate env_logger;
//! use env_logger::filter::Filter;
//! use log::{Log, Metadata, Record};
//!
//! struct MyLogger {
//! filter: Filter
//! }
//!
//! impl MyLogger {
//! fn new() -> MyLogger {
//! use env_logger::filter::Builder;
//! let mut builder = Builder::new();
//!
//! // Parse a directives string from an environment variable
//! if let Ok(ref filter) = std::env::var("MY_LOG_LEVEL") {
//! builder.parse(filter);
//! }
//!
//! MyLogger {
//! filter: builder.build()
//! }
//! }
//! }
//!
//! impl Log for MyLogger {
//! fn enabled(&self, metadata: &Metadata) -> bool {
//! self.filter.enabled(metadata)
//! }
//!
//! fn log(&self, record: &Record) {
//! // Check if the record is matched by the filter
//! if self.filter.matches(record) {
//! println!("{:?}", record);
//! }
//! }
//!
//! fn flush(&self) {}
//! }
//! # fn main() {}
//! ```
//!
//! [Enabling Logging]: ../index.html#enabling-logging
//! [`Builder::parse`]: struct.Builder.html#method.parse
//! [`Filter::matches`]: struct.Filter.html#method.matches
use std::env;
use std::mem;
use std::fmt;
use log::{Level, LevelFilter, Record, Metadata};
#[cfg(feature = "regex")]
#[path = "regex.rs"]
mod inner;
#[cfg(not(feature = "regex"))]
#[path = "string.rs"]
mod inner;
/// A log filter.
///
/// This struct can be used to determine whether or not a log record
/// should be written to the output.
/// Use the [`Builder`] type to parse and construct a `Filter`.
///
/// [`Builder`]: struct.Builder.html
pub struct Filter {
directives: Vec<Directive>,
filter: Option<inner::Filter>,
}
/// A builder for a log filter.
///
/// It can be used to parse a set of directives from a string before building
/// a [`Filter`] instance.
///
/// ## Example
///
/// ```
/// #[macro_use]
/// extern crate log;
/// extern crate env_logger;
///
/// use std::env;
/// use std::io;
/// use env_logger::filter::Builder;
///
/// fn main() {
/// let mut builder = Builder::new();
///
/// // Parse a logging filter from an environment variable.
/// if let Ok(rust_log) = env::var("RUST_LOG") {
/// builder.parse(&rust_log);
/// }
///
/// let filter = builder.build();
/// }
/// ```
///
/// [`Filter`]: struct.Filter.html
pub struct Builder {
directives: Vec<Directive>,
filter: Option<inner::Filter>,
built: bool,
}
#[derive(Debug)]
struct Directive {
name: Option<String>,
level: LevelFilter,
}
impl Filter {
/// Returns the maximum `LevelFilter` that this filter instance is
/// configured to output.
///
/// # Example
///
/// ```rust
/// extern crate log;
/// extern crate env_logger;
///
/// use log::LevelFilter;
/// use env_logger::filter::Builder;
///
/// fn main() {
/// let mut builder = Builder::new();
/// builder.filter(Some("module1"), LevelFilter::Info);
/// builder.filter(Some("module2"), LevelFilter::Error);
///
/// let filter = builder.build();
/// assert_eq!(filter.filter(), LevelFilter::Info);
/// }
/// ```
pub fn filter(&self) -> LevelFilter {
self.directives.iter()
.map(|d| d.level)
.max()
.unwrap_or(LevelFilter::Off)
}
/// Checks if this record matches the configured filter.
pub fn matches(&self, record: &Record) -> bool {
if !self.enabled(record.metadata()) {
return false;
}
if let Some(filter) = self.filter.as_ref() {
if !filter.is_match(&*record.args().to_string()) {
return false;
}
}
true
}
/// Determines if a log message with the specified metadata would be logged.
pub fn enabled(&self, metadata: &Metadata) -> bool {
let level = metadata.level();
let target = metadata.target();
enabled(&self.directives, level, target)
}
}
impl Builder {
/// Initializes the filter builder with defaults.
pub fn new() -> Builder {
Builder {
directives: Vec::new(),
filter: None,
built: false,
}
}
/// Initializes the filter builder from an environment.
pub fn from_env(env: &str) -> Builder {
let mut builder = Builder::new();
if let Ok(s) = env::var(env) {
builder.parse(&s);
}
builder
}
/// Adds a directive to the filter for a specific module.
pub fn filter_module(&mut self, module: &str, level: LevelFilter) -> &mut Self {
self.filter(Some(module), level)
}
/// Adds a directive to the filter for all modules.
pub fn filter_level(&mut self, level: LevelFilter) -> &mut Self {
self.filter(None, level)
}
/// Adds a directive to the filter.
///
/// 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: LevelFilter) -> &mut Self {
self.directives.push(Directive {
name: module.map(|s| s.to_string()),
level,
});
self
}
/// Parses the directives string.
///
/// See the [Enabling Logging] section for more details.
///
/// [Enabling Logging]: ../index.html#enabling-logging
pub fn parse(&mut self, filters: &str) -> &mut Self {
let (directives, filter) = parse_spec(filters);
self.filter = filter;
for directive in directives {
self.directives.push(directive);
}
self
}
/// Build a log filter.
pub fn build(&mut self) -> Filter {
assert!(!self.built, "attempt to re-use consumed builder");
self.built = true;
if self.directives.is_empty() {
// Adds the default filter if none exist
self.directives.push(Directive {
name: None,
level: LevelFilter::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)
});
}
Filter {
directives: mem::replace(&mut self.directives, Vec::new()),
filter: mem::replace(&mut self.filter, None),
}
}
}
impl Default for Builder {
fn default() -> Self {
Builder::new()
}
}
impl fmt::Debug for Filter {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
f.debug_struct("Filter")
.field("filter", &self.filter)
.field("directives", &self.directives)
.finish()
}
}
impl fmt::Debug for Builder {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
if self.built {
f.debug_struct("Filter")
.field("built", &true)
.finish()
} else {
f.debug_struct("Filter")
.field("filter", &self.filter)
.field("directives", &self.directives)
.finish()
}
}
}
/// Parse a logging specification string (e.g: "crate1,crate2::mod3,crate3::x=error/foo")
/// and return a vector with log directives.
fn parse_spec(spec: &str) -> (Vec<Directive>, Option<inner::Filter>) {
let mut dirs = Vec::new();
let mut parts = spec.split('/');
let mods = parts.next();
let filter = parts.next();
if parts.next().is_some() {
eprintln!("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(_) => (LevelFilter::max(), Some(part0)),
}
}
(Some(part0), Some(""), None) => (LevelFilter::max(), Some(part0)),
(Some(part0), Some(part1), None) => {
match part1.parse() {
Ok(num) => (num, Some(part0)),
_ => {
eprintln!("warning: invalid logging spec '{}', \
ignoring it", part1);
continue
}
}
},
_ => {
eprintln!("warning: invalid logging spec '{}', \
ignoring it", s);
continue
}
};
dirs.push(Directive {
name: name.map(|s| s.to_string()),
level: log_level,
});
}});
let filter = filter.map_or(None, |filter| {
match inner::Filter::new(filter) {
Ok(re) => Some(re),
Err(e) => {
eprintln!("warning: invalid regex filter - {}", e);
None
}
}
});
return (dirs, filter);
}
// Check whether a level and target are enabled by the set of directives.
fn enabled(directives: &[Directive], level: Level, target: &str) -> bool {
// Search for the longest match, the vector is assumed to be pre-sorted.
for directive in directives.iter().rev() {
match directive.name {
Some(ref name) if !target.starts_with(&**name) => {},
Some(..) | None => {
return level <= directive.level
}
}
}
false
}
#[cfg(test)]
mod tests {
use log::{Level, LevelFilter};
use super::{Builder, Filter, Directive, parse_spec, enabled};
fn make_logger_filter(dirs: Vec<Directive>) -> Filter {
let mut logger = Builder::new().build();
logger.directives = dirs;
logger
}
#[test]
fn filter_info() {
let logger = Builder::new().filter(None, LevelFilter::Info).build();
assert!(enabled(&logger.directives, Level::Info, "crate1"));
assert!(!enabled(&logger.directives, Level::Debug, "crate1"));
}
#[test]
fn filter_beginning_longest_match() {
let logger = Builder::new()
.filter(Some("crate2"), LevelFilter::Info)
.filter(Some("crate2::mod"), LevelFilter::Debug)
.filter(Some("crate1::mod1"), LevelFilter::Warn)
.build();
assert!(enabled(&logger.directives, Level::Debug, "crate2::mod1"));
assert!(!enabled(&logger.directives, Level::Debug, "crate2"));
}
#[test]
fn parse_default() {
let logger = Builder::new().parse("info,crate1::mod1=warn").build();
assert!(enabled(&logger.directives, Level::Warn, "crate1::mod1"));
assert!(enabled(&logger.directives, Level::Info, "crate2::mod2"));
}
#[test]
fn match_full_path() {
let logger = make_logger_filter(vec![
Directive {
name: Some("crate2".to_string()),
level: LevelFilter::Info
},
Directive {
name: Some("crate1::mod1".to_string()),
level: LevelFilter::Warn
}
]);
assert!(enabled(&logger.directives, Level::Warn, "crate1::mod1"));
assert!(!enabled(&logger.directives, Level::Info, "crate1::mod1"));
assert!(enabled(&logger.directives, Level::Info, "crate2"));
assert!(!enabled(&logger.directives, Level::Debug, "crate2"));
}
#[test]
fn no_match() {
let logger = make_logger_filter(vec![
Directive { name: Some("crate2".to_string()), level: LevelFilter::Info },
Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn }
]);
assert!(!enabled(&logger.directives, Level::Warn, "crate3"));
}
#[test]
fn match_beginning() {
let logger = make_logger_filter(vec![
Directive { name: Some("crate2".to_string()), level: LevelFilter::Info },
Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn }
]);
assert!(enabled(&logger.directives, Level::Info, "crate2::mod1"));
}
#[test]
fn match_beginning_longest_match() {
let logger = make_logger_filter(vec![
Directive { name: Some("crate2".to_string()), level: LevelFilter::Info },
Directive { name: Some("crate2::mod".to_string()), level: LevelFilter::Debug },
Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn }
]);
assert!(enabled(&logger.directives, Level::Debug, "crate2::mod1"));
assert!(!enabled(&logger.directives, Level::Debug, "crate2"));
}
#[test]
fn match_default() {
let logger = make_logger_filter(vec![
Directive { name: None, level: LevelFilter::Info },
Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn }
]);
assert!(enabled(&logger.directives, Level::Warn, "crate1::mod1"));
assert!(enabled(&logger.directives, Level::Info, "crate2::mod2"));
}
#[test]
fn zero_level() {
let logger = make_logger_filter(vec![
Directive { name: None, level: LevelFilter::Info },
Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Off }
]);
assert!(!enabled(&logger.directives, Level::Error, "crate1::mod1"));
assert!(enabled(&logger.directives, Level::Info, "crate2::mod2"));
}
#[test]
fn parse_spec_valid() {
let (dirs, filter) = parse_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, LevelFilter::Error);
assert_eq!(dirs[1].name, Some("crate1::mod2".to_string()));
assert_eq!(dirs[1].level, LevelFilter::max());
assert_eq!(dirs[2].name, Some("crate2".to_string()));
assert_eq!(dirs[2].level, LevelFilter::Debug);
assert!(filter.is_none());
}
#[test]
fn parse_spec_invalid_crate() {
// test parse_spec with multiple = in specification
let (dirs, filter) = parse_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, LevelFilter::Debug);
assert!(filter.is_none());
}
#[test]
fn parse_spec_invalid_level() {
// test parse_spec with 'noNumber' as log level
let (dirs, filter) = parse_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, LevelFilter::Debug);
assert!(filter.is_none());
}
#[test]
fn parse_spec_string_level() {
// test parse_spec with 'warn' as log level
let (dirs, filter) = parse_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, LevelFilter::Warn);
assert!(filter.is_none());
}
#[test]
fn parse_spec_empty_level() {
// test parse_spec with '' as log level
let (dirs, filter) = parse_spec("crate1::mod1=wrong,crate2=");
assert_eq!(dirs.len(), 1);
assert_eq!(dirs[0].name, Some("crate2".to_string()));
assert_eq!(dirs[0].level, LevelFilter::max());
assert!(filter.is_none());
}
#[test]
fn parse_spec_global() {
// test parse_spec with no crate
let (dirs, filter) = parse_spec("warn,crate2=debug");
assert_eq!(dirs.len(), 2);
assert_eq!(dirs[0].name, None);
assert_eq!(dirs[0].level, LevelFilter::Warn);
assert_eq!(dirs[1].name, Some("crate2".to_string()));
assert_eq!(dirs[1].level, LevelFilter::Debug);
assert!(filter.is_none());
}
#[test]
fn parse_spec_valid_filter() {
let (dirs, filter) = parse_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, LevelFilter::Error);
assert_eq!(dirs[1].name, Some("crate1::mod2".to_string()));
assert_eq!(dirs[1].level, LevelFilter::max());
assert_eq!(dirs[2].name, Some("crate2".to_string()));
assert_eq!(dirs[2].level, LevelFilter::Debug);
assert!(filter.is_some() && filter.unwrap().to_string() == "abc");
}
#[test]
fn parse_spec_invalid_crate_filter() {
let (dirs, filter) = parse_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, LevelFilter::Debug);
assert!(filter.is_some() && filter.unwrap().to_string() == "a.c");
}
#[test]
fn parse_spec_empty_with_filter() {
let (dirs, filter) = parse_spec("crate1/a*c");
assert_eq!(dirs.len(), 1);
assert_eq!(dirs[0].name, Some("crate1".to_string()));
assert_eq!(dirs[0].level, LevelFilter::max());
assert!(filter.is_some() && filter.unwrap().to_string() == "a*c");
}
}

View File

@ -0,0 +1,29 @@
extern crate regex;
use std::fmt;
use self::regex::Regex;
#[derive(Debug)]
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)
}
}

View File

@ -0,0 +1,22 @@
use std::fmt;
#[derive(Debug)]
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)
}
}

View File

@ -0,0 +1,84 @@
use std::fmt;
use std::time::SystemTime;
use humantime::{format_rfc3339_nanos, format_rfc3339_seconds};
use ::fmt::Formatter;
pub(in ::fmt) mod glob {
pub use super::*;
}
impl Formatter {
/// Get a [`Timestamp`] for the current date and time in UTC.
///
/// # Examples
///
/// Include the current timestamp with the log record:
///
/// ```
/// use std::io::Write;
///
/// let mut builder = env_logger::Builder::new();
///
/// builder.format(|buf, record| {
/// let ts = buf.timestamp();
///
/// writeln!(buf, "{}: {}: {}", ts, record.level(), record.args())
/// });
/// ```
///
/// [`Timestamp`]: struct.Timestamp.html
pub fn timestamp(&self) -> Timestamp {
Timestamp(SystemTime::now())
}
/// Get a [`PreciseTimestamp`] for the current date and time in UTC with nanos.
pub fn precise_timestamp(&self) -> PreciseTimestamp {
PreciseTimestamp(SystemTime::now())
}
}
/// An [RFC3339] formatted timestamp.
///
/// The timestamp implements [`Display`] and can be written to a [`Formatter`].
///
/// [RFC3339]: https://www.ietf.org/rfc/rfc3339.txt
/// [`Display`]: https://doc.rust-lang.org/stable/std/fmt/trait.Display.html
/// [`Formatter`]: struct.Formatter.html
pub struct Timestamp(SystemTime);
/// An [RFC3339] formatted timestamp with nanos.
///
/// [RFC3339]: https://www.ietf.org/rfc/rfc3339.txt
#[derive(Debug)]
pub struct PreciseTimestamp(SystemTime);
impl fmt::Debug for Timestamp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
/// A `Debug` wrapper for `Timestamp` that uses the `Display` implementation.
struct TimestampValue<'a>(&'a Timestamp);
impl<'a> fmt::Debug for TimestampValue<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
f.debug_tuple("Timestamp")
.field(&TimestampValue(&self))
.finish()
}
}
impl fmt::Display for Timestamp {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
format_rfc3339_seconds(self.0).fmt(f)
}
}
impl fmt::Display for PreciseTimestamp {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
format_rfc3339_nanos(self.0).fmt(f)
}
}

View File

@ -0,0 +1,11 @@
/*
This internal module contains the timestamp implementation.
Its public API is available when the `humantime` crate is available.
*/
#[cfg_attr(feature = "humantime", path = "extern_impl.rs")]
#[cfg_attr(not(feature = "humantime"), path = "shim_impl.rs")]
mod imp;
pub(in ::fmt) use self::imp::*;

View File

@ -0,0 +1,7 @@
/*
Timestamps aren't available when we don't have a `humantime` dependency.
*/
pub(in ::fmt) mod glob {
}

View File

@ -0,0 +1,358 @@
//! Formatting for log records.
//!
//! This module contains a [`Formatter`] that can be used to format log records
//! into without needing temporary allocations. Usually you won't need to worry
//! about the contents of this module and can use the `Formatter` like an ordinary
//! [`Write`].
//!
//! # Formatting log records
//!
//! The format used to print log records can be customised using the [`Builder::format`]
//! method.
//! Custom formats can apply different color and weight to printed values using
//! [`Style`] builders.
//!
//! ```
//! use std::io::Write;
//!
//! let mut builder = env_logger::Builder::new();
//!
//! builder.format(|buf, record| {
//! writeln!(buf, "{}: {}",
//! record.level(),
//! record.args())
//! });
//! ```
//!
//! [`Formatter`]: struct.Formatter.html
//! [`Style`]: struct.Style.html
//! [`Builder::format`]: ../struct.Builder.html#method.format
//! [`Write`]: https://doc.rust-lang.org/stable/std/io/trait.Write.html
use std::io::prelude::*;
use std::{io, fmt, mem};
use std::rc::Rc;
use std::cell::RefCell;
use std::fmt::Display;
use log::Record;
pub(crate) mod writer;
mod humantime;
pub use self::humantime::glob::*;
pub use self::writer::glob::*;
use self::writer::{Writer, Buffer};
pub(crate) mod glob {
pub use super::{Target, WriteStyle};
}
/// A formatter to write logs into.
///
/// `Formatter` implements the standard [`Write`] trait for writing log records.
/// It also supports terminal colors, through the [`style`] method.
///
/// # Examples
///
/// Use the [`writeln`] macro to format a log record.
/// An instance of a `Formatter` is passed to an `env_logger` format as `buf`:
///
/// ```
/// use std::io::Write;
///
/// let mut builder = env_logger::Builder::new();
///
/// builder.format(|buf, record| writeln!(buf, "{}: {}", record.level(), record.args()));
/// ```
///
/// [`Write`]: https://doc.rust-lang.org/stable/std/io/trait.Write.html
/// [`writeln`]: https://doc.rust-lang.org/stable/std/macro.writeln.html
/// [`style`]: #method.style
pub struct Formatter {
buf: Rc<RefCell<Buffer>>,
write_style: WriteStyle,
}
impl Formatter {
pub(crate) fn new(writer: &Writer) -> Self {
Formatter {
buf: Rc::new(RefCell::new(writer.buffer())),
write_style: writer.write_style(),
}
}
pub(crate) fn write_style(&self) -> WriteStyle {
self.write_style
}
pub(crate) fn print(&self, writer: &Writer) -> io::Result<()> {
writer.print(&self.buf.borrow())
}
pub(crate) fn clear(&mut self) {
self.buf.borrow_mut().clear()
}
}
impl Write for Formatter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.buf.borrow_mut().write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.buf.borrow_mut().flush()
}
}
impl fmt::Debug for Formatter {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
f.debug_struct("Formatter").finish()
}
}
pub(crate) struct Builder {
pub default_format_timestamp: bool,
pub default_format_timestamp_nanos: bool,
pub default_format_module_path: bool,
pub default_format_level: bool,
#[allow(unknown_lints, bare_trait_objects)]
pub custom_format: Option<Box<Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send>>,
built: bool,
}
impl Default for Builder {
fn default() -> Self {
Builder {
default_format_timestamp: true,
default_format_timestamp_nanos: false,
default_format_module_path: true,
default_format_level: true,
custom_format: None,
built: false,
}
}
}
impl Builder {
/// Convert the format into a callable function.
///
/// If the `custom_format` is `Some`, then any `default_format` switches are ignored.
/// If the `custom_format` is `None`, then a default format is returned.
/// Any `default_format` switches set to `false` won't be written by the format.
#[allow(unknown_lints, bare_trait_objects)]
pub fn build(&mut self) -> Box<Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send> {
assert!(!self.built, "attempt to re-use consumed builder");
let built = mem::replace(self, Builder {
built: true,
..Default::default()
});
if let Some(fmt) = built.custom_format {
fmt
}
else {
Box::new(move |buf, record| {
let fmt = DefaultFormat {
timestamp: built.default_format_timestamp,
timestamp_nanos: built.default_format_timestamp_nanos,
module_path: built.default_format_module_path,
level: built.default_format_level,
written_header_value: false,
buf,
};
fmt.write(record)
})
}
}
}
#[cfg(feature = "termcolor")]
type SubtleStyle = StyledValue<'static, &'static str>;
#[cfg(not(feature = "termcolor"))]
type SubtleStyle = &'static str;
/// The default format.
///
/// This format needs to work with any combination of crate features.
struct DefaultFormat<'a> {
timestamp: bool,
module_path: bool,
level: bool,
timestamp_nanos: bool,
written_header_value: bool,
buf: &'a mut Formatter,
}
impl<'a> DefaultFormat<'a> {
fn write(mut self, record: &Record) -> io::Result<()> {
self.write_timestamp()?;
self.write_level(record)?;
self.write_module_path(record)?;
self.finish_header()?;
self.write_args(record)
}
fn subtle_style(&self, text: &'static str) -> SubtleStyle {
#[cfg(feature = "termcolor")]
{
self.buf.style()
.set_color(Color::Black)
.set_intense(true)
.into_value(text)
}
#[cfg(not(feature = "termcolor"))]
{
text
}
}
fn write_header_value<T>(&mut self, value: T) -> io::Result<()>
where
T: Display,
{
if !self.written_header_value {
self.written_header_value = true;
let open_brace = self.subtle_style("[");
write!(self.buf, "{}{}", open_brace, value)
} else {
write!(self.buf, " {}", value)
}
}
fn write_level(&mut self, record: &Record) -> io::Result<()> {
if !self.level {
return Ok(())
}
let level = {
#[cfg(feature = "termcolor")]
{
self.buf.default_styled_level(record.level())
}
#[cfg(not(feature = "termcolor"))]
{
record.level()
}
};
self.write_header_value(format_args!("{:<5}", level))
}
fn write_timestamp(&mut self) -> io::Result<()> {
#[cfg(feature = "humantime")]
{
if !self.timestamp {
return Ok(())
}
if self.timestamp_nanos {
let ts_nanos = self.buf.precise_timestamp();
self.write_header_value(ts_nanos)
} else {
let ts = self.buf.timestamp();
self.write_header_value(ts)
}
}
#[cfg(not(feature = "humantime"))]
{
let _ = self.timestamp;
let _ = self.timestamp_nanos;
Ok(())
}
}
fn write_module_path(&mut self, record: &Record) -> io::Result<()> {
if !self.module_path {
return Ok(())
}
if let Some(module_path) = record.module_path() {
self.write_header_value(module_path)
} else {
Ok(())
}
}
fn finish_header(&mut self) -> io::Result<()> {
if self.written_header_value {
let close_brace = self.subtle_style("]");
write!(self.buf, "{} ", close_brace)
} else {
Ok(())
}
}
fn write_args(&mut self, record: &Record) -> io::Result<()> {
writeln!(self.buf, "{}", record.args())
}
}
#[cfg(test)]
mod tests {
use super::*;
use log::{Level, Record};
fn write(fmt: DefaultFormat) -> String {
let buf = fmt.buf.buf.clone();
let record = Record::builder()
.args(format_args!("log message"))
.level(Level::Info)
.file(Some("test.rs"))
.line(Some(144))
.module_path(Some("test::path"))
.build();
fmt.write(&record).expect("failed to write record");
let buf = buf.borrow();
String::from_utf8(buf.bytes().to_vec()).expect("failed to read record")
}
#[test]
fn default_format_with_header() {
let writer = writer::Builder::new()
.write_style(WriteStyle::Never)
.build();
let mut f = Formatter::new(&writer);
let written = write(DefaultFormat {
timestamp: false,
timestamp_nanos: false,
module_path: true,
level: true,
written_header_value: false,
buf: &mut f,
});
assert_eq!("[INFO test::path] log message\n", written);
}
#[test]
fn default_format_no_header() {
let writer = writer::Builder::new()
.write_style(WriteStyle::Never)
.build();
let mut f = Formatter::new(&writer);
let written = write(DefaultFormat {
timestamp: false,
timestamp_nanos: false,
module_path: false,
level: false,
written_header_value: false,
buf: &mut f,
});
assert_eq!("log message\n", written);
}
}

View File

@ -0,0 +1,34 @@
/*
This internal module contains the terminal detection implementation.
If the `atty` crate is available then we use it to detect whether we're
attached to a particular TTY. If the `atty` crate is not available we
assume we're not attached to anything. This effectively prevents styles
from being printed.
*/
#[cfg(feature = "atty")]
mod imp {
use atty;
pub(in ::fmt) fn is_stdout() -> bool {
atty::is(atty::Stream::Stdout)
}
pub(in ::fmt) fn is_stderr() -> bool {
atty::is(atty::Stream::Stderr)
}
}
#[cfg(not(feature = "atty"))]
mod imp {
pub(in ::fmt) fn is_stdout() -> bool {
false
}
pub(in ::fmt) fn is_stderr() -> bool {
false
}
}
pub(in ::fmt) use self::imp::*;

View File

@ -0,0 +1,206 @@
mod termcolor;
mod atty;
use std::{fmt, io};
use self::termcolor::BufferWriter;
use self::atty::{is_stdout, is_stderr};
pub(in ::fmt) mod glob {
pub use super::termcolor::glob::*;
pub use super::*;
}
pub(in ::fmt) use self::termcolor::Buffer;
/// Log target, either `stdout` or `stderr`.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Target {
/// Logs will be sent to standard output.
Stdout,
/// Logs will be sent to standard error.
Stderr,
}
impl Default for Target {
fn default() -> Self {
Target::Stderr
}
}
/// Whether or not to print styles to the target.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum WriteStyle {
/// Try to print styles, but don't force the issue.
Auto,
/// Try very hard to print styles.
Always,
/// Never print styles.
Never,
}
impl Default for WriteStyle {
fn default() -> Self {
WriteStyle::Auto
}
}
/// A terminal target with color awareness.
pub(crate) struct Writer {
inner: BufferWriter,
write_style: WriteStyle,
}
impl Writer {
pub fn write_style(&self) -> WriteStyle {
self.write_style
}
pub(in ::fmt) fn buffer(&self) -> Buffer {
self.inner.buffer()
}
pub(in ::fmt) fn print(&self, buf: &Buffer) -> io::Result<()> {
self.inner.print(buf)
}
}
/// A builder for a terminal writer.
///
/// The target and style choice can be configured before building.
pub(crate) struct Builder {
target: Target,
write_style: WriteStyle,
is_test: bool,
built: bool,
}
impl Builder {
/// Initialize the writer builder with defaults.
pub(crate) fn new() -> Self {
Builder {
target: Default::default(),
write_style: Default::default(),
is_test: false,
built: false,
}
}
/// Set the target to write to.
pub(crate) fn target(&mut self, target: Target) -> &mut Self {
self.target = target;
self
}
/// Parses a style choice string.
///
/// See the [Disabling colors] section for more details.
///
/// [Disabling colors]: ../index.html#disabling-colors
pub(crate) fn parse_write_style(&mut self, write_style: &str) -> &mut Self {
self.write_style(parse_write_style(write_style))
}
/// Whether or not to print style characters when writing.
pub(crate) fn write_style(&mut self, write_style: WriteStyle) -> &mut Self {
self.write_style = write_style;
self
}
/// Whether or not to capture logs for `cargo test`.
pub(crate) fn is_test(&mut self, is_test: bool) -> &mut Self {
self.is_test = is_test;
self
}
/// Build a terminal writer.
pub(crate) fn build(&mut self) -> Writer {
assert!(!self.built, "attempt to re-use consumed builder");
self.built = true;
let color_choice = match self.write_style {
WriteStyle::Auto => {
if match self.target {
Target::Stderr => is_stderr(),
Target::Stdout => is_stdout(),
} {
WriteStyle::Auto
} else {
WriteStyle::Never
}
},
color_choice => color_choice,
};
let writer = match self.target {
Target::Stderr => BufferWriter::stderr(self.is_test, color_choice),
Target::Stdout => BufferWriter::stdout(self.is_test, color_choice),
};
Writer {
inner: writer,
write_style: self.write_style,
}
}
}
impl Default for Builder {
fn default() -> Self {
Builder::new()
}
}
impl fmt::Debug for Builder {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
f.debug_struct("Logger")
.field("target", &self.target)
.field("write_style", &self.write_style)
.finish()
}
}
impl fmt::Debug for Writer {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
f.debug_struct("Writer").finish()
}
}
fn parse_write_style(spec: &str) -> WriteStyle {
match spec {
"auto" => WriteStyle::Auto,
"always" => WriteStyle::Always,
"never" => WriteStyle::Never,
_ => Default::default(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_write_style_valid() {
let inputs = vec![
("auto", WriteStyle::Auto),
("always", WriteStyle::Always),
("never", WriteStyle::Never),
];
for (input, expected) in inputs {
assert_eq!(expected, parse_write_style(input));
}
}
#[test]
fn parse_write_style_invalid() {
let inputs = vec![
"",
"true",
"false",
"NEVER!!"
];
for input in inputs {
assert_eq!(WriteStyle::Auto, parse_write_style(input));
}
}
}

View File

@ -0,0 +1,490 @@
use std::borrow::Cow;
use std::fmt;
use std::io::{self, Write};
use std::cell::RefCell;
use std::rc::Rc;
use log::Level;
use termcolor::{self, ColorChoice, ColorSpec, WriteColor};
use ::WriteStyle;
use ::fmt::{Formatter, Target};
pub(in ::fmt::writer) mod glob {
pub use super::*;
}
impl Formatter {
/// Begin a new [`Style`].
///
/// # Examples
///
/// Create a bold, red colored style and use it to print the log level:
///
/// ```
/// use std::io::Write;
/// use env_logger::fmt::Color;
///
/// let mut builder = env_logger::Builder::new();
///
/// builder.format(|buf, record| {
/// let mut level_style = buf.style();
///
/// level_style.set_color(Color::Red).set_bold(true);
///
/// writeln!(buf, "{}: {}",
/// level_style.value(record.level()),
/// record.args())
/// });
/// ```
///
/// [`Style`]: struct.Style.html
pub fn style(&self) -> Style {
Style {
buf: self.buf.clone(),
spec: ColorSpec::new(),
}
}
/// Get the default [`Style`] for the given level.
///
/// The style can be used to print other values besides the level.
pub fn default_level_style(&self, level: Level) -> Style {
let mut level_style = self.style();
match level {
Level::Trace => level_style.set_color(Color::Black).set_intense(true),
Level::Debug => level_style.set_color(Color::White),
Level::Info => level_style.set_color(Color::Green),
Level::Warn => level_style.set_color(Color::Yellow),
Level::Error => level_style.set_color(Color::Red).set_bold(true),
};
level_style
}
/// Get a printable [`Style`] for the given level.
///
/// The style can only be used to print the level.
pub fn default_styled_level(&self, level: Level) -> StyledValue<'static, Level> {
self.default_level_style(level).into_value(level)
}
}
pub(in ::fmt::writer) struct BufferWriter {
inner: termcolor::BufferWriter,
test_target: Option<Target>,
}
pub(in ::fmt) struct Buffer {
inner: termcolor::Buffer,
test_target: Option<Target>,
}
impl BufferWriter {
pub(in ::fmt::writer) fn stderr(is_test: bool, write_style: WriteStyle) -> Self {
BufferWriter {
inner: termcolor::BufferWriter::stderr(write_style.into_color_choice()),
test_target: if is_test {
Some(Target::Stderr)
} else {
None
},
}
}
pub(in ::fmt::writer) fn stdout(is_test: bool, write_style: WriteStyle) -> Self {
BufferWriter {
inner: termcolor::BufferWriter::stdout(write_style.into_color_choice()),
test_target: if is_test {
Some(Target::Stdout)
} else {
None
},
}
}
pub(in ::fmt::writer) fn buffer(&self) -> Buffer {
Buffer {
inner: self.inner.buffer(),
test_target: self.test_target,
}
}
pub(in ::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
if let Some(target) = self.test_target {
// This impl uses the `eprint` and `print` macros
// instead of `termcolor`'s buffer.
// This is so their output can be captured by `cargo test`
let log = String::from_utf8_lossy(buf.bytes());
match target {
Target::Stderr => eprint!("{}", log),
Target::Stdout => print!("{}", log),
}
Ok(())
} else {
self.inner.print(&buf.inner)
}
}
}
impl Buffer {
pub(in ::fmt) fn clear(&mut self) {
self.inner.clear()
}
pub(in ::fmt) fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)
}
pub(in ::fmt) fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
pub(in ::fmt) fn bytes(&self) -> &[u8] {
self.inner.as_slice()
}
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
// Ignore styles for test captured logs because they can't be printed
if self.test_target.is_none() {
self.inner.set_color(spec)
} else {
Ok(())
}
}
fn reset(&mut self) -> io::Result<()> {
// Ignore styles for test captured logs because they can't be printed
if self.test_target.is_none() {
self.inner.reset()
} else {
Ok(())
}
}
}
impl WriteStyle {
fn into_color_choice(self) -> ColorChoice {
match self {
WriteStyle::Always => ColorChoice::Always,
WriteStyle::Auto => ColorChoice::Auto,
WriteStyle::Never => ColorChoice::Never,
}
}
}
/// A set of styles to apply to the terminal output.
///
/// Call [`Formatter::style`] to get a `Style` and use the builder methods to
/// set styling properties, like [color] and [weight].
/// To print a value using the style, wrap it in a call to [`value`] when the log
/// record is formatted.
///
/// # Examples
///
/// Create a bold, red colored style and use it to print the log level:
///
/// ```
/// use std::io::Write;
/// use env_logger::fmt::Color;
///
/// let mut builder = env_logger::Builder::new();
///
/// builder.format(|buf, record| {
/// let mut level_style = buf.style();
///
/// level_style.set_color(Color::Red).set_bold(true);
///
/// writeln!(buf, "{}: {}",
/// level_style.value(record.level()),
/// record.args())
/// });
/// ```
///
/// Styles can be re-used to output multiple values:
///
/// ```
/// use std::io::Write;
/// use env_logger::fmt::Color;
///
/// let mut builder = env_logger::Builder::new();
///
/// builder.format(|buf, record| {
/// let mut bold = buf.style();
///
/// bold.set_bold(true);
///
/// writeln!(buf, "{}: {} {}",
/// bold.value(record.level()),
/// bold.value("some bold text"),
/// record.args())
/// });
/// ```
///
/// [`Formatter::style`]: struct.Formatter.html#method.style
/// [color]: #method.set_color
/// [weight]: #method.set_bold
/// [`value`]: #method.value
#[derive(Clone)]
pub struct Style {
buf: Rc<RefCell<Buffer>>,
spec: ColorSpec,
}
/// A value that can be printed using the given styles.
///
/// It is the result of calling [`Style::value`].
///
/// [`Style::value`]: struct.Style.html#method.value
pub struct StyledValue<'a, T> {
style: Cow<'a, Style>,
value: T,
}
impl Style {
/// Set the text color.
///
/// # Examples
///
/// Create a style with red text:
///
/// ```
/// use std::io::Write;
/// use env_logger::fmt::Color;
///
/// let mut builder = env_logger::Builder::new();
///
/// builder.format(|buf, record| {
/// let mut style = buf.style();
///
/// style.set_color(Color::Red);
///
/// writeln!(buf, "{}", style.value(record.args()))
/// });
/// ```
pub fn set_color(&mut self, color: Color) -> &mut Style {
self.spec.set_fg(color.into_termcolor());
self
}
/// Set the text weight.
///
/// If `yes` is true then text will be written in bold.
/// If `yes` is false then text will be written in the default weight.
///
/// # Examples
///
/// Create a style with bold text:
///
/// ```
/// use std::io::Write;
///
/// let mut builder = env_logger::Builder::new();
///
/// builder.format(|buf, record| {
/// let mut style = buf.style();
///
/// style.set_bold(true);
///
/// writeln!(buf, "{}", style.value(record.args()))
/// });
/// ```
pub fn set_bold(&mut self, yes: bool) -> &mut Style {
self.spec.set_bold(yes);
self
}
/// Set the text intensity.
///
/// If `yes` is true then text will be written in a brighter color.
/// If `yes` is false then text will be written in the default color.
///
/// # Examples
///
/// Create a style with intense text:
///
/// ```
/// use std::io::Write;
///
/// let mut builder = env_logger::Builder::new();
///
/// builder.format(|buf, record| {
/// let mut style = buf.style();
///
/// style.set_intense(true);
///
/// writeln!(buf, "{}", style.value(record.args()))
/// });
/// ```
pub fn set_intense(&mut self, yes: bool) -> &mut Style {
self.spec.set_intense(yes);
self
}
/// Set the background color.
///
/// # Examples
///
/// Create a style with a yellow background:
///
/// ```
/// use std::io::Write;
/// use env_logger::fmt::Color;
///
/// let mut builder = env_logger::Builder::new();
///
/// builder.format(|buf, record| {
/// let mut style = buf.style();
///
/// style.set_bg(Color::Yellow);
///
/// writeln!(buf, "{}", style.value(record.args()))
/// });
/// ```
pub fn set_bg(&mut self, color: Color) -> &mut Style {
self.spec.set_bg(color.into_termcolor());
self
}
/// Wrap a value in the style.
///
/// The same `Style` can be used to print multiple different values.
///
/// # Examples
///
/// Create a bold, red colored style and use it to print the log level:
///
/// ```
/// use std::io::Write;
/// use env_logger::fmt::Color;
///
/// let mut builder = env_logger::Builder::new();
///
/// builder.format(|buf, record| {
/// let mut style = buf.style();
///
/// style.set_color(Color::Red).set_bold(true);
///
/// writeln!(buf, "{}: {}",
/// style.value(record.level()),
/// record.args())
/// });
/// ```
pub fn value<T>(&self, value: T) -> StyledValue<T> {
StyledValue {
style: Cow::Borrowed(self),
value
}
}
/// Wrap a value in the style by taking ownership of it.
pub(crate) fn into_value<T>(&mut self, value: T) -> StyledValue<'static, T> {
StyledValue {
style: Cow::Owned(self.clone()),
value
}
}
}
impl<'a, T> StyledValue<'a, T> {
fn write_fmt<F>(&self, f: F) -> fmt::Result
where
F: FnOnce() -> fmt::Result,
{
self.style.buf.borrow_mut().set_color(&self.style.spec).map_err(|_| fmt::Error)?;
// Always try to reset the terminal style, even if writing failed
let write = f();
let reset = self.style.buf.borrow_mut().reset().map_err(|_| fmt::Error);
write.and(reset)
}
}
impl fmt::Debug for Style {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
f.debug_struct("Style").field("spec", &self.spec).finish()
}
}
macro_rules! impl_styled_value_fmt {
($($fmt_trait:path),*) => {
$(
impl<'a, T: $fmt_trait> $fmt_trait for StyledValue<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
self.write_fmt(|| T::fmt(&self.value, f))
}
}
)*
};
}
impl_styled_value_fmt!(
fmt::Debug,
fmt::Display,
fmt::Pointer,
fmt::Octal,
fmt::Binary,
fmt::UpperHex,
fmt::LowerHex,
fmt::UpperExp,
fmt::LowerExp);
// The `Color` type is copied from https://github.com/BurntSushi/ripgrep/tree/master/termcolor
/// The set of available colors for the terminal foreground/background.
///
/// The `Ansi256` and `Rgb` colors will only output the correct codes when
/// paired with the `Ansi` `WriteColor` implementation.
///
/// The `Ansi256` and `Rgb` color types are not supported when writing colors
/// on Windows using the console. If they are used on Windows, then they are
/// silently ignored and no colors will be emitted.
///
/// This set may expand over time.
///
/// This type has a `FromStr` impl that can parse colors from their human
/// readable form. The format is as follows:
///
/// 1. Any of the explicitly listed colors in English. They are matched
/// case insensitively.
/// 2. A single 8-bit integer, in either decimal or hexadecimal format.
/// 3. A triple of 8-bit integers separated by a comma, where each integer is
/// in decimal or hexadecimal format.
///
/// Hexadecimal numbers are written with a `0x` prefix.
#[allow(missing_docs)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Color {
Black,
Blue,
Green,
Red,
Cyan,
Magenta,
Yellow,
White,
Ansi256(u8),
Rgb(u8, u8, u8),
#[doc(hidden)]
__Nonexhaustive,
}
impl Color {
fn into_termcolor(self) -> Option<termcolor::Color> {
match self {
Color::Black => Some(termcolor::Color::Black),
Color::Blue => Some(termcolor::Color::Blue),
Color::Green => Some(termcolor::Color::Green),
Color::Red => Some(termcolor::Color::Red),
Color::Cyan => Some(termcolor::Color::Cyan),
Color::Magenta => Some(termcolor::Color::Magenta),
Color::Yellow => Some(termcolor::Color::Yellow),
Color::White => Some(termcolor::Color::White),
Color::Ansi256(value) => Some(termcolor::Color::Ansi256(value)),
Color::Rgb(r, g, b) => Some(termcolor::Color::Rgb(r, g, b)),
_ => None,
}
}
}

View File

@ -0,0 +1,12 @@
/*
This internal module contains the style and terminal writing implementation.
Its public API is available when the `termcolor` crate is available.
The terminal printing is shimmed when the `termcolor` crate is not available.
*/
#[cfg_attr(feature = "termcolor", path = "extern_impl.rs")]
#[cfg_attr(not(feature = "termcolor"), path = "shim_impl.rs")]
mod imp;
pub(in ::fmt) use self::imp::*;

View File

@ -0,0 +1,65 @@
use std::io;
use fmt::{WriteStyle, Target};
pub(in ::fmt::writer) mod glob {
}
pub(in ::fmt::writer) struct BufferWriter {
target: Target,
}
pub(in ::fmt) struct Buffer(Vec<u8>);
impl BufferWriter {
pub(in ::fmt::writer) fn stderr(_is_test: bool, _write_style: WriteStyle) -> Self {
BufferWriter {
target: Target::Stderr,
}
}
pub(in ::fmt::writer) fn stdout(_is_test: bool, _write_style: WriteStyle) -> Self {
BufferWriter {
target: Target::Stdout,
}
}
pub(in ::fmt::writer) fn buffer(&self) -> Buffer {
Buffer(Vec::new())
}
pub(in ::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
// This impl uses the `eprint` and `print` macros
// instead of using the streams directly.
// This is so their output can be captured by `cargo test`
let log = String::from_utf8_lossy(&buf.0);
match self.target {
Target::Stderr => eprint!("{}", log),
Target::Stdout => print!("{}", log),
}
Ok(())
}
}
impl Buffer {
pub(in ::fmt) fn clear(&mut self) {
self.0.clear();
}
pub(in ::fmt) fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.extend(buf);
Ok(buf.len())
}
pub(in ::fmt) fn flush(&mut self) -> io::Result<()> {
Ok(())
}
#[cfg(test)]
pub(in ::fmt) fn bytes(&self) -> &[u8] {
&self.0
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,40 @@
extern crate log;
extern crate env_logger;
use std::process;
use std::env;
use std::str;
fn main() {
if env::var("YOU_ARE_TESTING_NOW").is_ok() {
// Init from the env (which should set the max level to `Debug`)
env_logger::init();
assert_eq!(log::LevelFilter::Debug, log::max_level());
// Init again using a different max level
// This shouldn't clobber the level that was previously set
env_logger::Builder::new()
.parse_filters("info")
.try_init()
.unwrap_err();
assert_eq!(log::LevelFilter::Debug, log::max_level());
return
}
let exe = env::current_exe().unwrap();
let out = process::Command::new(exe)
.env("YOU_ARE_TESTING_NOW", "1")
.env("RUST_LOG", "debug")
.output()
.unwrap_or_else(|e| panic!("Unable to start child process: {}", e));
if out.status.success() {
return
}
println!("test failed: {}", out.status);
println!("--- stdout\n{}", str::from_utf8(&out.stdout).unwrap());
println!("--- stderr\n{}", str::from_utf8(&out.stderr).unwrap());
process::exit(1);
}

View File

@ -0,0 +1,38 @@
#[macro_use] extern crate log;
extern crate env_logger;
use std::process;
use std::fmt;
use std::env;
use std::str;
struct Foo;
impl fmt::Display for Foo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
info!("test");
f.write_str("bar")
}
}
fn main() {
env_logger::init();
if env::var("YOU_ARE_TESTING_NOW").is_ok() {
return info!("{}", Foo);
}
let exe = env::current_exe().unwrap();
let out = process::Command::new(exe)
.env("YOU_ARE_TESTING_NOW", "1")
.env("RUST_LOG", "debug")
.output()
.unwrap_or_else(|e| panic!("Unable to start child process: {}", e));
if out.status.success() {
return
}
println!("test failed: {}", out.status);
println!("--- stdout\n{}", str::from_utf8(&out.stdout).unwrap());
println!("--- stderr\n{}", str::from_utf8(&out.stderr).unwrap());
process::exit(1);
}

View File

@ -0,0 +1,51 @@
#[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();
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");
}

View File

@ -1 +1 @@
{"files":{"CHANGELOG.md":"7c044d74477515ab39287a4caff27eb96daebaed8b9f9b6a1d1c081a7b42d4a7","Cargo.lock":"132c1f881b80a79314567a6993141c6204495fec144cdcec1729f2a3e0fec18b","Cargo.toml":"b60137f1fd54001ca4d8be1d0bbec154225a44c8f4fa3576078bdad55216d357","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"6485b8ed310d3f0340bf1ad1f47645069ce4069dcc6bb46c7d5c6faf41de1fdb","README.md":"0e231c1c4ad51ff0239062297bdaa69aeb34a8692e3f814188ce1e0ade8583d5","examples/custom_default_format.rs":"799c439f61cb711078f8aa584db537a5758c25b90d44767849dae2ad3822885c","examples/custom_format.rs":"ac8323e2febf8b8ff7238bd254fbbbfb3183da5af84f7f3a261fd9ad892c9ab6","examples/custom_logger.rs":"99fb3c9761ad4c5fe73f4ec2a2bd44b4acf6d1f7b7cfaa16bf0373665d3e2a4b","examples/default.rs":"ac96427611784d310704f738c7a29ebddd7930c8a70ad3c464c4d3eae4cf74a3","examples/direct_logger.rs":"549f6a10e0903d06aca2cc7ba82415b07a23392676101c9bc7aa72b4a9b0b9e2","examples/filters_from_code.rs":"84bd82803683d19ae96f85edcf4ee38cda028c2dbde923dddecc8563453b18e2","src/filter/mod.rs":"de471579c5db400c5ed11b9d7c9fc62686068b42798c58f7165806319ab7ec09","src/filter/regex.rs":"5fff47d1d4d0aa3f2bab90636127d3e72aebf800c3b78faba99637220ffdf865","src/filter/string.rs":"52bbd047c31a1afdb3cd1c11629b956f21b3f47bf22e06421baf3d693a045e59","src/fmt/humantime/extern_impl.rs":"cd2538e7a03fd3ad6c843af3c3d4016ca96cadaefee32cf9b37329c4787e6552","src/fmt/humantime/mod.rs":"408496eb21344c654b9e06da2a2df86de56e427147bb7f7b47851e0da976c003","src/fmt/humantime/shim_impl.rs":"7c2fdf4031f5568b716df14842b0d32bc03ff398763f4849960df7f9632a5bb2","src/fmt/mod.rs":"5104dad2fd14bc18ab6ab46e7c2bc5752b509d9fc934fb99f0ebc126728f8f04","src/fmt/writer/atty.rs":"3e9fd61d291d0919f7aa7119a26dd15d920df8783b4ae57bcf2c3cb6f3ff06b5","src/fmt/writer/mod.rs":"583f6616e0cf21955a530baa332fb7a99bf4fcd418a2367bbd1e733a06a22318","src/fmt/writer/termcolor/extern_impl.rs":"15e048be128568abcdd0ce99dafffe296df26131d4aa05921585761d31c11db5","src/fmt/writer/termcolor/mod.rs":"a3cf956aec030e0f940e4eaefe58d7703857eb900022286e328e05e5f61de183","src/fmt/writer/termcolor/shim_impl.rs":"bdd479c4e933b14ba02a3c1a9fe30eb51bcdf600e23cebd044d68683fdaad037","src/lib.rs":"2c5ab92ee141022f3e657b0f81e84e5ee4e7fad9fb648204e00ed4fb03d4166f","tests/init-twice-retains-filter.rs":"00524ce0f6779981b695bad1fdd244f87b76c126aeccd8b4ff77ef9e6325478b","tests/log-in-log.rs":"41126910998adfbac771c2a1237fecbc5437344f8e4dfc2f93235bab764a087e","tests/regexp_filter.rs":"44aa6c39de894be090e37083601e501cfffb15e3c0cd36209c48abdf3e2cb120"},"package":"aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3"}
{"files":{"CHANGELOG.md":"7c044d74477515ab39287a4caff27eb96daebaed8b9f9b6a1d1c081a7b42d4a7","Cargo.lock":"b1394b6c58241027832cc714a0754902d82aa1f6923ab478c318739462e565ca","Cargo.toml":"2961879155d753ba90ecd98c17875c82007a6973c95867e86bc1ec5bd4f5db41","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"6485b8ed310d3f0340bf1ad1f47645069ce4069dcc6bb46c7d5c6faf41de1fdb","README.md":"0bf17650e07b88f1486f033643c1e82517caa69410e6faeaa352782d9231d63e","examples/custom_default_format.rs":"ae18cd0e765cf1f16568f9879925861d6f004481f955b58af5ed8fd04b0fca99","examples/custom_format.rs":"b0f41b7a3e6fe7582871281f4244c62c66b0d724bfc678907f67185a784e82b4","examples/custom_logger.rs":"6eeef506681a46925117e8f89395cdf4fea60a0d1f6a420e51768e790272dcde","examples/default.rs":"7ed1c6a8a8fe457a86676bd3a75c07d4ec7fb54147cf2825c9d299a5878a24cd","examples/direct_logger.rs":"ee20c25379c396e5e74e963290a4d8773a86f3fe10193f61fb1efd1c7271faf4","examples/filters_from_code.rs":"7f007b0dfa5a3964f839134824dc3684bf2f3c3d7b4c36c580cd029df5f9308b","src/filter/mod.rs":"5da7e51e7b77efdd4d2b5445e5d0264be2c897909d0f86eb553e16611307aed2","src/filter/regex.rs":"bdf875bac25e089e1e462f5dd01a88678067c24118ecd6268561c6a6af39747d","src/filter/string.rs":"fac54d51189fc0b5d2bff334b7a7e465177b431e3428299e345e1f90062d832e","src/fmt/humantime/extern_impl.rs":"f3087b29eedb8b4d5573621ad206e48a2eac72a77277be3b0e631d7dc9fb7a2e","src/fmt/humantime/mod.rs":"f4111c26cf2ffb85c1d639bd7674d55af7e1736e7e98c52f7be3070046a3253f","src/fmt/humantime/shim_impl.rs":"cce9a252abd5952fa109a72b1dfb85a593d237e22606b2b608a32c69184560e9","src/fmt/mod.rs":"4ab11971a73eb5fe9b40f0bca6dfc404321dd9e2ffcf87d911408e7183dc8362","src/fmt/writer/atty.rs":"69d9dd26c430000cd2d40f9c68b2e77cd492fec22921dd2c16864301252583e0","src/fmt/writer/mod.rs":"1e0feb4dee3ee86c4c24f49566673e99ec85765869105a07a2fc7436d7640cfe","src/fmt/writer/termcolor/extern_impl.rs":"89e9f2e66b914ddc960ad9a4355265a5db5d7be410b139cf2b54ca99207374a7","src/fmt/writer/termcolor/mod.rs":"a790f9391a50cd52be6823e3e55942de13a8d12e23d63765342ae9e8dd6d091c","src/fmt/writer/termcolor/shim_impl.rs":"d93786671d6a89fc2912f77f04b8cb0b82d67277d255d15ac31bfc1bc4464e30","src/lib.rs":"3cbc4f4d3fe51c43fc45a2f435c141f0de5b40b65ba0d2c7d16bb58c04d10898","tests/init-twice-retains-filter.rs":"be5cd2132342d89ede1f5c4266173bb3c4d51cc22a1847f133d299a1c5430ccb","tests/log-in-log.rs":"29fecc65c1e0d1c22d79c97e7ca843ad44a91f27934148d7a05c48899a3f39d8","tests/log_tls_dtors.rs":"7320667d774a9b05037f7bf273fb2574dec0705707692a9cd2f46f4cd5bc68dd","tests/regexp_filter.rs":"a84263c995b534b6479a1d0abadf63f4f0264958ff86d9173d6b2139b82c4dc5"},"package":"44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"}

View File

@ -25,18 +25,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "env_logger"
version = "0.6.2"
version = "0.7.1"
dependencies = [
"atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "humantime"
version = "1.2.0"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -54,7 +54,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "log"
version = "0.4.6"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
@ -189,10 +189,10 @@ dependencies = [
"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e"
"checksum atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "af80143d6f7608d746df1520709e5d141c96f240b0e62b0aa41bdfb53374d9d4"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114"
"checksum humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d"
"checksum libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd41f331ac7c5b8ac259b8bf82c75c0fb2e469bbf37d2becbba9a6a2221965b"
"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6"
"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
"checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16"
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
"checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd"

View File

@ -11,8 +11,9 @@
# will likely look very different (and much more reasonable)
[package]
edition = "2018"
name = "env_logger"
version = "0.6.2"
version = "0.7.1"
authors = ["The Rust Project Developers"]
description = "A logging implementation for `log` which is configured via an environment\nvariable.\n"
documentation = "https://docs.rs/env_logger"
@ -30,6 +31,10 @@ harness = false
name = "log-in-log"
harness = false
[[test]]
name = "log_tls_dtors"
harness = false
[[test]]
name = "init-twice-retains-filter"
harness = false
@ -38,11 +43,11 @@ version = "0.2.5"
optional = true
[dependencies.humantime]
version = "1.1"
version = "1.3"
optional = true
[dependencies.log]
version = "0.4"
version = "0.4.8"
features = ["std"]
[dependencies.regex]

View File

@ -16,7 +16,7 @@ It must be added along with `log` to the project dependencies:
```toml
[dependencies]
log = "0.4.0"
env_logger = "0.6.2"
env_logger = "0.7.1"
```
`env_logger` must be initialized as early as possible in the project. After it's initialized, you can use the `log` macros to do actual logging.
@ -24,7 +24,6 @@ env_logger = "0.6.2"
```rust
#[macro_use]
extern crate log;
extern crate env_logger;
fn main() {
env_logger::init();
@ -54,7 +53,7 @@ Tests can use the `env_logger` crate to see log messages generated during that t
log = "0.4.0"
[dev-dependencies]
env_logger = "0.6.2"
env_logger = "0.7.1"
```
```rust
@ -69,7 +68,6 @@ fn add_one(num: i32) -> i32 {
#[cfg(test)]
mod tests {
use super::*;
extern crate env_logger;
fn init() {
let _ = env_logger::builder().is_test(true).try_init();

View File

@ -19,9 +19,8 @@ If you want to control the logging output completely, see the `custom_logger` ex
#[macro_use]
extern crate log;
extern crate env_logger;
use env_logger::{Env, Builder};
use env_logger::{Builder, Env};
fn init_logger() {
let env = Env::default()
@ -30,9 +29,7 @@ fn init_logger() {
let mut builder = Builder::from_env(env);
builder
.default_format_level(false)
.default_format_timestamp_nanos(true);
builder.format_level(false).format_timestamp_nanos();
builder.init();
}

View File

@ -17,38 +17,37 @@ $ export MY_LOG_STYLE=never
If you want to control the logging output completely, see the `custom_logger` example.
*/
#[macro_use]
extern crate log;
extern crate env_logger;
use std::io::Write;
use env_logger::{Env, Builder, fmt};
fn init_logger() {
let env = Env::default()
.filter("MY_LOG_LEVEL")
.write_style("MY_LOG_STYLE");
let mut builder = Builder::from_env(env);
// Use a different format for writing log records
// The colors are only available when the `termcolor` dependency is (which it is by default)
#[cfg(feature = "termcolor")]
builder.format(|buf, record| {
let mut style = buf.style();
style.set_bg(fmt::Color::Yellow).set_bold(true);
let timestamp = buf.timestamp();
writeln!(buf, "My formatted log ({}): {}", timestamp, style.value(record.args()))
});
builder.init();
}
#[cfg(all(feature = "termcolor", feature = "humantime"))]
fn main() {
use env_logger::{fmt, Builder, Env};
use std::io::Write;
fn init_logger() {
let env = Env::default()
.filter("MY_LOG_LEVEL")
.write_style("MY_LOG_STYLE");
Builder::from_env(env)
.format(|buf, record| {
let mut style = buf.style();
style.set_bg(fmt::Color::Yellow).set_bold(true);
let timestamp = buf.timestamp();
writeln!(
buf,
"My formatted log ({}): {}",
timestamp,
style.value(record.args())
)
})
.init();
}
init_logger();
info!("a log from `MyLogger`");
log::info!("a log from `MyLogger`");
}
#[cfg(not(all(feature = "termcolor", feature = "humantime")))]
fn main() {}

View File

@ -12,12 +12,12 @@ If you only want to change the way logs are formatted, look at the `custom_forma
#[macro_use]
extern crate log;
extern crate env_logger;
use env_logger::filter::Filter;
use log::{Log, Metadata, Record, SetLoggerError};
struct MyLogger {
inner: Filter
inner: Filter,
}
impl MyLogger {
@ -26,7 +26,7 @@ impl MyLogger {
let mut builder = Builder::from_env("MY_LOG_LEVEL");
MyLogger {
inner: builder.build()
inner: builder.build(),
}
}
@ -50,7 +50,7 @@ impl Log for MyLogger {
}
}
fn flush(&self) { }
fn flush(&self) {}
}
fn main() {

View File

@ -17,7 +17,6 @@ $ export MY_LOG_STYLE=never
#[macro_use]
extern crate log;
extern crate env_logger;
use env_logger::Env;

View File

@ -4,9 +4,6 @@ Using `env_logger::Logger` and the `log::Log` trait directly.
This example doesn't rely on environment variables, or having a static logger installed.
*/
extern crate log;
extern crate env_logger;
fn record() -> log::Record<'static> {
let error_metadata = log::MetadataBuilder::new()
.target("myApp")
@ -34,7 +31,7 @@ fn main() {
.filter(None, log::LevelFilter::Error)
.write_style(env_logger::WriteStyle::Never)
.build();
stylish_logger.log(&record());
unstylish_logger.log(&record());
}
}

View File

@ -4,7 +4,6 @@ Specify logging filters in code instead of using an environment variable.
#[macro_use]
extern crate log;
extern crate env_logger;
fn main() {
env_logger::builder()

View File

@ -1,15 +1,15 @@
//! Filtering for log records.
//!
//!
//! This module contains the log filtering used by `env_logger` to match records.
//! You can use the `Filter` type in your own logger implementation to use the same
//! filter parsing and matching as `env_logger`. For more details about the format
//! You can use the `Filter` type in your own logger implementation to use the same
//! filter parsing and matching as `env_logger`. For more details about the format
//! for directive strings see [Enabling Logging].
//!
//!
//! ## Using `env_logger` in your own logger
//!
//! You can use `env_logger`'s filtering functionality with your own logger.
//! Call [`Builder::parse`] to parse directives from a string when constructing
//! your logger. Call [`Filter::matches`] to check whether a record should be
//! Call [`Builder::parse`] to parse directives from a string when constructing
//! your logger. Call [`Filter::matches`] to check whether a record should be
//! logged based on the parsed filters when log records are received.
//!
//! ```
@ -54,15 +54,15 @@
//! }
//! # fn main() {}
//! ```
//!
//!
//! [Enabling Logging]: ../index.html#enabling-logging
//! [`Builder::parse`]: struct.Builder.html#method.parse
//! [`Filter::matches`]: struct.Filter.html#method.matches
use log::{Level, LevelFilter, Metadata, Record};
use std::env;
use std::mem;
use std::fmt;
use log::{Level, LevelFilter, Record, Metadata};
use std::mem;
#[cfg(feature = "regex")]
#[path = "regex.rs"]
@ -73,11 +73,11 @@ mod inner;
mod inner;
/// A log filter.
///
///
/// This struct can be used to determine whether or not a log record
/// should be written to the output.
/// Use the [`Builder`] type to parse and construct a `Filter`.
///
///
/// [`Builder`]: struct.Builder.html
pub struct Filter {
directives: Vec<Directive>,
@ -85,10 +85,10 @@ pub struct Filter {
}
/// A builder for a log filter.
///
///
/// It can be used to parse a set of directives from a string before building
/// a [`Filter`] instance.
///
///
/// ## Example
///
/// ```
@ -111,7 +111,7 @@ pub struct Filter {
/// let filter = builder.build();
/// }
/// ```
///
///
/// [`Filter`]: struct.Filter.html
pub struct Builder {
directives: Vec<Directive>,
@ -148,7 +148,8 @@ impl Filter {
/// }
/// ```
pub fn filter(&self) -> LevelFilter {
self.directives.iter()
self.directives
.iter()
.map(|d| d.level)
.max()
.unwrap_or(LevelFilter::Off)
@ -213,9 +214,7 @@ impl Builder {
///
/// 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: LevelFilter) -> &mut Self {
pub fn filter(&mut self, module: Option<&str>, level: LevelFilter) -> &mut Self {
self.directives.push(Directive {
name: module.map(|s| s.to_string()),
level,
@ -226,7 +225,7 @@ impl Builder {
/// Parses the directives string.
///
/// See the [Enabling Logging] section for more details.
///
///
/// [Enabling Logging]: ../index.html#enabling-logging
pub fn parse(&mut self, filters: &str) -> &mut Self {
let (directives, filter) = parse_spec(filters);
@ -274,7 +273,7 @@ impl Default for Builder {
}
impl fmt::Debug for Filter {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Filter")
.field("filter", &self.filter)
.field("directives", &self.directives)
@ -283,16 +282,14 @@ impl fmt::Debug for Filter {
}
impl fmt::Debug for Builder {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.built {
f.debug_struct("Filter")
.field("built", &true)
.finish()
f.debug_struct("Filter").field("built", &true).finish()
} else {
f.debug_struct("Filter")
.field("filter", &self.filter)
.field("directives", &self.directives)
.finish()
.field("filter", &self.filter)
.field("directives", &self.directives)
.finish()
}
}
}
@ -306,68 +303,75 @@ fn parse_spec(spec: &str) -> (Vec<Directive>, Option<inner::Filter>) {
let mods = parts.next();
let filter = parts.next();
if parts.next().is_some() {
eprintln!("warning: invalid logging spec '{}', \
ignoring it (too many '/'s)", spec);
eprintln!(
"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(_) => (LevelFilter::max(), Some(part0)),
}
mods.map(|m| {
for s in m.split(',') {
if s.len() == 0 {
continue;
}
(Some(part0), Some(""), None) => (LevelFilter::max(), Some(part0)),
(Some(part0), Some(part1), None) => {
match part1.parse() {
Ok(num) => (num, Some(part0)),
_ => {
eprintln!("warning: invalid logging spec '{}', \
ignoring it", part1);
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(_) => (LevelFilter::max(), Some(part0)),
}
}
}
},
_ => {
eprintln!("warning: invalid logging spec '{}', \
ignoring it", s);
continue
}
};
dirs.push(Directive {
name: name.map(|s| s.to_string()),
level: log_level,
});
}});
(Some(part0), Some(""), None) => (LevelFilter::max(), Some(part0)),
(Some(part0), Some(part1), None) => match part1.parse() {
Ok(num) => (num, Some(part0)),
_ => {
eprintln!(
"warning: invalid logging spec '{}', \
ignoring it",
part1
);
continue;
}
},
_ => {
eprintln!(
"warning: invalid logging spec '{}', \
ignoring it",
s
);
continue;
}
};
dirs.push(Directive {
name: name.map(|s| s.to_string()),
level: log_level,
});
}
});
let filter = filter.map_or(None, |filter| {
match inner::Filter::new(filter) {
Ok(re) => Some(re),
Err(e) => {
eprintln!("warning: invalid regex filter - {}", e);
None
}
let filter = filter.map_or(None, |filter| match inner::Filter::new(filter) {
Ok(re) => Some(re),
Err(e) => {
eprintln!("warning: invalid regex filter - {}", e);
None
}
});
return (dirs, filter);
}
// Check whether a level and target are enabled by the set of directives.
fn enabled(directives: &[Directive], level: Level, target: &str) -> bool {
// Search for the longest match, the vector is assumed to be pre-sorted.
for directive in directives.iter().rev() {
match directive.name {
Some(ref name) if !target.starts_with(&**name) => {},
Some(..) | None => {
return level <= directive.level
}
Some(ref name) if !target.starts_with(&**name) => {}
Some(..) | None => return level <= directive.level,
}
}
false
@ -377,7 +381,7 @@ fn enabled(directives: &[Directive], level: Level, target: &str) -> bool {
mod tests {
use log::{Level, LevelFilter};
use super::{Builder, Filter, Directive, parse_spec, enabled};
use super::{enabled, parse_spec, Builder, Directive, Filter};
fn make_logger_filter(dirs: Vec<Directive>) -> Filter {
let mut logger = Builder::new().build();
@ -395,10 +399,10 @@ mod tests {
#[test]
fn filter_beginning_longest_match() {
let logger = Builder::new()
.filter(Some("crate2"), LevelFilter::Info)
.filter(Some("crate2::mod"), LevelFilter::Debug)
.filter(Some("crate1::mod1"), LevelFilter::Warn)
.build();
.filter(Some("crate2"), LevelFilter::Info)
.filter(Some("crate2::mod"), LevelFilter::Debug)
.filter(Some("crate1::mod1"), LevelFilter::Warn)
.build();
assert!(enabled(&logger.directives, Level::Debug, "crate2::mod1"));
assert!(!enabled(&logger.directives, Level::Debug, "crate2"));
}
@ -415,12 +419,12 @@ mod tests {
let logger = make_logger_filter(vec![
Directive {
name: Some("crate2".to_string()),
level: LevelFilter::Info
level: LevelFilter::Info,
},
Directive {
name: Some("crate1::mod1".to_string()),
level: LevelFilter::Warn
}
level: LevelFilter::Warn,
},
]);
assert!(enabled(&logger.directives, Level::Warn, "crate1::mod1"));
assert!(!enabled(&logger.directives, Level::Info, "crate1::mod1"));
@ -431,8 +435,14 @@ mod tests {
#[test]
fn no_match() {
let logger = make_logger_filter(vec![
Directive { name: Some("crate2".to_string()), level: LevelFilter::Info },
Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn }
Directive {
name: Some("crate2".to_string()),
level: LevelFilter::Info,
},
Directive {
name: Some("crate1::mod1".to_string()),
level: LevelFilter::Warn,
},
]);
assert!(!enabled(&logger.directives, Level::Warn, "crate3"));
}
@ -440,8 +450,14 @@ mod tests {
#[test]
fn match_beginning() {
let logger = make_logger_filter(vec![
Directive { name: Some("crate2".to_string()), level: LevelFilter::Info },
Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn }
Directive {
name: Some("crate2".to_string()),
level: LevelFilter::Info,
},
Directive {
name: Some("crate1::mod1".to_string()),
level: LevelFilter::Warn,
},
]);
assert!(enabled(&logger.directives, Level::Info, "crate2::mod1"));
}
@ -449,9 +465,18 @@ mod tests {
#[test]
fn match_beginning_longest_match() {
let logger = make_logger_filter(vec![
Directive { name: Some("crate2".to_string()), level: LevelFilter::Info },
Directive { name: Some("crate2::mod".to_string()), level: LevelFilter::Debug },
Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn }
Directive {
name: Some("crate2".to_string()),
level: LevelFilter::Info,
},
Directive {
name: Some("crate2::mod".to_string()),
level: LevelFilter::Debug,
},
Directive {
name: Some("crate1::mod1".to_string()),
level: LevelFilter::Warn,
},
]);
assert!(enabled(&logger.directives, Level::Debug, "crate2::mod1"));
assert!(!enabled(&logger.directives, Level::Debug, "crate2"));
@ -460,8 +485,14 @@ mod tests {
#[test]
fn match_default() {
let logger = make_logger_filter(vec![
Directive { name: None, level: LevelFilter::Info },
Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Warn }
Directive {
name: None,
level: LevelFilter::Info,
},
Directive {
name: Some("crate1::mod1".to_string()),
level: LevelFilter::Warn,
},
]);
assert!(enabled(&logger.directives, Level::Warn, "crate1::mod1"));
assert!(enabled(&logger.directives, Level::Info, "crate2::mod2"));
@ -470,8 +501,14 @@ mod tests {
#[test]
fn zero_level() {
let logger = make_logger_filter(vec![
Directive { name: None, level: LevelFilter::Info },
Directive { name: Some("crate1::mod1".to_string()), level: LevelFilter::Off }
Directive {
name: None,
level: LevelFilter::Info,
},
Directive {
name: Some("crate1::mod1".to_string()),
level: LevelFilter::Off,
},
]);
assert!(!enabled(&logger.directives, Level::Error, "crate1::mod1"));
assert!(enabled(&logger.directives, Level::Info, "crate2::mod2"));

View File

@ -11,7 +11,7 @@ pub struct Filter {
impl Filter {
pub fn new(spec: &str) -> Result<Filter, String> {
match Regex::new(spec){
match Regex::new(spec) {
Ok(r) => Ok(Filter { inner: r }),
Err(e) => Err(e.to_string()),
}

View File

@ -7,7 +7,9 @@ pub struct Filter {
impl Filter {
pub fn new(spec: &str) -> Result<Filter, String> {
Ok(Filter { inner: spec.to_string() })
Ok(Filter {
inner: spec.to_string(),
})
}
pub fn is_match(&self, s: &str) -> bool {

View File

@ -1,11 +1,13 @@
use std::fmt;
use std::time::SystemTime;
use humantime::{format_rfc3339_nanos, format_rfc3339_seconds};
use humantime::{
format_rfc3339_micros, format_rfc3339_millis, format_rfc3339_nanos, format_rfc3339_seconds,
};
use ::fmt::Formatter;
use crate::fmt::{Formatter, TimestampPrecision};
pub(in ::fmt) mod glob {
pub(in crate::fmt) mod glob {
pub use super::*;
}
@ -30,12 +32,46 @@ impl Formatter {
///
/// [`Timestamp`]: struct.Timestamp.html
pub fn timestamp(&self) -> Timestamp {
Timestamp(SystemTime::now())
Timestamp {
time: SystemTime::now(),
precision: TimestampPrecision::Seconds,
}
}
/// Get a [`PreciseTimestamp`] for the current date and time in UTC with nanos.
pub fn precise_timestamp(&self) -> PreciseTimestamp {
PreciseTimestamp(SystemTime::now())
/// Get a [`Timestamp`] for the current date and time in UTC with full
/// second precision.
pub fn timestamp_seconds(&self) -> Timestamp {
Timestamp {
time: SystemTime::now(),
precision: TimestampPrecision::Seconds,
}
}
/// Get a [`Timestamp`] for the current date and time in UTC with
/// millisecond precision.
pub fn timestamp_millis(&self) -> Timestamp {
Timestamp {
time: SystemTime::now(),
precision: TimestampPrecision::Millis,
}
}
/// Get a [`Timestamp`] for the current date and time in UTC with
/// microsecond precision.
pub fn timestamp_micros(&self) -> Timestamp {
Timestamp {
time: SystemTime::now(),
precision: TimestampPrecision::Micros,
}
}
/// Get a [`Timestamp`] for the current date and time in UTC with
/// nanosecond precision.
pub fn timestamp_nanos(&self) -> Timestamp {
Timestamp {
time: SystemTime::now(),
precision: TimestampPrecision::Nanos,
}
}
}
@ -46,13 +82,10 @@ impl Formatter {
/// [RFC3339]: https://www.ietf.org/rfc/rfc3339.txt
/// [`Display`]: https://doc.rust-lang.org/stable/std/fmt/trait.Display.html
/// [`Formatter`]: struct.Formatter.html
pub struct Timestamp(SystemTime);
/// An [RFC3339] formatted timestamp with nanos.
///
/// [RFC3339]: https://www.ietf.org/rfc/rfc3339.txt
#[derive(Debug)]
pub struct PreciseTimestamp(SystemTime);
pub struct Timestamp {
time: SystemTime,
precision: TimestampPrecision,
}
impl fmt::Debug for Timestamp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -66,19 +99,20 @@ impl fmt::Debug for Timestamp {
}
f.debug_tuple("Timestamp")
.field(&TimestampValue(&self))
.finish()
.field(&TimestampValue(&self))
.finish()
}
}
impl fmt::Display for Timestamp {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
format_rfc3339_seconds(self.0).fmt(f)
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let formatter = match self.precision {
TimestampPrecision::Seconds => format_rfc3339_seconds,
TimestampPrecision::Millis => format_rfc3339_millis,
TimestampPrecision::Micros => format_rfc3339_micros,
TimestampPrecision::Nanos => format_rfc3339_nanos,
};
formatter(self.time).fmt(f)
}
}
impl fmt::Display for PreciseTimestamp {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
format_rfc3339_nanos(self.0).fmt(f)
}
}

View File

@ -8,4 +8,4 @@ Its public API is available when the `humantime` crate is available.
#[cfg_attr(not(feature = "humantime"), path = "shim_impl.rs")]
mod imp;
pub(in ::fmt) use self::imp::*;
pub(in crate::fmt) use self::imp::*;

View File

@ -2,6 +2,4 @@
Timestamps aren't available when we don't have a `humantime` dependency.
*/
pub(in ::fmt) mod glob {
}
pub(in crate::fmt) mod glob {}

View File

@ -29,24 +29,48 @@
//! [`Builder::format`]: ../struct.Builder.html#method.format
//! [`Write`]: https://doc.rust-lang.org/stable/std/io/trait.Write.html
use std::io::prelude::*;
use std::{io, fmt, mem};
use std::rc::Rc;
use std::cell::RefCell;
use std::fmt::Display;
use std::io::prelude::*;
use std::rc::Rc;
use std::{fmt, io, mem};
use log::Record;
pub(crate) mod writer;
mod humantime;
pub(crate) mod writer;
pub use self::humantime::glob::*;
pub use self::writer::glob::*;
use self::writer::{Writer, Buffer};
use self::writer::{Buffer, Writer};
pub(crate) mod glob {
pub use super::{Target, WriteStyle};
pub use super::{Target, TimestampPrecision, WriteStyle};
}
/// Formatting precision of timestamps.
///
/// Seconds give precision of full seconds, milliseconds give thousands of a
/// second (3 decimal digits), microseconds are millionth of a second (6 decimal
/// digits) and nanoseconds are billionth of a second (9 decimal digits).
#[derive(Copy, Clone, Debug)]
pub enum TimestampPrecision {
/// Full second precision (0 decimal digits)
Seconds,
/// Millisecond precision (3 decimal digits)
Millis,
/// Microsecond precision (6 decimal digits)
Micros,
/// Nanosecond precision (9 decimal digits)
Nanos,
}
/// The default timestamp precision is seconds.
impl Default for TimestampPrecision {
fn default() -> Self {
TimestampPrecision::Seconds
}
}
/// A formatter to write logs into.
@ -107,16 +131,16 @@ impl Write for Formatter {
}
impl fmt::Debug for Formatter {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Formatter").finish()
}
}
pub(crate) struct Builder {
pub default_format_timestamp: bool,
pub default_format_timestamp_nanos: bool,
pub default_format_module_path: bool,
pub default_format_level: bool,
pub format_timestamp: Option<TimestampPrecision>,
pub format_module_path: bool,
pub format_level: bool,
pub format_indent: Option<usize>,
#[allow(unknown_lints, bare_trait_objects)]
pub custom_format: Option<Box<Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send>>,
built: bool,
@ -125,10 +149,10 @@ pub(crate) struct Builder {
impl Default for Builder {
fn default() -> Self {
Builder {
default_format_timestamp: true,
default_format_timestamp_nanos: false,
default_format_module_path: true,
default_format_level: true,
format_timestamp: Some(Default::default()),
format_module_path: true,
format_level: true,
format_indent: Some(4),
custom_format: None,
built: false,
}
@ -137,7 +161,7 @@ impl Default for Builder {
impl Builder {
/// Convert the format into a callable function.
///
///
/// If the `custom_format` is `Some`, then any `default_format` switches are ignored.
/// If the `custom_format` is `None`, then a default format is returned.
/// Any `default_format` switches set to `false` won't be written by the format.
@ -145,22 +169,24 @@ impl Builder {
pub fn build(&mut self) -> Box<Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send> {
assert!(!self.built, "attempt to re-use consumed builder");
let built = mem::replace(self, Builder {
built: true,
..Default::default()
});
let built = mem::replace(
self,
Builder {
built: true,
..Default::default()
},
);
if let Some(fmt) = built.custom_format {
fmt
}
else {
} else {
Box::new(move |buf, record| {
let fmt = DefaultFormat {
timestamp: built.default_format_timestamp,
timestamp_nanos: built.default_format_timestamp_nanos,
module_path: built.default_format_module_path,
level: built.default_format_level,
timestamp: built.format_timestamp,
module_path: built.format_module_path,
level: built.format_level,
written_header_value: false,
indent: built.format_indent,
buf,
};
@ -176,14 +202,14 @@ type SubtleStyle = StyledValue<'static, &'static str>;
type SubtleStyle = &'static str;
/// The default format.
///
///
/// This format needs to work with any combination of crate features.
struct DefaultFormat<'a> {
timestamp: bool,
timestamp: Option<TimestampPrecision>,
module_path: bool,
level: bool,
timestamp_nanos: bool,
written_header_value: bool,
indent: Option<usize>,
buf: &'a mut Formatter,
}
@ -200,7 +226,8 @@ impl<'a> DefaultFormat<'a> {
fn subtle_style(&self, text: &'static str) -> SubtleStyle {
#[cfg(feature = "termcolor")]
{
self.buf.style()
self.buf
.style()
.set_color(Color::Black)
.set_intense(true)
.into_value(text)
@ -227,7 +254,7 @@ impl<'a> DefaultFormat<'a> {
fn write_level(&mut self, record: &Record) -> io::Result<()> {
if !self.level {
return Ok(())
return Ok(());
}
let level = {
@ -247,29 +274,29 @@ impl<'a> DefaultFormat<'a> {
fn write_timestamp(&mut self) -> io::Result<()> {
#[cfg(feature = "humantime")]
{
if !self.timestamp {
return Ok(())
}
use self::TimestampPrecision::*;
let ts = match self.timestamp {
None => return Ok(()),
Some(Seconds) => self.buf.timestamp_seconds(),
Some(Millis) => self.buf.timestamp_millis(),
Some(Micros) => self.buf.timestamp_micros(),
Some(Nanos) => self.buf.timestamp_nanos(),
};
if self.timestamp_nanos {
let ts_nanos = self.buf.precise_timestamp();
self.write_header_value(ts_nanos)
} else {
let ts = self.buf.timestamp();
self.write_header_value(ts)
}
self.write_header_value(ts)
}
#[cfg(not(feature = "humantime"))]
{
// Trick the compiler to think we have used self.timestamp
// Workaround for "field is never used: `timestamp`" compiler nag.
let _ = self.timestamp;
let _ = self.timestamp_nanos;
Ok(())
}
}
fn write_module_path(&mut self, record: &Record) -> io::Result<()> {
if !self.module_path {
return Ok(())
return Ok(());
}
if let Some(module_path) = record.module_path() {
@ -289,7 +316,51 @@ impl<'a> DefaultFormat<'a> {
}
fn write_args(&mut self, record: &Record) -> io::Result<()> {
writeln!(self.buf, "{}", record.args())
match self.indent {
// Fast path for no indentation
None => writeln!(self.buf, "{}", record.args()),
Some(indent_count) => {
// Create a wrapper around the buffer only if we have to actually indent the message
struct IndentWrapper<'a, 'b: 'a> {
fmt: &'a mut DefaultFormat<'b>,
indent_count: usize,
}
impl<'a, 'b> Write for IndentWrapper<'a, 'b> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let mut first = true;
for chunk in buf.split(|&x| x == b'\n') {
if !first {
write!(self.fmt.buf, "\n{:width$}", "", width = self.indent_count)?;
}
self.fmt.buf.write_all(chunk)?;
first = false;
}
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
self.fmt.buf.flush()
}
}
// The explicit scope here is just to make older versions of Rust happy
{
let mut wrapper = IndentWrapper {
fmt: self,
indent_count,
};
write!(wrapper, "{}", record.args())?;
}
writeln!(self.buf)?;
Ok(())
}
}
}
}
@ -303,7 +374,7 @@ mod tests {
let buf = fmt.buf.buf.clone();
let record = Record::builder()
.args(format_args!("log message"))
.args(format_args!("log\nmessage"))
.level(Level::Info)
.file(Some("test.rs"))
.line(Some(144))
@ -317,7 +388,7 @@ mod tests {
}
#[test]
fn default_format_with_header() {
fn format_with_header() {
let writer = writer::Builder::new()
.write_style(WriteStyle::Never)
.build();
@ -325,19 +396,19 @@ mod tests {
let mut f = Formatter::new(&writer);
let written = write(DefaultFormat {
timestamp: false,
timestamp_nanos: false,
timestamp: None,
module_path: true,
level: true,
written_header_value: false,
indent: None,
buf: &mut f,
});
assert_eq!("[INFO test::path] log message\n", written);
assert_eq!("[INFO test::path] log\nmessage\n", written);
}
#[test]
fn default_format_no_header() {
fn format_no_header() {
let writer = writer::Builder::new()
.write_style(WriteStyle::Never)
.build();
@ -345,14 +416,74 @@ mod tests {
let mut f = Formatter::new(&writer);
let written = write(DefaultFormat {
timestamp: false,
timestamp_nanos: false,
timestamp: None,
module_path: false,
level: false,
written_header_value: false,
indent: None,
buf: &mut f,
});
assert_eq!("log message\n", written);
assert_eq!("log\nmessage\n", written);
}
#[test]
fn format_indent_spaces() {
let writer = writer::Builder::new()
.write_style(WriteStyle::Never)
.build();
let mut f = Formatter::new(&writer);
let written = write(DefaultFormat {
timestamp: None,
module_path: true,
level: true,
written_header_value: false,
indent: Some(4),
buf: &mut f,
});
assert_eq!("[INFO test::path] log\n message\n", written);
}
#[test]
fn format_indent_zero_spaces() {
let writer = writer::Builder::new()
.write_style(WriteStyle::Never)
.build();
let mut f = Formatter::new(&writer);
let written = write(DefaultFormat {
timestamp: None,
module_path: true,
level: true,
written_header_value: false,
indent: Some(0),
buf: &mut f,
});
assert_eq!("[INFO test::path] log\nmessage\n", written);
}
#[test]
fn format_indent_spaces_no_header() {
let writer = writer::Builder::new()
.write_style(WriteStyle::Never)
.build();
let mut f = Formatter::new(&writer);
let written = write(DefaultFormat {
timestamp: None,
module_path: false,
level: false,
written_header_value: false,
indent: Some(4),
buf: &mut f,
});
assert_eq!("log\n message\n", written);
}
}

View File

@ -11,24 +11,24 @@ from being printed.
mod imp {
use atty;
pub(in ::fmt) fn is_stdout() -> bool {
pub(in crate::fmt) fn is_stdout() -> bool {
atty::is(atty::Stream::Stdout)
}
pub(in ::fmt) fn is_stderr() -> bool {
pub(in crate::fmt) fn is_stderr() -> bool {
atty::is(atty::Stream::Stderr)
}
}
#[cfg(not(feature = "atty"))]
mod imp {
pub(in ::fmt) fn is_stdout() -> bool {
pub(in crate::fmt) fn is_stdout() -> bool {
false
}
pub(in ::fmt) fn is_stderr() -> bool {
pub(in crate::fmt) fn is_stderr() -> bool {
false
}
}
pub(in ::fmt) use self::imp::*;
pub(in crate::fmt) use self::imp::*;

View File

@ -1,16 +1,16 @@
mod termcolor;
mod atty;
mod termcolor;
use std::{fmt, io};
use self::atty::{is_stderr, is_stdout};
use self::termcolor::BufferWriter;
use self::atty::{is_stdout, is_stderr};
use std::{fmt, io};
pub(in ::fmt) mod glob {
pub(in crate::fmt) mod glob {
pub use super::termcolor::glob::*;
pub use super::*;
}
pub(in ::fmt) use self::termcolor::Buffer;
pub(in crate::fmt) use self::termcolor::Buffer;
/// Log target, either `stdout` or `stderr`.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
@ -55,11 +55,11 @@ impl Writer {
self.write_style
}
pub(in ::fmt) fn buffer(&self) -> Buffer {
pub(in crate::fmt) fn buffer(&self) -> Buffer {
self.inner.buffer()
}
pub(in ::fmt) fn print(&self, buf: &Buffer) -> io::Result<()> {
pub(in crate::fmt) fn print(&self, buf: &Buffer) -> io::Result<()> {
self.inner.print(buf)
}
}
@ -127,7 +127,7 @@ impl Builder {
} else {
WriteStyle::Never
}
},
}
color_choice => color_choice,
};
@ -150,16 +150,16 @@ impl Default for Builder {
}
impl fmt::Debug for Builder {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Logger")
.field("target", &self.target)
.field("write_style", &self.write_style)
.finish()
.field("target", &self.target)
.field("write_style", &self.write_style)
.finish()
}
}
impl fmt::Debug for Writer {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Writer").finish()
}
}
@ -192,12 +192,7 @@ mod tests {
#[test]
fn parse_write_style_invalid() {
let inputs = vec![
"",
"true",
"false",
"NEVER!!"
];
let inputs = vec!["", "true", "false", "NEVER!!"];
for input in inputs {
assert_eq!(WriteStyle::Auto, parse_write_style(input));

View File

@ -1,16 +1,15 @@
use std::borrow::Cow;
use std::cell::RefCell;
use std::fmt;
use std::io::{self, Write};
use std::cell::RefCell;
use std::rc::Rc;
use log::Level;
use termcolor::{self, ColorChoice, ColorSpec, WriteColor};
use ::WriteStyle;
use ::fmt::{Formatter, Target};
use crate::fmt::{Formatter, Target, WriteStyle};
pub(in ::fmt::writer) mod glob {
pub(in crate::fmt::writer) mod glob {
pub use super::*;
}
@ -47,7 +46,7 @@ impl Formatter {
}
/// Get the default [`Style`] for the given level.
///
///
/// The style can be used to print other values besides the level.
pub fn default_level_style(&self, level: Level) -> Style {
let mut level_style = self.style();
@ -62,54 +61,46 @@ impl Formatter {
}
/// Get a printable [`Style`] for the given level.
///
///
/// The style can only be used to print the level.
pub fn default_styled_level(&self, level: Level) -> StyledValue<'static, Level> {
self.default_level_style(level).into_value(level)
}
}
pub(in ::fmt::writer) struct BufferWriter {
pub(in crate::fmt::writer) struct BufferWriter {
inner: termcolor::BufferWriter,
test_target: Option<Target>,
}
pub(in ::fmt) struct Buffer {
pub(in crate::fmt) struct Buffer {
inner: termcolor::Buffer,
test_target: Option<Target>,
}
impl BufferWriter {
pub(in ::fmt::writer) fn stderr(is_test: bool, write_style: WriteStyle) -> Self {
pub(in crate::fmt::writer) fn stderr(is_test: bool, write_style: WriteStyle) -> Self {
BufferWriter {
inner: termcolor::BufferWriter::stderr(write_style.into_color_choice()),
test_target: if is_test {
Some(Target::Stderr)
} else {
None
},
test_target: if is_test { Some(Target::Stderr) } else { None },
}
}
pub(in ::fmt::writer) fn stdout(is_test: bool, write_style: WriteStyle) -> Self {
pub(in crate::fmt::writer) fn stdout(is_test: bool, write_style: WriteStyle) -> Self {
BufferWriter {
inner: termcolor::BufferWriter::stdout(write_style.into_color_choice()),
test_target: if is_test {
Some(Target::Stdout)
} else {
None
},
test_target: if is_test { Some(Target::Stdout) } else { None },
}
}
pub(in ::fmt::writer) fn buffer(&self) -> Buffer {
pub(in crate::fmt::writer) fn buffer(&self) -> Buffer {
Buffer {
inner: self.inner.buffer(),
test_target: self.test_target,
}
}
pub(in ::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
if let Some(target) = self.test_target {
// This impl uses the `eprint` and `print` macros
// instead of `termcolor`'s buffer.
@ -129,19 +120,19 @@ impl BufferWriter {
}
impl Buffer {
pub(in ::fmt) fn clear(&mut self) {
pub(in crate::fmt) fn clear(&mut self) {
self.inner.clear()
}
pub(in ::fmt) fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
pub(in crate::fmt) fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)
}
pub(in ::fmt) fn flush(&mut self) -> io::Result<()> {
pub(in crate::fmt) fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
pub(in ::fmt) fn bytes(&self) -> &[u8] {
pub(in crate::fmt) fn bytes(&self) -> &[u8] {
self.inner.as_slice()
}
@ -374,7 +365,7 @@ impl Style {
pub fn value<T>(&self, value: T) -> StyledValue<T> {
StyledValue {
style: Cow::Borrowed(self),
value
value,
}
}
@ -382,7 +373,7 @@ impl Style {
pub(crate) fn into_value<T>(&mut self, value: T) -> StyledValue<'static, T> {
StyledValue {
style: Cow::Owned(self.clone()),
value
value,
}
}
}
@ -392,7 +383,11 @@ impl<'a, T> StyledValue<'a, T> {
where
F: FnOnce() -> fmt::Result,
{
self.style.buf.borrow_mut().set_color(&self.style.spec).map_err(|_| fmt::Error)?;
self.style
.buf
.borrow_mut()
.set_color(&self.style.spec)
.map_err(|_| fmt::Error)?;
// Always try to reset the terminal style, even if writing failed
let write = f();
@ -403,7 +398,7 @@ impl<'a, T> StyledValue<'a, T> {
}
impl fmt::Debug for Style {
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Style").field("spec", &self.spec).finish()
}
}
@ -429,7 +424,8 @@ impl_styled_value_fmt!(
fmt::UpperHex,
fmt::LowerHex,
fmt::UpperExp,
fmt::LowerExp);
fmt::LowerExp
);
// The `Color` type is copied from https://github.com/BurntSushi/ripgrep/tree/master/termcolor

View File

@ -9,4 +9,4 @@ The terminal printing is shimmed when the `termcolor` crate is not available.
#[cfg_attr(not(feature = "termcolor"), path = "shim_impl.rs")]
mod imp;
pub(in ::fmt) use self::imp::*;
pub(in crate::fmt) use self::imp::*;

View File

@ -1,35 +1,33 @@
use std::io;
use fmt::{WriteStyle, Target};
use crate::fmt::{Target, WriteStyle};
pub(in ::fmt::writer) mod glob {
}
pub(in crate::fmt::writer) mod glob {}
pub(in ::fmt::writer) struct BufferWriter {
pub(in crate::fmt::writer) struct BufferWriter {
target: Target,
}
pub(in ::fmt) struct Buffer(Vec<u8>);
pub(in crate::fmt) struct Buffer(Vec<u8>);
impl BufferWriter {
pub(in ::fmt::writer) fn stderr(_is_test: bool, _write_style: WriteStyle) -> Self {
pub(in crate::fmt::writer) fn stderr(_is_test: bool, _write_style: WriteStyle) -> Self {
BufferWriter {
target: Target::Stderr,
}
}
pub(in ::fmt::writer) fn stdout(_is_test: bool, _write_style: WriteStyle) -> Self {
pub(in crate::fmt::writer) fn stdout(_is_test: bool, _write_style: WriteStyle) -> Self {
BufferWriter {
target: Target::Stdout,
}
}
pub(in ::fmt::writer) fn buffer(&self) -> Buffer {
pub(in crate::fmt::writer) fn buffer(&self) -> Buffer {
Buffer(Vec::new())
}
pub(in ::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
pub(in crate::fmt::writer) fn print(&self, buf: &Buffer) -> io::Result<()> {
// This impl uses the `eprint` and `print` macros
// instead of using the streams directly.
// This is so their output can be captured by `cargo test`
@ -45,21 +43,21 @@ impl BufferWriter {
}
impl Buffer {
pub(in ::fmt) fn clear(&mut self) {
pub(in crate::fmt) fn clear(&mut self) {
self.0.clear();
}
pub(in ::fmt) fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
pub(in crate::fmt) fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.extend(buf);
Ok(buf.len())
}
pub(in ::fmt) fn flush(&mut self) -> io::Result<()> {
pub(in crate::fmt) fn flush(&mut self) -> io::Result<()> {
Ok(())
}
#[cfg(test)]
pub(in ::fmt) fn bytes(&self) -> &[u8] {
pub(in crate::fmt) fn bytes(&self) -> &[u8] {
&self.0
}
}
}

View File

@ -16,7 +16,6 @@
//!
//! ```
//! #[macro_use] extern crate log;
//! extern crate env_logger;
//!
//! use log::Level;
//!
@ -139,33 +138,32 @@
//! * `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'.
//!
//!
//! ## Capturing logs in tests
//!
//!
//! Records logged during `cargo test` will not be captured by the test harness by default.
//! The [`Builder::is_test`] method can be used in unit tests to ensure logs will be captured:
//!
//!
//! ```
//! # #[macro_use] extern crate log;
//! # extern crate env_logger;
//! # fn main() {}
//! #[cfg(test)]
//! mod tests {
//! fn init() {
//! let _ = env_logger::builder().is_test(true).try_init();
//! }
//!
//!
//! #[test]
//! fn it_works() {
//! init();
//!
//!
//! info!("This record will be captured by `cargo test`");
//!
//!
//! assert_eq!(2, 1 + 1);
//! }
//! }
//! ```
//!
//!
//! Enabling test capturing comes at the expense of color and other style support
//! and may have performance implications.
//!
@ -179,32 +177,32 @@
//! * `always` will always print style characters even if they aren't supported by the terminal.
//! This includes emitting ANSI colors on Windows if the console API is unavailable.
//! * `never` will never print style characters.
//!
//!
//! ## Tweaking the default format
//!
//!
//! Parts of the default format can be excluded from the log output using the [`Builder`].
//! The following example excludes the timestamp from the log output:
//!
//!
//! ```
//! env_logger::builder()
//! .default_format_timestamp(false)
//! .format_timestamp(None)
//! .init();
//! ```
//!
//!
//! ### Stability of the default format
//!
//! The default format won't optimise for long-term stability, and explicitly makes no
//! guarantees about the stability of its output across major, minor or patch version
//!
//! The default format won't optimise for long-term stability, and explicitly makes no
//! guarantees about the stability of its output across major, minor or patch version
//! bumps during `0.x`.
//!
//! If you want to capture or interpret the output of `env_logger` programmatically
//!
//! If you want to capture or interpret the output of `env_logger` programmatically
//! then you should use a custom format.
//!
//!
//! ### Using a custom format
//!
//!
//! Custom formats can be provided as closures to the [`Builder`].
//! These closures take a [`Formatter`] and `log::Record` as arguments:
//!
//!
//! ```
//! use std::io::Write;
//!
@ -214,54 +212,43 @@
//! })
//! .init();
//! ```
//!
//!
//! See the [`fmt`] module for more details about custom formats.
//!
//!
//! ## Specifying defaults for environment variables
//!
//!
//! `env_logger` can read configuration from environment variables.
//! If these variables aren't present, the default value to use can be tweaked with the [`Env`] type.
//! The following example defaults to log `warn` and above if the `RUST_LOG` environment variable
//! isn't set:
//!
//!
//! ```
//! use env_logger::Env;
//!
//! env_logger::from_env(Env::default().default_filter_or("warn")).init();
//! ```
//!
//!
//! [log-crate-url]: https://docs.rs/log/
//! [`Builder`]: struct.Builder.html
//! [`Builder::is_test`]: struct.Builder.html#method.is_test
//! [`Env`]: struct.Env.html
//! [`fmt`]: fmt/index.html
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
html_favicon_url = "https://www.rust-lang.org/static/images/favicon.ico",
html_root_url = "https://docs.rs/env_logger/0.6.2")]
#![doc(
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
html_favicon_url = "https://www.rust-lang.org/static/images/favicon.ico",
html_root_url = "https://docs.rs/env_logger/0.7.1"
)]
#![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"))]
#![deny(missing_debug_implementations, missing_docs, warnings)]
extern crate log;
use std::{borrow::Cow, cell::RefCell, env, io};
#[cfg(feature = "termcolor")]
extern crate termcolor;
#[cfg(feature = "humantime")]
extern crate humantime;
#[cfg(feature = "atty")]
extern crate atty;
use std::{env, io};
use std::borrow::Cow;
use std::cell::RefCell;
use log::{Log, LevelFilter, Record, SetLoggerError, Metadata};
use log::{LevelFilter, Log, Metadata, Record, SetLoggerError};
pub mod filter;
pub mod fmt;
@ -269,8 +256,8 @@ pub mod fmt;
pub use self::fmt::glob::*;
use self::filter::Filter;
use self::fmt::Formatter;
use self::fmt::writer::{self, Writer};
use self::fmt::Formatter;
/// The default name for the environment variable to read filters from.
pub const DEFAULT_FILTER_ENV: &'static str = "RUST_LOG";
@ -334,9 +321,7 @@ pub struct Logger {
/// # Examples
///
/// ```
/// #[macro_use]
/// extern crate log;
/// extern crate env_logger;
/// #[macro_use] extern crate log;
///
/// use std::env;
/// use std::io::Write;
@ -364,30 +349,28 @@ pub struct Builder {
impl Builder {
/// Initializes the log builder with defaults.
///
///
/// **NOTE:** This method won't read from any environment variables.
/// Use the [`filter`] and [`write_style`] methods to configure the builder
/// or use [`from_env`] or [`from_default_env`] instead.
///
///
/// # Examples
///
///
/// Create a new builder and configure filters and style:
///
///
/// ```
/// # extern crate log;
/// # extern crate env_logger;
/// # fn main() {
/// use log::LevelFilter;
/// use env_logger::{Builder, WriteStyle};
///
///
/// let mut builder = Builder::new();
///
///
/// builder.filter(None, LevelFilter::Info)
/// .write_style(WriteStyle::Always)
/// .init();
/// # }
/// ```
///
///
/// [`filter`]: #method.filter
/// [`write_style`]: #method.write_style
/// [`from_env`]: #method.from_env
@ -402,13 +385,13 @@ impl Builder {
/// passing in.
///
/// # Examples
///
///
/// Initialise a logger reading the log filter from an environment variable
/// called `MY_LOG`:
///
///
/// ```
/// use env_logger::Builder;
///
///
/// let mut builder = Builder::from_env("MY_LOG");
/// builder.init();
/// ```
@ -426,7 +409,7 @@ impl Builder {
/// ```
pub fn from_env<'a, E>(env: E) -> Self
where
E: Into<Env<'a>>
E: Into<Env<'a>>,
{
let mut builder = Builder::new();
let env = env.into();
@ -443,18 +426,18 @@ impl Builder {
}
/// Initializes the log builder from the environment using default variable names.
///
///
/// This method is a convenient way to call `from_env(Env::default())` without
/// having to use the `Env` type explicitly. The builder will use the
/// [default environment variables].
///
///
/// # Examples
///
///
/// Initialise a logger using the default environment variables:
///
///
/// ```
/// use env_logger::Builder;
///
///
/// let mut builder = Builder::from_default_env();
/// builder.init();
/// ```
@ -473,17 +456,17 @@ impl Builder {
/// `Formatter` so that implementations can use the [`std::fmt`] macros
/// to format and output without intermediate heap allocations. The default
/// `env_logger` formatter takes advantage of this.
///
///
/// # Examples
///
///
/// Use a custom format to write only the log message:
///
///
/// ```
/// use std::io::Write;
/// use env_logger::Builder;
///
///
/// let mut builder = Builder::new();
///
///
/// builder.format(|buf, record| writeln!(buf, "{}", record.args()));
/// ```
///
@ -491,44 +474,66 @@ impl Builder {
/// [`String`]: https://doc.rust-lang.org/stable/std/string/struct.String.html
/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
pub fn format<F: 'static>(&mut self, format: F) -> &mut Self
where F: Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send
where
F: Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send,
{
self.format.custom_format = Some(Box::new(format));
self
}
/// Use the default format.
///
///
/// This method will clear any custom format set on the builder.
pub fn default_format(&mut self) -> &mut Self {
self.format.custom_format = None;
self.format = Default::default();
self
}
/// Whether or not to write the level in the default format.
pub fn default_format_level(&mut self, write: bool) -> &mut Self {
self.format.default_format_level = write;
pub fn format_level(&mut self, write: bool) -> &mut Self {
self.format.format_level = write;
self
}
/// Whether or not to write the module path in the default format.
pub fn default_format_module_path(&mut self, write: bool) -> &mut Self {
self.format.default_format_module_path = write;
pub fn format_module_path(&mut self, write: bool) -> &mut Self {
self.format.format_module_path = write;
self
}
/// Whether or not to write the timestamp in the default format.
pub fn default_format_timestamp(&mut self, write: bool) -> &mut Self {
self.format.default_format_timestamp = write;
/// Configures the amount of spaces to use to indent multiline log records.
/// A value of `None` disables any kind of indentation.
pub fn format_indent(&mut self, indent: Option<usize>) -> &mut Self {
self.format.format_indent = indent;
self
}
/// Whether or not to write the timestamp with nanos.
pub fn default_format_timestamp_nanos(&mut self, write: bool) -> &mut Self {
self.format.default_format_timestamp_nanos = write;
/// Configures if timestamp should be included and in what precision.
pub fn format_timestamp(&mut self, timestamp: Option<fmt::TimestampPrecision>) -> &mut Self {
self.format.format_timestamp = timestamp;
self
}
/// Configures the timestamp to use second precision.
pub fn format_timestamp_secs(&mut self) -> &mut Self {
self.format_timestamp(Some(fmt::TimestampPrecision::Seconds))
}
/// Configures the timestamp to use millisecond precision.
pub fn format_timestamp_millis(&mut self) -> &mut Self {
self.format_timestamp(Some(fmt::TimestampPrecision::Millis))
}
/// Configures the timestamp to use microsecond precision.
pub fn format_timestamp_micros(&mut self) -> &mut Self {
self.format_timestamp(Some(fmt::TimestampPrecision::Micros))
}
/// Configures the timestamp to use nanosecond precision.
pub fn format_timestamp_nanos(&mut self) -> &mut Self {
self.format_timestamp(Some(fmt::TimestampPrecision::Nanos))
}
/// Adds a directive to the filter for a specific module.
///
/// # Examples
@ -536,8 +541,6 @@ impl Builder {
/// Only include messages for warning and above for logs in `path::to::module`:
///
/// ```
/// # extern crate log;
/// # extern crate env_logger;
/// # fn main() {
/// use log::LevelFilter;
/// use env_logger::Builder;
@ -559,8 +562,6 @@ impl Builder {
/// Only include messages for warning and above for logs in `path::to::module`:
///
/// ```
/// # extern crate log;
/// # extern crate env_logger;
/// # fn main() {
/// use log::LevelFilter;
/// use env_logger::Builder;
@ -579,39 +580,26 @@ impl Builder {
///
/// 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.
///
///
/// # Examples
///
///
/// Only include messages for warning and above for logs in `path::to::module`:
///
///
/// ```
/// # extern crate log;
/// # extern crate env_logger;
/// # fn main() {
/// use log::LevelFilter;
/// use env_logger::Builder;
///
///
/// let mut builder = Builder::new();
///
///
/// builder.filter(Some("path::to::module"), LevelFilter::Info);
/// # }
/// ```
pub fn filter(&mut self,
module: Option<&str>,
level: LevelFilter) -> &mut Self {
pub fn filter(&mut self, module: Option<&str>, level: LevelFilter) -> &mut Self {
self.filter.filter(module, level);
self
}
/// Parses the directives string in the same form as the `RUST_LOG`
/// environment variable.
///
/// See the module documentation for more details.
#[deprecated(since = "0.6.1", note = "use `parse_filters` instead.")]
pub fn parse(&mut self, filters: &str) -> &mut Self {
self.parse_filters(filters)
}
/// Parses the directives string in the same form as the `RUST_LOG`
/// environment variable.
///
@ -624,16 +612,16 @@ impl Builder {
/// Sets the target for the log output.
///
/// Env logger can log to either stdout or stderr. The default is stderr.
///
///
/// # Examples
///
///
/// Write log message to `stdout`:
///
///
/// ```
/// use env_logger::{Builder, Target};
///
///
/// let mut builder = Builder::new();
///
///
/// builder.target(Target::Stdout);
/// ```
pub fn target(&mut self, target: fmt::Target) -> &mut Self {
@ -645,16 +633,16 @@ impl Builder {
///
/// This can be useful in environments that don't support control characters
/// for setting colors.
///
///
/// # Examples
///
///
/// Never attempt to write styles:
///
///
/// ```
/// use env_logger::{Builder, WriteStyle};
///
///
/// let mut builder = Builder::new();
///
///
/// builder.write_style(WriteStyle::Never);
/// ```
pub fn write_style(&mut self, write_style: fmt::WriteStyle) -> &mut Self {
@ -672,7 +660,7 @@ impl Builder {
}
/// Sets whether or not the logger will be used in unit tests.
///
///
/// If `is_test` is `true` then the logger will allow the testing framework to
/// capture log records rather than printing them to the terminal directly.
pub fn is_test(&mut self, is_test: bool) -> &mut Self {
@ -712,7 +700,8 @@ impl Builder {
/// This function will panic if it is called more than once, or if another
/// library has already initialized a global logger.
pub fn init(&mut self) {
self.try_init().expect("Builder::init should not be called after logger initialized");
self.try_init()
.expect("Builder::init should not be called after logger initialized");
}
/// Build an env logger.
@ -759,8 +748,8 @@ impl Logger {
/// let logger = Logger::from_env(env);
/// ```
pub fn from_env<'a, E>(env: E) -> Self
where
E: Into<Env<'a>>
where
E: Into<Env<'a>>,
{
Builder::from_env(env).build()
}
@ -818,40 +807,51 @@ impl Log for Logger {
static FORMATTER: RefCell<Option<Formatter>> = RefCell::new(None);
}
FORMATTER.with(|tl_buf| {
// It's possible for implementations to sometimes
// log-while-logging (e.g. a `std::fmt` implementation logs
// internally) but it's super rare. If this happens make sure we
// at least don't panic and ship some output to the screen.
let mut a;
let mut b = None;
let tl_buf = match tl_buf.try_borrow_mut() {
Ok(f) => {
a = f;
&mut *a
}
Err(_) => &mut b,
};
// Check the buffer style. If it's different from the logger's
// style then drop the buffer and recreate it.
match *tl_buf {
Some(ref mut formatter) => {
if formatter.write_style() != self.writer.write_style() {
*formatter = Formatter::new(&self.writer)
}
},
ref mut tl_buf => *tl_buf = Some(Formatter::new(&self.writer))
}
// The format is guaranteed to be `Some` by this point
let mut formatter = tl_buf.as_mut().unwrap();
let _ = (self.format)(&mut formatter, record).and_then(|_| formatter.print(&self.writer));
let print = |formatter: &mut Formatter, record: &Record| {
let _ =
(self.format)(formatter, record).and_then(|_| formatter.print(&self.writer));
// Always clear the buffer afterwards
formatter.clear();
});
};
let printed = FORMATTER
.try_with(|tl_buf| {
match tl_buf.try_borrow_mut() {
// There are no active borrows of the buffer
Ok(mut tl_buf) => match *tl_buf {
// We have a previously set formatter
Some(ref mut formatter) => {
// Check the buffer style. If it's different from the logger's
// style then drop the buffer and recreate it.
if formatter.write_style() != self.writer.write_style() {
*formatter = Formatter::new(&self.writer);
}
print(formatter, record);
}
// We don't have a previously set formatter
None => {
let mut formatter = Formatter::new(&self.writer);
print(&mut formatter, record);
*tl_buf = Some(formatter);
}
},
// There's already an active borrow of the buffer (due to re-entrancy)
Err(_) => {
print(&mut Formatter::new(&self.writer), record);
}
}
})
.is_ok();
if !printed {
// The thread-local storage was not available (because its
// destructor has already run). Create a new single-use
// Formatter on the stack for this call.
print(&mut Formatter::new(&self.writer), record);
}
}
}
@ -867,7 +867,7 @@ impl<'a> Env<'a> {
/// Specify an environment variable to read the filter from.
pub fn filter<E>(mut self, filter_env: E) -> Self
where
E: Into<Cow<'a, str>>
E: Into<Cow<'a, str>>,
{
self.filter = Var::new(filter_env);
@ -888,7 +888,7 @@ impl<'a> Env<'a> {
}
/// Use the default environment variable to read the filter from.
///
///
/// If the variable is not set, the default value will be used.
pub fn default_filter_or<V>(mut self, default: V) -> Self
where
@ -906,7 +906,7 @@ impl<'a> Env<'a> {
/// Specify an environment variable to read the style from.
pub fn write_style<E>(mut self, write_style_env: E) -> Self
where
E: Into<Cow<'a, str>>
E: Into<Cow<'a, str>>,
{
self.write_style = Var::new(write_style_env);
@ -917,9 +917,9 @@ impl<'a> Env<'a> {
///
/// If the variable is not set, the default value will be used.
pub fn write_style_or<E, V>(mut self, write_style_env: E, default: V) -> Self
where
E: Into<Cow<'a, str>>,
V: Into<Cow<'a, str>>,
where
E: Into<Cow<'a, str>>,
V: Into<Cow<'a, str>>,
{
self.write_style = Var::new_with_default(write_style_env, default);
@ -930,8 +930,8 @@ impl<'a> Env<'a> {
///
/// If the variable is not set, the default value will be used.
pub fn default_write_style_or<V>(mut self, default: V) -> Self
where
V: Into<Cow<'a, str>>,
where
V: Into<Cow<'a, str>>,
{
self.write_style = Var::new_with_default(DEFAULT_WRITE_STYLE_ENV, default);
@ -945,8 +945,8 @@ impl<'a> Env<'a> {
impl<'a> Var<'a> {
fn new<E>(name: E) -> Self
where
E: Into<Cow<'a, str>>,
where
E: Into<Cow<'a, str>>,
{
Var {
name: name.into(),
@ -968,15 +968,13 @@ impl<'a> Var<'a> {
fn get(&self) -> Option<String> {
env::var(&*self.name)
.ok()
.or_else(|| self.default
.to_owned()
.map(|v| v.into_owned()))
.or_else(|| self.default.to_owned().map(|v| v.into_owned()))
}
}
impl<'a, T> From<T> for Env<'a>
where
T: Into<Cow<'a, str>>
T: Into<Cow<'a, str>>,
{
fn from(filter_env: T) -> Self {
Env::default().filter(filter_env.into())
@ -993,28 +991,26 @@ impl<'a> Default for Env<'a> {
}
mod std_fmt_impls {
use std::fmt;
use super::*;
use std::fmt;
impl fmt::Debug for Logger{
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
impl fmt::Debug for Logger {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Logger")
.field("filter", &self.filter)
.finish()
}
}
impl fmt::Debug for Builder{
fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result {
impl fmt::Debug for Builder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.built {
f.debug_struct("Logger")
.field("built", &true)
.finish()
f.debug_struct("Logger").field("built", &true).finish()
} else {
f.debug_struct("Logger")
.field("filter", &self.filter)
.field("writer", &self.writer)
.finish()
.field("filter", &self.filter)
.field("writer", &self.writer)
.finish()
}
}
}
@ -1077,7 +1073,7 @@ pub fn init() {
/// library has already initialized a global logger.
pub fn try_init_from_env<'a, E>(env: E) -> Result<(), SetLoggerError>
where
E: Into<Env<'a>>
E: Into<Env<'a>>,
{
let mut builder = Builder::from_env(env);
@ -1109,24 +1105,25 @@ where
/// library has already initialized a global logger.
pub fn init_from_env<'a, E>(env: E)
where
E: Into<Env<'a>>
E: Into<Env<'a>>,
{
try_init_from_env(env).expect("env_logger::init_from_env should not be called after logger initialized");
try_init_from_env(env)
.expect("env_logger::init_from_env should not be called after logger initialized");
}
/// Create a new builder with the default environment variables.
///
///
/// The builder can be configured before being initialized.
pub fn builder() -> Builder {
Builder::from_default_env()
}
/// Create a builder from the given environment variables.
///
///
/// The builder can be configured before being initialized.
pub fn from_env<'a, E>(env: E) -> Builder
where
E: Into<Env<'a>>
E: Into<Env<'a>>,
{
Builder::from_env(env)
}
@ -1148,7 +1145,10 @@ mod tests {
fn env_get_filter_reads_from_default_if_var_not_set() {
env::remove_var("env_get_filter_reads_from_default_if_var_not_set");
let env = Env::new().filter_or("env_get_filter_reads_from_default_if_var_not_set", "from default");
let env = Env::new().filter_or(
"env_get_filter_reads_from_default_if_var_not_set",
"from default",
);
assert_eq!(Some("from default".to_owned()), env.get_filter());
}
@ -1157,7 +1157,8 @@ mod tests {
fn env_get_write_style_reads_from_var_if_set() {
env::set_var("env_get_write_style_reads_from_var_if_set", "from var");
let env = Env::new().write_style_or("env_get_write_style_reads_from_var_if_set", "from default");
let env =
Env::new().write_style_or("env_get_write_style_reads_from_var_if_set", "from default");
assert_eq!(Some("from var".to_owned()), env.get_write_style());
}
@ -1166,7 +1167,10 @@ mod tests {
fn env_get_write_style_reads_from_default_if_var_not_set() {
env::remove_var("env_get_write_style_reads_from_default_if_var_not_set");
let env = Env::new().write_style_or("env_get_write_style_reads_from_default_if_var_not_set", "from default");
let env = Env::new().write_style_or(
"env_get_write_style_reads_from_default_if_var_not_set",
"from default",
);
assert_eq!(Some("from default".to_owned()), env.get_write_style());
}

View File

@ -1,8 +1,8 @@
extern crate log;
extern crate env_logger;
extern crate log;
use std::process;
use std::env;
use std::process;
use std::str;
fn main() {
@ -20,7 +20,7 @@ fn main() {
.unwrap_err();
assert_eq!(log::LevelFilter::Debug, log::max_level());
return
return;
}
let exe = env::current_exe().unwrap();
@ -30,7 +30,7 @@ fn main() {
.output()
.unwrap_or_else(|e| panic!("Unable to start child process: {}", e));
if out.status.success() {
return
return;
}
println!("test failed: {}", out.status);

View File

@ -1,9 +1,10 @@
#[macro_use] extern crate log;
#[macro_use]
extern crate log;
extern crate env_logger;
use std::process;
use std::fmt;
use std::env;
use std::fmt;
use std::process;
use std::str;
struct Foo;
@ -28,7 +29,7 @@ fn main() {
.output()
.unwrap_or_else(|e| panic!("Unable to start child process: {}", e));
if out.status.success() {
return
return;
}
println!("test failed: {}", out.status);

View File

@ -0,0 +1,66 @@
#[macro_use]
extern crate log;
extern crate env_logger;
use std::env;
use std::process;
use std::str;
use std::thread;
struct DropMe;
impl Drop for DropMe {
fn drop(&mut self) {
debug!("Dropping now");
}
}
fn run() {
// Use multiple thread local values to increase the chance that our TLS
// value will get destroyed after the FORMATTER key in the library
thread_local! {
static DROP_ME_0: DropMe = DropMe;
static DROP_ME_1: DropMe = DropMe;
static DROP_ME_2: DropMe = DropMe;
static DROP_ME_3: DropMe = DropMe;
static DROP_ME_4: DropMe = DropMe;
static DROP_ME_5: DropMe = DropMe;
static DROP_ME_6: DropMe = DropMe;
static DROP_ME_7: DropMe = DropMe;
static DROP_ME_8: DropMe = DropMe;
static DROP_ME_9: DropMe = DropMe;
}
DROP_ME_0.with(|_| {});
DROP_ME_1.with(|_| {});
DROP_ME_2.with(|_| {});
DROP_ME_3.with(|_| {});
DROP_ME_4.with(|_| {});
DROP_ME_5.with(|_| {});
DROP_ME_6.with(|_| {});
DROP_ME_7.with(|_| {});
DROP_ME_8.with(|_| {});
DROP_ME_9.with(|_| {});
}
fn main() {
env_logger::init();
if env::var("YOU_ARE_TESTING_NOW").is_ok() {
// Run on a separate thread because TLS values on the main thread
// won't have their destructors run if pthread is used.
// https://doc.rust-lang.org/std/thread/struct.LocalKey.html#platform-specific-behavior
thread::spawn(run).join().unwrap();
} else {
let exe = env::current_exe().unwrap();
let out = process::Command::new(exe)
.env("YOU_ARE_TESTING_NOW", "1")
.env("RUST_LOG", "debug")
.output()
.unwrap_or_else(|e| panic!("Unable to start child process: {}", e));
if !out.status.success() {
println!("test failed: {}", out.status);
println!("--- stdout\n{}", str::from_utf8(&out.stdout).unwrap());
println!("--- stderr\n{}", str::from_utf8(&out.stderr).unwrap());
process::exit(1);
}
}
}

View File

@ -1,8 +1,9 @@
#[macro_use] extern crate log;
#[macro_use]
extern crate log;
extern crate env_logger;
use std::process;
use std::env;
use std::process;
use std::str;
fn main() {
@ -25,7 +26,9 @@ fn run_child(rust_log: String) -> bool {
.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")
str::from_utf8(out.stderr.as_ref())
.unwrap()
.contains("XYZ Message")
}
fn assert_message_printed(rust_log: &str) {
@ -36,7 +39,10 @@ fn assert_message_printed(rust_log: &str) {
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)
panic!(
"RUST_LOG={} should not allow the test log message",
rust_log
)
}
}

View File

@ -1 +1 @@
{"files":{"Cargo.toml":"0fd12ce59d9614688687bd711a7a2899c3c299a3f58078e4e75e6c12a0cb5235","src/codec.rs":"6ec44d7fbc4c2a4db39da4530e3a463e1608256528d5c68fc59ea90752b2d94a","src/datagram.rs":"569f8d9e34d7ee17144bf63d34136ecd9778da0d337e513f338738c50284615e","src/incrdecoder.rs":"f65afa390317ab2a306e8cd7e08f33490dc824242f9be87b2a16b136715f8094","src/lib.rs":"611fc76c0650225ebe15fa2fd56fcaa77dbf0690b50f2c09bee0ea2dc6678572","src/log.rs":"b8da388073f72a21128d52b0d0c963e07a3d3cf3368438ae3a50be34b8add3a4","src/qlog.rs":"87822ebdec0b7ca8a652f45b8d30919f20a814009b0470b498accee0212ec605","src/timer.rs":"706333bf1b07f65df9d18904b1cb269e4b80dee93a9b239dd8cb128b293955ae","tests/log.rs":"480b165b7907ec642c508b303d63005eee1427115d6973a349eaf6b2242ed18d"},"package":null}
{"files":{"Cargo.toml":"0058144aec5e2ce4061a71640d537e4ab6314b7896dcd6f8061c4273e20c9070","src/codec.rs":"6c0f0138967cc927555241ff5b1266deffcf58b9b1d6d09c633a8d5294d148f2","src/datagram.rs":"569f8d9e34d7ee17144bf63d34136ecd9778da0d337e513f338738c50284615e","src/incrdecoder.rs":"f65afa390317ab2a306e8cd7e08f33490dc824242f9be87b2a16b136715f8094","src/lib.rs":"611fc76c0650225ebe15fa2fd56fcaa77dbf0690b50f2c09bee0ea2dc6678572","src/log.rs":"b8da388073f72a21128d52b0d0c963e07a3d3cf3368438ae3a50be34b8add3a4","src/qlog.rs":"17448c854ea0ced8da08fabb4f08aeaf5126b94f44bd8a7aee6be5f1bdb2cf9d","src/timer.rs":"66886b3697e1b4232d9d9892a12d93afd3381812a8ff901bceac4bb48b264607","tests/log.rs":"480b165b7907ec642c508b303d63005eee1427115d6973a349eaf6b2242ed18d"},"package":null}

View File

@ -1,6 +1,6 @@
[package]
name = "neqo-common"
version = "0.4.8"
version = "0.4.9"
authors = ["Bobby Holley <bobbyholley@gmail.com>"]
edition = "2018"
license = "MIT/Apache-2.0"

View File

@ -108,7 +108,7 @@ impl<'a> Decoder<'a> {
pub fn decode_varint(&mut self) -> Option<u64> {
let b1 = match self.decode_byte() {
Some(b) => b,
_ => return None,
None => return None,
};
match b1 >> 6 {
0 => Some(u64::from(b1 & 0x3f)),
@ -129,7 +129,7 @@ impl<'a> Decoder<'a> {
fn decode_checked(&mut self, n: Option<u64>) -> Option<&'a [u8]> {
let len = match n {
Some(l) => l,
_ => return None,
None => return None,
};
if let Ok(l) = usize::try_from(len) {
self.decode(l)
@ -176,6 +176,16 @@ impl<'a> From<&'a [u8]> for Decoder<'a> {
}
}
impl<'a, T> From<&'a T> for Decoder<'a>
where
T: AsRef<[u8]>,
{
#[must_use]
fn from(buf: &'a T) -> Decoder<'a> {
Decoder::new(buf.as_ref())
}
}
impl<'a, 'b> PartialEq<Decoder<'b>> for Decoder<'a> {
#[must_use]
fn eq(&self, other: &Decoder<'b>) -> bool {
@ -225,7 +235,8 @@ impl Encoder {
/// Don't use this except in testing.
#[must_use]
pub fn from_hex(s: &str) -> Self {
pub fn from_hex(s: impl AsRef<str>) -> Self {
let s = s.as_ref();
if s.len() % 2 != 0 {
panic!("Needs to be even length");
}
@ -425,7 +436,7 @@ mod tests {
assert_eq!(dec.decode_remainder(), &[0x01, 0x23, 0x45]);
assert!(dec.decode(2).is_none());
let mut dec = Decoder::from(&enc[0..0]);
let mut dec = Decoder::from(&[]);
assert_eq!(dec.decode_remainder().len(), 0);
}

View File

@ -95,6 +95,12 @@ impl NeqoQlog {
}
}
impl Default for NeqoQlog {
fn default() -> Self {
Self::disabled()
}
}
impl fmt::Debug for NeqoQlogShared {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "NeqoQlog writing to {}", self.qlog_path.display())

View File

@ -89,7 +89,10 @@ impl<T> Timer<T> {
}
/// Slide forward in time by `n * self.granularity`.
#[allow(clippy::cast_possible_truncation)] // guarded by assertion
#[allow(clippy::unknown_clippy_lints)] // Until we require rust 1.45.
#[allow(clippy::cast_possible_truncation, clippy::reversed_empty_ranges)]
// cast_possible_truncation is ok because we have an assertion guard.
// reversed_empty_ranges is to avoid different types on the if/else.
fn tick(&mut self, n: usize) {
let new = self.bucket(n);
let iter = if new < self.cursor {
@ -147,7 +150,7 @@ impl<T> Timer<T> {
let bucket = self.time_bucket(time);
let start_index = match self.items[bucket].binary_search_by_key(&time, TimerItem::time) {
Ok(idx) => idx,
_ => return None,
Err(_) => return None,
};
// start_index is just one of potentially many items with the same time.
// Search backwards for a match, ...

View File

@ -1 +1 @@
{"files":{"Cargo.toml":"e1e3edec4a8e6e90f1fcda51f6112a469a5a68f15fdc2e07defa07ea36fe7901","TODO":"ac0f1c2ebcca03f5b3c0cc56c5aedbb030a4b511e438bc07a57361c789f91e9f","bindings/bindings.toml":"0ae7922bb20f2b8cf54b307cd642303e65b00cfbc3e681877e2a7a86f7b22530","bindings/mozpkix.hpp":"77072c8bb0f6eb6bfe8cbadc111dcd92e0c79936d13f2e501aae1e5d289a6675","bindings/nspr_err.h":"2d5205d017b536c2d838bcf9bc4ec79f96dd50e7bb9b73892328781f1ee6629d","bindings/nspr_error.h":"e41c03c77b8c22046f8618832c9569fbcc7b26d8b9bbc35eea7168f35e346889","bindings/nspr_io.h":"085b289849ef0e77f88512a27b4d9bdc28252bd4d39c6a17303204e46ef45f72","bindings/nspr_time.h":"2e637fd338a5cf0fd3fb0070a47f474a34c2a7f4447f31b6875f5a9928d0a261","bindings/nss_ciphers.h":"95ec6344a607558b3c5ba8510f463b6295f3a2fb3f538a01410531045a5f62d1","bindings/nss_init.h":"ef49045063782fb612aff459172cc6a89340f15005808608ade5320ca9974310","bindings/nss_p11.h":"0b81e64fe6db49b2ecff94edd850be111ef99ec11220e88ceb1c67be90143a78","bindings/nss_secerr.h":"713e8368bdae5159af7893cfa517dabfe5103cede051dee9c9557c850a2defc6","bindings/nss_ssl.h":"af222fb957b989e392e762fa2125c82608a0053aff4fb97e556691646c88c335","bindings/nss_sslerr.h":"24b97f092183d8486f774cdaef5030d0249221c78343570d83a4ee5b594210ae","bindings/nss_sslopt.h":"b7807eb7abdad14db6ad7bc51048a46b065a0ea65a4508c95a12ce90e59d1eea","build.rs":"0be611e6b25a18d5ed724071a3a471c2c5b584649b5fe36de28a56ed8caaf800","src/aead.rs":"e26ad34f7168f42aa87eb3a6455af3191a0e8555782b1d70032fe1d248922f98","src/agent.rs":"d653735397becd2832b4d15f699cfb6be44c6066ef8a99404fa0139e98bf093c","src/agentio.rs":"cc562d09a09719b90b4e1d147fd579e3e89b683448709e920033bceaea108a61","src/auth.rs":"71ac7e297a5f872d26cf67b6bbd96e4548ea38374bdd84c1094f76a5de4ed1cb","src/cert.rs":"fd3fd2bbb38754bdcee3898549feae412943c9f719032531c1ad6e61783b5394","src/constants.rs":"c39ee506a10d685fda77c1d2ddf691b595b067b4e1044ac7a21e360119d6002b","src/err.rs":"04f38831ca62d29d8aadfe9daf95fd29e68ece184e6d3e00bfb9ee1d12744033","src/exp.rs":"61586662407359c1ecb8ed4987bc3c702f26ba2e203a091a51b6d6363cbd510f","src/ext.rs":"97cba23247e5f9656f27587214f7d7370a69174bae5960a012ce3e6fc99f9116","src/hkdf.rs":"40e44f4280497ef525c2b4c465f14f06d241150851668b264ee958f74321cfbe","src/hp.rs":"7fce64e0cc3a6a7e744bc797886bcfaa39679f0a81853b2e55ea0f54fb6bf700","src/lib.rs":"f66cd7a1c949eb47dfa33189d5e28ebbe653c812a93e9cd96583f82b0495707f","src/once.rs":"b9850384899a1a016e839743d3489c0d4d916e1973746ef8c89872105d7d9736","src/p11.rs":"0b62ee5938aefb82e8faee5aa14e990a00442cc9744e8ba22eda80b32030c42c","src/prio.rs":"bc4e97049563b136cb7b39f5171e7909d56a77ed46690aaacb781eeb4a4743e0","src/replay.rs":"40924865994396441a68e6009ecbdf352d6a02fdf539aa65604124e26bffb4d3","src/result.rs":"cef34dfcb907723e195b56501132e4560e250b327783cb5e41201da5b63e9b5c","src/secrets.rs":"acb5befa74e06281c6f80d7298efc58f568bb4e6d949b4225c335e3f392be741","src/selfencrypt.rs":"429cb889a4e9e2345888cc033115c0aa306d2ff90bdfe22b3067700eb1426c37","src/ssl.rs":"d64c20ed2a0b63c5fa3aee674a622408a89a764ee225098f18d0c61ce6c6df29","src/time.rs":"5b2ab4028b04b6245c666f33f1c1449816d3d1eb8141f723f5773f21f8fe4388","tests/aead.rs":"a1d8eb69f5672e064f84dce3d214b347a396718e3de56d57ccc108ee87f1cbc1","tests/agent.rs":"2312590910dc3cba4c446c0dee844773779d8a3870cf543fcc863821fcee50dd","tests/ext.rs":"5f5de777599cbe1295a4461b32c249de74666edb0a13173f76948f2939963dfd","tests/handshake.rs":"6f12fb9a02d36f64254ffe49385de69fce8bc95b73af80be011f0e065d65a5a3","tests/hkdf.rs":"539235e9dcf2a56b72961a9a04f0080409adf6bf465bfad7c30026421b2d4326","tests/hp.rs":"e52a7d2f4387f2dfe8bfe1da5867e8e0d3eb51e171c6904e18b18c4343536af8","tests/init.rs":"20aad800ac793aaf83059cf860593750509fdedeeff0c08a648e7a5cb398dae0","tests/selfencrypt.rs":"46e9a1a09c2ae577eb106d23a5cdacf762575c0dea1948aedab06ef7389ce713"},"package":null}
{"files":{"Cargo.toml":"ec995e040bbf7cb16fa8220bcec8932ccab13bb4e22e575c13d71c0bee7b06d5","TODO":"ac0f1c2ebcca03f5b3c0cc56c5aedbb030a4b511e438bc07a57361c789f91e9f","bindings/bindings.toml":"0ae7922bb20f2b8cf54b307cd642303e65b00cfbc3e681877e2a7a86f7b22530","bindings/mozpkix.hpp":"77072c8bb0f6eb6bfe8cbadc111dcd92e0c79936d13f2e501aae1e5d289a6675","bindings/nspr_err.h":"2d5205d017b536c2d838bcf9bc4ec79f96dd50e7bb9b73892328781f1ee6629d","bindings/nspr_error.h":"e41c03c77b8c22046f8618832c9569fbcc7b26d8b9bbc35eea7168f35e346889","bindings/nspr_io.h":"085b289849ef0e77f88512a27b4d9bdc28252bd4d39c6a17303204e46ef45f72","bindings/nspr_time.h":"2e637fd338a5cf0fd3fb0070a47f474a34c2a7f4447f31b6875f5a9928d0a261","bindings/nss_ciphers.h":"95ec6344a607558b3c5ba8510f463b6295f3a2fb3f538a01410531045a5f62d1","bindings/nss_init.h":"ef49045063782fb612aff459172cc6a89340f15005808608ade5320ca9974310","bindings/nss_p11.h":"0b81e64fe6db49b2ecff94edd850be111ef99ec11220e88ceb1c67be90143a78","bindings/nss_secerr.h":"713e8368bdae5159af7893cfa517dabfe5103cede051dee9c9557c850a2defc6","bindings/nss_ssl.h":"af222fb957b989e392e762fa2125c82608a0053aff4fb97e556691646c88c335","bindings/nss_sslerr.h":"24b97f092183d8486f774cdaef5030d0249221c78343570d83a4ee5b594210ae","bindings/nss_sslopt.h":"b7807eb7abdad14db6ad7bc51048a46b065a0ea65a4508c95a12ce90e59d1eea","build.rs":"0be611e6b25a18d5ed724071a3a471c2c5b584649b5fe36de28a56ed8caaf800","src/aead.rs":"e26ad34f7168f42aa87eb3a6455af3191a0e8555782b1d70032fe1d248922f98","src/agent.rs":"0cf454a0d272dacb31a0e49e9a97c880d1f18c16b9c158b1772ce34c4f15963a","src/agentio.rs":"cc562d09a09719b90b4e1d147fd579e3e89b683448709e920033bceaea108a61","src/auth.rs":"71ac7e297a5f872d26cf67b6bbd96e4548ea38374bdd84c1094f76a5de4ed1cb","src/cert.rs":"fd3fd2bbb38754bdcee3898549feae412943c9f719032531c1ad6e61783b5394","src/constants.rs":"c39ee506a10d685fda77c1d2ddf691b595b067b4e1044ac7a21e360119d6002b","src/err.rs":"04f38831ca62d29d8aadfe9daf95fd29e68ece184e6d3e00bfb9ee1d12744033","src/exp.rs":"61586662407359c1ecb8ed4987bc3c702f26ba2e203a091a51b6d6363cbd510f","src/ext.rs":"97cba23247e5f9656f27587214f7d7370a69174bae5960a012ce3e6fc99f9116","src/hkdf.rs":"40e44f4280497ef525c2b4c465f14f06d241150851668b264ee958f74321cfbe","src/hp.rs":"7fce64e0cc3a6a7e744bc797886bcfaa39679f0a81853b2e55ea0f54fb6bf700","src/lib.rs":"f66cd7a1c949eb47dfa33189d5e28ebbe653c812a93e9cd96583f82b0495707f","src/once.rs":"b9850384899a1a016e839743d3489c0d4d916e1973746ef8c89872105d7d9736","src/p11.rs":"0b62ee5938aefb82e8faee5aa14e990a00442cc9744e8ba22eda80b32030c42c","src/prio.rs":"bc4e97049563b136cb7b39f5171e7909d56a77ed46690aaacb781eeb4a4743e0","src/replay.rs":"40924865994396441a68e6009ecbdf352d6a02fdf539aa65604124e26bffb4d3","src/result.rs":"cef34dfcb907723e195b56501132e4560e250b327783cb5e41201da5b63e9b5c","src/secrets.rs":"acb5befa74e06281c6f80d7298efc58f568bb4e6d949b4225c335e3f392be741","src/selfencrypt.rs":"429cb889a4e9e2345888cc033115c0aa306d2ff90bdfe22b3067700eb1426c37","src/ssl.rs":"d64c20ed2a0b63c5fa3aee674a622408a89a764ee225098f18d0c61ce6c6df29","src/time.rs":"5b2ab4028b04b6245c666f33f1c1449816d3d1eb8141f723f5773f21f8fe4388","tests/aead.rs":"a1d8eb69f5672e064f84dce3d214b347a396718e3de56d57ccc108ee87f1cbc1","tests/agent.rs":"2312590910dc3cba4c446c0dee844773779d8a3870cf543fcc863821fcee50dd","tests/ext.rs":"eba9f03accdd598e38292ac88263a81b367d60d5a736a43117a3663de105ec48","tests/handshake.rs":"6f12fb9a02d36f64254ffe49385de69fce8bc95b73af80be011f0e065d65a5a3","tests/hkdf.rs":"539235e9dcf2a56b72961a9a04f0080409adf6bf465bfad7c30026421b2d4326","tests/hp.rs":"e52a7d2f4387f2dfe8bfe1da5867e8e0d3eb51e171c6904e18b18c4343536af8","tests/init.rs":"20aad800ac793aaf83059cf860593750509fdedeeff0c08a648e7a5cb398dae0","tests/selfencrypt.rs":"46e9a1a09c2ae577eb106d23a5cdacf762575c0dea1948aedab06ef7389ce713"},"package":null}

View File

@ -1,6 +1,6 @@
[package]
name = "neqo-crypto"
version = "0.4.8"
version = "0.4.9"
authors = ["Martin Thomson <mt@lowentropy.net>"]
edition = "2018"
build = "build.rs"
@ -11,7 +11,7 @@ neqo-common = { path = "../neqo-common" }
log = {version = "0.4.0", default-features = false}
[build-dependencies]
bindgen = {version = "0.53.2", default-features = false}
bindgen = {version = "0.53.2", features= ["runtime"]}
serde = "1.0"
serde_derive = "1.0"
toml = "0.4"

View File

@ -76,7 +76,7 @@ fn get_alpn(fd: *mut ssl::PRFileDesc, pre: bool) -> Res<Option<String>> {
chosen.truncate(usize::try_from(chosen_len)?);
Some(match String::from_utf8(chosen) {
Ok(a) => a,
_ => return Err(Error::InternalError),
Err(_) => return Err(Error::InternalError),
})
}
_ => None,
@ -780,7 +780,7 @@ impl DerefMut for Client {
/// `ZeroRttCheckResult` encapsulates the options for handling a `ClientHello`.
#[derive(Clone, Debug, PartialEq)]
pub enum ZeroRttCheckResult {
/// Accept 0-RTT; the default.
/// Accept 0-RTT.
Accept,
/// Reject 0-RTT, but continue the handshake normally.
Reject,

View File

@ -80,14 +80,14 @@ fn simple_extension() {
let mut server = Server::new(&["key"]).expect("should create server");
let client_handler = Rc::new(RefCell::new(SimpleExtensionHandler::default()));
let ch2 = Rc::clone(&client_handler);
let ch = Rc::clone(&client_handler);
client
.extension_handler(0xffff, ch2)
.extension_handler(0xffff, ch)
.expect("client handler installed");
let server_handler = Rc::new(RefCell::new(SimpleExtensionHandler::default()));
let sh2 = Rc::clone(&server_handler);
let sh = Rc::clone(&server_handler);
server
.extension_handler(0xffff, sh2)
.extension_handler(0xffff, sh)
.expect("server handler installed");
connect(&mut client, &mut server);

View File

@ -1 +1 @@
{"files":{"Cargo.toml":"e1eaeb4dd4d2edac1c2afc74f39596e01257d9cb3566e6abdba3a6252efe0d7d","src/client_events.rs":"e912179817153f8e4be39d3db429a0b36c07c3619ec5a12786a719237860fcf1","src/connection.rs":"3ced80d0f32da5b742bc517fa273a56db70269fbaa3b72d8e136edf866c9dc62","src/connection_client.rs":"221ce0ac40530c64e204be37c0a99e4d1d39d1ce95faa007001d6b155d4fdf2f","src/connection_server.rs":"5d32fe8280e451c9853741d3910b1e0aa71220d35ba9cd672919fe74e621de1f","src/control_stream_local.rs":"03d6259599543da2154388d5e48efbc06271e079429d3d946278c4c58c0521c7","src/control_stream_remote.rs":"1dfac4956a7d6971e2cef2c83963d838e73aa3bf3286b7bde97099978c41d527","src/hframe.rs":"5b7349c8f6d18416d4159f0f8da46b5019e82b1eead7e95e151c2d0e1d4cd959","src/lib.rs":"48c632a5171671e5a13f40d81315096a16677d2a4ba6e0b287b50526f28f044a","src/push_controller.rs":"1bda78b2d649489d6a0256121046a1b2bf41f2a5bf779f5c28441531ab811966","src/push_stream.rs":"dad3691b919d1daba0b39489dd00d975a7c25526cda4b8a46d8d7808c543cca7","src/qlog.rs":"29c0e3c4c9571eb7fe905967edeb1c4bc236b1e35a0e0f11a4a847f1d246681d","src/recv_message.rs":"764f59a150ec12f44327687a45bfddb25453574b1ee8a400621cf2e626e4d84b","src/send_message.rs":"7121543a5774b0a590fbfa69d0fab6f1aea6fb61e36314782a63c568ab3b0b83","src/server.rs":"2fbb040e42a168b839cbba5a28b29595c7d0e06faaf94816f05ed856ba7cbf51","src/server_connection_events.rs":"0c2b8831ce9d254a15a3af24d155a119ca1d4a29dd6d287114bf0607efe93076","src/server_events.rs":"27f23dc49f649fb66113c5a71345d9af30e7de04f791d4e1928d32c66b47d3f1","src/settings.rs":"7f0729b0b342132ac3d6dfffc8006732ef15262c071a1629f8883227a289a457","src/stream_type_reader.rs":"aacb2e865f79b3ac55a887fd670f2286d8ffef94f7d8b3ecfa7e0cccbfa9ec04","tests/httpconn.rs":"57ef3a309ca3e9ac768c51ee4c98d16511b855ce4133760bf088e4ac0b967084"},"package":null}
{"files":{"Cargo.toml":"79d2df171ec6ed54088a9ef58ab6af5f4ad6c7d17549e76991451f170a127dc1","src/client_events.rs":"e912179817153f8e4be39d3db429a0b36c07c3619ec5a12786a719237860fcf1","src/connection.rs":"3e203b343f36bb54299425939838003310b8bf13e7d1aca37d2d18779345214c","src/connection_client.rs":"f48a0051cc5b0027189646a17ebcf27c64f467835f3fb1479badb17ba8e37346","src/connection_server.rs":"5d32fe8280e451c9853741d3910b1e0aa71220d35ba9cd672919fe74e621de1f","src/control_stream_local.rs":"03d6259599543da2154388d5e48efbc06271e079429d3d946278c4c58c0521c7","src/control_stream_remote.rs":"1dfac4956a7d6971e2cef2c83963d838e73aa3bf3286b7bde97099978c41d527","src/hframe.rs":"5b7349c8f6d18416d4159f0f8da46b5019e82b1eead7e95e151c2d0e1d4cd959","src/lib.rs":"b29d3140536fcf26cbf0c00d597b30ece4cffbba0fadc3616fabb0968453297c","src/push_controller.rs":"744372679db12ab179e10cf196397d8f0c2f4085cd627e8fbde07889f918638c","src/push_stream.rs":"5044e2d5a8a7aa0711901342f532a952be407327f844613afdae4f8a5e14d21a","src/qlog.rs":"29c0e3c4c9571eb7fe905967edeb1c4bc236b1e35a0e0f11a4a847f1d246681d","src/recv_message.rs":"ee1f20d6b96079c557f1d675b8f88d274eaa9b73626a9f2da152e0a0c091e3dd","src/send_message.rs":"7e3f5280eca9b007b3b189134ce6832d3996f10405d396790acbced06a72af81","src/server.rs":"29af820be014adc338c67284f4df0584757a7ff14d7202e0b9a7a5384ab7b4ed","src/server_connection_events.rs":"0c2b8831ce9d254a15a3af24d155a119ca1d4a29dd6d287114bf0607efe93076","src/server_events.rs":"27f23dc49f649fb66113c5a71345d9af30e7de04f791d4e1928d32c66b47d3f1","src/settings.rs":"92fb232a11d1f37f22dee99c1fa6e0becf287fc6c6996ce890e30b2a6e37cf56","src/stream_type_reader.rs":"aacb2e865f79b3ac55a887fd670f2286d8ffef94f7d8b3ecfa7e0cccbfa9ec04","tests/httpconn.rs":"57ef3a309ca3e9ac768c51ee4c98d16511b855ce4133760bf088e4ac0b967084"},"package":null}

View File

@ -1,6 +1,6 @@
[package]
name = "neqo-http3"
version = "0.4.8"
version = "0.4.9"
authors = ["Dragana Damjanovic <dragana.damjano@gmail.com>"]
edition = "2018"
license = "MIT/Apache-2.0"

View File

@ -353,7 +353,7 @@ impl Http3Connection {
);
if let Some(s) = self.recv_streams.remove(&stream_id) {
s.stream_reset(app_error);
s.stream_reset_recv(app_error, &mut self.qpack_decoder);
Ok(())
} else if self.is_critical_stream(stream_id) {
Err(Error::HttpClosedCriticalStream)
@ -527,9 +527,11 @@ impl Http3Connection {
) -> Res<()> {
qinfo!([self], "Reset stream {} error={}.", stream_id, error);
// We want to execute both statements, therefore we use | instead of ||.
let found = self.send_streams.remove(&stream_id).is_some()
| self.recv_streams.remove(&stream_id).is_some();
let mut found = self.send_streams.remove(&stream_id).is_some();
if let Some(s) = self.recv_streams.remove(&stream_id) {
s.stream_reset(&mut self.qpack_decoder);
found = true;
}
// Stream maybe already be closed and we may get an error here, but we do not care.
let _ = conn.stream_reset_send(stream_id, error);
@ -588,7 +590,7 @@ impl Http3Connection {
HSettingType::BlockedStreams => {
self.qpack_encoder.set_max_blocked_streams(s.value)?
}
_ => {}
HSettingType::MaxHeaderListSize => (),
}
}
Ok(())

View File

@ -20,8 +20,8 @@ use neqo_common::{
use neqo_crypto::{agent::CertificateInfo, AuthenticationStatus, SecretAgentInfo};
use neqo_qpack::{stats::Stats, QpackSettings};
use neqo_transport::{
AppError, Connection, ConnectionEvent, ConnectionIdManager, Output, QuicVersion, StreamId,
StreamType, ZeroRttState,
AppError, Connection, ConnectionEvent, ConnectionId, ConnectionIdManager, Output, QuicVersion,
StreamId, StreamType, ZeroRttState,
};
use std::cell::RefCell;
use std::fmt::Display;
@ -144,6 +144,13 @@ impl Http3Client {
self.conn.set_qlog(qlog);
}
/// Get the connection id, which is useful for disambiguating connections to
/// the same origin.
#[must_use]
pub fn connection_id(&self) -> &ConnectionId {
&self.conn.odcid().expect("Client always has odcid")
}
/// Returns a resumption token if present.
/// A resumption token encodes transport and settings parameter as well.
#[must_use]
@ -172,7 +179,7 @@ impl Http3Client {
let mut dec = Decoder::from(token);
let settings_slice = match dec.decode_vvec() {
Some(v) => v,
_ => return Err(Error::InvalidResumptionToken),
None => return Err(Error::InvalidResumptionToken),
};
qtrace!([self], " settings {}", hex_with_len(&settings_slice));
let mut dec_settings = Decoder::from(settings_slice);
@ -697,6 +704,7 @@ mod tests {
CloseError, ConnectionEvent, FixedConnectionIdManager, QuicVersion, State,
RECV_BUFFER_SIZE, SEND_BUFFER_SIZE,
};
use std::convert::TryFrom;
use test_fixture::{
default_server, fixture_init, loopback, now, DEFAULT_ALPN, DEFAULT_SERVER_NAME,
};
@ -836,6 +844,9 @@ mod tests {
.unwrap(),
1
);
self.encoder
.add_recv_stream(CLIENT_SIDE_DECODER_STREAM_ID)
.unwrap();
}
pub fn create_control_stream(&mut self) {
@ -3089,9 +3100,7 @@ mod tests {
fn test_read_frames_header_blocked() {
let (mut client, mut server, request_stream_id) = connect_and_send_request(true);
server.encoder.set_max_capacity(100).unwrap();
server.encoder.set_max_blocked_streams(100).unwrap();
server.encoder.send(&mut server.conn).unwrap();
setup_server_side_encoder(&mut client, &mut server);
let headers = vec![
(String::from(":status"), String::from("200")),
@ -3127,11 +3136,11 @@ mod tests {
assert!(!client.events().any(header_ready_event));
// Let client receive the encoder instructions.
let _out = client.process(encoder_inst_pkt.dgram(), now());
let _ = client.process(encoder_inst_pkt.dgram(), now());
let out = server.conn.process(None, now());
let _out = client.process(out.dgram(), now());
let _out = client.process(None, now());
let _ = client.process(out.dgram(), now());
let _ = client.process(None, now());
let mut recv_header = false;
let mut recv_data = false;
@ -3159,9 +3168,7 @@ mod tests {
fn test_read_frames_header_blocked_with_fin_after_headers() {
let (mut hconn, mut server, request_stream_id) = connect_and_send_request(true);
server.encoder.set_max_capacity(100).unwrap();
server.encoder.set_max_blocked_streams(100).unwrap();
server.encoder.send(&mut server.conn).unwrap();
setup_server_side_encoder(&mut hconn, &mut server);
let sent_headers = vec![
(String::from(":status"), String::from("200")),
@ -3266,7 +3273,7 @@ mod tests {
let out = client.process(out.dgram(), now());
assert_eq!(client.state(), Http3State::Connected);
let _out = server.conn.process(out.dgram(), now());
let _ = server.conn.process(out.dgram(), now());
assert!(server.conn.state().connected());
assert!(client.tls_info().unwrap().resumed());
@ -4005,9 +4012,7 @@ mod tests {
let out = client.process(None, now());
let _ = server.conn.process(out.dgram(), now());
server.encoder.set_max_capacity(100).unwrap();
server.encoder.set_max_blocked_streams(100).unwrap();
server.encoder.send(&mut server.conn).unwrap();
setup_server_side_encoder(&mut client, &mut server);
let headers = vec![
(String::from(":status"), String::from("200")),
@ -4036,13 +4041,13 @@ mod tests {
server.conn.stream_close_send(request_stream_id).unwrap();
let out = server.conn.process(None, now());
let _out = client.process(out.dgram(), now());
let _ = client.process(out.dgram(), now());
let header_ready_event = |e| matches!(e, Http3ClientEvent::HeaderReady { .. });
assert!(!client.events().any(header_ready_event));
// Let client receive the encoder instructions.
let _out = client.process(qpack_pkt1.dgram(), now());
let _ = client.process(qpack_pkt1.dgram(), now());
assert!(client.events().any(header_ready_event));
}
@ -5088,7 +5093,7 @@ mod tests {
.unwrap();
let out = client.process(None, now());
let _out = server.conn.process(out.dgram(), now());
let _ = server.conn.process(out.dgram(), now());
// Check that encoder got stream_canceled instruction.
let mut inst = [0_u8; 100];
let (amount, fin) = server
@ -5099,4 +5104,258 @@ mod tests {
assert_eq!(amount, STREAM_CANCELED_ID_0.len());
assert_eq!(&inst[..amount], STREAM_CANCELED_ID_0);
}
#[test]
fn data_readable_in_decoder_blocked_state() {
let (mut client, mut server, request_stream_id) = connect_and_send_request(true);
setup_server_side_encoder(&mut client, &mut server);
let headers = vec![
(String::from(":status"), String::from("200")),
(String::from("my-header"), String::from("my-header")),
(String::from("content-length"), String::from("0")),
];
let encoded_headers = server
.encoder
.encode_header_block(&mut server.conn, &headers, request_stream_id)
.unwrap();
let hframe = HFrame::Headers {
header_block: encoded_headers.to_vec(),
};
// Delay encoder instruction so that the stream will be blocked.
let encoder_insts = server.conn.process(None, now());
// Send response headers.
let mut d = Encoder::default();
hframe.encode(&mut d);
server_send_response_and_exchange_packet(
&mut client,
&mut server,
request_stream_id,
&d,
false,
);
// Headers are blocked waiting fro the encoder instructions.
let header_ready_event = |e| matches!(e, Http3ClientEvent::HeaderReady { .. });
assert!(!client.events().any(header_ready_event));
// Now send data frame. This will trigger DataRead event.
let mut d = Encoder::default();
hframe.encode(&mut d);
let d_frame = HFrame::Data { len: 0 };
d_frame.encode(&mut d);
server_send_response_and_exchange_packet(
&mut client,
&mut server,
request_stream_id,
&d,
true,
);
// Now read headers.
let _ = client.process(encoder_insts.dgram(), now());
}
#[test]
fn qpack_stream_reset() {
let (mut client, mut server, request_stream_id) = connect_and_send_request(true);
setup_server_side_encoder(&mut client, &mut server);
// Cancel request.
let _ = client.stream_reset(request_stream_id, Error::HttpRequestCancelled.code());
assert_eq!(server.encoder.stats().stream_cancelled_recv, 0);
let out = client.process(None, now());
let _ = server.conn.process(out.dgram(), now());
let _ = server
.encoder
.recv_if_encoder_stream(&mut server.conn, CLIENT_SIDE_DECODER_STREAM_ID);
assert_eq!(server.encoder.stats().stream_cancelled_recv, 1);
}
fn send_headers_using_encoder(
client: &mut Http3Client,
server: &mut TestServer,
request_stream_id: u64,
headers: &[(String, String)],
data: &[u8],
) -> Option<Datagram> {
let encoded_headers = server
.encoder
.encode_header_block(&mut server.conn, &headers, request_stream_id)
.unwrap();
let hframe = HFrame::Headers {
header_block: encoded_headers.to_vec(),
};
let out = server.conn.process(None, now());
// Send response
let mut d = Encoder::default();
hframe.encode(&mut d);
let d_frame = HFrame::Data {
len: u64::try_from(data.len()).unwrap(),
};
d_frame.encode(&mut d);
d.encode(data);
server_send_response_and_exchange_packet(client, server, request_stream_id, &d, true);
out.dgram()
}
#[test]
fn qpack_stream_reset_recv() {
let (mut client, mut server, request_stream_id) = connect_and_send_request(true);
setup_server_side_encoder(&mut client, &mut server);
// Cancel request.
let _ = server
.conn
.stream_reset_send(request_stream_id, Error::HttpRequestCancelled.code());
assert_eq!(server.encoder.stats().stream_cancelled_recv, 0);
let out = server.conn.process(None, now());
let out = client.process(out.dgram(), now());
let _ = server.conn.process(out.dgram(), now());
let _ = server
.encoder
.recv_if_encoder_stream(&mut server.conn, CLIENT_SIDE_DECODER_STREAM_ID);
assert_eq!(server.encoder.stats().stream_cancelled_recv, 1);
}
#[test]
fn qpack_stream_reset_during_header_qpack_blocked() {
let (mut client, mut server, request_stream_id) = connect_and_send_request(true);
setup_server_side_encoder(&mut client, &mut server);
let _ = send_headers_using_encoder(
&mut client,
&mut server,
request_stream_id,
&[
(String::from(":status"), String::from("200")),
(String::from("my-header"), String::from("my-header")),
(String::from("content-length"), String::from("3")),
],
&[0x61, 0x62, 0x63],
);
let header_ready_event = |e| matches!(e, Http3ClientEvent::HeaderReady { .. });
assert!(!client.events().any(header_ready_event));
// Cancel request.
let _ = client.stream_reset(request_stream_id, Error::HttpRequestCancelled.code());
assert_eq!(server.encoder.stats().stream_cancelled_recv, 0);
let out = client.process(None, now());
let _ = server.conn.process(out.dgram(), now());
let _ = server
.encoder
.recv_if_encoder_stream(&mut server.conn, CLIENT_SIDE_DECODER_STREAM_ID);
assert_eq!(server.encoder.stats().stream_cancelled_recv, 1);
}
#[test]
fn qpack_no_stream_cancelled_after_fin() {
let (mut client, mut server, request_stream_id) = connect_and_send_request(true);
setup_server_side_encoder(&mut client, &mut server);
let encoder_instruct = send_headers_using_encoder(
&mut client,
&mut server,
request_stream_id,
&[
(String::from(":status"), String::from("200")),
(String::from("my-header"), String::from("my-header")),
(String::from("content-length"), String::from("3")),
],
&[],
);
// Exchange encoder instructions
let _ = client.process(encoder_instruct, now());
let header_ready_event = |e| matches!(e, Http3ClientEvent::HeaderReady { .. });
assert!(client.events().any(header_ready_event));
// After this the recv_stream is in ClosePending state
// Cancel request.
let _ = client.stream_reset(request_stream_id, Error::HttpRequestCancelled.code());
assert_eq!(server.encoder.stats().stream_cancelled_recv, 0);
let out = client.process(None, now());
let _ = server.conn.process(out.dgram(), now());
let _ = server
.encoder
.recv_if_encoder_stream(&mut server.conn, CLIENT_SIDE_DECODER_STREAM_ID);
assert_eq!(server.encoder.stats().stream_cancelled_recv, 0);
}
#[test]
fn qpack_stream_reset_push_promise_header_decoder_block() {
let (mut client, mut server, request_stream_id) = connect_and_send_request(true);
setup_server_side_encoder(&mut client, &mut server);
let headers = vec![
(String::from(":status"), String::from("200")),
(String::from("content-length"), String::from("3")),
];
let encoded_headers = server
.encoder
.encode_header_block(&mut server.conn, &headers, request_stream_id)
.unwrap();
let hframe = HFrame::Headers {
header_block: encoded_headers.to_vec(),
};
// Send the encoder instructions.
let out = server.conn.process(None, now());
let _ = client.process(out.dgram(), now());
// Send PushPromise that will be blocked waiting for decoder instructions.
let _ = send_push_promise_using_encoder(&mut client, &mut server, request_stream_id, 0);
// Send response
let mut d = Encoder::default();
hframe.encode(&mut d);
let d_frame = HFrame::Data { len: 0 };
d_frame.encode(&mut d);
server_send_response_and_exchange_packet(
&mut client,
&mut server,
request_stream_id,
&d,
true,
);
let header_ready_event = |e| matches!(e, Http3ClientEvent::HeaderReady { .. });
assert!(client.events().any(header_ready_event));
// Cancel request.
let _ = client.stream_reset(request_stream_id, Error::HttpRequestCancelled.code());
let out = client.process(None, now());
let _ = server.conn.process(out.dgram(), now());
let _ = server
.encoder
.recv_if_encoder_stream(&mut server.conn, CLIENT_SIDE_DECODER_STREAM_ID);
assert_eq!(server.encoder.stats().stream_cancelled_recv, 1);
}
#[test]
fn qpack_stream_reset_dynamic_table_zero() {
let (mut client, mut server, request_stream_id) = connect_and_send_request(true);
// Cancel request.
let _ = client.stream_reset(request_stream_id, Error::HttpRequestCancelled.code());
assert_eq!(server.encoder.stats().stream_cancelled_recv, 0);
let out = client.process(None, now());
let _ = server.conn.process(out.dgram(), now());
let _ = server
.encoder
.recv_if_encoder_stream(&mut server.conn, CLIENT_SIDE_DECODER_STREAM_ID);
assert_eq!(server.encoder.stats().stream_cancelled_recv, 0);
}
}

View File

@ -244,7 +244,8 @@ impl ::std::fmt::Display for Error {
}
pub trait RecvStream: Debug {
fn stream_reset(&self, app_error: AppError);
fn stream_reset_recv(&self, app_error: AppError, decoder: &mut QPackDecoder);
fn stream_reset(&self, decoder: &mut QPackDecoder);
/// # Errors
/// An error may happen while reading a stream, e.g. early close, protocol error, etc.
fn receive(&mut self, conn: &mut Connection, decoder: &mut QPackDecoder) -> Res<()>;

View File

@ -365,7 +365,7 @@ impl PushController {
self.push_streams.close(push_id);
Ok(())
}
_ => Err(Error::InvalidStreamId),
Some(_) => Err(Error::InvalidStreamId),
}
}
@ -434,7 +434,7 @@ impl PushController {
Some(PushState::Active { .. }) => {
self.conn_events.insert(event);
}
_ => {
Some(_) => {
debug_assert!(false, "No record of a stream!");
}
}

View File

@ -139,12 +139,21 @@ impl RecvStream for PushStream {
matches!(self.state, PushStreamState::Closed)
}
fn stream_reset(&self, _app_error: AppError) {
fn stream_reset_recv(&self, _app_error: AppError, decoder: &mut QPackDecoder) {
if !self.done() {
decoder.cancel_stream(self.stream_id);
}
if let Some(push_id) = self.state.push_id() {
self.push_handler.borrow_mut().push_stream_reset(push_id);
}
}
fn stream_reset(&self, decoder: &mut QPackDecoder) {
if !self.done() {
decoder.cancel_stream(self.stream_id);
}
}
fn read_data(
&mut self,
conn: &mut Connection,

View File

@ -294,6 +294,13 @@ impl RecvMessage {
}
self.state = RecvMessageState::Closed;
}
fn closing(&self) -> bool {
matches!(
self.state,
RecvMessageState::ClosePending | RecvMessageState::Closed
)
}
}
impl RecvStream for RecvMessage {
@ -323,10 +330,19 @@ impl RecvStream for RecvMessage {
matches!(self.state, RecvMessageState::Closed)
}
fn stream_reset(&self, app_error: AppError) {
fn stream_reset_recv(&self, app_error: AppError, decoder: &mut QPackDecoder) {
if !self.closing() || !self.blocked_push_promise.is_empty() {
decoder.cancel_stream(self.stream_id);
}
self.conn_events.reset(self.stream_id, app_error);
}
fn stream_reset(&self, decoder: &mut QPackDecoder) {
if !self.closing() || !self.blocked_push_promise.is_empty() {
decoder.cancel_stream(self.stream_id);
}
}
fn read_data(
&mut self,
conn: &mut Connection,

View File

@ -13,7 +13,6 @@ use neqo_common::{qdebug, qinfo, qtrace, Encoder};
use neqo_qpack::encoder::QPackEncoder;
use neqo_transport::{AppError, Connection};
use std::cmp::min;
use std::convert::TryFrom;
use std::fmt::Debug;
const MAX_DATA_HEADER_SIZE_2: usize = (1 << 6) - 1; // Maximal amount of data with DATA frame header size 2
@ -140,11 +139,9 @@ impl SendMessage {
| SendMessageState::Initialized { .. }
| SendMessageState::SendingInitialMessage { .. } => Ok(0),
SendMessageState::SendingData => {
let available = usize::try_from(
conn.stream_avail_send_space(self.stream_id)
.map_err(|e| Error::map_stream_send_errors(&e))?,
)
.unwrap_or(usize::max_value());
let available = conn
.stream_avail_send_space(self.stream_id)
.map_err(|e| Error::map_stream_send_errors(&e))?;
if available <= 2 {
return Ok(0);
}

View File

@ -87,7 +87,7 @@ impl Http3Server {
}
/// Process HTTP3 layer.
pub fn process_http3(&mut self, now: Instant) {
fn process_http3(&mut self, now: Instant) {
qtrace!([self], "Process http3 internal.");
let mut active_conns = self.server.active_connections();

View File

@ -166,7 +166,7 @@ impl ZeroRttChecker for HttpZeroRttChecker {
u64::from(self.settings.max_blocked_streams) >= setting.value
}
HSettingType::MaxTableCapacity => self.settings.max_table_size_decoder >= setting.value,
_ => true,
HSettingType::MaxHeaderListSize => true,
}) {
ZeroRttCheckResult::Accept
} else {

View File

@ -1 +1 @@
{"files":{"Cargo.toml":"5f585c883d2c53b2934848b0022f18bb85b7fe6dbe68a719f25478435081fe1b","src/decoder.rs":"1933c031d3de604479dcd753e46b6c6a7f5a9e6957b5e8e425ac6ceb80bb567b","src/decoder_instructions.rs":"a8e04dff5fc4c658322a10daadab947dc2e41932c00c3f8d387671a86d0516af","src/encoder.rs":"f881a54e020a41a3a68bcfa8290e309ea5deab4dbbc3fe3c0103b5d6d9d3f800","src/encoder_instructions.rs":"1d4424bf21c0ac26b7c8fee6450b943346c5493ab86dd7ec2edc5f566454721e","src/header_block.rs":"f935872919154f678947732270d688be4790309f9e390a0c8eb6c9484a41a8dd","src/huffman.rs":"68fa0bada0c35d20f793980596accdcc548970214841f71789290fc334e51fc1","src/huffman_decode_helper.rs":"2970c57f052878b727c2f764490c54184f5c2608e1d6aa961c3b01509e290122","src/huffman_table.rs":"06fea766a6276ac56c7ee0326faed800a742c15fda1f33bf2513e6cc6a5e6d27","src/lib.rs":"8ee2082fd94064e61286e24e5192ee02eee50b1ca82b9105a1e1945e596ccaa8","src/prefix.rs":"72c587c40aef4ed38cf13b2de91091d671611679be2a9da6f0b24abafaf50dc5","src/qlog.rs":"7618085e27bb3fb1f4d1c73ba501b9a293723293c4020b7cc4129676eb278131","src/qpack_send_buf.rs":"5170b93afaf0c1609463e6c5ae4dccb1a2ce4e4407296db1bcaf06c6b5bb97ab","src/reader.rs":"4bcea0de1d7dc09ec0cdff364d8f62da54bbbe1f6db55a495f943f31369b4074","src/static_table.rs":"fda9d5c6f38f94b0bf92d3afdf8432dce6e27e189736596e16727090c77b78ec","src/stats.rs":"39b8bb0af37c7f05e9db40f74dfec2f6be4a3f112c2347dd235c2488403eb03c","src/table.rs":"f7091bdd9ad1f8fe3b2298a7dbfd3d285c212d69569cda54f9bcf251cb758a21"},"package":null}
{"files":{"Cargo.toml":"da9afbbea6416a849713467e4658890ab8a2d873198628ce8055a06e37177227","src/decoder.rs":"9f19a014fd800553904ae26407b744b4adb4608c314fbedae1c1aee4fa5902fd","src/decoder_instructions.rs":"a8e04dff5fc4c658322a10daadab947dc2e41932c00c3f8d387671a86d0516af","src/encoder.rs":"511f53f32fe499f8956372a4c23d058c1f8017f396e9601788efbf26d541140d","src/encoder_instructions.rs":"1d4424bf21c0ac26b7c8fee6450b943346c5493ab86dd7ec2edc5f566454721e","src/header_block.rs":"f935872919154f678947732270d688be4790309f9e390a0c8eb6c9484a41a8dd","src/huffman.rs":"68fa0bada0c35d20f793980596accdcc548970214841f71789290fc334e51fc1","src/huffman_decode_helper.rs":"2970c57f052878b727c2f764490c54184f5c2608e1d6aa961c3b01509e290122","src/huffman_table.rs":"06fea766a6276ac56c7ee0326faed800a742c15fda1f33bf2513e6cc6a5e6d27","src/lib.rs":"8ee2082fd94064e61286e24e5192ee02eee50b1ca82b9105a1e1945e596ccaa8","src/prefix.rs":"72c587c40aef4ed38cf13b2de91091d671611679be2a9da6f0b24abafaf50dc5","src/qlog.rs":"7618085e27bb3fb1f4d1c73ba501b9a293723293c4020b7cc4129676eb278131","src/qpack_send_buf.rs":"5170b93afaf0c1609463e6c5ae4dccb1a2ce4e4407296db1bcaf06c6b5bb97ab","src/reader.rs":"4bcea0de1d7dc09ec0cdff364d8f62da54bbbe1f6db55a495f943f31369b4074","src/static_table.rs":"fda9d5c6f38f94b0bf92d3afdf8432dce6e27e189736596e16727090c77b78ec","src/stats.rs":"bc82c8f86b54981be234948e285af3d778ba9d23ddaaf2fbedf9c03121eaada7","src/table.rs":"f7091bdd9ad1f8fe3b2298a7dbfd3d285c212d69569cda54f9bcf251cb758a21"},"package":null}

View File

@ -1,6 +1,6 @@
[package]
name = "neqo-qpack"
version = "0.4.8"
version = "0.4.9"
authors = ["Dragana Damjanovic <dragana.damjano@gmail.com>"]
edition = "2018"
license = "MIT/Apache-2.0"

View File

@ -151,7 +151,10 @@ impl QPackDecoder {
}
pub fn cancel_stream(&mut self, stream_id: u64) {
DecoderInstruction::StreamCancellation { stream_id }.marshal(&mut self.send_buf);
if self.table.capacity() > 0 {
self.blocked_streams.retain(|(id, _)| *id != stream_id);
DecoderInstruction::StreamCancellation { stream_id }.marshal(&mut self.send_buf);
}
}
/// # Errors
@ -189,10 +192,20 @@ impl QPackDecoder {
match decoder.decode_header_block(&self.table, self.max_entries, self.table.base()) {
Ok(HeaderDecoderResult::Blocked(req_insert_cnt)) => {
self.blocked_streams.push((stream_id, req_insert_cnt));
if self.blocked_streams.len() > self.max_blocked_streams {
Err(Error::DecompressionFailed)
} else {
let r = self
.blocked_streams
.iter()
.filter_map(|(id, req)| if *id <= stream_id { Some(*req) } else { None })
.collect::<Vec<_>>();
if !r.is_empty() {
debug_assert!(r.len() == 1);
debug_assert!(r[0] == req_insert_cnt);
return Ok(None);
}
self.blocked_streams.push((stream_id, req_insert_cnt));
Ok(None)
}
}

View File

@ -223,6 +223,7 @@ impl QPackEncoder {
}
DecoderInstruction::HeaderAck { stream_id } => self.header_ack(stream_id),
DecoderInstruction::StreamCancellation { stream_id } => {
self.stats.stream_cancelled_recv += 1;
self.stream_cancellation(stream_id)
}
_ => Ok(()),

View File

@ -12,4 +12,5 @@ pub struct Stats {
pub dynamic_table_inserts: usize,
// This is the munber of header blockes that reference the dynamic table.
pub dynamic_table_references: usize,
pub stream_cancelled_recv: usize,
}

View File

@ -1 +1 @@
{"files":{"Cargo.toml":"140f9401908daff563cc06e0e647bceace5f316dcbba8bef987f9f2d085d4166","TODO":"d759cb804b32fa9d96ea8d3574a3c4073da9fe6a0b02b708a0e22cce5a5b4a0f","src/cc.rs":"e89194d22ac09f4e7b6ab520079c6d580aa82faa40021dce9802304e2e2a2585","src/cid.rs":"936ed772eddf533cb962332a68034a0e209023991fa44ec72e2805cdff29a3c2","src/connection.rs":"62098192bc2e40ff42d62ee663b3b9f509aac7ce0ac6be982dfecd07bbae6314","src/crypto.rs":"dcd99f95b20fb5f336338f35574af1a2e96245aeea27e0164597cbba31161609","src/dump.rs":"d69ccb0e3b240823b886a791186afbac9f2e26d1f1f67a55dbf86f8cd3e6203e","src/events.rs":"1a17db9b910b5f7260ff363f0f13700767545a149d0e4a7c0454f1ff73a78fa6","src/flow_mgr.rs":"ff820f4f45fb5dbed5e4b1e9433483a36fc624bd5fa1a22dd7abb779ba723caf","src/frame.rs":"d35059f97154d918e46c3d9b9fc5e6bf18b45265f523da57eee56a3c3e153be4","src/lib.rs":"ab39d43753c19cabdd697ab4265aaf079ecf19f1c72bc31aef74dca9c9ab59b9","src/pace.rs":"cee74faccfba9de2a827782b5a34015626b7748be27ffd4942a4caaaf14c6c18","src/packet/mod.rs":"4440a6aa20bd47b5798f8b2728d1e089787743a4eeff954d2aaa63d899d24114","src/packet/retry.rs":"842b1fa864e03e2bbe76dd2daeaa727cf20440fed2b654ca3b50f62bc4df26a8","src/path.rs":"3ef5d00c247cfe0eced6885cdd283e25766ee979d5bdc662abfdf4d4b962dbf1","src/qlog.rs":"0bd5acb9e8be04fc9f65852d7144acf8d2ac5ebf70a351ec6bceb3e3bcb4a14e","src/recovery.rs":"7abec6675d296ab98d176abb17d72025b42431c0a487e67723ba3c5472c11d83","src/recv_stream.rs":"6c164fa54dee1a0ff6aa2d3ab4c5ec48d7ed624907d980d811a8c361995e85b6","src/send_stream.rs":"0d65328d63ccdefde6fff3eb3b3d7dcbb3c7b5605e0dc290d6fd6abd3e29c89c","src/server.rs":"6dd25aa224da98ed970fd96ab5c462bf3a35376ed8f69352b686a70dc95f833f","src/stats.rs":"8593be6985c4545371c3d1563ba9582118d6005b0f51b9c04b98a0dd01a55002","src/stream_id.rs":"74c3523085bfdddfb009a33a747b329b27007b3f0ba728b18a5b6e8ab8ad1d26","src/tparams.rs":"91da52cd894ae9c6097100e728c1b6679cafa7743dedc1902d0dff0f2d17521d","src/tracking.rs":"ad41a166ecef97730ba1dc215f732778e9e043bf05941f2225930d546a4931cf","tests/conn_vectors.rs":"4ff98a0a65fe31e3503d1ec3d5fc9cdb99815a4e0343510a17d0a0ab122650ac","tests/connection.rs":"a93985c199a9ef987106f4a20b35ebf803cdbbb855c07b1362b403eed7101ef8","tests/server.rs":"759557986f606b4b2d3a02e6c99ab07ca29dbe92d4d7fdc229ccdbc52899d125"},"package":null}
{"files":{"Cargo.toml":"12aa405824e4721413b634d7ab23d1a5fec4afd65b3f08ed18dcf04f29f89325","TODO":"d759cb804b32fa9d96ea8d3574a3c4073da9fe6a0b02b708a0e22cce5a5b4a0f","src/cc.rs":"4a3409ddcf653fc67057f337c6a07a58d52979f683b43c0b8b802100f2685aa7","src/cid.rs":"936ed772eddf533cb962332a68034a0e209023991fa44ec72e2805cdff29a3c2","src/connection.rs":"a6760732ba17649636cd1ccbc9c4f2678ccb7ad34f143fea7aa74fa67c753efe","src/crypto.rs":"ee5ae4b73a8d55aa1e65fb83d86c03b4e0f2db27b6c0d410ca944da0268906e8","src/dump.rs":"d69ccb0e3b240823b886a791186afbac9f2e26d1f1f67a55dbf86f8cd3e6203e","src/events.rs":"3d545f4bf0625dee172c580c3881b3a87ece055335dd08780ec66780a5398547","src/flow_mgr.rs":"ff820f4f45fb5dbed5e4b1e9433483a36fc624bd5fa1a22dd7abb779ba723caf","src/frame.rs":"f56d38e67524e19c37cde41fe74c01831d45ebe76b595e8f6e275a34360b5126","src/lib.rs":"7f8b642ef73bbb5a498514a9617e2650ea4f73204311eca971f0c41b8ff06e77","src/pace.rs":"eb9094cfcae54162022f70f230b6a9811add0063b878100f147a9365473f6612","src/packet/mod.rs":"a77866c8f6c07a2ca9e41093644da495ca6370103b6154180a70cddcb6d9528e","src/packet/retry.rs":"842b1fa864e03e2bbe76dd2daeaa727cf20440fed2b654ca3b50f62bc4df26a8","src/path.rs":"3ef5d00c247cfe0eced6885cdd283e25766ee979d5bdc662abfdf4d4b962dbf1","src/qlog.rs":"01c2d07ca4572f4283d0b0120bf661c5031c129d6f2e5917235600c33add18a4","src/recovery.rs":"9fb6349a0a6430bc034b1981039611e731b780fb8c7ae9b53adf1592b7c5ab7b","src/recv_stream.rs":"b1688b9300320e0aa2c6b862030bfda6b50d9a4e6d14a3d89dd8554e92b780e0","src/send_stream.rs":"223547f3140e9b459c303d46393760d7523b5b9d74c72c82d383b0ceb5a9f429","src/server.rs":"6dd25aa224da98ed970fd96ab5c462bf3a35376ed8f69352b686a70dc95f833f","src/stats.rs":"65927fbdd26632730e7029020ad02e9e6c59b2ec9ef26693369e81d4530149fd","src/stream_id.rs":"74c3523085bfdddfb009a33a747b329b27007b3f0ba728b18a5b6e8ab8ad1d26","src/tparams.rs":"91da52cd894ae9c6097100e728c1b6679cafa7743dedc1902d0dff0f2d17521d","src/tracking.rs":"20c1fd08edfaa77aba8adac586d3efe9c204d8e8cc484c1bbf7be85ceac84e71","tests/conn_vectors.rs":"4ff98a0a65fe31e3503d1ec3d5fc9cdb99815a4e0343510a17d0a0ab122650ac","tests/connection.rs":"a93985c199a9ef987106f4a20b35ebf803cdbbb855c07b1362b403eed7101ef8","tests/network.rs":"a986c22da7132ec843a44c4bcb5a7d2726132aa27a47a8ea91634cd88e1b763b","tests/server.rs":"24ead84b0436daadbd22436dccf3625ab7ed83804829519e102ab7f226044a61","tests/sim/connection.rs":"d3c565990e1ca05d872bcdf83e48e66af79e6bac82a6a40d7cdd3ba193fee3c6","tests/sim/delay.rs":"9efa722adb89e37262369e9f3c67405f0acc8c24997271811e48df9e856e5a8d","tests/sim/drop.rs":"bd89e5c71cdd1b27cd755faaedd87d5feadf2f424df721a7df41a51bcebcbb58","tests/sim/mod.rs":"c6230ea030e69b9c6c4fcb30ba3f397c6da588498454f34f5075d16ce4db0542","tests/sim/net.rs":"597f4d37bc26c3d82eeeaa6d14dd03bc2be3930686df2b293748b43c07c497d7","tests/sim/rng.rs":"2c90b0bbaf0c952ebee232deb3594f7a86af387737b15474de3e97ee6b623d90","tests/sim/taildrop.rs":"5c505d150f0071e8cc2d540b3a817a6942fdf13df32f1fbc6822952f2e146176"},"package":null}

View File

@ -1,6 +1,6 @@
[package]
name = "neqo-transport"
version = "0.4.8"
version = "0.4.9"
authors = ["EKR <ekr@rtfm.com>", "Andy Grover <agrover@mozilla.com>"]
edition = "2018"
license = "MIT/Apache-2.0"
@ -12,6 +12,7 @@ lazy_static = "1.3.0"
log = {version = "0.4.0", default-features = false}
smallvec = "1.0.0"
qlog = "0.3.0"
indexmap = "1.0"
[dev-dependencies]
test-fixture = { path = "../test-fixture" }

Some files were not shown because too many files have changed in this diff Show More