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

[sozo] add trace logs #1867

Merged
merged 28 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
741924c
Added trace logs for Sozo.
btirth Apr 22, 2024
ea535ab
Added Sozo trace logs.
btirth Apr 22, 2024
3a7bcd8
Added Sozo trace logs.
btirth Apr 22, 2024
dddbb05
Merge branch 'dojoengine:main' into 1409-sozo-trace-logs
btirth Apr 23, 2024
843507b
Merge branch 'dojoengine:main' into 1409-sozo-trace-logs
btirth Apr 23, 2024
fc0bdf2
Updated Sozo trace logs.
btirth Apr 23, 2024
1b40f20
Merge branch '1409-sozo-trace-logs' of github.com:btirth/dojo into 14…
btirth Apr 23, 2024
0229235
Updated Sozo trace logs.
btirth Apr 23, 2024
04ec215
Merge branch 'main' into 1409-sozo-trace-logs
btirth Apr 23, 2024
7b8a3fa
Updated Sozo trace logs
btirth Apr 24, 2024
d73c919
Merge branch 'dojoengine:main' into 1409-sozo-trace-logs
btirth Apr 24, 2024
ac24152
Updated Sozo trace logs.
btirth Apr 24, 2024
066dcaa
Updated Sozo trace logs
btirth Apr 24, 2024
2c4124d
Updated Sozo trace logs.
btirth Apr 24, 2024
5bb716a
Removed json for Sozo log.
btirth Apr 25, 2024
b7e0319
Resolved merge conflicts.
btirth Apr 25, 2024
1d66680
Removed LOG_TARGET for Sozo trace logs.
btirth Apr 26, 2024
ecc407f
Merge branch 'dojoengine:main' into 1409-sozo-trace-logs
btirth Apr 26, 2024
4e4f539
Merge branch '1409-sozo-trace-logs' of github.com:btirth/dojo into 14…
btirth Apr 26, 2024
d8ee5ec
Updated Sozo trace logs
btirth Apr 27, 2024
fc2b5c0
Updated Sozo trace logs - removed fetch keyword.
btirth Apr 27, 2024
0a305bc
Resolved merge conflicts.
btirth Apr 27, 2024
b36d0d0
Merge branch 'dojoengine:main' into 1409-sozo-trace-logs
btirth Apr 29, 2024
e1b71a0
Resolved failing checks.
btirth Apr 29, 2024
182ee2e
Updated Sozo trace logs.
btirth Apr 30, 2024
4449b7e
Merge branch 'dojoengine:main' into 1409-sozo-trace-logs
btirth Apr 30, 2024
f68986f
Updated Sozo trace logs.
btirth Apr 30, 2024
a741fa9
fix: ensure sozo commands return expected Result
glihm Apr 30, 2024
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
1 change: 1 addition & 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 bin/sozo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ thiserror.workspace = true
tokio.workspace = true
tracing-log = "0.1.3"
tracing.workspace = true
tracing-subscriber.workspace = true
url.workspace = true
katana-rpc-api.workspace = true

Expand Down
27 changes: 25 additions & 2 deletions bin/sozo/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use scarb_ui::Verbosity;
use smol_str::SmolStr;
use tracing::level_filters::LevelFilter;
use tracing_log::AsTrace;

use tracing::Subscriber;
use tracing_subscriber::{fmt, EnvFilter};
use crate::commands::Commands;
use crate::utils::generate_version;

Expand Down Expand Up @@ -35,6 +36,10 @@ pub struct SozoArgs {
#[arg(help = "Run without accessing the network.")]
pub offline: bool,

#[arg(long)]
#[arg(help = "Output logs in JSON format.")]
pub json_log: bool,
btirth marked this conversation as resolved.
Show resolved Hide resolved

#[command(subcommand)]
pub command: Commands,
}
Expand All @@ -50,6 +55,24 @@ impl SozoArgs {
Verbosity::Quiet
}
}

pub fn init_logging(&self) -> Result<(), Box<dyn std::error::Error>> {
const DEFAULT_LOG_FILTER: &str = "info,server=debug,\
blockifier=off,jsonrpsee_server=off,\
hyper=off,messaging=debug,node=error";
btirth marked this conversation as resolved.
Show resolved Hide resolved

let builder = fmt::Subscriber::builder().with_env_filter(
EnvFilter::try_from_default_env().or(EnvFilter::try_new(DEFAULT_LOG_FILTER))?,
);

let subscriber: Box<dyn Subscriber + Send + Sync> = if self.json_log {
Box::new(builder.json().finish())
} else {
Box::new(builder.finish())
};

Ok(tracing::subscriber::set_global_default(subscriber)?)
}
}

