atlas_msg/
lib.rs

1#![no_std]
2#[cfg(feature = "std")]
3extern crate std;
4/// Print a message to the log.
5///
6/// Supports simple strings as well as Rust [format strings][fs]. When passed a
7/// single expression it will be passed directly to [`atlas_log`]. The expression
8/// must have type `&str`, and is typically used for logging static strings.
9/// When passed something other than an expression, particularly
10/// a sequence of expressions, the tokens will be passed through the
11/// [`format!`] macro before being logged with `atlas_log`.
12///
13/// [fs]: https://doc.rust-lang.org/std/fmt/
14/// [`format!`]: https://doc.rust-lang.org/std/fmt/fn.format.html
15///
16/// Note that Rust's formatting machinery is relatively CPU-intensive
17/// for constrained environments like the Atlas VM.
18///
19/// # Examples
20///
21/// ```
22/// use atlas_msg::msg;
23///
24/// // The fast form
25/// msg!("verifying multisig");
26///
27/// // With formatting
28/// let err = "not enough signers";
29/// msg!("multisig failed: {}", err);
30/// ```
31#[cfg(feature = "std")]
32#[macro_export]
33macro_rules! msg {
34    ($msg:expr) => {
35        $crate::atlas_log($msg)
36    };
37    ($($arg:tt)*) => ($crate::atlas_log(&format!($($arg)*)));
38}
39
40#[cfg(target_os = "atlas")]
41pub mod syscalls;
42
43/// Print a string to the log.
44#[inline]
45pub fn atlas_log(message: &str) {
46    #[cfg(target_os = "atlas")]
47    unsafe {
48        syscalls::atlas_log_(message.as_ptr(), message.len() as u64);
49    }
50
51    #[cfg(all(not(target_os = "atlas"), feature = "std"))]
52    std::println!("{message}");
53
54    #[cfg(all(not(target_os = "atlas"), not(feature = "std")))]
55    core::hint::black_box(message);
56}