Skip to content

Commit

Permalink
fix(template): fix log macros (#1298)
Browse files Browse the repository at this point in the history
Description
---
fixes `info!`, `warn!` and `error!` macros compile errors
Support "inline" syntax for macros `info!("format {this}")`

Motivation and Context
---
Uses publicly-accessible modules in the macro-generated code.

How Has This Been Tested?
---
Added log calls to access rules unit tests

What process can a PR reviewer use to test or verify this change?
---
Use the log macros in templates

Breaking Changes
---

- [x] None
- [ ] Requires data directory to be deleted
- [ ] Other - Please specify
  • Loading branch information
sdbondi authored Feb 20, 2025
1 parent 79e2e6e commit 400f1df
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
27 changes: 19 additions & 8 deletions dan_layer/engine/tests/templates/access_rules/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ mod access_rules_template {
.initial_supply(1000);

let badges = create_badge_resource(recall_rule);
info!("Badges resource address: {}", badges.resource_address());

Component::new(Self {
value: 0,
Expand Down Expand Up @@ -94,6 +95,8 @@ mod access_rules_template {
let badges = create_badge_resource(rule!(deny_all));

let address_alloc = CallerContext::allocate_component_address(None);
let id = address_alloc.id();
info!("Allocated address: {id}");

let tokens = ResourceBuilder::fungible()
.with_authorization_hook(
Expand All @@ -119,14 +122,22 @@ mod access_rules_template {

let badge_resource = badges.resource_address();
let tokens = ResourceBuilder::fungible()
.mintable(rule!(non_fungible(
NonFungibleAddress::new(badge_resource, NonFungibleId::from_string("mint")))))
.burnable(rule!(non_fungible(
NonFungibleAddress::new(badge_resource, NonFungibleId::from_string("burn")))))
.withdrawable(rule!(non_fungible(
NonFungibleAddress::new(badge_resource, NonFungibleId::from_string("withdraw")))))
.depositable(rule!(non_fungible(
NonFungibleAddress::new(badge_resource, NonFungibleId::from_string("deposit")))))
.mintable(rule!(non_fungible(NonFungibleAddress::new(
badge_resource,
NonFungibleId::from_string("mint")
))))
.burnable(rule!(non_fungible(NonFungibleAddress::new(
badge_resource,
NonFungibleId::from_string("burn")
))))
.withdrawable(rule!(non_fungible(NonFungibleAddress::new(
badge_resource,
NonFungibleId::from_string("withdraw")
))))
.depositable(rule!(non_fungible(NonFungibleAddress::new(
badge_resource,
NonFungibleId::from_string("deposit")
))))
.initial_supply(1000);

Component::new(Self {
Expand Down
16 changes: 8 additions & 8 deletions dan_layer/template_lib/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,42 @@ macro_rules! debug {
#[macro_export]
macro_rules! log {
($lvl:expr, $fmt:expr) => {
$crate::engine::engine().emit_log($lvl, $fmt)
$crate::engine().emit_log($lvl, format!($fmt))
};
($lvl:expr, $fmt:expr, $($args:tt)*) => {
$crate::engine::engine().emit_log($lvl, format!($fmt, $($args)*))
$crate::engine().emit_log($lvl, format!($fmt, $($args)*))
};
}

/// Macro for emitting log messages from inside templates
#[macro_export]
macro_rules! info {
($fmt:expr) => {
$crate::macros::log!($crate::args::LogLevel::Info, $fmt)
$crate::log!($crate::args::LogLevel::Info, $fmt)
};
($fmt:expr, $($args:tt)*) => {
$crate::macros::log!($crate::args::LogLevel::Info, $fmt, $($args)*)
$crate::log!($crate::args::LogLevel::Info, $fmt, $($args)*)
};
}

/// Macro for emitting warn log messages from inside templates
#[macro_export]
macro_rules! warn {
($fmt:expr) => {
$crate::macros::log!($crate::args::LogLevel::Warn, $fmt)
$crate::log!($crate::args::LogLevel::Warn, $fmt)
};
($fmt:expr, $($args:tt)*) => {
$crate::macros::log!($crate::args::LogLevel::Warn, $fmt, $($args)*)
$crate::log!($crate::args::LogLevel::Warn, $fmt, $($args)*)
};
}

/// Macro for emitting error log messages from inside templates
#[macro_export]
macro_rules! error {
($fmt:expr) => {
$crate::macros::log!($crate::args::LogLevel::Error, $fmt)
$crate::log!($crate::args::LogLevel::Error, $fmt)
};
($fmt:expr, $($args:tt)*) => {
$crate::macros::log!($crate::args::LogLevel::Error, $fmt, $($args)*)
$crate::log!($crate::args::LogLevel::Error, $fmt, $($args)*)
};
}
6 changes: 3 additions & 3 deletions dan_layer/template_test_tooling/templates/faucet/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 400f1df

Please sign in to comment.