Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Switch prints to tracing #55

Merged
merged 5 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
16 changes: 14 additions & 2 deletions examples/src/spooning.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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) = {
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions workspaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"] }
14 changes: 7 additions & 7 deletions workspaces/src/network/server.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -20,15 +19,15 @@ 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:
let _ = std::fs::remove_dir_all(&home_dir);
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(())
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion workspaces/tests/create_account.rs
Original file line number Diff line number Diff line change
@@ -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?;
Expand Down
4 changes: 2 additions & 2 deletions workspaces/tests/deploy.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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)?;
Expand Down
6 changes: 3 additions & 3 deletions workspaces/tests/patch_state.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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?;
Expand All @@ -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?;
Expand Down