Skip to main content

rialo_s_msg/
lib.rs

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