seda_sdk_rs/log.rs
1//! Logging and debugging macros for the `seda_runtime_sdk`.
2
3/// A debug macro that prints the expression and its value to stderr.
4///
5/// This macro is a more gas-efficient alternative to the standard `dbg!` macro.
6/// It evaluates the given expression, prints the source location, expression text,
7/// and its debug representation to standard error.
8///
9/// # Examples
10///
11/// ```
12/// use seda_sdk_rs::debug;
13///
14/// let a = 2;
15/// let b = debug!(a * 2) + 1;
16/// // Prints to stderr: [src/main.rs:2:9] a * 2 = 4
17/// assert_eq!(b, 5);
18/// ```
19///
20/// Multiple values can be debugged at once:
21///
22/// ```
23/// use seda_sdk_rs::debug;
24///
25/// let x = 1;
26/// let y = 2;
27/// debug!(x, y, x + y);
28/// // Prints each value on a separate line with location information
29/// ```
30///
31/// # Notes
32///
33/// This macro requires that the expression's type implements the `Debug` trait.
34#[macro_export]
35macro_rules! debug {
36 ($expr:expr) => {
37 match $expr {
38 expr => {
39 use std::io::Write;
40 // Format the debug message
41 if let Err(e) = std::io::stderr().write_all(format!("[{}:{}] {} = {:#?}\n", file!(), line!(), stringify!($expr), &expr).as_bytes()) {
42 panic!("Failed to write debug message: {e}");
43 }
44
45 expr
46 }
47 }
48 };
49 ($($val:expr),+ $(,)?) => {
50 ($($crate::debug!($val)),+,)
51 };
52}
53
54/// A logging macro that prints the value to stdout, with a newline.
55///
56/// This macro is a more gas-efficient alternative to regular println! statements.
57/// It evaluates the given expression and writes its display representation to standard output,
58/// followed by a newline.
59///
60/// # Examples
61///
62/// ```rust
63/// # use seda_sdk_rs::log;
64/// let value = 42;
65/// log!("{}", value); // Prints: 42\n
66/// log!("The answer is: {}", value); // Prints: The answer is: 42\n
67/// ```
68///
69/// # Notes
70///
71/// This macro requires that the expression's type implements the `Display` trait.
72#[macro_export]
73macro_rules! log {
74 ($($arg:tt)*) => {{
75 use std::io::Write;
76 let _ = std::io::stdout().write_all(format!("{}\n", format_args!($($arg)*)).as_bytes());
77 }};
78}
79
80/// A logging macro that prints the value to stderr, with a newline.
81///
82/// This macro is a more gas-efficient alternative to regular eprintln! statements.
83/// It evaluates the given expression and writes its display representation to standard error,
84/// followed by a newline.
85///
86/// # Examples
87///
88/// ```rust
89/// # use seda_sdk_rs::elog;
90/// let error_code = 404;
91/// elog!("{}", error_code); // Prints: 404\n
92/// elog!("Error {}: Not Found", error_code); // Prints: Error 404: Not Found\n
93/// ```
94///
95/// # Notes
96///
97/// This macro requires that the expression's type implements the `Display` trait.
98#[macro_export]
99macro_rules! elog {
100 ($($arg:tt)*) => {{
101 use std::io::Write;
102 let _ = std::io::stderr().write_all(format!("{}\n", format_args!($($arg)*)).as_bytes());
103 }};
104}