Add msg! macro for program logging, deprecate info! macro

This commit is contained in:
Michael Vines
2020-11-30 13:28:58 -08:00
parent 254790f8c8
commit 6705b5a98c
22 changed files with 162 additions and 141 deletions

View File

@ -2,11 +2,8 @@
use crate::account_info::AccountInfo;
/// Prints a string
/// There are two forms and are fast
/// 1. Single string
/// 2. 5 integers
#[macro_export]
#[deprecated(since = "1.4.14", note = "use `msg` macro instead")]
macro_rules! info {
($msg:expr) => {
$crate::log::sol_log($msg)
@ -19,12 +16,37 @@ macro_rules! info {
$arg4 as u64,
$arg5 as u64,
)
}; // `format!()` is not supported yet, Issue #3099
// `format!()` incurs a very large runtime overhead so it should be used with care
// ($($arg:tt)*) => ($crate::log::sol_log(&format!($($arg)*)));
};
}
/// Prints a string to stdout
/// Print a message to the log
///
/// There are two fast forms:
/// 1. Single string: `msg!("hi")`
/// 2. 5 integers: `msg!(1, 2, 3, 4, 5)`
///
/// The third form is more generic and incurs a very large runtime overhead so it should be used
/// with care:
/// 3. Generalized format string: `msg!("Hello {}: 1, 2, {}", "World", 3)`
///
#[macro_export]
macro_rules! msg {
($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,
)
};
($($arg:tt)*) => ($crate::log::sol_log(&format!($($arg)*)));
}
/// Print a string to the log
///
/// @param message - Message to print
#[inline]
@ -43,7 +65,7 @@ extern "C" {
fn sol_log_(message: *const u8, len: u64);
}
/// Prints 64 bit values represented as hexadecimal to stdout
/// Print 64-bit values represented as hexadecimal to the log
///
/// @param argx - integer arguments to print
@ -63,41 +85,41 @@ extern "C" {
fn sol_log_64_(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64);
}
/// Prints the hexadecimal representation of a slice
/// Print the hexadecimal representation of a slice
///
/// @param slice - The array to print
#[allow(dead_code)]
pub fn sol_log_slice(slice: &[u8]) {
for (i, s) in slice.iter().enumerate() {
info!(0, 0, 0, i, *s);
msg!(0, 0, 0, i, *s);
}
}
/// Prints the hexadecimal representation of the program's input parameters
/// Print the hexadecimal representation of the program's input parameters
///
/// @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() {
info!("AccountInfo");
info!(0, 0, 0, 0, i);
info!("- Is signer");
info!(0, 0, 0, 0, account.is_signer);
info!("- Key");
msg!("AccountInfo");
msg!(0, 0, 0, 0, i);
msg!("- Is signer");
msg!(0, 0, 0, 0, account.is_signer);
msg!("- Key");
account.key.log();
info!("- Lamports");
info!(0, 0, 0, 0, account.lamports());
info!("- Account data length");
info!(0, 0, 0, 0, account.data_len());
info!("- Owner");
msg!("- Lamports");
msg!(0, 0, 0, 0, account.lamports());
msg!("- Account data length");
msg!(0, 0, 0, 0, account.data_len());
msg!("- Owner");
account.owner.log();
}
info!("Instruction data");
msg!("Instruction data");
sol_log_slice(data);
}
/// Logs the remaining compute units the program may consume
/// Print the remaining compute units the program may consume
#[inline]
pub fn sol_log_compute_units() {
#[cfg(target_arch = "bpf")]

View File

@ -1,5 +1,4 @@
use crate::info;
use crate::{decode_error::DecodeError, instruction::InstructionError, pubkey::PubkeyError};
use crate::{decode_error::DecodeError, instruction::InstructionError, msg, pubkey::PubkeyError};
use num_traits::{FromPrimitive, ToPrimitive};
use std::convert::TryFrom;
use thiserror::Error;
@ -56,22 +55,22 @@ impl PrintProgramError for ProgramError {
if let Some(custom_error) = E::decode_custom_error_to_enum(*error) {
custom_error.print::<E>();
} else {
info!("Error: Unknown");
msg!("Error: Unknown");
}
}
Self::InvalidArgument => info!("Error: InvalidArgument"),
Self::InvalidInstructionData => info!("Error: InvalidInstructionData"),
Self::InvalidAccountData => info!("Error: InvalidAccountData"),
Self::AccountDataTooSmall => info!("Error: AccountDataTooSmall"),
Self::InsufficientFunds => info!("Error: InsufficientFunds"),
Self::IncorrectProgramId => info!("Error: IncorrectProgramId"),
Self::MissingRequiredSignature => info!("Error: MissingRequiredSignature"),
Self::AccountAlreadyInitialized => info!("Error: AccountAlreadyInitialized"),
Self::UninitializedAccount => info!("Error: UninitializedAccount"),
Self::NotEnoughAccountKeys => info!("Error: NotEnoughAccountKeys"),
Self::AccountBorrowFailed => info!("Error: AccountBorrowFailed"),
Self::MaxSeedLengthExceeded => info!("Error: MaxSeedLengthExceeded"),
Self::InvalidSeeds => info!("Error: InvalidSeeds"),
Self::InvalidArgument => msg!("Error: InvalidArgument"),
Self::InvalidInstructionData => msg!("Error: InvalidInstructionData"),
Self::InvalidAccountData => msg!("Error: InvalidAccountData"),
Self::AccountDataTooSmall => msg!("Error: AccountDataTooSmall"),
Self::InsufficientFunds => msg!("Error: InsufficientFunds"),
Self::IncorrectProgramId => msg!("Error: IncorrectProgramId"),
Self::MissingRequiredSignature => msg!("Error: MissingRequiredSignature"),
Self::AccountAlreadyInitialized => msg!("Error: AccountAlreadyInitialized"),
Self::UninitializedAccount => msg!("Error: UninitializedAccount"),
Self::NotEnoughAccountKeys => msg!("Error: NotEnoughAccountKeys"),
Self::AccountBorrowFailed => msg!("Error: AccountBorrowFailed"),
Self::MaxSeedLengthExceeded => msg!("Error: MaxSeedLengthExceeded"),
Self::InvalidSeeds => msg!("Error: InvalidSeeds"),
}
}
}