Skip to content

Commit

Permalink
feat: Function scope
Browse files Browse the repository at this point in the history
Adds details on the current function scope.
  • Loading branch information
Jonathan Woollett-Light committed Oct 12, 2023
1 parent 859e16c commit d591852
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 168 deletions.
82 changes: 31 additions & 51 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 27 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Offers an attribute procedural macro that adds [`log::trace!`](https://docs.rs/l
### Example

```rust
use log::*;

fn main() {
env_logger::builder()
.filter_level(LevelFilter::Trace)
Expand All @@ -24,23 +26,35 @@ fn one(x: u32) -> u32 {
if cmp {
return 4;
}
x + 3
two(x + 3)
}
#[log_instrument::instrument]
fn two(x: u32) -> u32 {
let res = x % 2;
debug!("res: {res}");
res
}
```

Outputs:

```
[2023-08-30T13:58:20Z TRACE log_instrument] one enter
[2023-08-30T13:58:20Z DEBUG one] cmp: true
[2023-08-30T13:58:20Z TRACE log_instrument] one exit
[2023-08-30T13:58:20Z INFO one] 4
[2023-08-30T13:58:20Z TRACE log_instrument] one enter
[2023-08-30T13:58:20Z DEBUG one] cmp: false
[2023-08-30T13:58:20Z TRACE log_instrument] one exit
[2023-08-30T13:58:20Z INFO one] 6
[2023-08-30T13:58:20Z TRACE log_instrument] one enter
[2023-08-30T13:58:20Z DEBUG one] cmp: false
[2023-08-30T13:58:20Z TRACE log_instrument] one exit
[2023-08-30T13:58:20Z INFO one] 7
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)>>one
[2023-10-12T16:38:00Z DEBUG six] cmp: true
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)<<one
[2023-10-12T16:38:00Z INFO six] 4
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)>>one
[2023-10-12T16:38:00Z DEBUG six] cmp: false
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)::one>>two
[2023-10-12T16:38:00Z DEBUG six] res: 0
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)::one<<two
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)<<one
[2023-10-12T16:38:00Z INFO six] 0
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)>>one
[2023-10-12T16:38:00Z DEBUG six] cmp: false
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)::one>>two
[2023-10-12T16:38:00Z DEBUG six] res: 1
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)::one<<two
[2023-10-12T16:38:00Z TRACE log_instrument] ThreadId(1)<<one
[2023-10-12T16:38:00Z INFO six] 1
```
11 changes: 0 additions & 11 deletions log-instrument/examples/five.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@

use log::{debug, info, warn, LevelFilter};

/// Outputs:
/// ```text
/// [2023-08-20T22:59:16Z TRACE log_instrument] one enter
/// [2023-08-20T22:59:16Z TRACE log_instrument] one exit
/// [2023-08-20T22:59:16Z INFO five] None
/// [2023-08-20T22:59:16Z TRACE log_instrument] one enter
/// [2023-08-20T22:59:16Z DEBUG five] ["a", "b"]
/// [2023-08-20T22:59:16Z DEBUG five] 23
/// [2023-08-20T22:59:16Z TRACE log_instrument] one exit
/// [2023-08-20T22:59:16Z INFO five] Some(["a", "b"])
/// ```
fn main() {
env_logger::builder()
.filter_level(LevelFilter::Trace)
Expand Down
10 changes: 0 additions & 10 deletions log-instrument/examples/four.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
use log::*;

/// Outputs:
/// ```text
/// [2023-08-20T22:22:43Z TRACE log_instrument] one enter
/// [2023-08-20T22:22:43Z TRACE log_instrument] one exit
/// [2023-08-20T22:22:43Z INFO four] None
/// [2023-08-20T22:22:43Z TRACE log_instrument] one enter
/// [2023-08-20T22:22:43Z DEBUG four] ["a", "b"]
/// [2023-08-20T22:22:43Z TRACE log_instrument] one exit
/// [2023-08-20T22:22:43Z INFO four] Some(["a", "b"])
/// ```
fn main() {
env_logger::builder()
.filter_level(LevelFilter::Trace)
Expand Down
15 changes: 0 additions & 15 deletions log-instrument/examples/one.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
use log::*;

/// Outputs:
/// ```text
/// [2023-08-20T22:21:32Z TRACE log_instrument] one enter
/// [2023-08-20T22:21:32Z DEBUG one] cmp: true
/// [2023-08-20T22:21:32Z TRACE log_instrument] one exit
/// [2023-08-20T22:21:32Z INFO one] 4
/// [2023-08-20T22:21:32Z TRACE log_instrument] one enter
/// [2023-08-20T22:21:32Z DEBUG one] cmp: false
/// [2023-08-20T22:21:32Z TRACE log_instrument] one exit
/// [2023-08-20T22:21:32Z INFO one] 6
/// [2023-08-20T22:21:32Z TRACE log_instrument] one enter
/// [2023-08-20T22:21:32Z DEBUG one] cmp: false
/// [2023-08-20T22:21:32Z TRACE log_instrument] one exit
/// [2023-08-20T22:21:32Z INFO one] 7
/// ```
fn main() {
env_logger::builder()
.filter_level(LevelFilter::Trace)
Expand Down
25 changes: 25 additions & 0 deletions log-instrument/examples/six.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use log::*;

fn main() {
env_logger::builder()
.filter_level(LevelFilter::Trace)
.init();
info!("{}", one(2));
info!("{}", one(3));
info!("{}", one(4));
}
#[log_instrument::instrument]
fn one(x: u32) -> u32 {
let cmp = x == 2;
debug!("cmp: {cmp}");
if cmp {
return 4;
}
two(x + 3)
}
#[log_instrument::instrument]
fn two(x: u32) -> u32 {
let res = x % 2;
debug!("res: {res}");
res
}
10 changes: 0 additions & 10 deletions log-instrument/examples/three.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
use log::*;

/// Outputs:
/// ```text
/// [2023-08-20T22:22:25Z TRACE log_instrument] one enter
/// [2023-08-20T22:22:25Z TRACE log_instrument] one exit
/// [2023-08-20T22:22:25Z INFO three] None
/// [2023-08-20T22:22:25Z TRACE log_instrument] one enter
/// [2023-08-20T22:22:25Z DEBUG three] ["a", "b"]
/// [2023-08-20T22:22:25Z TRACE log_instrument] one exit
/// [2023-08-20T22:22:25Z INFO three] Some(["a", "b"])
/// ```
fn main() {
env_logger::builder()
.filter_level(LevelFilter::Trace)
Expand Down
10 changes: 0 additions & 10 deletions log-instrument/examples/two.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
use log::*;

/// Outputs:
/// ```text
/// [2023-08-20T22:21:51Z TRACE log_instrument] one enter
/// [2023-08-20T22:21:51Z TRACE log_instrument] one exit
/// [2023-08-20T22:21:51Z INFO two] None
/// [2023-08-20T22:21:51Z TRACE log_instrument] one enter
/// [2023-08-20T22:21:51Z DEBUG two] ["a", "b"]
/// [2023-08-20T22:21:51Z TRACE log_instrument] one exit
/// [2023-08-20T22:21:51Z INFO two] Some(["a", "b"]
/// ```
fn main() {
env_logger::builder()
.filter_level(LevelFilter::Trace)
Expand Down
Loading

0 comments on commit d591852

Please sign in to comment.