#How do I log `Address` in a contract in local testing mode?

14 messages · Page 1 of 1 (latest)

upper acorn
#

Consider the following code:

pub fn test(env: Env, val: Address) -> () {
  log!(&env, "address is {}", val);
}```

which yields ```address is Object(Address(#1))```

Is there a way to extract the actual contract strkey instead?
spice topaz
#

do you run this as wasm or as compiled-in contract?

upper acorn
#

compiled-in contract (isn't this what "local-testing mode" means?)

spice topaz
#

depends on register_contract vs register_contract_wasm

upper acorn
#

register_contract

spice topaz
#

ah, I think you need the debug format {:?}

#

not sure why it's done exactly this way, but SDK mostly implements debug formatting for the data structures (and not Display that is used for {})

upper acorn
#

what is the exact syntax? If I just replace {} with {:?} I get address is :?

spice topaz
#

yeah, it seems like it's not used for the logging inside contracts...

#

sorry for the confusion, I haven't really looked much into this. basically we have debug formatting for the SDK types, but apparently it's only usable outside the contracts (like println!("{:?}", address); would output a strkey). I suppose outputting it that way from contract via host would require some trickery to only enable that when the contract is used in compiled mode (not sure if that's feasible)

#

a hacky-ish way to do debug outputs from the contracts would be to disable #[no_std] temporarily and just use println/eprintln with debug formatting

#

(you can make it less hacky by enabling #[no_std] conditionally for wasm target)

upper acorn
#

yeah this all seems pretty hackish. Am I doing something idiosyncratic by debugging using log messages in local testing mode?

spice topaz
#

you should be able to use the regular debugger