| 
									
										
										
										
											2019-05-21 13:39:27 -07:00
										 |  |  | //! @brief Solana Rust-based BPF program logging
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-08 13:49:35 -08:00
										 |  |  | #![cfg(feature = "program")]
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-15 19:40:20 +00:00
										 |  |  | use crate::account_info::AccountInfo;
 | 
					
						
							| 
									
										
										
										
											2019-05-21 13:39:27 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 16:07:12 -07:00
										 |  |  | /// Prints a string
 | 
					
						
							|  |  |  | /// There are two forms and are fast
 | 
					
						
							|  |  |  | /// 1. Single string
 | 
					
						
							|  |  |  | /// 2. 5 integers
 | 
					
						
							|  |  |  | #[macro_export]
 | 
					
						
							|  |  |  | 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,
 | 
					
						
							|  |  |  |         )
 | 
					
						
							|  |  |  |     }; // `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)*)));
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-21 13:39:27 -07:00
										 |  |  | /// Prints a string to stdout
 | 
					
						
							| 
									
										
										
										
											2019-06-20 16:07:12 -07:00
										 |  |  | ///
 | 
					
						
							|  |  |  | /// @param message - Message to print
 | 
					
						
							| 
									
										
										
										
											2019-09-20 15:40:41 -07:00
										 |  |  | #[inline]
 | 
					
						
							| 
									
										
										
										
											2019-05-21 13:39:27 -07:00
										 |  |  | pub fn sol_log(message: &str) {
 | 
					
						
							|  |  |  |     unsafe {
 | 
					
						
							| 
									
										
										
										
											2019-06-20 19:09:50 -07:00
										 |  |  |         sol_log_(message.as_ptr(), message.len() as u64);
 | 
					
						
							| 
									
										
										
										
											2019-05-21 13:39:27 -07:00
										 |  |  |     }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | extern "C" {
 | 
					
						
							| 
									
										
										
										
											2020-01-24 13:41:14 -08:00
										 |  |  |     fn sol_log_(message: *const u8, len: u64);
 | 
					
						
							| 
									
										
										
										
											2019-05-21 13:39:27 -07:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /// Prints 64 bit values represented as hexadecimal to stdout
 | 
					
						
							| 
									
										
										
										
											2019-06-20 16:07:12 -07:00
										 |  |  | ///
 | 
					
						
							|  |  |  | /// @param argx - integer arguments to print
 | 
					
						
							| 
									
										
										
										
											2019-09-18 19:54:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 15:40:41 -07:00
										 |  |  | #[inline]
 | 
					
						
							| 
									
										
										
										
											2019-05-21 13:39:27 -07:00
										 |  |  | pub fn sol_log_64(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64) {
 | 
					
						
							|  |  |  |     unsafe {
 | 
					
						
							|  |  |  |         sol_log_64_(arg1, arg2, arg3, arg4, arg5);
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | extern "C" {
 | 
					
						
							|  |  |  |     fn sol_log_64_(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64);
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /// Prints 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() {
 | 
					
						
							| 
									
										
										
										
											2020-01-24 13:41:14 -08:00
										 |  |  |         info!(0, 0, 0, i, *s);
 | 
					
						
							| 
									
										
										
										
											2019-05-21 13:39:27 -07:00
										 |  |  |     }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /// Prints the hexadecimal representation of the program's input parameters
 | 
					
						
							|  |  |  | ///
 | 
					
						
							| 
									
										
										
										
											2019-09-10 18:53:02 -07:00
										 |  |  | /// @param ka - A pointer to an array of `AccountInfo` to print
 | 
					
						
							| 
									
										
										
										
											2019-05-21 13:39:27 -07:00
										 |  |  | /// @param data - A pointer to the instruction data to print
 | 
					
						
							|  |  |  | #[allow(dead_code)]
 | 
					
						
							| 
									
										
										
										
											2019-09-06 17:32:14 -07:00
										 |  |  | pub fn sol_log_params(accounts: &[AccountInfo], data: &[u8]) {
 | 
					
						
							|  |  |  |     for (i, account) in accounts.iter().enumerate() {
 | 
					
						
							| 
									
										
										
										
											2020-01-24 13:41:14 -08:00
										 |  |  |         info!("AccountInfo");
 | 
					
						
							|  |  |  |         info!(0, 0, 0, 0, i);
 | 
					
						
							|  |  |  |         info!("- Is signer");
 | 
					
						
							|  |  |  |         info!(0, 0, 0, 0, account.is_signer);
 | 
					
						
							|  |  |  |         info!("- Key");
 | 
					
						
							| 
									
										
										
										
											2019-09-06 17:32:14 -07:00
										 |  |  |         account.key.log();
 | 
					
						
							| 
									
										
										
										
											2020-01-24 13:41:14 -08:00
										 |  |  |         info!("- Lamports");
 | 
					
						
							| 
									
										
										
										
											2020-01-24 14:34:59 -08:00
										 |  |  |         info!(0, 0, 0, 0, account.lamports());
 | 
					
						
							| 
									
										
										
										
											2020-01-24 13:41:14 -08:00
										 |  |  |         info!("- Account data length");
 | 
					
						
							| 
									
										
										
										
											2020-01-24 14:34:59 -08:00
										 |  |  |         info!(0, 0, 0, 0, account.data_len());
 | 
					
						
							| 
									
										
										
										
											2020-01-24 13:41:14 -08:00
										 |  |  |         info!("- Owner");
 | 
					
						
							| 
									
										
										
										
											2019-09-06 17:32:14 -07:00
										 |  |  |         account.owner.log();
 | 
					
						
							| 
									
										
										
										
											2019-05-21 13:39:27 -07:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2020-01-24 13:41:14 -08:00
										 |  |  |     info!("Instruction data");
 | 
					
						
							| 
									
										
										
										
											2019-05-21 13:39:27 -07:00
										 |  |  |     sol_log_slice(data);
 | 
					
						
							|  |  |  | }
 |