#compact #dbg #smart-contracts #debugging #gear #gstd #messages

no-std gdbg

为 gstd(gear 智能合约)和 std 提供紧凑的 dbg! 宏。

6 个版本

0.1.2 2024 年 1 月 17 日
0.1.1 2024 年 1 月 17 日
0.0.2 2023 年 6 月 28 日
0.0.1 2023 年 4 月 25 日
0.0.0 2022 年 12 月 30 日

#1819 in 神奇豆


用于 poems

MIT 许可证

6KB
52

gdbg

为 [1] gstd 和 std 提供了 [2] dbg! 宏的紧凑版本。

std::dbg 不同,gdbg::dbg 使用 {:?} 作为格式化,这比 {:#?} 更紧凑。

您可以通过运行程序并使用 gtest 链接来查看调试消息。要查看在节点上执行程序时的这些消息,应使用具有 RUST_LOG="gwasm=debug" 环境变量的节点运行。

示例合约(no_std)

[dependeicies]
gstd = { version = "1", features = ["debug"] }
gdbg = { version = "0.1" }

examples/example-contract/lib.rs

#![no_std]

use gdbg::dbg;
use gstd::prelude::String;

#[no_mangle]
extern "C" fn init() {
    dbg!(gstd::msg::source());
    let payload = dbg!(String::from_utf8(
        dbg!(gstd::msg::load_bytes()).expect("Failed to load a message")
    ))
    .expect("Invalid init message");
    dbg!(&payload);
}

#[cfg(test)]
mod tests {
    use gtest::{Program, System};

    #[test]
    fn it_works() {
        let system = System::new();
        system.init_logger();

        let program = Program::current(&system);

        let res = program.send_bytes(42, "INIT");
        assert!(res.log().is_empty());
    }
}

运行 cargo test

...
running 1 test
[DEBUG tests::it_works] [/home/navigaid/gdbg/examples/example/lib.rs:8] gstd::msg::source() = ActorId([ 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])
[DEBUG tests::it_works] [/home/navigaid/gdbg/examples/example/lib.rs:9] gstd::msg::load_bytes() = Ok([ 73, 78, 73, 84 ])
[DEBUG tests::it_works] [/home/navigaid/gdbg/examples/example/lib.rs:9] String::from_utf8(dbg!(gstd::msg::load_bytes()).expect("Failed to load a message")) = Ok("INIT")
[DEBUG tests::it_works] [/home/navigaid/gdbg/examples/example/lib.rs:11] &payload = "INIT"                                                                                    
test tests::it_works ... ok
...

示例二进制文件(std)

[dependeicies]
gdbg = { version = "0.1", features = ["std"] }

examples/example-binary/main.rs

fn main() {
    gdbg::dbg!(add(2, 2));

    std::dbg!(add(2, 2));

    // gdbg::dbg prints more compact result than std::dbg
    gdbg::dbg!(Point { x: 2, y: 2 });

    std::dbg!(Point { x: 2, y: 2 });
}

运行 cargo r

[examples/example-lib/main.rs:20] add(2, 2) = 4                                // gdbg::dbg
[examples/example-lib/main.rs:22] add(2, 2) = 4                                // std::dbg
[examples/example-lib/main.rs:25] Point { x: 2, y: 2 } = Point { x: 2, y: 2 }  // gdbg::dbg
[examples/example-lib/main.rs:27] Point { x: 2, y: 2 } = Point {               // std::dbg
    x: 2,
    y: 2,
}

参考资料

无运行时依赖

功能