Skip to content

Commit

Permalink
Allow debugging output in the testing library.
Browse files Browse the repository at this point in the history
  • Loading branch information
abizjak committed Dec 11, 2023
1 parent 2e555d8 commit a45776f
Show file tree
Hide file tree
Showing 8 changed files with 547 additions and 234 deletions.
48 changes: 48 additions & 0 deletions concordium-std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,51 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
note = "Deprecated in favor of [concordium-smart-contract-testing](https://docs.rs/concordium-smart-contract-testing)."
)]
pub mod test_infrastructure;

#[cfg(all(feature = "debug", not(feature = "std")))]
pub use alloc::format;
#[cfg(all(feature = "debug", feature = "std"))]
pub use std::format;

#[macro_export]
#[cfg(feature = "debug")]
macro_rules! concordium_dbg {
() => {
{
$crate::debug_print("", file!(), line!(), column!());
}
};
($($arg:tt),+) => {
{
let msg = $crate::format!($($arg),+);
$crate::debug_print(&msg, file!(), line!(), column!());
}
};
}

#[macro_export]
#[cfg(not(feature = "debug"))]
macro_rules! concordium_dbg {
() => {{}};
($($arg:tt),+) => {{}};
}

/// Emit a message in debug mode.
/// Used internally, not meant to be called directly by contract writers,
/// and a contract with this debug print cannot be deployed to the chain.
#[doc(hidden)]
#[cfg(feature = "debug")]
pub fn debug_print(message: &str, filename: &str, line: u32, column: u32) {
let msg_bytes = message.as_bytes();
let filename_bytes = filename.as_bytes();
unsafe {
crate::prims::debug_print(
msg_bytes.as_ptr(),
msg_bytes.len() as u32,
filename_bytes.as_ptr(),
filename_bytes.len() as u32,
line,
column,
)
};
}
16 changes: 16 additions & 0 deletions concordium-std/src/prims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,22 @@ extern "C" {
column: u32,
);

#[cfg(feature = "debug")]
/// Emit text together with the source location.
/// This is used as the equivalent of the `dbg!` macro when the
/// `enable-debug` feature is enabled.
///
/// Note that this function is not allowed on the chain, it is only there to
/// ease local testing.
pub fn debug_print(
msg_start: *const u8,
msg_length: u32,
filename_start: *const u8,
filename_length: u32,
line: u32,
column: u32,
);

#[cfg(all(feature = "wasm-test", feature = "concordium-quickcheck", target_arch = "wasm32"))]
/// Generating random numbers for randomised testing.
/// Not available for contracts deployed on the chain.
Expand Down
Loading

0 comments on commit a45776f

Please sign in to comment.