diff --git a/examples/Cargo.toml b/examples/Cargo.toml index faddb0bb..edc859ef 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -12,6 +12,8 @@ near-units = "0.1.0" # arbitrary_precision enabled for u128 types that workspaces requires for Balance types serde_json = { version = "1.0", features = ["arbitrary_precision"] } tokio = { version = "1.10.0", features = ["full"] } +tracing = "0.1" +tracing-subscriber = { version = "0.3.5", features = ["env-filter"] } workspaces = { path = "../workspaces" } [[example]] diff --git a/examples/src/spooning.rs b/examples/src/spooning.rs index 0b73bcf2..aed05c8e 100644 --- a/examples/src/spooning.rs +++ b/examples/src/spooning.rs @@ -1,4 +1,8 @@ use borsh::{self, BorshDeserialize, BorshSerialize}; +use std::env; +use tracing::info; +use tracing_subscriber::filter::LevelFilter; +use tracing_subscriber::EnvFilter; use workspaces::prelude::*; use workspaces::{AccountId, Contract, DevNetwork, Worker}; @@ -59,6 +63,14 @@ async fn deploy_status_contract( #[tokio::main] async fn main() -> anyhow::Result<()> { + // Parse log filters from RUST_LOG or fallback to INFO if empty + let filter = if env::var(EnvFilter::DEFAULT_ENV).is_ok() { + EnvFilter::from_default_env() + } else { + EnvFilter::default().add_directive(LevelFilter::INFO.into()) + }; + tracing_subscriber::fmt().with_env_filter(filter).init(); + // Grab STATE from the testnet status_message contract. This contract contains the following data: // get_status(dev-20211013002148-59466083160385) => "hello from testnet" let (testnet_contract_id, status_msg) = { @@ -75,7 +87,7 @@ async fn main() -> anyhow::Result<()> { (contract_id, status_msg) }; - println!("Testnet: {:?}\n", status_msg); + info!(target: "spooning", "Testnet: {:?}", status_msg); // Create our sandboxed environment and grab a worker to do stuff in it: let worker = workspaces::sandbox(); @@ -106,7 +118,7 @@ async fn main() -> anyhow::Result<()> { .await? .json()?; - println!("New status patched: {:?}", status); + info!(target: "spooning", "New status patched: {:?}", status); assert_eq!(&status, "hello from testnet"); // See that sandbox state was overriden. Grabbing get_status(sandbox_contract_id) should yield Null diff --git a/workspaces/Cargo.toml b/workspaces/Cargo.toml index 9ae5a78f..cc57552a 100644 --- a/workspaces/Cargo.toml +++ b/workspaces/Cargo.toml @@ -23,6 +23,7 @@ serde = "1.0" serde_json = "1.0" tokio = { version = "1", features = ["full"] } tokio-retry = "0.3" +tracing = "0.1" url = { version = "2.2.2", features = ["serde"] } near-account-id = "0.5" @@ -40,3 +41,5 @@ libc = "0.2" [dev-dependencies] borsh = "0.9" +test-log = { version = "0.2.8", default-features = false, features = ["trace"] } +tracing-subscriber = { version = "0.3.5", features = ["env-filter"] } diff --git a/workspaces/src/network/server.rs b/workspaces/src/network/server.rs index e37b41e8..ef26c593 100644 --- a/workspaces/src/network/server.rs +++ b/workspaces/src/network/server.rs @@ -1,8 +1,7 @@ -use std::process::Child; - -use portpicker::pick_unused_port; - use crate::network::Sandbox; +use portpicker::pick_unused_port; +use std::process::Child; +use tracing::info; pub struct SandboxServer { pub(crate) rpc_port: u16, @@ -20,7 +19,7 @@ impl SandboxServer { } pub fn start(&mut self) -> anyhow::Result<()> { - println!("Starting up sandbox at localhost:{}", self.rpc_port); + info!(target: "workspaces", "Starting up sandbox at localhost:{}", self.rpc_port); let home_dir = Sandbox::home_dir(self.rpc_port); // Remove dir if it already exists: @@ -28,7 +27,7 @@ impl SandboxServer { near_sandbox_utils::init(&home_dir)?.wait()?; let child = near_sandbox_utils::run(&home_dir, self.rpc_port, self.net_port)?; - println!("Started sandbox: pid={:?}", child.id()); + info!(target: "workspaces", "Started sandbox: pid={:?}", child.id()); self.process = Some(child); Ok(()) @@ -55,7 +54,8 @@ impl Drop for SandboxServer { let child = self.process.as_mut().unwrap(); - eprintln!( + info!( + target: "workspaces", "Cleaning up sandbox: port={}, pid={}", self.rpc_port, child.id() diff --git a/workspaces/tests/create_account.rs b/workspaces/tests/create_account.rs index 040bd2cc..5e64b53c 100644 --- a/workspaces/tests/create_account.rs +++ b/workspaces/tests/create_account.rs @@ -1,6 +1,7 @@ +use test_log::test; use workspaces::prelude::*; -#[tokio::test] +#[test(tokio::test)] async fn test_subaccount_creation() -> anyhow::Result<()> { let worker = workspaces::sandbox(); let account = worker.dev_create_account().await?; diff --git a/workspaces/tests/deploy.rs b/workspaces/tests/deploy.rs index 9a5f6e6c..741afbf9 100644 --- a/workspaces/tests/deploy.rs +++ b/workspaces/tests/deploy.rs @@ -1,5 +1,5 @@ use serde::{Deserialize, Serialize}; - +use test_log::test; use workspaces::prelude::*; const NFT_WASM_FILEPATH: &str = "../examples/res/non_fungible_token.wasm"; @@ -28,7 +28,7 @@ fn expected() -> NftMetadata { serde_json::from_str(EXPECTED_NFT_METADATA).unwrap() } -#[tokio::test] +#[test(tokio::test)] async fn test_dev_deploy() -> anyhow::Result<()> { let worker = workspaces::sandbox(); let wasm = std::fs::read(NFT_WASM_FILEPATH)?; diff --git a/workspaces/tests/patch_state.rs b/workspaces/tests/patch_state.rs index 8d1a16da..97e80e0b 100644 --- a/workspaces/tests/patch_state.rs +++ b/workspaces/tests/patch_state.rs @@ -1,6 +1,6 @@ use borsh::{self, BorshDeserialize, BorshSerialize}; use serde_json::json; - +use test_log::test; use workspaces::prelude::*; use workspaces::{AccountId, DevNetwork, Worker}; @@ -40,7 +40,7 @@ async fn view_status_state( Ok((contract.id().clone(), status_msg)) } -#[tokio::test] +#[test(tokio::test)] async fn test_view_state() -> anyhow::Result<()> { let worker = workspaces::sandbox(); let (contract_id, status_msg) = view_status_state(worker).await?; @@ -58,7 +58,7 @@ async fn test_view_state() -> anyhow::Result<()> { Ok(()) } -#[tokio::test] +#[test(tokio::test)] async fn test_patch_state() -> anyhow::Result<()> { let worker = workspaces::sandbox(); let (contract_id, mut status_msg) = view_status_state(worker.clone()).await?;