solana_msg/
lib.rs

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