@@ -22,6 +22,10 @@ pub struct Config {
22
22
pub indent_amount : usize ,
23
23
/// Whether to show the module paths.
24
24
pub targets : bool ,
25
+ /// Whether to show thread ids.
26
+ pub render_thread_ids : bool ,
27
+ /// Whether to show thread names.
28
+ pub render_thread_names : bool ,
25
29
}
26
30
27
31
impl Config {
@@ -39,6 +43,42 @@ impl Config {
39
43
pub fn with_targets ( self , targets : bool ) -> Self {
40
44
Self { targets, ..self }
41
45
}
46
+
47
+ pub fn with_thread_ids ( self , render_thread_ids : bool ) -> Self {
48
+ Self {
49
+ render_thread_ids,
50
+ ..self
51
+ }
52
+ }
53
+
54
+ pub fn with_thread_names ( self , render_thread_names : bool ) -> Self {
55
+ Self {
56
+ render_thread_names,
57
+ ..self
58
+ }
59
+ }
60
+
61
+ pub ( crate ) fn prefix ( & self ) -> String {
62
+ let mut buf = String :: new ( ) ;
63
+ if self . render_thread_ids {
64
+ write ! ( buf, "{:?}" , std:: thread:: current( ) . id( ) ) . unwrap ( ) ;
65
+ if buf. ends_with ( ')' ) {
66
+ buf. truncate ( buf. len ( ) - 1 ) ;
67
+ }
68
+ if buf. starts_with ( "ThreadId(" ) {
69
+ buf. drain ( 0 .."ThreadId(" . len ( ) ) ;
70
+ }
71
+ }
72
+ if self . render_thread_names {
73
+ if let Some ( name) = std:: thread:: current ( ) . name ( ) {
74
+ if self . render_thread_ids {
75
+ buf. push ( ':' ) ;
76
+ }
77
+ buf. push_str ( name) ;
78
+ }
79
+ }
80
+ buf
81
+ }
42
82
}
43
83
44
84
impl Default for Config {
@@ -48,6 +88,8 @@ impl Default for Config {
48
88
indent_lines : false ,
49
89
indent_amount : 2 ,
50
90
targets : false ,
91
+ render_thread_ids : false ,
92
+ render_thread_names : false ,
51
93
}
52
94
}
53
95
}
@@ -77,14 +119,17 @@ impl Buffers {
77
119
}
78
120
79
121
pub fn indent_current ( & mut self , indent : usize , config : & Config ) {
122
+ self . current_buf . push ( '\n' ) ;
80
123
indent_block (
81
124
& mut self . current_buf ,
82
125
& mut self . indent_buf ,
83
126
indent,
84
127
config. indent_amount ,
85
128
config. indent_lines ,
129
+ & config. prefix ( ) ,
86
130
) ;
87
131
self . current_buf . clear ( ) ;
132
+ self . flush_indent_buf ( ) ;
88
133
}
89
134
}
90
135
@@ -108,14 +153,6 @@ impl<'a> Visit for FmtEvent<'a> {
108
153
}
109
154
}
110
155
111
- impl < ' a > FmtEvent < ' a > {
112
- pub fn finish ( & mut self , indent : usize , config : & Config ) {
113
- self . bufs . current_buf . push ( '\n' ) ;
114
- self . bufs . indent_current ( indent, config) ;
115
- self . bufs . flush_indent_buf ( ) ;
116
- }
117
- }
118
-
119
156
pub struct ColorLevel < ' a > ( pub & ' a Level ) ;
120
157
121
158
impl < ' a > fmt:: Display for ColorLevel < ' a > {
@@ -131,18 +168,26 @@ impl<'a> fmt::Display for ColorLevel<'a> {
131
168
}
132
169
}
133
170
134
- fn indent_block_with_lines ( lines : & [ & str ] , buf : & mut String , indent : usize , indent_amount : usize ) {
171
+ fn indent_block_with_lines (
172
+ lines : & [ & str ] ,
173
+ buf : & mut String ,
174
+ indent : usize ,
175
+ indent_amount : usize ,
176
+ prefix : & str ,
177
+ ) {
135
178
let indent_spaces = indent * indent_amount;
136
179
if lines. is_empty ( ) {
137
180
return ;
138
181
} else if indent_spaces == 0 {
139
182
for line in lines {
183
+ buf. push_str ( prefix) ;
140
184
buf. push_str ( line) ;
141
185
buf. push ( '\n' ) ;
142
186
}
143
187
return ;
144
188
}
145
- let mut s = String :: with_capacity ( indent_spaces) ;
189
+ let mut s = String :: with_capacity ( indent_spaces + prefix. len ( ) ) ;
190
+ s. push_str ( prefix) ;
146
191
147
192
// instead of using all spaces to indent, draw a vertical line at every indent level
148
193
// up until the last indent
@@ -189,15 +234,17 @@ fn indent_block(
189
234
indent : usize ,
190
235
indent_amount : usize ,
191
236
indent_lines : bool ,
237
+ prefix : & str ,
192
238
) {
193
239
let lines: Vec < & str > = block. lines ( ) . collect ( ) ;
194
240
let indent_spaces = indent * indent_amount;
195
241
buf. reserve ( block. len ( ) + ( lines. len ( ) * indent_spaces) ) ;
196
242
if indent_lines {
197
- indent_block_with_lines ( & lines, buf, indent, indent_amount) ;
243
+ indent_block_with_lines ( & lines, buf, indent, indent_amount, prefix ) ;
198
244
} else {
199
245
let indent_str = String :: from ( " " ) . repeat ( indent_spaces) ;
200
246
for line in lines {
247
+ buf. push_str ( prefix) ;
201
248
buf. push_str ( & indent_str) ;
202
249
buf. push_str ( line) ;
203
250
buf. push ( '\n' ) ;
0 commit comments