/// Profile specifier.
Expand Down Expand Up @@ -78,4 +101,4 @@ impl ProfileSpec {
_ => Profile::default(),
})
}
}
}
35 changes: 33 additions & 2 deletions bin/sozo/src/commands/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use super::options::fee::FeeOptions;
use super::options::signer::SignerOptions;
use super::options::starknet::StarknetOptions;
use crate::utils;
use tracing::trace;

pub(crate) const LOG_TARGET: &str = "sozo::cli::commands::account";

#[derive(Debug, Args)]
pub struct AccountArgs {
Expand Down Expand Up @@ -83,11 +86,19 @@ pub enum AccountCommand {

impl AccountArgs {
pub fn run(self, config: &Config) -> Result<()> {
trace!(target: LOG_TARGET, command=?self.command, "Executing command.");
let env_metadata = utils::load_metadata_from_config(config)?;

config.tokio_handle().block_on(async {
match self.command {
AccountCommand::New { signer, force, file } => {
trace!(
target: LOG_TARGET,
?signer,
force,
?file,
"Executing New command"
btirth marked this conversation as resolved.
Show resolved Hide resolved
);
let signer: LocalWallet = signer.signer(env_metadata.as_ref(), false)?;
account::new(signer, force, file).await
}
Expand All @@ -101,8 +112,20 @@ impl AccountArgs {
file,
no_confirmation,
} => {
trace!(
target: LOG_TARGET,
?starknet,
?signer,
?fee,
simulate,
?nonce,
poll_interval,
?file,
no_confirmation,
"Executing Deploy command."
);
let provider = starknet.provider(env_metadata.as_ref())?;
let signer = signer.signer(env_metadata.as_ref(), false)?;
let signer: LocalWallet = signer.signer(env_metadata.as_ref(), false)?;
let fee_setting = fee.into_setting()?;
account::deploy(
provider,
Expand All @@ -117,10 +140,18 @@ impl AccountArgs {
.await
}
AccountCommand::Fetch { starknet, force, output, address } => {
trace!(
target: LOG_TARGET,
?starknet,
force,
?output,
?address,
"Executing Fetch command."
);
let provider = starknet.provider(env_metadata.as_ref())?;
account::fetch(provider, force, output, address).await
}
}
})
}
}
}
30 changes: 29 additions & 1 deletion bin/sozo/src/commands/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ use clap::{Args, Subcommand};
use dojo_world::metadata::Environment;
use scarb::core::Config;
use sozo_ops::auth;
use tracing::trace;

use super::options::account::AccountOptions;
use super::options::starknet::StarknetOptions;
use super::options::transaction::TransactionOptions;
use super::options::world::WorldOptions;
use crate::utils;

pub(crate) const LOG_TARGET: &str = "sozo::cli::commands::auth";

#[derive(Debug, Args)]
pub struct AuthArgs {
#[command(subcommand)]
Expand Down Expand Up @@ -56,7 +59,10 @@ pub enum AuthCommand {

impl AuthArgs {
pub fn run(self, config: &Config) -> Result<()> {
trace!(target: LOG_TARGET, command=?self.command, "Executing Auth command.", );

let env_metadata = utils::load_metadata_from_config(config)?;
trace!(target: LOG_TARGET, metadata=?env_metadata, "Loaded environment.");

match self.command {
AuthCommand::Grant { kind, world, starknet, account, transaction } => config
Expand Down Expand Up @@ -102,14 +108,25 @@ pub async fn grant(
kind: AuthKind,
transaction: TransactionOptions,
) -> Result<()> {
trace!(target: LOG_TARGET, ?kind, ?world, ?starknet, ?account, ?transaction, "Executing 'Grant' command.");
btirth marked this conversation as resolved.
Show resolved Hide resolved
let world =
utils::world_from_env_metadata(world, account, starknet, &env_metadata).await.unwrap();

match kind {
AuthKind::Writer { models_contracts } => {
trace!(
target: LOG_TARGET,
contracts=?models_contracts,
"Granting 'Writer' permissions."
btirth marked this conversation as resolved.
Show resolved Hide resolved
);
auth::grant_writer(&world, models_contracts, transaction.into()).await
}
AuthKind::Owner { owners_resources } => {
trace!(
target: LOG_TARGET,
resources=?owners_resources,
"Granting 'Owner' permissions."
btirth marked this conversation as resolved.
Show resolved Hide resolved
);
auth::grant_owner(&world, owners_resources, transaction.into()).await
}
}
Expand All @@ -123,13 +140,24 @@ pub async fn revoke(
kind: AuthKind,
transaction: TransactionOptions,
) -> Result<()> {
trace!(target: LOG_TARGET, ?kind, ?world, ?starknet, ?account, ?transaction, "Executing 'Revoke' command.");
btirth marked this conversation as resolved.
Show resolved Hide resolved
let world =
utils::world_from_env_metadata(world, account, starknet, &env_metadata).await.unwrap();
match kind {
AuthKind::Writer { models_contracts } => {
trace!(
target: LOG_TARGET,
contracts=?models_contracts,
"Revoking 'Writer' permissions."
btirth marked this conversation as resolved.
Show resolved Hide resolved
);
auth::revoke_writer(&world, models_contracts, transaction.into()).await
}
AuthKind::Owner { owners_resources } => {
trace!(
target: LOG_TARGET,
resources=?owners_resources,
"Revoking 'Owner' permissions."
btirth marked this conversation as resolved.
Show resolved Hide resolved
);
auth::revoke_owner(&world, owners_resources, transaction.into()).await
}
}
Expand Down Expand Up @@ -185,4 +213,4 @@ mod tests {
let result = auth::ModelContract::from_str(input);
assert!(result.is_err());
}
}
}
20 changes: 18 additions & 2 deletions bin/sozo/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use prettytable::{format, Cell, Row, Table};
use scarb::core::{Config, TargetKind};
use scarb::ops::CompileOpts;
use sozo_ops::statistics::{get_contract_statistics_for_dir, ContractStatistics};
use tracing::trace;

pub(crate) const LOG_TARGET: &str = "sozo::cli::commands::build";

#[derive(Debug, Args)]
pub struct BuildArgs {
Expand Down Expand Up @@ -36,6 +39,7 @@ impl BuildArgs {
config,
CompileOpts { include_targets: vec![], exclude_targets: vec![TargetKind::TEST] },
)?;
trace!(target: LOG_TARGET, ?compile_info, "Compiled workspace.");

let mut builtin_plugins = vec![];
if self.typescript {
Expand All @@ -54,6 +58,8 @@ impl BuildArgs {
let target_dir = &compile_info.target_dir;
let contracts_statistics = get_contract_statistics_for_dir(target_dir)
.context("Error getting contracts stats")?;
trace!(target: LOG_TARGET, ?contracts_statistics, ?target_dir, "Fetched contract statistics for target directory.");

let table = create_stats_table(contracts_statistics);
table.printstd()
}
Expand All @@ -69,11 +75,13 @@ impl BuildArgs {
plugins: vec![],
builtin_plugins,
};
trace!(target: LOG_TARGET, pluginManager=?bindgen, "Generating bindings.");

tokio::runtime::Runtime::new()
.unwrap()
.block_on(bindgen.generate())
.expect("Error generating bindings");
trace!(target: LOG_TARGET, "Completed generating bindings.");
btirth marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}
Expand All @@ -89,20 +97,28 @@ fn create_stats_table(contracts_statistics: Vec<ContractStatistics>) -> Table {
Cell::new_align("Bytecode size (felts)", format::Alignment::CENTER),
Cell::new_align("Class size (bytes)", format::Alignment::CENTER),
]));
trace!(target: LOG_TARGET, "Creating table for contract statistics.");
btirth marked this conversation as resolved.
Show resolved Hide resolved

for contract_stats in contracts_statistics {
// Add table rows
let contract_name = contract_stats.contract_name;
let number_felts = contract_stats.number_felts;
let file_size = contract_stats.file_size;

trace!(
target: LOG_TARGET,
contract_name,
number_felts,
file_size,
"Adding row to table."
);
btirth marked this conversation as resolved.
Show resolved Hide resolved
table.add_row(Row::new(vec![
Cell::new_align(&contract_name, format::Alignment::LEFT),
Cell::new_align(format!("{}", number_felts).as_str(), format::Alignment::RIGHT),
Cell::new_align(format!("{}", file_size).as_str(), format::Alignment::RIGHT),
]));
}

trace!(target: LOG_TARGET, "Completed creating stats table.");
btirth marked this conversation as resolved.
Show resolved Hide resolved
table
}

Expand Down Expand Up @@ -180,4 +196,4 @@ mod tests {
// Assert
assert_eq!(table, expected_table, "Tables mismatch")
}
}
}
10 changes: 8 additions & 2 deletions bin/sozo/src/commands/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use starknet::core::types::FieldElement;
use super::options::starknet::StarknetOptions;
use super::options::world::WorldOptions;
use crate::utils;
use tracing::trace;

pub(crate) const LOG_TARGET: &str = "sozo::cli::commands::call";

#[derive(Debug, Args)]
#[command(about = "Call a system with the given calldata.")]
Expand Down Expand Up @@ -35,9 +38,12 @@ pub struct CallArgs {

impl CallArgs {
pub fn run(self, config: &Config) -> Result<()> {
let env_metadata = utils::load_metadata_from_config(config)?;
trace!(target: LOG_TARGET, contract=?self.contract, entrypoint=self.entrypoint, calldata=?self.calldata, block_id=self.block_id);
btirth marked this conversation as resolved.
Show resolved Hide resolved

let env_metadata = utils::load_metadata_from_config(config)?;
trace!(target: LOG_TARGET, ?env_metadata, "Fetched environment metadata.");
config.tokio_handle().block_on(async {

let world_reader =
utils::world_reader_from_env_metadata(self.world, self.starknet, &env_metadata)
.await
Expand All @@ -53,4 +59,4 @@ impl CallArgs {
.await
})
}
}
}
14 changes: 13 additions & 1 deletion bin/sozo/src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use camino::Utf8PathBuf;
use clap::Args;
use dojo_lang::compiler::{ABIS_DIR, BASE_DIR, MANIFESTS_DIR};
use scarb::core::Config;
use tracing::trace;

pub(crate) const LOG_TARGET: &str = "sozo::cli::commands::clean";

#[derive(Debug, Args)]
pub struct CleanArgs {
Expand All @@ -21,11 +24,15 @@ impl CleanArgs {
///
/// * `profile_dir` - The directory where the profile files are located.
pub fn clean_manifests(&self, profile_dir: &Utf8PathBuf) -> Result<()> {
trace!(target: LOG_TARGET, ?profile_dir, "Cleaning manifests.");
let dirs = vec![profile_dir.join(BASE_DIR), profile_dir.join(ABIS_DIR).join(BASE_DIR)];

for d in dirs {
if d.exists() {
trace!(target: LOG_TARGET, directory=?d, "Removing directory.");
fs::remove_dir_all(d)?;
} else {
trace!(target: LOG_TARGET, directory=?d, "Directory does not exist.");
btirth marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -34,6 +41,7 @@ impl CleanArgs {

pub fn run(self, config: &Config) -> Result<()> {
let ws = scarb::ops::read_workspace(config.manifest_path(), config)?;
trace!(target: LOG_TARGET, ws=?ws, "Workspace read successfully.");

let profile_name =
ws.current_profile().expect("Scarb profile is expected at this point.").to_string();
Expand All @@ -45,11 +53,15 @@ impl CleanArgs {
let profile_dir = manifest_dir.join(MANIFESTS_DIR).join(profile_name);

// By default, this command cleans the build manifests and scarb artifacts.
trace!(target: LOG_TARGET, "Cleaning Scarb artifacts and build manifests.");
scarb::ops::clean(config)?;
self.clean_manifests(&profile_dir)?;

if self.all && profile_dir.exists() {
trace!(target: LOG_TARGET, ?profile_dir, "Removing entire profile directory.");
fs::remove_dir_all(profile_dir)?;
} else {
trace!(target: LOG_TARGET, ?profile_dir, "Profile directory does not exist.");
btirth marked this conversation as resolved.
Show resolved Hide resolved
}

Ok(())
Expand Down Expand Up @@ -131,4 +143,4 @@ mod tests {
assert!(!manifest_toml.exists(), "Expected 'manifest.toml' to not exist");
assert!(!manifest_json.exists(), "Expected 'manifest.json' to not exist");
}
}
}
Loading