]>
git.proxmox.com Git - rustc.git/blob - vendor/tracing-tree/src/format.rs
3 fmt
::{self, Write as _}
,
11 pub(crate) const LINE_VERT
: &str = "│";
12 const LINE_HORIZ
: &str = "─";
13 pub(crate) const LINE_BRANCH
: &str = "├";
14 pub(crate) const LINE_CLOSE
: &str = "┘";
15 pub(crate) const LINE_OPEN
: &str = "┐";
17 #[derive(Copy, Clone)]
18 pub(crate) enum SpanMode
{
20 Open { verbose: bool }
,
21 Close { verbose: bool }
,
28 /// Whether to use colors.
30 /// Whether an ascii art tree is used or (if false) whether to just use whitespace indent
31 pub indent_lines
: bool
,
32 /// The amount of chars to indent.
33 pub indent_amount
: usize,
34 /// Whether to show the module paths.
36 /// Whether to show thread ids.
37 pub render_thread_ids
: bool
,
38 /// Whether to show thread names.
39 pub render_thread_names
: bool
,
40 /// Specifies after how many indentation levels we will wrap back around to zero
41 pub wraparound
: usize,
42 /// Whether to print the current span before activating a new one
43 pub verbose_entry
: bool
,
44 /// Whether to print the current span before exiting it.
45 pub verbose_exit
: bool
,
46 /// Whether to print squiggly brackets (`{}`) around the list of fields in a span.
47 pub bracketed_fields
: bool
,
51 pub fn with_ansi(self, ansi
: bool
) -> Self {
55 pub fn with_indent_lines(self, indent_lines
: bool
) -> Self {
62 pub fn with_targets(self, targets
: bool
) -> Self {
63 Self { targets, ..self }
66 pub fn with_thread_ids(self, render_thread_ids
: bool
) -> Self {
73 pub fn with_thread_names(self, render_thread_names
: bool
) -> Self {
80 pub fn with_wraparound(self, wraparound
: usize) -> Self {
81 Self { wraparound, ..self }
84 pub fn with_verbose_entry(self, verbose_entry
: bool
) -> Self {
91 pub fn with_verbose_exit(self, verbose_exit
: bool
) -> Self {
98 pub fn with_bracketed_fields(self, bracketed_fields
: bool
) -> Self {
105 pub(crate) fn prefix(&self) -> String
{
106 let mut buf
= String
::new();
107 if self.render_thread_ids
{
108 write
!(buf
, "{:?}", std
::thread
::current().id()).unwrap();
109 if buf
.ends_with('
)'
) {
110 buf
.truncate(buf
.len() - 1);
112 if buf
.starts_with("ThreadId(") {
113 buf
.drain(0.."ThreadId(".len());
116 if self.render_thread_names
{
117 if let Some(name
) = std
::thread
::current().name() {
118 if self.render_thread_ids
{
128 impl Default
for Config
{
129 fn default() -> Self {
135 render_thread_ids
: false,
136 render_thread_names
: false,
137 wraparound
: usize::max_value(),
138 verbose_entry
: false,
140 bracketed_fields
: false,
147 pub current_buf
: String
,
148 pub indent_buf
: String
,
152 pub fn new() -> Self {
154 current_buf
: String
::new(),
155 indent_buf
: String
::new(),
159 pub fn flush_current_buf(&mut self, mut writer
: impl io
::Write
) {
160 write
!(writer
, "{}", &self.current_buf
).unwrap();
161 self.current_buf
.clear();
164 pub fn flush_indent_buf(&mut self) {
165 self.current_buf
.push_str(&self.indent_buf
);
166 self.indent_buf
.clear();
169 pub(crate) fn indent_current(&mut self, indent
: usize, config
: &Config
, style
: SpanMode
) {
170 self.current_buf
.push('
\n'
);
171 let prefix
= config
.prefix();
173 // Render something when wraparound occurs so the user is aware of it
174 if config
.indent_lines
{
176 SpanMode
::Close { .. }
| SpanMode
::PostClose
=> {
177 if indent
> 0 && (indent
+ 1) % config
.wraparound
== 0 {
178 self.indent_buf
.push_str(&prefix
);
179 for _
in 0..(indent
% config
.wraparound
* config
.indent_amount
) {
180 self.indent_buf
.push_str(LINE_HORIZ
);
182 self.indent_buf
.push_str(LINE_OPEN
);
183 self.indent_buf
.push('
\n'
);
191 &mut self.current_buf
,
192 &mut self.indent_buf
,
193 indent
% config
.wraparound
,
194 config
.indent_amount
,
199 self.current_buf
.clear();
200 self.flush_indent_buf();
202 // Render something when wraparound occurs so the user is aware of it
203 if config
.indent_lines
{
205 SpanMode
::PreOpen
| SpanMode
::Open { .. }
=> {
206 if indent
> 0 && (indent
+ 1) % config
.wraparound
== 0 {
207 self.current_buf
.push_str(&prefix
);
208 for _
in 0..(indent
% config
.wraparound
* config
.indent_amount
) {
209 self.current_buf
.push_str(LINE_HORIZ
);
211 self.current_buf
.push_str(LINE_CLOSE
);
212 self.current_buf
.push('
\n'
);
221 pub struct FmtEvent
<'a
> {
222 pub bufs
: &'a
mut Buffers
,
226 impl<'a
> Visit
for FmtEvent
<'a
> {
227 fn record_debug(&mut self, field
: &Field
, value
: &dyn fmt
::Debug
) {
228 let buf
= &mut self.bufs
.current_buf
;
229 let comma
= if self.comma { "," }
else { "" }
;
232 write
!(buf
, "{} {:?}", comma
, value
).unwrap();
235 // Skip fields that are actually log metadata that have already been handled
236 #[cfg(feature = "tracing-log")]
237 name
if name
.starts_with("log.") => {}
239 write
!(buf
, "{} {}={:?}", comma
, name
, value
).unwrap();
246 pub struct ColorLevel
<'a
>(pub &'a Level
);
248 impl<'a
> fmt
::Display
for ColorLevel
<'a
> {
249 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
251 Level
::TRACE
=> Color
::Purple
.bold().paint("TRACE"),
252 Level
::DEBUG
=> Color
::Blue
.bold().paint("DEBUG"),
253 Level
::INFO
=> Color
::Green
.bold().paint(" INFO"),
254 Level
::WARN
=> Color
::RGB(252, 234, 160).bold().paint(" WARN"), // orange
255 Level
::ERROR
=> Color
::Red
.bold().paint("ERROR"),
261 fn indent_block_with_lines(
265 indent_amount
: usize,
269 let indent_spaces
= indent
* indent_amount
;
270 if lines
.is_empty() {
272 } else if indent_spaces
== 0 {
274 buf
.push_str(prefix
);
275 // The first indent is special, we only need to print open/close and nothing else
278 SpanMode
::Open { .. }
=> buf
.push_str(LINE_OPEN
),
279 SpanMode
::Close { .. }
=> buf
.push_str(LINE_CLOSE
),
280 SpanMode
::PreOpen
| SpanMode
::PostClose
=> {}
281 SpanMode
::Event
=> {}
289 let mut s
= String
::with_capacity(indent_spaces
+ prefix
.len());
292 // instead of using all spaces to indent, draw a vertical line at every indent level
293 // up until the last indent
294 for i
in 0..(indent_spaces
- indent_amount
) {
295 if i
% indent_amount
== 0 {
296 s
.push_str(LINE_VERT
);
306 SpanMode
::PreOpen
=> {
307 buf
.push_str(LINE_BRANCH
);
308 for _
in 1..(indent_amount
/ 2) {
309 buf
.push_str(LINE_HORIZ
);
311 buf
.push_str(LINE_OPEN
);
313 SpanMode
::Open { verbose: false }
=> {
314 buf
.push_str(LINE_BRANCH
);
315 for _
in 1..indent_amount
{
316 buf
.push_str(LINE_HORIZ
);
318 buf
.push_str(LINE_OPEN
);
320 SpanMode
::Open { verbose: true }
=> {
321 buf
.push_str(LINE_VERT
);
322 for _
in 1..(indent_amount
/ 2) {
325 // We don't have the space for fancy rendering at single space indent.
326 if indent_amount
> 1 {
329 for _
in (indent_amount
/ 2)..(indent_amount
- 1) {
330 buf
.push_str(LINE_HORIZ
);
332 // We don't have the space for fancy rendering at single space indent.
333 if indent_amount
> 1 {
334 buf
.push_str(LINE_OPEN
);
336 buf
.push_str(LINE_VERT
);
339 SpanMode
::Close { verbose: false }
=> {
340 buf
.push_str(LINE_BRANCH
);
341 for _
in 1..indent_amount
{
342 buf
.push_str(LINE_HORIZ
);
344 buf
.push_str(LINE_CLOSE
);
346 SpanMode
::Close { verbose: true }
=> {
347 buf
.push_str(LINE_VERT
);
348 for _
in 1..(indent_amount
/ 2) {
351 // We don't have the space for fancy rendering at single space indent.
352 if indent_amount
> 1 {
355 for _
in (indent_amount
/ 2)..(indent_amount
- 1) {
356 buf
.push_str(LINE_HORIZ
);
358 // We don't have the space for fancy rendering at single space indent.
359 if indent_amount
> 1 {
360 buf
.push_str(LINE_CLOSE
);
362 buf
.push_str(LINE_VERT
);
365 SpanMode
::PostClose
=> {
366 buf
.push_str(LINE_BRANCH
);
367 for _
in 1..(indent_amount
/ 2) {
368 buf
.push_str(LINE_HORIZ
);
370 buf
.push_str(LINE_CLOSE
);
373 buf
.push_str(LINE_BRANCH
);
375 // add `indent_amount - 1` horizontal lines before the span/event
376 for _
in 0..(indent_amount
- 1) {
377 buf
.push_str(LINE_HORIZ
);
381 buf
.push_str(&lines
[0]);
384 // add the rest of the indentation, since we don't want to draw horizontal lines
385 // for subsequent lines
386 for i
in 0..indent_amount
{
387 if i
% indent_amount
== 0 {
388 s
.push_str(LINE_VERT
);
394 // add all of the actual content, with each line preceded by the indent string
395 for line
in &lines
[1..] {
406 indent_amount
: usize,
411 let lines
: Vec
<&str> = block
.lines().collect();
412 let indent_spaces
= indent
* indent_amount
;
413 buf
.reserve(block
.len() + (lines
.len() * indent_spaces
));
415 indent_block_with_lines(&lines
, buf
, indent
, indent_amount
, prefix
, style
);
417 let indent_str
= String
::from(" ").repeat(indent_spaces
);
419 buf
.push_str(prefix
);
420 buf
.push_str(&indent_str
);