Skip to content

Commit

Permalink
logging for tests too
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Dec 22, 2023
1 parent cb9a5e9 commit 848ca93
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions xps-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ async-trait = "0.1"
jsonrpsee = { version = "0.21", features = ["macros", "server", "client-core"] }
anyhow = "1"
thiserror = "1"
ctor = "0.2"
13 changes: 2 additions & 11 deletions xps-gateway/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
mod rpc;
mod types;
mod util;

use anyhow::Result;
use jsonrpsee::server::Server;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};

pub use crate::rpc::{XpsMethods, XpsServer};

/// Entrypoint for the xps Gateway
pub async fn run() -> Result<()> {
init_logging();
crate::util::init_logging();

// a port of 0 allows the OS to choose an open port
let server = Server::builder().build("127.0.0.1:0").await?;
Expand All @@ -20,12 +20,3 @@ pub async fn run() -> Result<()> {
handle.stopped().await;
Ok(())
}

/// Start [`tracing`] logging
pub(crate) fn init_logging() {
let fmt = fmt::layer().compact();
Registry::default()
.with(EnvFilter::from_default_env())
.with(fmt)
.init()
}
28 changes: 28 additions & 0 deletions xps-gateway/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Internal Utility functions for use in crate

#[cfg(test)]
use std::sync::Once;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};

#[cfg(test)]
static INIT: Once = Once::new();

pub(crate) fn init_logging() {
let fmt = fmt::layer().compact();
Registry::default()
.with(EnvFilter::from_default_env())
.with(fmt)
.init()
}

#[cfg(test)]
#[ctor::ctor]
fn __init_test_logging() {
INIT.call_once(|| {
let fmt = fmt::layer().compact();
Registry::default()
.with(EnvFilter::from_default_env())
.with(fmt)
.init()
})
}

0 comments on commit 848ca93

Please sign in to comment.