Skip to main content

hopper_runtime/
log.rs

1//! Backend-neutral logging helpers.
2//!
3//! Two tiers are exposed:
4//!
5//! - [`log`] for arbitrary UTF-8 text through the active backend's
6//!   `sol_log_` syscall.
7//! - [`log_64`] for integer-heavy logs through the five-u64 `sol_log_64_`
8//!   syscall, which is the cheapest structured-log path on Solana. This
9//!   backs the `hopper_log!` macro's "label + values" form and lets
10//!   hot handlers emit telemetry without the `core::fmt::Write` setup
11//!   cost that `msg!` pays.
12
13/// Log a UTF-8 message through the active backend.
14#[inline(always)]
15pub fn log(message: &str) {
16    #[cfg(all(target_os = "solana", feature = "hopper-native-backend"))]
17    unsafe {
18        hopper_native::syscalls::sol_log_(message.as_ptr(), message.len() as u64);
19    }
20
21    #[cfg(all(target_os = "solana", feature = "legacy-pinocchio-compat"))]
22    unsafe {
23        pinocchio::syscalls::sol_log_(message.as_ptr(), message.len() as u64);
24    }
25
26    #[cfg(all(target_os = "solana", feature = "solana-program-backend"))]
27    {
28        ::solana_program::log::sol_log(message);
29    }
30
31    #[cfg(not(target_os = "solana"))]
32    {
33        let _ = message;
34    }
35}
36
37/// Log up to five `u64` values through the `sol_log_64_` syscall.
38///
39/// One syscall, no allocation, no format parsing. Pad unused slots
40/// with zero. The Solana runtime renders the five values as a single
41/// line "Program log: 0x... 0x... ...". Use this as the tight-loop
42/// escape hatch when the output is going to be grep'd, not read.
43///
44/// ```ignore
45/// // Emit "balance, delta, new_balance":
46/// hopper_runtime::log::log_64(balance, delta, new_balance, 0, 0);
47/// ```
48#[inline(always)]
49pub fn log_64(a: u64, b: u64, c: u64, d: u64, e: u64) {
50    #[cfg(all(target_os = "solana", feature = "hopper-native-backend"))]
51    unsafe {
52        hopper_native::syscalls::sol_log_64_(a, b, c, d, e);
53    }
54
55    #[cfg(all(target_os = "solana", feature = "legacy-pinocchio-compat"))]
56    unsafe {
57        pinocchio::syscalls::sol_log_64_(a, b, c, d, e);
58    }
59
60    #[cfg(all(target_os = "solana", feature = "solana-program-backend"))]
61    {
62        ::solana_program::log::sol_log_64(a, b, c, d, e);
63    }
64
65    #[cfg(not(target_os = "solana"))]
66    {
67        let _ = (a, b, c, d, e);
68    }
69}
70
71/// Stack-allocated write buffer for formatted log messages.
72pub struct StackWriter<'a> {
73    buf: &'a mut [u8],
74    pos: usize,
75}
76
77impl<'a> StackWriter<'a> {
78    #[inline(always)]
79    pub fn new(buf: &'a mut [u8]) -> Self {
80        Self { buf, pos: 0 }
81    }
82
83    #[inline(always)]
84    pub fn pos(&self) -> usize {
85        self.pos
86    }
87}
88
89impl core::fmt::Write for StackWriter<'_> {
90    fn write_str(&mut self, s: &str) -> core::fmt::Result {
91        let bytes = s.as_bytes();
92        let remaining = self.buf.len().saturating_sub(self.pos);
93        let to_write = bytes.len().min(remaining);
94        self.buf[self.pos..self.pos + to_write].copy_from_slice(&bytes[..to_write]);
95        self.pos += to_write;
96        Ok(())
97    }
98}