Skip to content

Commit c14ab07

Browse files
feborex4539SITADRITA1github-actionsosrm
authored
program-log: Add new crate (#356)
* Lightweight log macro (#32) * Add log standalone crate * Refactored * Fix return value * Remove additional crate * Tidy up * Use memcpy * Improve digit formatting * Improve slice formatting * Improve comments * Add readme * Update README.md * Update README.md * Remove doctests * Use workspace values * Added linked readme * Add precision formatting (#47) * Add precision argument * Add std feature * Add std feature cfg * Add precision to solana target * Add precision support for signed types * Add inline * Update docs * Fix slice offset * Fix delta for signed values * Fix doc tests * Add precision formatting to `log!` macro (#50) * Add precision formatting * Remove lazy lock * Add precision formatting details * Update banner image * Add subtitle * log: Add precision support to `str` (#51) * Bump version * Inline never * Add precision support to str * Add trucated attributes * macro: Add truncate formatting (#52) * Add truncate format parsing * Add formatting options * Update README.md * Bump log version * Add formatting examples * Update banner image * Update banner image * Add crate type * log: Add support for `usize` and `isize` (#69) * Fix typo * Add support for usize and isize * Improve sign conversion * Add tests * Simplify isize handling * Avoid duplicate crates (#74) * Avoid duplicate crates * Fix vulnerable package * Fix Grammar and Typographical Errors in Documentation (#86) * Update README.md * Update README.md * Update README.md * Address review comments (#78) * [wip]: Address review comments * [wip]: Fix pointer reference * [wip]: Add logger buffer size tests * Remove unused * More logger tests * Rename program to cpi * Remove dynamic allocation * Fixed signed tests * Fix review comments * Fix unsigned test case * Add is_owner_by helper * Update README layout (#100) * Update readme * Add docs * Fix link * chore: Release * chore: Release * log: Use alloc crate (#120) Use alloc * Publish pinocchio-log-macro v0.4.1 * Add rust version to crates (#135) Add rust version * chore: fix typos (#150) fix typos README.md * log: Improve digit logic (#155) Tweaks * log: Add support for `bool` type (#156) * Tweaks * Add bool support * Use static syscalls when available (#157) * fix: make `sol_log_` call public (#168) * fix: make sol_log_ call public * fix: logger formatting * feat: add`log_cu_usage` proc_macro_attribute (#162) * feat: add compute_fn proc_macro_attribute * WIP: use sol_remaining_compute_units syscall for calc * docs: actualize docs and add introspection cost comment * refactor: add static-syscall, remove dep on pinocchio from log, rename compute_fn -> log_cu_usage * fix: correct comment * fix: ci format step * log: Add unsafe to Log trait (#186) Add unsafe * log: Calculate maximum digits for `Log` implementation (#188) * Calculate max digits * Remove second const Co-authored-by: Jon C <[email protected]> --------- Co-authored-by: Jon C <[email protected]> * ci: Add spellcheck step (#164) * Add invoke instruction helper * Typos * Remove new helpers * Remove unused * Address review comments * Tweak inline attributes * Use invoke signed unchecked * Refactor inline * Renamed to with_bounds * Update docs * Revert change * Add constant length check * Add spellcheck step * Tweak action * Fix typos * More fixes * Yet more fixes * Fixes * Add j1 option * More and more fixes * Add missing acronym * Fix merge * Fix spelling * Fix spelling * Publish pinocchio-log-macro v0.5.0 * Publish pinocchio-log v0.5.0 * log: Update precision logic (#252) * Add precision cap * Add tests * Refactor precision logic * Fix miri warning * Avoid duplication * Add missing syscall * More tests * Fix truncate logic * Fix review comments * Publish pinocchio-log v0.5.1 * Add solana-program-log * Add no_std check * Fix formatting * Fix formatting Updated README to correct links and improve formatting. * Add std support * Fix docs * Fix nits * Bump regex dependency * Tweak std feature * Move macro to top-level * Move dependency to workspace * Update nits --------- Co-authored-by: Dimitris Apostolou <[email protected]> Co-authored-by: SITADRITA1 <[email protected]> Co-authored-by: github-actions <[email protected]> Co-authored-by: osrm <[email protected]> Co-authored-by: Lucas Ste <[email protected]> Co-authored-by: Sonic <[email protected]> Co-authored-by: Jon C <[email protected]>
1 parent 3f40809 commit c14ab07

File tree

11 files changed

+1893
-0
lines changed

11 files changed

+1893
-0
lines changed

Cargo.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ members = [
6060
"program",
6161
"program-entrypoint",
6262
"program-error",
63+
"program-log",
64+
"program-log-macro",
6365
"program-memory",
6466
"program-option",
6567
"program-pack",
@@ -200,6 +202,7 @@ qualifier_attr = { version = "0.2.2", default-features = false }
200202
quote = "1.0.35"
201203
rand = "0.8.5"
202204
rayon = "1.10.0"
205+
regex = "1.11"
203206
reqwest = { version = "0.11.27", default-features = false }
204207
serde = "1.0.226" # must match the serde_derive version, see https://github.com/serde-rs/serde/issues/2584#issuecomment-1685252251
205208
serde-big-array = "0.5.1"
@@ -274,6 +277,8 @@ solana-presigner = { path = "presigner", version = "3.0.0" }
274277
solana-program = { path = "program", version = "3.0.0", default-features = false }
275278
solana-program-entrypoint = { path = "program-entrypoint", version = "3.0.0" }
276279
solana-program-error = { path = "program-error", version = "3.0.0" }
280+
solana-program-log = { path = "program-log", version = "0.0.0" }
281+
solana-program-log-macro = { path = "program-log-macro", version = "0.0.0" }
277282
solana-program-memory = { path = "program-memory", version = "3.0.0" }
278283
solana-program-option = { path = "program-option", version = "3.0.0" }
279284
solana-program-pack = { path = "program-pack", version = "3.0.0" }

program-log-macro/Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
name = "solana-program-log-macro"
3+
description = "Macro for solana program log utility"
4+
documentation = "https://docs.rs/solana-program-log-macro"
5+
version = "0.0.0"
6+
authors = { workspace = true }
7+
repository = { workspace = true }
8+
homepage = { workspace = true }
9+
license = { workspace = true }
10+
edition = { workspace = true }
11+
readme = "./README.md"
12+
13+
[package.metadata.docs.rs]
14+
targets = ["x86_64-unknown-linux-gnu"]
15+
all-features = true
16+
rustdoc-args = ["--cfg=docsrs"]
17+
18+
[lib]
19+
proc-macro = true
20+
21+
[dependencies]
22+
quote = { workspace = true }
23+
regex = { workspace = true }
24+
syn = { workspace = true, features = ["extra-traits", "full"] }

program-log-macro/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<p align="center">
2+
<a href="https://solana.com">
3+
<img alt="Solana" src="https://github.com/user-attachments/assets/31bfc6b9-fdaa-4f3c-b802-2de70548b943" height="80" />
4+
</a>
5+
</p>
6+
7+
# `solana-program-log-macro`
8+
<a href="https://crates.io/crates/solana-program-log-macro"><img src="https://img.shields.io/crates/v/solana-program-log-macro?logo=rust" /></a>
9+
10+
11+
Companion <code>log!</code> macro for <a href="https://crates.io/crates/solana-program-log"><code>solana-program-log</code></a>. The macro automates the creation of a `Logger` object to log a message and supports a subset of the [`format!`](https://doc.rust-lang.org/std/fmt/) syntax. The format string is parsed at compile time and generates the calls to a `Logger` object to with the corresponding formatted message.
12+
13+
There is also a helper <code>log_cu_usage!</code> macro which can be used to instrument functions with compute unit logging.
14+
15+
## Usage
16+
17+
### `log!`
18+
19+
The macro works very similar to `solana-program` [`msg!`](https://docs.rs/solana-msg/3.0.0/solana_msg/macro.msg.html) macro.
20+
21+
To output a simple message (static `&str`):
22+
```rust
23+
use solana_program_log::log;
24+
25+
log!("a simple log");
26+
```
27+
28+
To output a formatted message:
29+
```rust
30+
use solana_program_log::log;
31+
32+
let amount = 1_000_000_000;
33+
log!("transfer amount: {}", amount);
34+
```
35+
36+
Since a `Logger` size is statically determined, messages are limited to `200` length by default. When logging larger messages, it is possible to increase the logger buffer size:
37+
```rust
38+
use solana_program_log::log;
39+
40+
let very_long_message = "...";
41+
log!(500, "message: {}", very_long_message);
42+
```
43+
44+
It is possible to include a precision formatting for numeric values:
45+
```rust
46+
use solana_program_log::log;
47+
48+
let lamports = 1_000_000_000;
49+
log!("transfer amount (SOL: {:.9}", lamports);
50+
```
51+
52+
For `&str` types, it is possible to specify a maximum length and a truncation strategy:
53+
```rust
54+
use solana_program_log::log;
55+
56+
let program_name = "solana-program";
57+
// log message: "...program"
58+
log!("{:<.10}", program_name);
59+
// log message: "solana-..."
60+
log!("{:>.10}", program_name);
61+
```
62+
63+
### `log_cu_usage!`
64+
65+
This macro wraps the decorated function with additional logging statements that print the function name and the number of compute units used before and after the function execution.
66+
67+
```rust
68+
#[solana_program_log::log_cu_usage]
69+
fn my_function() {
70+
// Function body
71+
}
72+
```
73+
74+
The generated output will be:
75+
```
76+
Program log: Function `my_function` consumed 36 compute units
77+
```
78+
79+
## License
80+
81+
The code is licensed under the [Apache License Version 2.0](../LICENSE)

0 commit comments

Comments
 (0)