radicle: expose shared logger

To be able to use log setup across the different components of
heartwood, expose a `logger` module from `radicle`. This also includes
a `test` logger.

These modules are guarded behind a `logger` feature flag, so that
other crates are not bringing any uneeded dependencies.

This allows logging to be setup in `radicle-cli-test` so that when a
test fails, the log output is present for debugging purposes.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny 2023-12-19 15:44:07 +00:00 committed by cloudhead
parent f9d9bdf8cd
commit 47a440a384
No known key found for this signature in database
11 changed files with 42 additions and 3 deletions

3
Cargo.lock generated
View File

@ -2354,6 +2354,8 @@ name = "radicle"
version = "0.2.0"
dependencies = [
"amplify",
"chrono",
"colored",
"crossbeam-channel",
"cyphernet",
"fastrand",
@ -2430,6 +2432,7 @@ dependencies = [
"escargot",
"log",
"pretty_assertions",
"radicle",
"shlex",
"snapbox",
"thiserror",

View File

@ -21,3 +21,8 @@ pretty_assertions = { version = "1.3.0" }
shlex = { version = "1.1.0" }
snapbox = { version = "0.4.3" }
thiserror = { version = "1" }
[dependencies.radicle]
path = "../radicle"
version = "0.2.0"
features = ["logger", "test"]

View File

@ -192,6 +192,10 @@ impl TestFormula {
// We don't need to re-build everytime the `build` function is called. Once is enough.
BUILD.call_once(|| {
use escargot::format::Message;
use radicle::logger::env_level;
use radicle::logger::test as logger;
logger::init(env_level().unwrap_or(log::Level::Debug));
for (package, binary) in binaries {
log::debug!(target: "test", "Building binaries for package `{package}`..");

View File

@ -42,6 +42,7 @@ thiserror = { version = "1" }
[dependencies.radicle]
path = "../radicle"
version = "0.2.0"
features = ["logger"]
[dependencies.radicle-fetch]
path = "../radicle-fetch"

View File

@ -1,7 +1,6 @@
pub mod bounded;
pub mod control;
pub mod deserializer;
pub mod logger;
pub mod runtime;
pub mod service;
pub mod signals;

View File

@ -4,12 +4,13 @@ use std::{env, fs, net, path::PathBuf, process};
use anyhow::Context;
use crossbeam_channel as chan;
use radicle::logger;
use radicle::prelude::Signer;
use radicle::profile;
use radicle::version;
use radicle_node::crypto::ssh::keystore::{Keystore, MemorySigner};
use radicle_node::signals;
use radicle_node::Runtime;
use radicle_node::{logger, signals};
pub const NAME: &str = "radicle-node";
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

View File

@ -2,9 +2,9 @@ pub mod arbitrary;
pub mod environment;
pub mod gossip;
pub mod handle;
pub mod logger;
pub mod peer;
pub mod simulator;
pub use radicle::assert_matches;
pub use radicle::logger::test as logger;
pub use radicle::test::*;

View File

@ -11,6 +11,7 @@ edition = "2021"
[features]
default = []
test = ["qcheck", "radicle-crypto/test"]
logger = ["colored", "chrono"]
[dependencies]
amplify = { version = "4.0.0", default-features = false, features = ["std"] }
@ -31,6 +32,16 @@ tempfile = { version = "3.3.0" }
thiserror = { version = "1" }
unicode-normalization = { version = "0.1" }
[dependencies.chrono]
version = "0.4.0"
default-features = false
features = ["clock"]
optional = true
[dependencies.colored]
version = "1.9.0"
optional = true
[dependencies.git2]
version = "0.18.1"
default-features = false

View File

@ -15,6 +15,8 @@ pub mod cob;
pub mod collections;
pub mod git;
pub mod identity;
#[cfg(feature = "logger")]
pub mod logger;
pub mod node;
pub mod profile;
pub mod rad;

View File

@ -1,4 +1,10 @@
//! Logging module.
//!
//! For test logging see [`test`].
#[cfg(feature = "test")]
pub mod test;
use std::io::{self, Write};
use chrono::prelude::*;
@ -55,3 +61,10 @@ pub fn init(level: Level) -> Result<(), SetLoggerError> {
Ok(())
}
/// Get the level set by the environment variable `RUST_LOG`, if
/// present.
pub fn env_level() -> Option<Level> {
let level = std::env::var("RUST_LOG").ok()?;
level.parse().ok()
}