solana_program/log.rs
1//! Logging utilities for Rust-based Safecoin programs.
2//!
3//! Logging is the main mechanism for getting debugging information out of
4//! running Safecoin 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!`]: msg
11//! [fs]: https://doc.rust-lang.org/std/fmt/
12//!
13//! Logs can be viewed in multiple ways:
14//!
15//! - The `safecoin 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 `safecoin_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=safecoin_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/safecoin-client/latest/safecoin_client/rpc_client/struct.RpcClient.html
27//! [`env_logger`]: https://docs.rs/env_logger
28//! [`RpcClient::get_transaction`]: https://docs.rs/safecoin-client/latest/safecoin_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;
37
38/// Print a message to the log.
39#[macro_export]
40#[deprecated(since = "1.4.14", note = "Please use `msg` macro instead")]
41macro_rules! info {
42 ($msg:expr) => {
43 $crate::log::sol_log($msg)
44 };
45 ($arg1:expr, $arg2:expr, $arg3:expr, $arg4:expr, $arg5:expr) => {
46 $crate::log::sol_log_64(
47 $arg1 as u64,
48 $arg2 as u64,
49 $arg3 as u64,
50 $arg4 as u64,
51 $arg5 as u64,
52 )
53 };
54}
55
56/// Print a message to the log.
57///
58/// Supports simple strings as well as Rust [format strings][fs]. When passed a
59/// single expression it will be passed directly to [`sol_log`]. The expression
60/// must have type `&str`, and is typically used for logging static strings.
61/// When passed something other than an expression, particularly
62/// a sequence of expressions, the tokens will be passed through the
63/// [`format!`] macro before being logged with `sol_log`.
64///
65/// [fs]: https://doc.rust-lang.org/std/fmt/
66/// [`format!`]: https://doc.rust-lang.org/std/fmt/fn.format.html
67///
68/// Note that Rust's formatting machinery is relatively CPU-intensive
69/// for constrained environments like the Safecoin VM.
70///
71/// # Examples
72///
73/// ```
74/// use solana_program::msg;
75///
76/// // The fast form
77/// msg!("verifying multisig");
78///
79/// // With formatting
80/// let err = "not enough signers";
81/// msg!("multisig failed: {}", err);
82/// ```
83#[macro_export]
84macro_rules! msg {
85 ($msg:expr) => {
86 $crate::log::sol_log($msg)
87 };
88 ($($arg:tt)*) => ($crate::log::sol_log(&format!($($arg)*)));
89}
90
91/// Print a string to the log.
92#[inline]
93pub fn sol_log(message: &str) {
94 #[cfg(target_os = "solana")]
95 unsafe {
96 crate::syscalls::sol_log_(message.as_ptr(), message.len() as u64);
97 }
98
99 #[cfg(not(target_os = "solana"))]
100 crate::program_stubs::sol_log(message);
101}
102
103/// Print 64-bit values represented as hexadecimal to the log.
104#[inline]
105pub fn sol_log_64(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64) {
106 #[cfg(target_os = "solana")]
107 unsafe {
108 crate::syscalls::sol_log_64_(arg1, arg2, arg3, arg4, arg5);
109 }
110
111 #[cfg(not(target_os = "solana"))]
112 crate::program_stubs::sol_log_64(arg1, arg2, arg3, arg4, arg5);
113}
114
115/// Print some slices as base64.
116pub fn sol_log_data(data: &[&[u8]]) {
117 #[cfg(target_os = "solana")]
118 unsafe {
119 crate::syscalls::sol_log_data(data as *const _ as *const u8, data.len() as u64)
120 };
121
122 #[cfg(not(target_os = "solana"))]
123 crate::program_stubs::sol_log_data(data);
124}
125
126/// Print the hexadecimal representation of a slice.
127#[allow(dead_code)]
128pub fn sol_log_slice(slice: &[u8]) {
129 for (i, s) in slice.iter().enumerate() {
130 sol_log_64(0, 0, 0, i as u64, *s as u64);
131 }
132}
133
134/// Print the hexadecimal representation of the program's input parameters.
135///
136/// - `accounts` - A slice of [`AccountInfo`].
137/// - `data` - The instruction data.
138#[allow(dead_code)]
139pub fn sol_log_params(accounts: &[AccountInfo], data: &[u8]) {
140 for (i, account) in accounts.iter().enumerate() {
141 msg!("AccountInfo");
142 sol_log_64(0, 0, 0, 0, i as u64);
143 msg!("- Is signer");
144 sol_log_64(0, 0, 0, 0, account.is_signer as u64);
145 msg!("- Key");
146 account.key.log();
147 msg!("- Lamports");
148 sol_log_64(0, 0, 0, 0, account.lamports());
149 msg!("- Account data length");
150 sol_log_64(0, 0, 0, 0, account.data_len() as u64);
151 msg!("- Owner");
152 account.owner.log();
153 }
154 msg!("Instruction data");
155 sol_log_slice(data);
156}
157
158/// Print the remaining compute units available to the program.
159#[inline]
160pub fn sol_log_compute_units() {
161 #[cfg(target_os = "solana")]
162 unsafe {
163 crate::syscalls::sol_log_compute_units_();
164 }
165 #[cfg(not(target_os = "solana"))]
166 crate::program_stubs::sol_log_compute_units();
167}