Add ic_msg()/ic_logger_msg() macros
This commit is contained in:
committed by
mergify[bot]
parent
ca95302038
commit
3c6dbd21d2
@ -65,6 +65,36 @@ pub trait InvokeContext {
|
||||
fn get_account(&self, pubkey: &Pubkey) -> Option<RefCell<Account>>;
|
||||
}
|
||||
|
||||
/// Convenience macro to log a message with an `Rc<RefCell<dyn Logger>>`
|
||||
#[macro_export]
|
||||
macro_rules! ic_logger_msg {
|
||||
($logger:expr, $message:expr) => {
|
||||
if let Ok(logger) = $logger.try_borrow_mut() {
|
||||
if logger.log_enabled() {
|
||||
logger.log($message);
|
||||
}
|
||||
}
|
||||
};
|
||||
($logger:expr, $fmt:expr, $($arg:tt)*) => {
|
||||
if let Ok(logger) = $logger.try_borrow_mut() {
|
||||
if logger.log_enabled() {
|
||||
logger.log(&format!($fmt, $($arg)*));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Convenience macro to log a message with an `InvokeContext`
|
||||
#[macro_export]
|
||||
macro_rules! ic_msg {
|
||||
($invoke_context:expr, $message:expr) => {
|
||||
$crate::ic_logger_msg!($invoke_context.get_logger(), $message)
|
||||
};
|
||||
($invoke_context:expr, $fmt:expr, $($arg:tt)*) => {
|
||||
$crate::ic_logger_msg!($invoke_context.get_logger(), $fmt, $($arg)*)
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, AbiExample)]
|
||||
pub struct BpfComputeBudget {
|
||||
/// Number of compute units that an instruction is allowed. Compute units
|
||||
@ -194,11 +224,7 @@ pub mod stable_log {
|
||||
program_id: &Pubkey,
|
||||
invoke_depth: usize,
|
||||
) {
|
||||
if let Ok(logger) = logger.try_borrow_mut() {
|
||||
if logger.log_enabled() {
|
||||
logger.log(&format!("Program {} invoke [{}]", program_id, invoke_depth));
|
||||
}
|
||||
}
|
||||
ic_logger_msg!(logger, "Program {} invoke [{}]", program_id, invoke_depth);
|
||||
}
|
||||
|
||||
/// Log a message from the program itself.
|
||||
@ -207,11 +233,7 @@ pub mod stable_log {
|
||||
/// "Program log: <program-generated output>"
|
||||
/// That is, any program-generated output is guaranteed to be prefixed by "Program log: "
|
||||
pub fn program_log(logger: &Rc<RefCell<dyn Logger>>, message: &str) {
|
||||
if let Ok(logger) = logger.try_borrow_mut() {
|
||||
if logger.log_enabled() {
|
||||
logger.log(&format!("Program log: {}", message))
|
||||
}
|
||||
}
|
||||
ic_logger_msg!(logger, "Program log: {}", message);
|
||||
}
|
||||
|
||||
/// Log successful program execution.
|
||||
@ -219,11 +241,7 @@ pub mod stable_log {
|
||||
/// The general form is:
|
||||
/// "Program <address> success"
|
||||
pub fn program_success(logger: &Rc<RefCell<dyn Logger>>, program_id: &Pubkey) {
|
||||
if let Ok(logger) = logger.try_borrow_mut() {
|
||||
if logger.log_enabled() {
|
||||
logger.log(&format!("Program {} success", program_id));
|
||||
}
|
||||
}
|
||||
ic_logger_msg!(logger, "Program {} success", program_id);
|
||||
}
|
||||
|
||||
/// Log program execution failure
|
||||
@ -235,11 +253,7 @@ pub mod stable_log {
|
||||
program_id: &Pubkey,
|
||||
err: &InstructionError,
|
||||
) {
|
||||
if let Ok(logger) = logger.try_borrow_mut() {
|
||||
if logger.log_enabled() {
|
||||
logger.log(&format!("Program {} failed: {}", program_id, err));
|
||||
}
|
||||
}
|
||||
ic_logger_msg!(logger, "Program {} failed: {}", program_id, err);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user