From 1ceb49812cb2df0867d5bb28a072e9fe51ba1e44 Mon Sep 17 00:00:00 2001
From: Javier Viola <pepoviola@gmail.com>
Date: Wed, 8 Nov 2023 07:37:25 -0300
Subject: [PATCH] feat: add basic logger/tracing, replacing `printlns` (#132)

Adding `tracing` and replace all the `println`. We should add more info
in a followup pr.
Thx!
---
 Cargo.toml                                    |  1 +
 crates/orchestrator/Cargo.toml                |  1 +
 .../orchestrator/src/generators/chain_spec.rs | 27 ++-----------------
 crates/orchestrator/src/lib.rs                | 16 ++++-------
 crates/orchestrator/src/network/parachain.rs  |  5 ++--
 .../src/network_helper/verifier.rs            |  4 +--
 crates/orchestrator/src/spawner.rs            | 21 ++++++++-------
 crates/orchestrator/src/tx_helper/balance.rs  |  0
 .../src/tx_helper/register_para.rs            |  5 ++--
 .../src/tx_helper/validator_actions.rs        | 13 +++------
 crates/provider/Cargo.toml                    |  3 ++-
 crates/provider/src/native/namespace.rs       | 12 ++++++---
 12 files changed, 43 insertions(+), 65 deletions(-)
 create mode 100644 crates/orchestrator/src/tx_helper/balance.rs

diff --git a/Cargo.toml b/Cargo.toml
index 90b06f5..d0ea384 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -36,3 +36,4 @@ sp-core = "22.0.0"
 libp2p = { version = "0.52" }
 subxt = "0.32.0"
 subxt-signer = { version = "0.32.0", features = ["subxt"]}
+tracing = "0.1.35"
diff --git a/crates/orchestrator/Cargo.toml b/crates/orchestrator/Cargo.toml
index 0b51dfe..12c10cb 100644
--- a/crates/orchestrator/Cargo.toml
+++ b/crates/orchestrator/Cargo.toml
@@ -26,3 +26,4 @@ libp2p = { workspace = true }
 subxt = { workspace = true }
 subxt-signer = { workspace = true }
 reqwest = { workspace = true }
+tracing = { workspace = true }
diff --git a/crates/orchestrator/src/generators/chain_spec.rs b/crates/orchestrator/src/generators/chain_spec.rs
index 11a50c8..5cfd5e6 100644
--- a/crates/orchestrator/src/generators/chain_spec.rs
+++ b/crates/orchestrator/src/generators/chain_spec.rs
@@ -11,6 +11,7 @@ use provider::{
 };
 use serde_json::json;
 use support::fs::FileSystem;
+use tracing::warn;
 
 use super::errors::GeneratorError;
 use crate::{
@@ -399,32 +400,12 @@ impl ChainSpec {
                 }
             }
 
-            // TODO: move to logger
-            // println!(
-            //     "{:#?}",
-            //     chain_spec_json.pointer(format!("{}/session/keys", pointer).as_str())
-            // );
             // Clear authorities
             clear_authorities(&pointer, &mut chain_spec_json);
 
-            // TODO: move to logger
-            // println!(
-            //     "{:#?}",
-            //     chain_spec_json.pointer(format!("{}/session/keys", pointer).as_str())
-            // );
-
-            // TODO: add to logger
-            // println!("BALANCES");
-            // println!("{:#?}", chain_spec_json.pointer(format!("{}/balances",pointer).as_str()));
             // add balances
             add_balances(&pointer, &mut chain_spec_json, &relaychain.nodes, 0);
 
-            // TODO: move to logger
-            // println!(
-            //     "{:#?}",
-            //     chain_spec_json.pointer(format!("{}/balances", pointer).as_str())
-            // );
-
             // Get validators to add as authorities
             let validators: Vec<&NodeSpec> = relaychain
                 .nodes
@@ -447,10 +428,6 @@ impl ChainSpec {
 
             // staking && nominators
 
-            // TODO: add to logger
-            // println!("KEYS");
-            // println!("{:#?}", chain_spec_json.pointer(format!("{}/session/keys",pointer).as_str()));
-
             // add_hrmp_channels
 
             // paras
@@ -674,7 +651,7 @@ fn add_balances(
     if let Some(val) = chain_spec_json.pointer_mut(runtime_config_ptr) {
         let Some(balances) = val.pointer("/balances/balances") else {
             // should be a info log
-            println!("NO 'balances' key in runtime config, skipping...");
+            warn!("NO 'balances' key in runtime config, skipping...");
             return;
         };
 
diff --git a/crates/orchestrator/src/lib.rs b/crates/orchestrator/src/lib.rs
index a705b72..2a8cf4c 100644
--- a/crates/orchestrator/src/lib.rs
+++ b/crates/orchestrator/src/lib.rs
@@ -21,12 +21,12 @@ use network_spec::{parachain::ParachainSpec, NetworkSpec};
 use provider::{constants::LOCALHOST, types::TransferedFile, Provider};
 use support::fs::{FileSystem, FileSystemError};
 use tokio::time::timeout;
+use tracing::{debug, info};
 
 use crate::{
     generators::chain_spec::ParaGenesisConfig, shared::types::RegisterParachainOptions,
     spawner::SpawnNodeCtx,
 };
-
 pub struct Orchestrator<T, P>
 where
     T: FileSystem + Sync + Send,
@@ -68,15 +68,13 @@ where
         mut network_spec: NetworkSpec,
     ) -> Result<Network<T>, OrchestratorError> {
         // main driver for spawn the network
-        // TODO: move to logger
-        // println!("{:#?}", network_spec);
+        debug!("Network spec to spawn, {:#?}", network_spec);
 
         // create namespace
         let ns = self.provider.create_namespace().await?;
 
-        println!("\n\n");
-        println!("🧰 ns: {:#?}", ns.name());
-        println!("🧰 base_dir: {:#?}", ns.base_dir());
+        info!("🧰 ns: {}", ns.name());
+        info!("🧰 base_dir: {:?}", ns.base_dir());
 
         // TODO: noop for native
         // Static setup
@@ -91,9 +89,6 @@ where
             .build(&ns, &scoped_fs)
             .await?;
 
-        // TODO: move to logger
-        // println!("{:#?}", network_spec.relaychain.chain_spec);
-
         // Create parachain artifacts (chain-spec, wasm, state)
         let relay_chain_id = network_spec
             .relaychain
@@ -106,8 +101,7 @@ where
             let para_cloned = para.clone();
             let chain_spec_raw_path = if let Some(chain_spec) = para.chain_spec.as_mut() {
                 chain_spec.build(&ns, &scoped_fs).await?;
-                // TODO: move to logger
-                // println!("{:#?}", chain_spec);
+                debug!("chain_spec: {:#?}", chain_spec);
 
                 chain_spec
                     .customize_para(&para_cloned, &relay_chain_id, &scoped_fs)
diff --git a/crates/orchestrator/src/network/parachain.rs b/crates/orchestrator/src/network/parachain.rs
index bd2b35a..0a3c54d 100644
--- a/crates/orchestrator/src/network/parachain.rs
+++ b/crates/orchestrator/src/network/parachain.rs
@@ -6,6 +6,7 @@ use std::{
 use subxt::{dynamic::Value, OnlineClient, SubstrateConfig};
 use subxt_signer::{sr25519::Keypair, SecretUri};
 use support::fs::FileSystem;
+use tracing::info;
 
 // use crate::generators::key::generate_pair;
 // use sp_core::{sr25519, Pair};
@@ -50,7 +51,7 @@ impl Parachain {
         options: RegisterParachainOptions,
         scoped_fs: &ScopedFilesystem<'_, impl FileSystem>,
     ) -> Result<(), anyhow::Error> {
-        println!("Registering parachain: {:?}", options);
+        info!("Registering parachain: {:?}", options);
         // get the seed
         let sudo: Keypair;
         if let Some(possible_seed) = options.seed {
@@ -100,7 +101,7 @@ impl Parachain {
             .await?;
 
         let result = result.wait_for_in_block().await?;
-        println!("In block: {:#?}", result.block_hash());
+        info!("In block: {:#?}", result.block_hash());
         Ok(())
     }
 }
diff --git a/crates/orchestrator/src/network_helper/verifier.rs b/crates/orchestrator/src/network_helper/verifier.rs
index 12d41cb..9da71e5 100644
--- a/crates/orchestrator/src/network_helper/verifier.rs
+++ b/crates/orchestrator/src/network_helper/verifier.rs
@@ -1,6 +1,7 @@
 use std::time::Duration;
 
 use tokio::time::timeout;
+use tracing::trace;
 
 use crate::network::node::NetworkNode;
 
@@ -18,8 +19,7 @@ async fn check_nodes(nodes: &[&NetworkNode]) {
         let tasks: Vec<_> = nodes
             .iter()
             .map(|node| {
-                // TODO: move to logger
-                // println!("getting from {}", node.name);
+                trace!("🔎 checking node: {} ", node.name);
                 reqwest::get(node.prometheus_uri.clone())
             })
             .collect();
diff --git a/crates/orchestrator/src/spawner.rs b/crates/orchestrator/src/spawner.rs
index 18db51f..b722aed 100644
--- a/crates/orchestrator/src/spawner.rs
+++ b/crates/orchestrator/src/spawner.rs
@@ -6,6 +6,7 @@ use provider::{
     DynNamespace,
 };
 use support::fs::FileSystem;
+use tracing::info;
 
 use crate::{
     generators,
@@ -124,9 +125,12 @@ where
                               * ZombieRole::Companion => todo!(), */
     };
 
-    println!("\n");
-    println!("🚀 {}, spawning.... with command:", node.name);
-    println!("{program} {}", args.join(" "));
+    info!(
+        "🚀 {}, spawning.... with command: {} {}",
+        node.name,
+        program,
+        args.join(" ")
+    );
 
     let spawn_ops = SpawnNodeOptions {
         name: node.name.clone(),
@@ -150,14 +154,13 @@ where
 
     let ws_uri = format!("ws://{}:{}", LOCALHOST, node.rpc_port.0);
     let prometheus_uri = format!("http://{}:{}/metrics", LOCALHOST, node.prometheus_port.0);
-    println!("🚀 {}, should be running now", node.name);
-    println!(
-        "🚀 {} : direct link https://polkadot.js.org/apps/?rpc={ws_uri}#/explorer",
+    info!("🚀 {}, should be running now", node.name);
+    info!(
+        "🚀 {}: direct link https://polkadot.js.org/apps/?rpc={ws_uri}#/explorer",
         node.name
     );
-    println!("🚀 {} : metrics link {prometheus_uri}", node.name);
-    println!("📓 logs cmd: tail -f {}/{}.log", base_dir, node.name);
-    println!("\n");
+    info!("🚀 {}: metrics link {prometheus_uri}", node.name);
+    info!("📓 logs cmd: tail -f {}/{}.log", base_dir, node.name);
     Ok(NetworkNode::new(
         node.name.clone(),
         ws_uri,
diff --git a/crates/orchestrator/src/tx_helper/balance.rs b/crates/orchestrator/src/tx_helper/balance.rs
new file mode 100644
index 0000000..e69de29
diff --git a/crates/orchestrator/src/tx_helper/register_para.rs b/crates/orchestrator/src/tx_helper/register_para.rs
index 55aaf6d..d9e19ab 100644
--- a/crates/orchestrator/src/tx_helper/register_para.rs
+++ b/crates/orchestrator/src/tx_helper/register_para.rs
@@ -5,13 +5,14 @@ use subxt_signer::{sr25519::Keypair, SecretUri};
 use support::fs::FileSystem;
 
 use crate::{shared::types::RegisterParachainOptions, ScopedFilesystem};
+use tracing::{debug, info, trace};
 
 
 pub async fn register(
     options: RegisterParachainOptions,
     scoped_fs: &ScopedFilesystem<'_, impl FileSystem>,
 ) -> Result<(), anyhow::Error> {
-    println!("Registering parachain: {:?}", options);
+    debug!("Registering parachain: {:?}", options);
     // get the seed
     let sudo: Keypair;
     if let Some(possible_seed) = options.seed {
@@ -61,6 +62,6 @@ pub async fn register(
         .await?;
 
     let result = result.wait_for_in_block().await?;
-    println!("In block: {:#?}", result.block_hash());
+    debug!("In block: {:#?}", result.block_hash());
     Ok(())
 }
\ No newline at end of file
diff --git a/crates/orchestrator/src/tx_helper/validator_actions.rs b/crates/orchestrator/src/tx_helper/validator_actions.rs
index ca01d8a..cbcc8aa 100644
--- a/crates/orchestrator/src/tx_helper/validator_actions.rs
+++ b/crates/orchestrator/src/tx_helper/validator_actions.rs
@@ -2,13 +2,14 @@ use std::str::FromStr;
 
 use subxt::{dynamic::Value, OnlineClient, SubstrateConfig};
 use subxt_signer::{sr25519::Keypair, SecretUri};
+use tracing::{debug, info, trace};
 
 
 pub async fn register(
     validator_ids: Vec<String>,
     node_ws_url: &str,
 ) -> Result<(), anyhow::Error> {
-    println!("Registering validators: {:?}", validator_ids);
+    debug!("Registering validators: {:?}", validator_ids);
     // get the seed
     // let sudo: Keypair;
     // if let Some(possible_seed) = options.seed {
@@ -18,12 +19,7 @@ pub async fn register(
         let sudo = Keypair::from_uri(&uri)?;
     // }
 
-    println!("pse");
     let api = OnlineClient::<SubstrateConfig>::from_url(node_ws_url).await?;
-    println!("pse connected");
-
-    // let bytes: Vec<Value> = validator_ids.iter().map(|id| Value::from_bytes(id)).collect();
-    // println!("{:?}", bytes);
 
     let register_call = subxt::dynamic::tx(
         "ValidatorManager",
@@ -33,7 +29,6 @@ pub async fn register(
 
     let sudo_call = subxt::dynamic::tx("Sudo", "sudo", vec![register_call.into_value()]);
 
-    println!("pse1");
     // TODO: uncomment below and fix the sign and submit (and follow afterwards until
     // finalized block) to register the parachain
     let result = api
@@ -41,8 +36,8 @@ pub async fn register(
         .sign_and_submit_then_watch_default(&sudo_call, &sudo)
         .await?;
 
-    println!("result: {:#?}", result);
+    debug!("result: {:#?}", result);
     let result = result.wait_for_in_block().await?;
-    println!("In block: {:#?}", result.block_hash());
+    debug!("In block: {:#?}", result.block_hash());
     Ok(())
 }
\ No newline at end of file
diff --git a/crates/provider/Cargo.toml b/crates/provider/Cargo.toml
index 374a1eb..367355d 100644
--- a/crates/provider/Cargo.toml
+++ b/crates/provider/Cargo.toml
@@ -24,4 +24,5 @@ tokio = { workspace = true, features = [
 thiserror = { workspace = true }
 anyhow = { workspace = true }
 uuid = { workspace = true, features = ["v4"] }
-nix = { workspace = true, features = ["signal"] }
\ No newline at end of file
+nix = { workspace = true, features = ["signal"] }
+tracing = { workspace = true }
diff --git a/crates/provider/src/native/namespace.rs b/crates/provider/src/native/namespace.rs
index 33d3bef..ef5e6fb 100644
--- a/crates/provider/src/native/namespace.rs
+++ b/crates/provider/src/native/namespace.rs
@@ -9,6 +9,7 @@ use async_trait::async_trait;
 use futures::{future::try_join_all, try_join};
 use support::{fs::FileSystem, process::ProcessManager};
 use tokio::sync::RwLock;
+use tracing::trace;
 use uuid::Uuid;
 
 use super::{
@@ -189,10 +190,13 @@ where
             local_output_path,
         } in options.commands
         {
-            // TODO: move to logger
-            // println!("{:#?}, {:#?}", command, args);
-            // println!("{:#?}", self.base_dir.to_string_lossy());
-            // println!("{:#?}", local_output_path.as_os_str());
+            trace!(
+                "🏗  building file {:?} in path {} with command {} {}",
+                local_output_path.as_os_str(),
+                self.base_dir.to_string_lossy(),
+                program,
+                args.join(" ")
+            );
             let local_output_full_path = format!(
                 "{}{}{}",
                 self.base_dir.to_string_lossy(),
-- 
GitLab