tezos_smart_rollup_debug/
lib.rs

1// SPDX-FileCopyrightText: 2022-2023 TriliTech <contact@trili.tech>
2//
3// SPDX-License-Identifier: MIT
4
5//! Provides *debug log* which can be written to, but does not affect the host state.
6//!
7//! The result of writing to the debug log is *implementation specific* - it may, for
8//! example, be written to a log file, or to `stdout` etc.
9#![no_std]
10#![deny(missing_docs)]
11#![deny(rustdoc::broken_intra_doc_links)]
12
13/// Write a formatted message to host debug log. Formats follow [`core::fmt`].
14///
15/// You can use `debug_msg!` with any variable such that implements [`Runtime`].
16/// This is supported by any type implementing [`SmartRollupCore`] - such as
17/// [`RollupHost`].
18///
19/// Using `debug_msg!` requires an allocator - so either you will need to include
20/// `extern crate alloc` when writing a kernel in a `no_std` context, or by
21/// depending on `std`. If you want to write debug logs without pulling in the
22/// allocator, use [debug_str] instead.
23///
24/// ```no_run
25/// extern crate alloc;
26/// use tezos_smart_rollup_debug::debug_msg;
27/// use tezos_smart_rollup_host::runtime::Runtime;
28///
29/// fn log_runtime(host: &impl Runtime) {
30///   debug_msg!(host, "Simple constant string");
31///
32///   debug_msg!(host, "A format {} with argument {}", "test", 5);
33/// }
34/// ```
35///
36/// [`Runtime`]: tezos_smart_rollup_host::runtime::Runtime
37/// [`SmartRollupCore`]: tezos_smart_rollup_core::smart_rollup_core::SmartRollupCore
38/// [`RollupHost`]: tezos_smart_rollup_core::rollup_host::RollupHost
39#[cfg(feature = "alloc")]
40#[macro_export]
41macro_rules! debug_msg {
42    ($host: expr, $($args: expr),*) => {
43        {
44            extern crate alloc;
45            $crate::debug_str!($host, { &alloc::format!($($args), *) });
46        }
47    };
48}
49
50/// Write a string to the debug log.
51///
52/// While this is less powerful than [`debug_msg`] (which supports format strings),
53/// it does _not_ require an allocator in order to be used.
54///
55/// Just like `debug_msg`, you can use it with any variable implementing the [`Runtime`]
56/// trait.
57///
58/// ```no_run
59/// use tezos_smart_rollup_debug::debug_str;
60/// use tezos_smart_rollup_host::runtime::Runtime;
61///
62/// fn do_something(host: &impl Runtime) {
63///   debug_str!(host, "Simple constant string");
64/// }
65/// ```
66///
67/// [`Runtime`]: tezos_smart_rollup_host::runtime::Runtime
68#[macro_export]
69macro_rules! debug_str {
70    ($host: expr, $msg: expr) => {{
71        use $crate::Runtime;
72        $host.write_debug($msg);
73    }};
74}
75
76#[doc(hidden)]
77pub use tezos_smart_rollup_host::runtime::Runtime;