|
1 |
| -use std::fmt; |
2 |
| -use std::io::Write; |
| 1 | +use std::{fmt, io::Write}; |
3 | 2 |
|
4 |
| -use crate::currentprocess::{ |
5 |
| - filesource::StderrSource, process, terminalsource, varsource::VarSource, |
| 3 | +use termcolor::{Color, ColorSpec, WriteColor}; |
| 4 | +use tracing::{Event, Subscriber}; |
| 5 | +use tracing_subscriber::fmt::{ |
| 6 | + format::{self, FormatEvent, FormatFields}, |
| 7 | + FmtContext, |
6 | 8 | };
|
| 9 | +use tracing_subscriber::registry::LookupSpan; |
7 | 10 |
|
8 |
| -macro_rules! warn { |
9 |
| - ( $ ( $ arg : tt ) * ) => ( $crate::cli::log::warn_fmt ( format_args ! ( $ ( $ arg ) * ) ) ) |
10 |
| -} |
11 |
| -macro_rules! err { |
12 |
| - ( $ ( $ arg : tt ) * ) => ( $crate::cli::log::err_fmt ( format_args ! ( $ ( $ arg ) * ) ) ) |
13 |
| -} |
14 |
| -macro_rules! info { |
15 |
| - ( $ ( $ arg : tt ) * ) => ( $crate::cli::log::info_fmt ( format_args ! ( $ ( $ arg ) * ) ) ) |
| 11 | +use crate::utils::notify::NotificationLevel; |
| 12 | + |
| 13 | +macro_rules! debug { |
| 14 | + ( $ ( $ arg : tt ) * ) => ( tracing::trace ! ( $ ( $ arg ) * ) ) |
16 | 15 | }
|
17 | 16 |
|
18 | 17 | macro_rules! verbose {
|
19 |
| - ( $ ( $ arg : tt ) * ) => ( $crate::cli::log::verbose_fmt ( format_args ! ( $ ( $ arg ) * ) ) ) |
| 18 | + ( $ ( $ arg : tt ) * ) => ( tracing::debug ! ( $ ( $ arg ) * ) ) |
20 | 19 | }
|
21 | 20 |
|
22 |
| -macro_rules! debug { |
23 |
| - ( $ ( $ arg : tt ) * ) => ( $crate::cli::log::debug_fmt ( format_args ! ( $ ( $ arg ) * ) ) ) |
| 21 | +macro_rules! info { |
| 22 | + ( $ ( $ arg : tt ) * ) => ( tracing::info ! ( $ ( $ arg ) * ) ) |
24 | 23 | }
|
25 | 24 |
|
26 |
| -pub(crate) fn warn_fmt(args: fmt::Arguments<'_>) { |
27 |
| - let mut t = process().stderr().terminal(); |
28 |
| - let _ = t.fg(terminalsource::Color::Yellow); |
29 |
| - let _ = t.attr(terminalsource::Attr::Bold); |
30 |
| - let _ = write!(t.lock(), "warning: "); |
31 |
| - let _ = t.reset(); |
32 |
| - let _ = t.lock().write_fmt(args); |
33 |
| - let _ = writeln!(t.lock()); |
| 25 | +macro_rules! warn { |
| 26 | + ( $ ( $ arg : tt ) * ) => ( tracing::warn ! ( $ ( $ arg ) * ) ) |
34 | 27 | }
|
35 | 28 |
|
36 |
| -pub(crate) fn err_fmt(args: fmt::Arguments<'_>) { |
37 |
| - let mut t = process().stderr().terminal(); |
38 |
| - let _ = t.fg(terminalsource::Color::Red); |
39 |
| - let _ = t.attr(terminalsource::Attr::Bold); |
40 |
| - let _ = write!(t.lock(), "error: "); |
41 |
| - let _ = t.reset(); |
42 |
| - let _ = t.lock().write_fmt(args); |
43 |
| - let _ = writeln!(t.lock()); |
| 29 | +macro_rules! err { |
| 30 | + ( $ ( $ arg : tt ) * ) => ( tracing::error ! ( $ ( $ arg ) * ) ) |
44 | 31 | }
|
45 | 32 |
|
46 |
| -pub(crate) fn info_fmt(args: fmt::Arguments<'_>) { |
47 |
| - let mut t = process().stderr().terminal(); |
48 |
| - let _ = t.attr(terminalsource::Attr::Bold); |
49 |
| - let _ = write!(t.lock(), "info: "); |
50 |
| - let _ = t.reset(); |
51 |
| - let _ = t.lock().write_fmt(args); |
52 |
| - let _ = writeln!(t.lock()); |
53 |
| -} |
| 33 | +// Adapted from |
| 34 | +// https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/trait.FormatEvent.html#examples |
| 35 | +pub struct EventFormatter; |
54 | 36 |
|
55 |
| -pub(crate) fn verbose_fmt(args: fmt::Arguments<'_>) { |
56 |
| - let mut t = process().stderr().terminal(); |
57 |
| - let _ = t.fg(terminalsource::Color::Magenta); |
58 |
| - let _ = t.attr(terminalsource::Attr::Bold); |
59 |
| - let _ = write!(t.lock(), "verbose: "); |
60 |
| - let _ = t.reset(); |
61 |
| - let _ = t.lock().write_fmt(args); |
62 |
| - let _ = writeln!(t.lock()); |
| 37 | +impl<S, N> FormatEvent<S, N> for EventFormatter |
| 38 | +where |
| 39 | + S: Subscriber + for<'a> LookupSpan<'a>, |
| 40 | + N: for<'a> FormatFields<'a> + 'static, |
| 41 | +{ |
| 42 | + fn format_event( |
| 43 | + &self, |
| 44 | + ctx: &FmtContext<'_, S, N>, |
| 45 | + mut writer: format::Writer<'_>, |
| 46 | + event: &Event<'_>, |
| 47 | + ) -> fmt::Result { |
| 48 | + let level = NotificationLevel::from(*event.metadata().level()); |
| 49 | + { |
| 50 | + let mut buf = termcolor::Buffer::ansi(); |
| 51 | + _ = buf.set_color(ColorSpec::new().set_bold(true).set_fg(level.fg_color())); |
| 52 | + _ = write!(buf, "{level}: "); |
| 53 | + _ = buf.reset(); |
| 54 | + writer.write_str(std::str::from_utf8(buf.as_slice()).unwrap())?; |
| 55 | + } |
| 56 | + ctx.field_format().format_fields(writer.by_ref(), event)?; |
| 57 | + writeln!(writer) |
| 58 | + } |
63 | 59 | }
|
64 | 60 |
|
65 |
| -pub(crate) fn debug_fmt(args: fmt::Arguments<'_>) { |
66 |
| - if process().var("RUSTUP_DEBUG").is_ok() { |
67 |
| - let mut t = process().stderr().terminal(); |
68 |
| - let _ = t.fg(terminalsource::Color::Blue); |
69 |
| - let _ = t.attr(terminalsource::Attr::Bold); |
70 |
| - let _ = write!(t.lock(), "debug: "); |
71 |
| - let _ = t.reset(); |
72 |
| - let _ = t.lock().write_fmt(args); |
73 |
| - let _ = writeln!(t.lock()); |
| 61 | +impl NotificationLevel { |
| 62 | + fn fg_color(&self) -> Option<Color> { |
| 63 | + match self { |
| 64 | + NotificationLevel::Debug => Some(Color::Blue), |
| 65 | + NotificationLevel::Verbose => Some(Color::Magenta), |
| 66 | + NotificationLevel::Info => None, |
| 67 | + NotificationLevel::Warn => Some(Color::Yellow), |
| 68 | + NotificationLevel::Error => Some(Color::Red), |
| 69 | + } |
74 | 70 | }
|
75 | 71 | }
|
0 commit comments