From 54eb8b67430bc32133ab63dbdbfac9a805e45a8b Mon Sep 17 00:00:00 2001 From: librelois <elois@ifee.fr> Date: Sun, 20 Oct 2019 22:16:00 +0200 Subject: [PATCH] [feat] core: #149 migrate logger to fern to filter logs by crate --- Cargo.lock | 92 ++++++++++++++----- lib/core/core/Cargo.toml | 3 +- lib/core/core/src/logger.rs | 53 +++++++---- .../ws2p/ws2p/tests/connection_negociation.rs | 17 +++- lib/tests-tools/common-tests-tools/Cargo.toml | 3 +- .../common-tests-tools/src/logger.rs | 53 +++++++---- 6 files changed, 155 insertions(+), 66 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cef8bf3b..e72e943e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,6 +21,11 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "approx" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "arrayref" version = "0.3.5" @@ -192,6 +197,16 @@ name = "cfg-if" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "cgmath" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "chacha20-poly1305-aead" version = "0.1.2" @@ -241,6 +256,15 @@ dependencies = [ "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "colored" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winconsole 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "constant_time_eq" version = "0.1.4" @@ -580,8 +604,9 @@ dependencies = [ name = "durs-common-tests-tools" version = "0.1.0" dependencies = [ + "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", + "fern 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", - "simplelog 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -613,6 +638,7 @@ dependencies = [ name = "durs-core" version = "0.3.0-dev" dependencies = [ + "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "clear_on_drop 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "dubp-currency-params 0.2.0", @@ -627,11 +653,11 @@ dependencies = [ "durs-network 0.3.0-dev", "durs-network-documents 0.4.0", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "fern 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", - "simplelog 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", "threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "unwrap 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -910,6 +936,15 @@ name = "fake-simd" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "fern" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "colored 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "flate2" version = "1.0.11" @@ -1250,6 +1285,14 @@ dependencies = [ "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "num-traits" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "num-traits" version = "0.2.8" @@ -1708,6 +1751,11 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rgb" +version = "0.8.14" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "ring" version = "0.16.9" @@ -1911,16 +1959,6 @@ dependencies = [ "syn 0.12.15 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "simplelog" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", - "term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "slab" version = "0.4.2" @@ -2034,16 +2072,6 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "term" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", - "dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "termcolor" version = "0.3.6" @@ -2330,6 +2358,17 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "winconsole" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cgmath 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rgb 0.8.14 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ws" version = "0.9.0" @@ -2363,6 +2402,7 @@ dependencies = [ "checksum adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7e522997b529f05601e05166c07ed17789691f562762c7f3b987263d2dedee5c" "checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d" "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +"checksum approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08abcc3b4e9339e33a3d0a5ed15d84a687350c05689d825e0f6655eef9e76a94" "checksum arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee" "checksum arrayvec 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b8d73f9beda665eaa98ab9e4f7442bd4e7de6652587de55b2525e52e29c1b0ba" "checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90" @@ -2384,11 +2424,13 @@ dependencies = [ "checksum c2-chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7d64d04786e0f528460fc884753cf8dddcc466be308f6026f8e355c41a0e4101" "checksum cc 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)" = "8dae9c4b8fedcae85592ba623c4fd08cfdab3e3b72d6df780c6ead964a69bfff" "checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" +"checksum cgmath 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)" = "64a4b57c8f4e3a2e9ac07e0f6abc9c24b6fc9e1b54c3478cfb598f3d0023e51c" "checksum chacha20-poly1305-aead 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77d2058ba29594f69c75e8a9018e0485e3914ca5084e3613cd64529042f5423b" "checksum chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "77d81f58b7301084de3b958691458a53c3f7e0b1d702f77e550b6a88e3a88abe" "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" "checksum clear_on_drop 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "97276801e127ffb46b66ce23f35cc96bd454fa311294bced4bbace7baa8b1d17" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +"checksum colored 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6cdb90b60f2927f8d76139c72dbde7e10c3a2bc47c8594c9c7a66529f2687c03" "checksum constant_time_eq 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "995a44c877f9212528ccc74b21a232f66ad69001e40ede5bcee2ac9ef2657120" "checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" "checksum crossbeam-deque 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b18cd2e169ad86297e6bc0ad9aa679aee9daa4f19e8163860faf7c164e4f5a71" @@ -2404,6 +2446,7 @@ dependencies = [ "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" "checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" +"checksum fern 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)" = "29d26fa0f4d433d1956746e66ec10d6bf4d6c8b93cd39965cceea7f7cc78c7dd" "checksum flate2 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "2adaffba6388640136149e18ed080b77a78611c1e1d6de75aedcdf78df5d4682" "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" "checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" @@ -2445,6 +2488,7 @@ dependencies = [ "checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945" "checksum nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6" "checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09" +"checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" "checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32" "checksum num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcef43580c035376c0705c42792c294b66974abbfd2789b511784023f71f3273" "checksum numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" @@ -2495,6 +2539,7 @@ dependencies = [ "checksum regex 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88c3d9193984285d544df4a30c23a4e62ead42edf70a4452ceb76dac1ce05c26" "checksum regex-syntax 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b143cceb2ca5e56d5671988ef8b15615733e7ee16cd348e064333b251b89343f" "checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e" +"checksum rgb 0.8.14 (registry+https://github.com/rust-lang/crates.io-index)" = "2089e4031214d129e201f8c3c8c2fe97cd7322478a0d1cdf78e7029b0042efdb" "checksum ring 0.16.9 (registry+https://github.com/rust-lang/crates.io-index)" = "6747f8da1f2b1fabbee1aaa4eb8a11abf9adef0bf58a41cee45db5d59cecdfac" "checksum rkv 0.9.7 (registry+https://github.com/rust-lang/crates.io-index)" = "4f9d6a4dd60be13a62ae1d19df68c0c85d77bbee3749b62bf35c49f207d3d750" "checksum rpassword 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b273c91bd242ca03ad6d71c143b6f17a48790e61f21a6c78568fa2b6774a24a4" @@ -2516,7 +2561,6 @@ dependencies = [ "checksum sha-1 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "23962131a91661d643c98940b20fcaffe62d776a823247be80a48fcb8b6fce68" "checksum sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4d8bfd0e469f417657573d8451fb33d16cfe0989359b93baf3a1ffc639543d" "checksum shrinkwraprs 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7d5f047b90b2ca2d1526ff73d67cba61f86f4cf9a8afddc99dd96702ded8e684" -"checksum simplelog 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e95345f185d5adeb8ec93459d2dc99654e294cc6ccf5b75414d8ea262de9a13" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum smallvec 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ab606a9c5e214920bb66c458cd7be8ef094f813f20fe77a54cc7dbfff220d4b7" "checksum sourcefile 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4bf77cb82ba8453b42b6ae1d692e4cdc92f9a47beaf89a847c8be83f4e328ad3" @@ -2531,7 +2575,6 @@ dependencies = [ "checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f" "checksum tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "15f2b5fb00ccdf689e0149d1b1b3c03fead81c2b37735d812fa8bddbbf41b6d8" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" -"checksum term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "edd106a334b7657c10b7c540a0106114feadeb4dc314513e97df481d5d966f42" "checksum termcolor 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "adc4587ead41bf016f11af03e55a624c06568b5a19db4e90fde573d805074f83" "checksum termion 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6a8fb22f7cde82c8220e5aeacb3258ed7ce996142c77cba193f203515e26c330" "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" @@ -2570,5 +2613,6 @@ dependencies = [ "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum wincolor 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eeb06499a3a4d44302791052df005d5232b927ed1a9658146d842165c4de7767" +"checksum winconsole 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ef84b96d10db72dd980056666d7f1e7663ce93d82fa33b63e71c966f4cf5032" "checksum ws 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8a6f5bb86663ff4d1639408410f50bf6050367a8525d644d49a6894cd618a631" "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" diff --git a/lib/core/core/Cargo.toml b/lib/core/core/Cargo.toml index 555fd03a..ba34ef37 100644 --- a/lib/core/core/Cargo.toml +++ b/lib/core/core/Cargo.toml @@ -10,6 +10,7 @@ edition = "2018" path = "src/lib.rs" [dependencies] +chrono = "0.4" clear_on_drop = "0.2.3" dirs = "1.0.2" durs-blockchain = { path = "../../modules/blockchain/blockchain" } @@ -24,11 +25,11 @@ durs-module = { path = "../module" } durs-network = { path = "../network" } durs-network-documents = { path = "../../dunp/network-documents" } failure = "0.1.5" +fern = { version = "0.5.8", features = ["colored"] } log = "0.4.*" serde = "1.0.*" serde_derive = "1.0.*" serde_json = "1.0.*" -simplelog = "0.5.*" structopt= "0.2.*" threadpool = "1.7.*" unwrap = "1.2.1" diff --git a/lib/core/core/src/logger.rs b/lib/core/core/src/logger.rs index 050200cc..e7285b9c 100644 --- a/lib/core/core/src/logger.rs +++ b/lib/core/core/src/logger.rs @@ -17,9 +17,9 @@ use crate::commands::DursCoreOptions; use failure::Fail; +use fern::colors::{Color, ColoredLevelConfig}; use log::{Level, SetLoggerError}; -use simplelog::{CombinedLogger, Config, TermLogger, WriteLogger}; -use std::fs::{File, OpenOptions}; +use std::fs::File; use std::path::PathBuf; #[derive(Debug, Fail)] @@ -65,36 +65,53 @@ pub fn init( File::create(log_file_path_str).map_err(InitLoggerError::FailCreateLogFile)?; } - // Open log file - let file_logger_opts = OpenOptions::new() - .write(true) - .append(true) - .open(log_file_path_str) - .map_err(InitLoggerError::FailOpenLogFile)?; - // Get log level filter let logs_level_filter = durs_core_opts .logs_level .unwrap_or(Level::Info) .to_level_filter(); - // Config logger - let logger_config = Config { + // Config loggers + let loggers_common_config = fern::Dispatch::new() + .level(logs_level_filter) + .level_for("ws::", log::LevelFilter::Off) + .format(|out, message, record| { + let colors = ColoredLevelConfig::new() + .info(Color::Green) + .debug(Color::Cyan); + out.finish(format_args!( + "{}[{}][{}] {}", + chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"), + record.target(), + colors.color(record.level()), + message + )) + }); + let file_config = fern::Dispatch::new() + .chain(fern::log_file(log_file_path_str).map_err(InitLoggerError::FailOpenLogFile)?); + let term_config = fern::Dispatch::new().chain(std::io::stdout()); + + /*let logger_config = Config { time: Some(Level::Error), level: Some(Level::Error), target: Some(Level::Debug), location: Some(Level::Debug), time_format: Some("%Y-%m-%d %H:%M:%S%:z"), - }; + };*/ if durs_core_opts.log_stdout { - CombinedLogger::init(vec![ - TermLogger::new(logs_level_filter, logger_config) - .ok_or(InitLoggerError::FailCreateTermLogger)?, - WriteLogger::new(logs_level_filter, logger_config, file_logger_opts), - ])?; + loggers_common_config + .chain(file_config) + .chain(term_config) + .apply()?; + /*CombinedLogger::init(vec![ + TermLogger::new(logs_level_filter, logger_config) + .ok_or(InitLoggerError::FailCreateTermLogger)?, + WriteLogger::new(logs_level_filter, logger_config, file_logger_opts), + ])?;*/ } else { - WriteLogger::init(logs_level_filter, logger_config, file_logger_opts)?; + loggers_common_config.chain(file_config).apply()?; + //WriteLogger::init(logs_level_filter, logger_config, file_logger_opts)?; } info!( diff --git a/lib/modules/ws2p/ws2p/tests/connection_negociation.rs b/lib/modules/ws2p/ws2p/tests/connection_negociation.rs index 6bd46cd9..88d9a79d 100644 --- a/lib/modules/ws2p/ws2p/tests/connection_negociation.rs +++ b/lib/modules/ws2p/ws2p/tests/connection_negociation.rs @@ -16,7 +16,6 @@ use dubp_currency_params::CurrencyName; use dup_crypto::keys::KeyPair; use dup_crypto::keys::*; -//use durs_common_tests_tools::logger::init_logger_stdout; use durs_message::DursMsg; use durs_network_documents::network_endpoint::*; use durs_network_documents::*; @@ -29,11 +28,19 @@ use durs_ws2p_protocol::controller::{WS2PControllerEvent, WebsocketActionOrder}; use durs_ws2p_protocol::orchestrator::OrchestratorMsg; use durs_ws2p_protocol::MySelfWs2pNode; use std::sync::mpsc; +use std::sync::Once; use std::thread; use std::time::Duration; -pub static TIMEOUT_IN_MS: &'static u64 = &20_000; -pub static PORT: &'static u16 = &10899; +static PORT: &'static u16 = &10899; +static SETUP: Once = Once::new(); +static TIMEOUT_IN_MS: &'static u64 = &20_000; + +fn setup() { + SETUP.call_once(|| { + durs_common_tests_tools::logger::init_logger_stdout(vec!["ws"]); + }); +} pub fn currency() -> CurrencyName { CurrencyName(String::from("g1")) @@ -75,7 +82,7 @@ fn client_node() -> MySelfWs2pNode { #[test] #[cfg(unix)] fn test_connection_negociation_denial() { - //init_logger_stdout(); + setup(); // ===== initialization ===== // client and server are initialized and launched in separate threads @@ -152,7 +159,7 @@ fn test_connection_negociation_denial() { #[test] #[cfg(unix)] fn test_connection_negociation_success() { - //init_logger_stdout(); + setup(); // ===== initialization ===== // client and server are initialized and launched in separate threads diff --git a/lib/tests-tools/common-tests-tools/Cargo.toml b/lib/tests-tools/common-tests-tools/Cargo.toml index e7fe4124..9d4d73f1 100644 --- a/lib/tests-tools/common-tests-tools/Cargo.toml +++ b/lib/tests-tools/common-tests-tools/Cargo.toml @@ -13,7 +13,8 @@ edition = "2018" path = "src/lib.rs" [dependencies] +chrono = "0.4" +fern = { version = "0.5.8", features = ["colored"] } log = "0.4.*" -simplelog = "0.5.*" [dev-dependencies] diff --git a/lib/tests-tools/common-tests-tools/src/logger.rs b/lib/tests-tools/common-tests-tools/src/logger.rs index 29c43aea..7eadbd24 100644 --- a/lib/tests-tools/common-tests-tools/src/logger.rs +++ b/lib/tests-tools/common-tests-tools/src/logger.rs @@ -15,11 +15,11 @@ //! Common test tools for DURS project. -use log::Level; -use simplelog::{Config, LevelFilter, SimpleLogger, TermLogger}; +use fern::colors::{Color, ColoredLevelConfig}; +use log::LevelFilter; -/// Initialize simple stdout logger -pub fn init_logger_stdout() { +/// Initialize stdout logger +pub fn init_logger_stdout(off_targets: Vec<&'static str>) { let colors = match std::env::var("DURS_TESTS_LOG_COLOR") .unwrap_or_else(|_| String::from("no")) .as_str() @@ -49,19 +49,38 @@ pub fn init_logger_stdout() { }; // Config logger - let logger_config = Config { - time: Some(Level::Error), - level: Some(Level::Error), - target: Some(Level::Debug), - location: Some(Level::Debug), - time_format: Some("%Y-%m-%d %H:%M:%S%:z"), - }; + let mut logger_config = fern::Dispatch::new() + .level(level_filter) + .format(move |out, message, record| { + if colors { + let colors_config = ColoredLevelConfig::new() + .info(Color::Green) + .debug(Color::Cyan); + out.finish(format_args!( + "{}[{}][{}] {}", + chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"), + record.target(), + colors_config.color(record.level()), + message + )) + } else { + out.finish(format_args!( + "{}[{}][{}] {}", + chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"), + record.target(), + record.level(), + message + )) + } + }) + .chain(std::io::stdout()); - // Active stdout logger - if colors { - TermLogger::init(level_filter, logger_config).expect("TESTS: fail to init stdout logger !"); - } else { - SimpleLogger::init(level_filter, logger_config) - .expect("TESTS: fail to init stdout logger !"); + for target in off_targets { + logger_config = logger_config.level_for(target, LevelFilter::Off); } + + // Active stdout logger + logger_config + .apply() + .expect("TESTS: fail to init stdout logger !"); } -- GitLab