Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions program-log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ std = []
[dependencies]
solana-program-log-macro = { workspace = true, optional = true }

[target.'cfg(target_arch = "bpf")'.dependencies]
solana-define-syscall = { workspace = true }

[target.'cfg(target_os = "solana")'.dependencies]
solana-define-syscall = { workspace = true }

Expand Down
5 changes: 3 additions & 2 deletions program-log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ pub use logger::{Argument, Logger};
#[cfg(feature = "macro")]
pub use solana_program_log_macro::*;

// Enabling the "std" feature when the target is "solana" has no effect.
#[cfg(all(not(target_os = "solana"), feature = "std"))]
// Enabling the "std" feature when `target_os = "solana"` or
// `target_arch = "bpf"` has no effect.
#[cfg(all(not(any(target_os = "solana", target_arch = "bpf")), feature = "std"))]
extern crate std;

#[cfg(test)]
Expand Down
42 changes: 24 additions & 18 deletions program-log/src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::{
cmp::min, mem::MaybeUninit, ops::Deref, ptr::copy_nonoverlapping, slice::from_raw_parts,
};
#[cfg(target_os = "solana")]
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
use solana_define_syscall::definitions::{
sol_log_, sol_memcpy_, sol_memset_, sol_remaining_compute_units,
};
Expand Down Expand Up @@ -112,31 +112,34 @@ impl<const BUFFER: usize> Logger<BUFFER> {
/// Log a message.
#[inline(always)]
pub fn log_message(message: &[u8]) {
#[cfg(target_os = "solana")]
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
// SAFETY: the message is always a valid pointer to a slice of bytes
// and `sol_log_` is a syscall.
unsafe {
sol_log_(message.as_ptr(), message.len() as u64);
}
#[cfg(all(not(target_os = "solana"), feature = "std"))]
#[cfg(all(not(any(target_os = "solana", target_arch = "bpf")), feature = "std"))]
{
let message = core::str::from_utf8(message).unwrap();
std::println!("{message}");
}

#[cfg(all(not(target_os = "solana"), not(feature = "std")))]
#[cfg(all(
not(any(target_os = "solana", target_arch = "bpf")),
not(feature = "std")
))]
core::hint::black_box(message);
}

/// Remaining CUs.
#[inline(always)]
pub fn remaining_compute_units() -> u64 {
#[cfg(target_os = "solana")]
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
// SAFETY: `sol_remaining_compute_units` is a syscall that returns the remaining compute units.
unsafe {
sol_remaining_compute_units()
}
#[cfg(not(target_os = "solana"))]
#[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
core::hint::black_box(0u64)
}

Expand Down Expand Up @@ -268,13 +271,13 @@ macro_rules! impl_log_for_unsigned_integer {

// Copy the number to the buffer if no precision is specified.
if precision == 0 {
#[cfg(target_os = "solana")]
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
sol_memcpy_(
ptr as *mut _,
source as *const _,
digits_to_write as u64,
);
#[cfg(not(target_os = "solana"))]
#[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
copy_nonoverlapping(source, ptr, digits_to_write);
}
// If padding is needed to satisfy the precision, add leading zeros
Expand All @@ -288,9 +291,9 @@ macro_rules! impl_log_for_unsigned_integer {
let padding = min(length - 2, precision - digits_to_write);

// Precision padding.
#[cfg(target_os = "solana")]
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
sol_memset_(ptr.add(2) as *mut _, b'0', padding as u64);
#[cfg(not(target_os = "solana"))]
#[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
(ptr.add(2) as *mut u8).write_bytes(b'0', padding);

let current = 2 + padding;
Expand All @@ -300,13 +303,16 @@ macro_rules! impl_log_for_unsigned_integer {
let remaining = min(digits_to_write, length - current);

// Number part.
#[cfg(target_os = "solana")]
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
sol_memcpy_(
ptr.add(current) as *mut _,
source as *const _,
remaining as u64,
);
#[cfg(not(target_os = "solana"))]
#[cfg(not(any(
target_os = "solana",
target_arch = "bpf"
)))]
copy_nonoverlapping(source, ptr.add(current), remaining);
}
}
Expand All @@ -317,9 +323,9 @@ macro_rules! impl_log_for_unsigned_integer {
let integer_part = digits_to_write - precision;

// Integer part of the number.
#[cfg(target_os = "solana")]
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
sol_memcpy_(ptr as *mut _, source as *const _, integer_part as u64);
#[cfg(not(target_os = "solana"))]
#[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
copy_nonoverlapping(source, ptr, integer_part);

// Decimal point.
Expand All @@ -331,13 +337,13 @@ macro_rules! impl_log_for_unsigned_integer {
let remaining = min(precision, length - current);

// Fractional part of the number.
#[cfg(target_os = "solana")]
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
sol_memcpy_(
ptr.add(current) as *mut _,
source.add(integer_part) as *const _,
remaining as u64,
);
#[cfg(not(target_os = "solana"))]
#[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
copy_nonoverlapping(
source.add(integer_part),
ptr.add(current),
Expand Down Expand Up @@ -573,13 +579,13 @@ unsafe impl Log for &str {
if length_to_write > 0 {
// SAFETY: the `destination` is always within `length_to_write` bounds.
unsafe {
#[cfg(target_os = "solana")]
#[cfg(any(target_os = "solana", target_arch = "bpf"))]
sol_memcpy_(
destination as *mut _,
source as *const _,
length_to_write as u64,
);
#[cfg(not(target_os = "solana"))]
#[cfg(not(any(target_os = "solana", target_arch = "bpf")))]
copy_nonoverlapping(source, destination as *mut _, length_to_write);
}

Expand Down