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 3 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
25 changes: 24 additions & 1 deletion 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,executor=trace,forked_backend=trace,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
36 changes: 36 additions & 0 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,12 +86,22 @@ pub enum AccountCommand {

impl AccountArgs {
pub fn run(self, config: &Config) -> Result<()> {
trace!(target: LOG_TARGET, "AccountArgs command: {:?}", self.command);
let env_metadata = utils::load_metadata_from_config(config)?;
trace!(target: LOG_TARGET, "Fetched environment metadata");
btirth marked this conversation as resolved.
Show resolved Hide resolved

config.tokio_handle().block_on(async {
match self.command {
AccountCommand::New { signer, force, file } => {
trace!(
target: LOG_TARGET,
"Executing New command with signer: {:?}, force: {}, file: {:?}",
signer,
force,
file
);
btirth marked this conversation as resolved.
Show resolved Hide resolved
let signer: LocalWallet = signer.signer(env_metadata.as_ref(), false)?;
trace!(target: LOG_TARGET, "Initialized LocalWallet for command");
btirth marked this conversation as resolved.
Show resolved Hide resolved
account::new(signer, force, file).await
}
AccountCommand::Deploy {
Expand All @@ -101,9 +114,23 @@ impl AccountArgs {
file,
no_confirmation,
} => {
trace!(
target: LOG_TARGET,
"Executing Deploy command with starknet: {:?}, signer: {:?}, fee: {:?}, simulate: {}, nonce: {:?}, poll_interval: {}, file: {:?}, no_confirmation: {}",
starknet,
signer,
fee,
simulate,
nonce,
poll_interval,
file,
no_confirmation
);
let provider = starknet.provider(env_metadata.as_ref())?;
let signer = signer.signer(env_metadata.as_ref(), false)?;
trace!(target: LOG_TARGET, "Initialized LocalWallet and Provider for 'Deploy' command");
btirth marked this conversation as resolved.
Show resolved Hide resolved
let fee_setting = fee.into_setting()?;
trace!(target: LOG_TARGET, "Converted FeeOptions to FeeSetting");
btirth marked this conversation as resolved.
Show resolved Hide resolved
account::deploy(
provider,
signer,
Expand All @@ -117,7 +144,16 @@ impl AccountArgs {
.await
}
AccountCommand::Fetch { starknet, force, output, address } => {
trace!(
target: LOG_TARGET,
"Executing Fetch command with starknet: {:?}, force: {}, output: {:?}, address: {:?}",
starknet,
force,
output,
address
);
let provider = starknet.provider(env_metadata.as_ref())?;
trace!(target: LOG_TARGET, "Initialized Provider for Fetch command");
btirth marked this conversation as resolved.
Show resolved Hide resolved
account::fetch(provider, force, output, address).await
}
}
Expand Down
28 changes: 28 additions & 0 deletions 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, "Executing Auth command: {:?}", self.command);

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

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, "Executing 'Grant' command with kind: {:?}, world: {:?}, starknet: {:?}, account: {:?}, transaction: {:?}", kind, world, starknet, account, transaction);
let world =
utils::world_from_env_metadata(world, account, starknet, &env_metadata).await.unwrap();

match kind {
AuthKind::Writer { models_contracts } => {
trace!(
target: LOG_TARGET,
"Granting 'Writer' permissions for contract: {:?}",
models_contracts
);
auth::grant_writer(&world, models_contracts, transaction.into()).await
}
AuthKind::Owner { owners_resources } => {
trace!(
target: LOG_TARGET,
"Granting 'Owner' permissions for resources: {:?}",
owners_resources
);
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, "Executing 'Revoke' command with kind: {:?}, world: {:?}, starknet: {:?}, account: {:?}, transaction: {:?}", kind, world, starknet, account, transaction);
let world =
utils::world_from_env_metadata(world, account, starknet, &env_metadata).await.unwrap();
match kind {
AuthKind::Writer { models_contracts } => {
trace!(
target: LOG_TARGET,
"Revoking 'Writer' permissions for contracts: {:?}",
models_contracts
);
auth::revoke_writer(&world, models_contracts, transaction.into()).await
}
AuthKind::Owner { owners_resources } => {
trace!(
target: LOG_TARGET,
"Revoking 'Owner' permissions for resources: {:?}",
owners_resources
);
auth::revoke_owner(&world, owners_resources, transaction.into()).await
}
}
Expand Down
20 changes: 19 additions & 1 deletion 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, "Compiled workspace: {:?}", compile_info);

let mut builtin_plugins = vec![];
if self.typescript {
Expand All @@ -54,7 +58,11 @@ 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, "Contract statistics: {:?}", contracts_statistics);

let table = create_stats_table(contracts_statistics);
trace!(target: LOG_TARGET, "Displaying contract statistics");
btirth marked this conversation as resolved.
Show resolved Hide resolved

table.printstd()
}

Expand All @@ -69,11 +77,13 @@ impl BuildArgs {
plugins: vec![],
builtin_plugins,
};
trace!(target: LOG_TARGET, "Generating bindings with PluginManager: {:?}", bindgen);

tokio::runtime::Runtime::new()
.unwrap()
.block_on(bindgen.generate())
.expect("Error generating bindings");
trace!(target: LOG_TARGET, "Completed generating bindings");

Ok(())
}
Expand All @@ -89,20 +99,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");

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,
"Adding row to table: contract = {}, bytecode size (felts) = {}, class size (bytes) = {}",
contract_name,
number_felts,
file_size
);
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");
table
}

Expand Down
12 changes: 11 additions & 1 deletion 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,14 +38,21 @@ 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: {}, Entrypoint: {}, Calldata: {:?}, Block ID: {:?}",
self.contract, self.entrypoint, self.calldata, self.block_id);

let env_metadata = utils::load_metadata_from_config(config)?;
trace!(target: LOG_TARGET, "Fetched environment metadata");
btirth marked this conversation as resolved.
Show resolved Hide resolved

config.tokio_handle().block_on(async {
trace!(target: LOG_TARGET, "Initializing world reader from metadata");

let world_reader =
utils::world_reader_from_env_metadata(self.world, self.starknet, &env_metadata)
.await
.unwrap();

trace!(target: LOG_TARGET, "World reader initialized");
sozo_ops::call::call(
world_reader,
self.contract,
Expand Down
12 changes: 12 additions & 0 deletions 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, "Cleaning manifests in directory: {:?}", profile_dir);
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, "Removing directory: {:?}", d);
fs::remove_dir_all(d)?;
} else {
trace!(target: LOG_TARGET, "Directory does not exist: {:?}", d);
}
}

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, "Workspace read successfully");
btirth marked this conversation as resolved.
Show resolved Hide resolved

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, "Removing entire profile directory: {:?}", profile_dir);
fs::remove_dir_all(profile_dir)?;
} else {
trace!(target: LOG_TARGET, "Profile directory does not exist: {:?}", profile_dir);
}

Ok(())
Expand Down
4 changes: 4 additions & 0 deletions bin/sozo/src/commands/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use std::io;
use anyhow::Result;
use clap::{Args, CommandFactory};
use clap_complete::{generate, Shell};
use tracing::trace;

use crate::args::SozoArgs;

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

#[derive(Debug, Args)]
pub struct CompletionsArgs {
shell: Shell,
Expand All @@ -15,6 +18,7 @@ impl CompletionsArgs {
pub fn run(self) -> Result<()> {
let mut command = SozoArgs::command();
let name = command.get_name().to_string();
trace!(target: LOG_TARGET, "Command name: {}", name);
generate(self.shell, &mut command, name, &mut io::stdout());
Ok(())
}
Expand Down
Loading