32
32
//! ```
33
33
34
34
use std:: {
35
- fmt, mem,
35
+ fmt:: Write ,
36
+ mem,
36
37
time:: { Duration , Instant } ,
37
38
} ;
38
39
@@ -99,21 +100,37 @@ impl SpanTree {
99
100
struct Data {
100
101
start : Instant ,
101
102
children : Vec < Node > ,
103
+ fields : String ,
102
104
}
103
105
104
106
impl Data {
105
107
fn new ( attrs : & Attributes < ' _ > ) -> Self {
106
- let mut span = Self { start : Instant :: now ( ) , children : Vec :: new ( ) } ;
107
- attrs. record ( & mut span) ;
108
- span
108
+ let mut data = Self { start : Instant :: now ( ) , children : Vec :: new ( ) , fields : String :: new ( ) } ;
109
+
110
+ let mut visitor = DataVisitor { string : & mut data. fields } ;
111
+ attrs. record ( & mut visitor) ;
112
+ data
109
113
}
114
+
110
115
fn into_node ( self , name : & ' static str ) -> Node {
111
- Node { name, count : 1 , duration : self . start . elapsed ( ) , children : self . children }
116
+ Node {
117
+ name,
118
+ fields : self . fields ,
119
+ count : 1 ,
120
+ duration : self . start . elapsed ( ) ,
121
+ children : self . children ,
122
+ }
112
123
}
113
124
}
114
125
115
- impl Visit for Data {
116
- fn record_debug ( & mut self , _field : & Field , _value : & dyn fmt:: Debug ) { }
126
+ pub struct DataVisitor < ' a > {
127
+ string : & ' a mut String ,
128
+ }
129
+
130
+ impl < ' a > Visit for DataVisitor < ' a > {
131
+ fn record_debug ( & mut self , field : & Field , value : & dyn std:: fmt:: Debug ) {
132
+ write ! ( self . string, "{} = {:?} " , field. name( ) , value) . unwrap ( ) ;
133
+ }
117
134
}
118
135
119
136
impl < S > Layer < S > for SpanTree
@@ -151,6 +168,7 @@ where
151
168
#[ derive( Default ) ]
152
169
struct Node {
153
170
name : & ' static str ,
171
+ fields : String ,
154
172
count : u32 ,
155
173
duration : Duration ,
156
174
children : Vec < Node > ,
@@ -163,16 +181,22 @@ impl Node {
163
181
164
182
fn go ( & self , level : usize , filter : & WriteFilter ) {
165
183
if self . duration > filter. longer_than && level < filter. depth {
166
- let duration = format ! ( "{:3.2?}" , self . duration) ;
167
- let count = if self . count > 1 { self . count . to_string ( ) } else { String :: new ( ) } ;
168
- eprintln ! (
169
- "{:width$} {:<9} {:<6} {}" ,
170
- "" ,
171
- duration,
172
- count,
173
- self . name,
174
- width = level * 2
175
- ) ;
184
+ let duration = ms ( self . duration ) ;
185
+ let current_indent = level * 2 ;
186
+
187
+ let mut out = String :: new ( ) ;
188
+ let _ = write ! ( out, "{:current_indent$} {duration} {:<6}" , "" , self . name) ;
189
+
190
+ if !self . fields . is_empty ( ) {
191
+ let _ = write ! ( out, " @ {}" , self . fields) ;
192
+ }
193
+
194
+ if self . count > 1 {
195
+ let _ = write ! ( out, " ({} calls)" , self . count) ;
196
+ }
197
+
198
+ eprintln ! ( "{}" , out) ;
199
+
176
200
for child in & self . children {
177
201
child. go ( level + 1 , filter)
178
202
}
@@ -236,3 +260,13 @@ impl WriteFilter {
236
260
( WriteFilter { depth, longer_than } , allowed)
237
261
}
238
262
}
263
+
264
+ #[ allow( non_camel_case_types) ]
265
+ struct ms ( Duration ) ;
266
+
267
+ impl std:: fmt:: Display for ms {
268
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
269
+ let n = self . 0 . as_millis ( ) ;
270
+ write ! ( f, "{n:5}ms" )
271
+ }
272
+ }
0 commit comments