Skip to content

Commit

Permalink
macro: Add truncate formatting (#52)
Browse files Browse the repository at this point in the history
* Add truncate format parsing

* Add formatting options
  • Loading branch information
febo authored Dec 17, 2024
1 parent 120e13b commit 034abe4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
26 changes: 24 additions & 2 deletions sdk/log/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,31 @@ logger.append_with_args(amount, &[Argument::Precision(9)]);
logger.log()
```

## Limitations
And a maximum length for `&str` types with a truncate strategy:
```rust
use pinocchio_log::logger::{Attribute, Logger};

let program_name = "pinocchio-program";
let mut logger = Logger::<100>::default();
logger.append_with_args(program_name, &[Argument::TruncateStart(10)]);
logger.log() // log message: "...program"

let mut logger = Logger::<100>::default();
logger.append_with_args(program_name, &[Argument::TruncateEnd(10)]);
logger.log() // log message: "pinocchio-..."
```

## Formatting Options

Formatting options are represented by `Attribute` variants and can be passed to the `Logger` when appending messages using `append_with_args`.

| Variant | Description | Macro Format |
| ---------------------- | ----------------------------------------------- | ---------------- |
| `Precision(u8)` | Number of decimal places to display for numbers`*` | "{.*precision*}" |
| `TruncateEnd(usize)` | Truncate the output at the end when the specified maximum number of characters (size) is exceeded | "{<.*size*}" |
| `TruncateStart(usize)` | Truncate the output at the start when the specified maximum number of characters (size) is exceeded | "{>.*size*}" |

Currently the `log!` macro only offers limited formatting options. Apart from the placeholder `"{}"` for argument values, it is possible to specify the number of decimals digits for numeric values.
`*` The `Precision` adds a decimal formatting to integer numbers. This is useful to log numeric integer amounts that represent values with decimal precision.

## License

Expand Down
7 changes: 5 additions & 2 deletions sdk/log/crate/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ impl<const BUFFER: usize> Deref for Logger<BUFFER> {
impl<const BUFFER: usize> Logger<BUFFER> {
/// Append a value to the logger.
#[inline(always)]
pub fn append<T: Log>(&mut self, value: T) {
pub fn append<T: Log>(&mut self, value: T) -> &mut Self {
self.append_with_args(value, &[]);
self
}

/// Append a value to the logger with formatting arguments.
#[inline]
pub fn append_with_args<T: Log>(&mut self, value: T, args: &[Argument]) {
pub fn append_with_args<T: Log>(&mut self, value: T, args: &[Argument]) -> &mut Self {
if self.is_full() {
if BUFFER > 0 {
unsafe {
Expand All @@ -73,6 +74,8 @@ impl<const BUFFER: usize> Logger<BUFFER> {
} else {
self.offset += value.write_with_args(&mut self.buffer[self.offset..], args);
}

self
}

/// Log the message in the buffer.
Expand Down
40 changes: 40 additions & 0 deletions sdk/log/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,46 @@ pub fn log(input: TokenStream) -> TokenStream {
)
});
}
value if value.starts_with("{:<.") || value.starts_with("{:>.") => {
let size = if let Ok(size) = value[4..value.len() - 1].parse::<usize>() {
size
} else {
return Error::new_spanned(
format_string,
format!("invalid truncate size format: {}", value),
)
.to_compile_error()
.into();
};

match value.chars().nth(2) {
Some('<') => {
replaced_parts.push(quote! {
logger.append_with_args(
#arg,
&[pinocchio_log::logger::Argument::TruncateStart(#size)]
)
});
}
Some('>') => {
replaced_parts.push(quote! {
logger.append_with_args(
#arg,
&[pinocchio_log::logger::Argument::TruncateEnd(#size)]
)
});
}
_ => {
// This should not happen since we already checked the format.
return Error::new_spanned(
format_string,
format!("invalid truncate format: {}", value),
)
.to_compile_error()
.into();
}
}
}
_ => {
return Error::new_spanned(
format_string,
Expand Down

0 comments on commit 034abe4

Please sign in to comment.