2021-10-19 16:48:15 -05:00
|
|
|
//! Solana Rust-based BPF program logging
|
2020-10-19 13:19:24 -07:00
|
|
|
|
|
|
|
use crate::account_info::AccountInfo;
|
|
|
|
|
|
|
|
#[macro_export]
|
2021-03-03 21:46:48 -08:00
|
|
|
#[deprecated(since = "1.4.14", note = "Please use `msg` macro instead")]
|
2020-10-19 13:19:24 -07:00
|
|
|
macro_rules! info {
|
|
|
|
($msg:expr) => {
|
|
|
|
$crate::log::sol_log($msg)
|
|
|
|
};
|
|
|
|
($arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr) => {
|
|
|
|
$crate::log::sol_log_64(
|
|
|
|
$arg1 as u64,
|
|
|
|
$arg2 as u64,
|
|
|
|
$arg3 as u64,
|
|
|
|
$arg4 as u64,
|
|
|
|
$arg5 as u64,
|
|
|
|
)
|
2020-11-30 13:28:58 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Print a message to the log
|
|
|
|
///
|
2021-12-10 13:13:30 -08:00
|
|
|
/// Fast form:
|
2020-11-30 13:28:58 -08:00
|
|
|
/// 1. Single string: `msg!("hi")`
|
|
|
|
///
|
2021-12-10 13:13:30 -08:00
|
|
|
/// The generic form incurs a very large runtime overhead so it should be used with care:
|
2020-11-30 13:28:58 -08:00
|
|
|
/// 3. Generalized format string: `msg!("Hello {}: 1, 2, {}", "World", 3)`
|
|
|
|
///
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! msg {
|
|
|
|
($msg:expr) => {
|
|
|
|
$crate::log::sol_log($msg)
|
|
|
|
};
|
|
|
|
($($arg:tt)*) => ($crate::log::sol_log(&format!($($arg)*)));
|
2020-10-19 13:19:24 -07:00
|
|
|
}
|
|
|
|
|
2020-11-30 13:28:58 -08:00
|
|
|
/// Print a string to the log
|
2020-10-19 13:19:24 -07:00
|
|
|
///
|
|
|
|
/// @param message - Message to print
|
|
|
|
#[inline]
|
|
|
|
pub fn sol_log(message: &str) {
|
|
|
|
#[cfg(target_arch = "bpf")]
|
|
|
|
unsafe {
|
|
|
|
sol_log_(message.as_ptr(), message.len() as u64);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "bpf"))]
|
|
|
|
crate::program_stubs::sol_log(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "bpf")]
|
|
|
|
extern "C" {
|
|
|
|
fn sol_log_(message: *const u8, len: u64);
|
|
|
|
}
|
|
|
|
|
2020-11-30 13:28:58 -08:00
|
|
|
/// Print 64-bit values represented as hexadecimal to the log
|
2020-10-19 13:19:24 -07:00
|
|
|
///
|
|
|
|
/// @param argx - integer arguments to print
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn sol_log_64(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64) {
|
|
|
|
#[cfg(target_arch = "bpf")]
|
|
|
|
unsafe {
|
|
|
|
sol_log_64_(arg1, arg2, arg3, arg4, arg5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "bpf"))]
|
|
|
|
crate::program_stubs::sol_log_64(arg1, arg2, arg3, arg4, arg5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "bpf")]
|
|
|
|
extern "C" {
|
|
|
|
fn sol_log_64_(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64);
|
|
|
|
}
|
|
|
|
|
2021-09-17 09:14:49 +01:00
|
|
|
/// Print some slices as base64
|
|
|
|
///
|
|
|
|
/// @param data - The slices to print
|
|
|
|
pub fn sol_log_data(data: &[&[u8]]) {
|
|
|
|
#[cfg(target_arch = "bpf")]
|
|
|
|
{
|
|
|
|
extern "C" {
|
|
|
|
fn sol_log_data(data: *const u8, data_len: u64);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { sol_log_data(data as *const _ as *const u8, data.len() as u64) };
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "bpf"))]
|
|
|
|
crate::program_stubs::sol_log_data(data);
|
|
|
|
}
|
|
|
|
|
2020-11-30 13:28:58 -08:00
|
|
|
/// Print the hexadecimal representation of a slice
|
2020-10-19 13:19:24 -07:00
|
|
|
///
|
|
|
|
/// @param slice - The array to print
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn sol_log_slice(slice: &[u8]) {
|
|
|
|
for (i, s) in slice.iter().enumerate() {
|
2021-12-10 13:13:30 -08:00
|
|
|
sol_log_64(0, 0, 0, i as u64, *s as u64);
|
2020-10-19 13:19:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 13:28:58 -08:00
|
|
|
/// Print the hexadecimal representation of the program's input parameters
|
2020-10-19 13:19:24 -07:00
|
|
|
///
|
|
|
|
/// @param ka - A pointer to an array of `AccountInfo` to print
|
|
|
|
/// @param data - A pointer to the instruction data to print
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn sol_log_params(accounts: &[AccountInfo], data: &[u8]) {
|
|
|
|
for (i, account) in accounts.iter().enumerate() {
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("AccountInfo");
|
2021-12-10 13:13:30 -08:00
|
|
|
sol_log_64(0, 0, 0, 0, i as u64);
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("- Is signer");
|
2021-12-10 13:13:30 -08:00
|
|
|
sol_log_64(0, 0, 0, 0, account.is_signer as u64);
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("- Key");
|
2020-10-19 13:19:24 -07:00
|
|
|
account.key.log();
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("- Lamports");
|
2021-12-10 13:13:30 -08:00
|
|
|
sol_log_64(0, 0, 0, 0, account.lamports());
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("- Account data length");
|
2021-12-10 13:13:30 -08:00
|
|
|
sol_log_64(0, 0, 0, 0, account.data_len() as u64);
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("- Owner");
|
2020-10-19 13:19:24 -07:00
|
|
|
account.owner.log();
|
|
|
|
}
|
2020-11-30 13:28:58 -08:00
|
|
|
msg!("Instruction data");
|
2020-10-19 13:19:24 -07:00
|
|
|
sol_log_slice(data);
|
|
|
|
}
|
2020-10-28 12:39:48 -07:00
|
|
|
|
2020-11-30 13:28:58 -08:00
|
|
|
/// Print the remaining compute units the program may consume
|
2020-10-28 12:39:48 -07:00
|
|
|
#[inline]
|
|
|
|
pub fn sol_log_compute_units() {
|
|
|
|
#[cfg(target_arch = "bpf")]
|
|
|
|
unsafe {
|
|
|
|
sol_log_compute_units_();
|
|
|
|
}
|
|
|
|
#[cfg(not(target_arch = "bpf"))]
|
|
|
|
crate::program_stubs::sol_log_compute_units();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "bpf")]
|
|
|
|
extern "C" {
|
|
|
|
fn sol_log_compute_units_();
|
|
|
|
}
|