Skip to content

Commit c00b401

Browse files
committed
[trace] move macros tests to the tracing crate
- This is needed for the tests to pass because the macros use 'hyperlight_guest_tracing::' which cannot be imported by 'hyperlight_guest_tracing_macro' bacause it would create a circular dependency. - The caveat is that when using 'hyperlight_guest_tracing_macro' one needs to have as a dependency 'hyperlight_guest_tracing' also Signed-off-by: Doru Blânzeanu <[email protected]>
1 parent bce51a2 commit c00b401

File tree

2 files changed

+85
-83
lines changed
  • src
    • hyperlight_guest_tracing_macro/src
    • hyperlight_guest_tracing/src

2 files changed

+85
-83
lines changed

src/hyperlight_guest_tracing/src/lib.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,91 @@ limitations under the License.
1717

1818
/// Re-export the tracing macros
1919
/// This allows users to use the macros without needing to import them explicitly.
20+
///
21+
/// # Tracing Macros Usage
22+
///
23+
/// ## The `trace_function` macro can be used to trace function calls.
24+
///
25+
/// ```rust
26+
/// #[hyperlight_guest_tracing_macro::trace_function]
27+
/// fn my_function() {
28+
/// // // Function body
29+
/// }
30+
/// ```
31+
///
32+
/// ## The `trace!` macro can be used to create trace records with a message.
33+
///
34+
/// ```rust
35+
/// use hyperlight_guest_tracing_macro::trace;
36+
/// trace!("message");
37+
/// trace!("message", { /* block of code */ });
38+
/// ```
39+
///
40+
/// ## Basic usage: trace with message only
41+
///
42+
/// ```
43+
/// use hyperlight_guest_tracing_macro::trace;
44+
/// trace!("hello");
45+
/// ```
46+
///
47+
/// ## Trace with a block, returning a value
48+
///
49+
/// ```
50+
/// use hyperlight_guest_tracing_macro::trace;
51+
/// let x = trace!("block", { 42 });
52+
/// assert_eq!(x, 42);
53+
/// ```
54+
///
55+
/// ## Trace with a block using local variables
56+
///
57+
/// ```
58+
/// use hyperlight_guest_tracing_macro::trace;
59+
/// let y = 10;
60+
/// let z = trace!("sum", { y + 5 });
61+
/// assert_eq!(z, 15);
62+
/// ```
63+
///
64+
/// ## Trace with a block that returns a reference
65+
///
66+
/// ```
67+
/// use hyperlight_guest_tracing_macro::trace;
68+
/// let s = String::from("abc");
69+
/// let r: &str = trace!("ref", { &s });
70+
/// assert_eq!(r, "abc");
71+
/// ```
72+
///
73+
/// ## Control flow: `return` inside the block returns from the function
74+
///
75+
/// ```
76+
/// use hyperlight_guest_tracing_macro::trace;
77+
/// fn foo() -> i32 {
78+
/// let _ = trace!("fail", {
79+
/// // This return only exits the closure, not the function `foo`.
80+
/// return 42;
81+
/// });
82+
/// assert!(false, "This should not be reached");
83+
/// }
84+
/// ```
85+
///
86+
/// ## Control flow: `break` inside the block exits the outer loop
87+
///
88+
/// ```
89+
/// use hyperlight_guest_tracing_macro::trace;
90+
/// let mut x = 0;
91+
/// for i in 1..3 {
92+
/// x = i;
93+
/// let _ = trace!("msg", {
94+
/// // This break should exit the loop.
95+
/// break;
96+
/// });
97+
/// }
98+
/// assert_eq!(x, 1, "Loop should break after the first iteration");
99+
/// ```
100+
///
101+
/// ## Flush the trace buffer
102+
/// ```rust
103+
/// hyperlight_guest_tracing_macro::flush!();
104+
/// ```
20105
pub use hyperlight_guest_tracing_macro::*;
21106
#[cfg(feature = "trace")]
22107
pub use trace::{create_trace_record, flush_trace_buffer};

