From 95bcab9395a383fca54abb2b8b2ecde873e18eda Mon Sep 17 00:00:00 2001 From: Felix Obenhuber Date: Wed, 27 Apr 2022 19:21:49 +0200 Subject: [PATCH] Add env-logger Add env-logger and configure with log level filter debug if the logger is not configured via the RUST_LOG env variable. --- Cargo.lock | 75 ++++++++++++++++++++++++++++++++++++++ mqtt-v5-broker/Cargo.toml | 1 + mqtt-v5-broker/src/main.rs | 8 ++++ 3 files changed, 84 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 99d4830..678bf87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,26 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "0.1.7" @@ -70,6 +90,19 @@ dependencies = [ "syn", ] +[[package]] +name = "env_logger" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "fuchsia-cprng" version = "0.1.1" @@ -196,6 +229,12 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acd94fdbe1d4ff688b67b04eee2e17bd50995534a61539e45adfefb45e5e5503" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "libc" version = "0.2.101" @@ -256,6 +295,7 @@ name = "mqtt-v5-broker" version = "0.1.0" dependencies = [ "bytes", + "env_logger", "futures", "log", "mqtt-v5", @@ -527,6 +567,23 @@ dependencies = [ "rand_core 0.3.1", ] +[[package]] +name = "regex" +version = "1.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" + [[package]] name = "serde" version = "1.0.129" @@ -556,6 +613,15 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + [[package]] name = "tokio" version = "1.10.1" @@ -648,6 +714,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/mqtt-v5-broker/Cargo.toml b/mqtt-v5-broker/Cargo.toml index 6115824..6204d8d 100644 --- a/mqtt-v5-broker/Cargo.toml +++ b/mqtt-v5-broker/Cargo.toml @@ -16,3 +16,4 @@ mqtt-v5 = { path = "../mqtt-v5", version = "0.2" } nanoid = "0.3" tokio = { version = "1", features = ["net", "rt-multi-thread", "sync", "time", "macros"] } tokio-util = { version = "0.6", features = ["codec"] } +env_logger = "0.8.4" diff --git a/mqtt-v5-broker/src/main.rs b/mqtt-v5-broker/src/main.rs index 1c272d4..8f91a7b 100644 --- a/mqtt-v5-broker/src/main.rs +++ b/mqtt-v5-broker/src/main.rs @@ -1,3 +1,5 @@ +use std::env; + use crate::{ broker::{Broker, BrokerMessage}, client::UnconnectedClient, @@ -197,6 +199,12 @@ async fn websocket_server_loop(broker_tx: Sender) { } fn main() -> Result<(), Box> { + if env::var("RUST_LOG").is_err() { + env_logger::builder().filter(None, log::LevelFilter::Debug).init(); + } else { + env_logger::init(); + } + // Creating a Runtime does the following: // * Spawn a background thread running a Reactor instance. // * Start a ThreadPool for executing futures.