solana_program/
log.rs

1//! Logging utilities for Rust-based Solana programs.
2//!
3//! Logging is the main mechanism for getting debugging information out of
4//! running Solana programs, and there are several functions available for doing
5//! so efficiently, depending on the type of data being logged.
6//!
7//! The most common way to emit logs is through the [`msg!`] macro, which logs
8//! simple strings, as well as [formatted strings][fs].
9//!
10//! [`msg!`]: crate::msg!
11//! [fs]: https://doc.rust-lang.org/std/fmt/
12//!
13//! Logs can be viewed in multiple ways:
14//!
15//! - The `solana logs` command displays logs for all transactions executed on a
16//!   network. Note though that transactions that fail during pre-flight
17//!   simulation are not displayed here.
18//! - When submitting transactions via [`RpcClient`], if Rust's own logging is
19//!   active then the `solana_rpc_client` crate logs at the "debug" level any logs
20//!   for transactions that failed during simulation. If using [`env_logger`]
21//!   these logs can be activated by setting `RUST_LOG=solana_rpc_client=debug`.
22//! - Logs can be retrieved from a finalized transaction by calling
23//!   [`RpcClient::get_transaction`].
24//! - Block explorers may display logs.
25//!
26//! [`RpcClient`]: https://docs.rs/solana-rpc-client/latest/solana_rpc_client/rpc_client/struct.RpcClient.html
27//! [`env_logger`]: https://docs.rs/env_logger
28//! [`RpcClient::get_transaction`]: https://docs.rs/solana-rpc-client/latest/solana_rpc_client/rpc_client/struct.RpcClient.html#method.get_transaction
29//!
30//! While most logging functions are defined in this module, [`Pubkey`]s can
31//! also be efficiently logged with the [`Pubkey::log`] function.
32//!
33//! [`Pubkey`]: crate::pubkey::Pubkey
34//! [`Pubkey::log`]: crate::pubkey::Pubkey::log
35
36use crate::account_info::AccountInfo;
37pub use solana_msg::{msg, sol_log};
38
39/// Print 64-bit values represented as hexadecimal to the log.
40#[inline]
41pub fn sol_log_64(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64) {
42    #[cfg(target_os = "solana")]
43    unsafe {
44        crate::syscalls::sol_log_64_(arg1, arg2, arg3, arg4, arg5);
45    }
46
47    #[cfg(not(target_os = "solana"))]
48    crate::program_stubs::sol_log_64(arg1, arg2, arg3, arg4, arg5);
49}
50
51/// Print some slices as base64.
52pub fn sol_log_data(data: &[&[u8]]) {
53    #[cfg(target_os = "solana")]
54    unsafe {
55        crate::syscalls::sol_log_data(data as *const _ as *const u8, data.len() as u64)
56    };
57
58    #[cfg(not(target_os = "solana"))]
59    crate::program_stubs::sol_log_data(data);
60}
61
62/// Print the hexadecimal representation of a slice.
63pub fn sol_log_slice(slice: &[u8]) {
64    for (i, s) in slice.iter().enumerate() {
65        sol_log_64(0, 0, 0, i as u64, *s as u64);
66    }
67}
68
69/// Print the hexadecimal representation of the program's input parameters.
70///
71/// - `accounts` - A slice of [`AccountInfo`].
72/// - `data` - The instruction data.
73pub fn sol_log_params(accounts: &[AccountInfo], data: &[u8]) {
74    for (i, account) in accounts.iter().enumerate() {
75        msg!("AccountInfo");
76        sol_log_64(0, 0, 0, 0, i as u64);
77        msg!("- Is signer");
78        sol_log_64(0, 0, 0, 0, account.is_signer as u64);
79        msg!("- Key");
80        account.key.log();
81        msg!("- Lamports");
82        sol_log_64(0, 0, 0, 0, account.lamports());
83        msg!("- Account data length");
84        sol_log_64(0, 0, 0, 0, account.data_len() as u64);
85        msg!("- Owner");
86        account.owner.log();
87    }
88    msg!("Instruction data");
89    sol_log_slice(data);
90}
91
92/// Print the remaining compute units available to the program.
93#[inline]
94pub fn sol_log_compute_units() {
95    #[cfg(target_os = "solana")]
96    unsafe {
97        crate::syscalls::sol_log_compute_units_();
98    }
99    #[cfg(not(target_os = "solana"))]
100    crate::program_stubs::sol_log_compute_units();
101}