src/hyperlight_guest_tracing_macro/src/lib.rs

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ use quote::quote;
1919
use syn::{ItemFn, parse_macro_input};
2020

2121
/// A procedural macro attribute for tracing function calls.
22-
/// Usage:
23-
/// ```rust
24-
/// #[hyperlight_guest_tracing_macro::trace_function]
25-
/// fn my_function() {
26-
/// // // Function body
27-
/// }
28-
/// ```
29-
///
3022
/// This macro will create a trace record when the function is called
3123
///
3224
/// The trace record will contain the function name as a string.
@@ -131,78 +123,8 @@ impl syn::parse::Parse for TraceMacroInput {
131123

132124
/// This macro creates a trace record with a message, or traces a block with entry/exit records.
133125
///
134-
/// Usage:
135-
/// ```rust
136-
/// use hyperlight_guest_tracing_macro::trace;
137-
/// trace!("message");
138-
/// trace!("message", { /* block of code */ });
139-
/// ```
140-
///
141126
/// When called with an expression or statement as the second argument, it is wrapped in a block,
142127
/// entry and exit trace records are created at the start and end of block, and the result of the block is returned.
143-
///
144-
/// # Examples
145-
///
146-
/// ## Basic usage: trace with message only
147-
///
148-
/// ```
149-
/// use hyperlight_guest_tracing_macro::trace;
150-
/// trace!("hello");
151-
/// ```
152-
///
153-
/// ## Trace with a block, returning a value
154-
///
155-
/// ```
156-
/// use hyperlight_guest_tracing_macro::trace;
157-
/// let x = trace!("block", { 42 });
158-
/// assert_eq!(x, 42);
159-
/// ```
160-
///
161-
/// ## Trace with a block using local variables
162-
///
163-
/// ```
164-
/// use hyperlight_guest_tracing_macro::trace;
165-
/// let y = 10;
166-
/// let z = trace!("sum", { y + 5 });
167-
/// assert_eq!(z, 15);
168-
/// ```
169-
///
170-
/// ## Trace with a block that returns a reference
171-
///
172-
/// ```
173-
/// use hyperlight_guest_tracing_macro::trace;
174-
/// let s = String::from("abc");
175-
/// let r: &str = trace!("ref", { &s });
176-
/// assert_eq!(r, "abc");
177-
/// ```
178-
///
179-
/// ## Control flow: `return` inside the block returns from the function
180-
///
181-
/// ```
182-
/// use hyperlight_guest_tracing_macro::trace;
183-
/// fn foo() -> i32 {
184-
/// let _ = trace!("fail", {
185-
/// // This return only exits the closure, not the function `foo`.
186-
/// return 42;
187-
/// });
188-
/// assert!(false, "This should not be reached");
189-
/// }
190-
/// ```
191-
///
192-
/// ## Control flow: `break` inside the block exits the outer loop
193-
///
194-
/// ```
195-
/// use hyperlight_guest_tracing_macro::trace;
196-
/// let mut x = 0;
197-
/// for i in 1..3 {
198-
/// x = i;
199-
/// let _ = trace!("msg", {
200-
/// // This break should exit the loop.
201-
/// break;
202-
/// });
203-
/// }
204-
/// assert_eq!(x, 1, "Loop should break after the first iteration");
205-
/// ```
206128
#[proc_macro]
207129
pub fn trace(input: TokenStream) -> TokenStream {
208130
let parsed = syn::parse_macro_input!(input as TraceMacroInput);
@@ -251,11 +173,6 @@ pub fn trace(input: TokenStream) -> TokenStream {
251173
}
252174

253175
/// This macro flushes the trace buffer, sending any remaining trace records to the host.
254-
///
255-
/// Usage:
256-
/// ```rust
257-
/// hyperlight_guest_tracing_macro::flush!();
258-
/// ```
259176
#[proc_macro]
260177
pub fn flush(_input: TokenStream) -> TokenStream {
261178
#[cfg(feature = "trace")]

0 commit comments

Comments
 (0)