solana_program_log/
wrapper.rs

1//! Logging wrapper for Solana syscalls.
2
3#[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
4use core::hint::black_box;
5#[cfg(any(target_os = "solana", target_arch = "bpf"))]
6use solana_define_syscall::definitions::{
7    sol_log_, sol_log_64_, sol_log_compute_units_, sol_log_data,
8};
9
10/// Print a string to the log.
11#[inline(always)]
12pub fn log(message: &str) {
13    #[cfg(any(target_os = "solana", target_arch = "bpf"))]
14    unsafe {
15        sol_log_(message.as_ptr(), message.len() as u64);
16    }
17
18    #[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
19    black_box(message);
20}
21
22/// Print 64-bit values represented as hexadecimal to the log.
23#[inline(always)]
24pub fn log_64(arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64) {
25    #[cfg(any(target_os = "solana", target_arch = "bpf"))]
26    unsafe {
27        sol_log_64_(arg1, arg2, arg3, arg4, arg5);
28    }
29
30    #[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
31    black_box((arg1, arg2, arg3, arg4, arg5));
32}
33
34/// Print some slices as `base64`.
35#[inline(always)]
36pub fn log_data(data: &[&[u8]]) {
37    #[cfg(any(target_os = "solana", target_arch = "bpf"))]
38    unsafe {
39        sol_log_data(data as *const _ as *const u8, data.len() as u64)
40    };
41
42    #[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
43    black_box(data);
44}
45
46/// Print the remaining compute units available to the program.
47#[inline]
48pub fn log_compute_units() {
49    #[cfg(any(target_os = "solana", target_arch = "bpf"))]
50    unsafe {
51        sol_log_compute_units_();
52    }
53}