Skip to content

Commit c78ea34

Browse files
committed
refactor(log): reimplement log using tracing
1 parent d640736 commit c78ea34

File tree

4 files changed

+93
-61
lines changed

4 files changed

+93
-61
lines changed

src/bin/rustup-init.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,12 @@ async fn maybe_trace_rustup() -> Result<utils::ExitCode> {
105105
.parse_lossy(directives.to_string_lossy());
106106
logger.compact().with_filter(env_filter).boxed()
107107
} else {
108-
let env_filter = EnvFilter::new("DEBUG");
109-
// FIXME: Add "classical" formatting
110-
logger.with_filter(env_filter).boxed()
108+
// Receive log lines from Rustup only.
109+
let env_filter = EnvFilter::new("rustup=DEBUG");
110+
logger
111+
.event_format(rustup::cli::log::EventFormatter)
112+
.with_filter(env_filter)
113+
.boxed()
111114
}
112115
};
113116
let subscriber = {

src/cli/log.rs

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,76 @@
1-
use std::fmt;
2-
use std::io::Write;
1+
use std::{fmt, io::Write};
32

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,
68
};
9+
use tracing_subscriber::registry::LookupSpan;
710

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 ) * ) )
1615
}
1716

1817
macro_rules! verbose {
19-
( $ ( $ arg : tt ) * ) => ( $crate::cli::log::verbose_fmt ( format_args ! ( $ ( $ arg ) * ) ) )
18+
( $ ( $ arg : tt ) * ) => ( ::tracing::debug ! ( $ ( $ arg ) * ) )
2019
}
2120

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 ) * ) )
2423
}
2524

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 ) * ) )
3427
}
3528

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 ) * ) )
4431
}
4532

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;
5436

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 has_ansi = writer.has_ansi_escapes();
49+
let level = NotificationLevel::from(*event.metadata().level());
50+
{
51+
let mut buf = termcolor::Buffer::ansi();
52+
if has_ansi {
53+
_ = buf.set_color(ColorSpec::new().set_bold(true).set_fg(level.fg_color()));
54+
}
55+
_ = write!(buf, "{level}: ");
56+
if has_ansi {
57+
_ = buf.reset();
58+
}
59+
writer.write_str(std::str::from_utf8(buf.as_slice()).unwrap())?;
60+
}
61+
ctx.field_format().format_fields(writer.by_ref(), event)?;
62+
writeln!(writer)
63+
}
6364
}
6465

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());
66+
impl NotificationLevel {
67+
fn fg_color(&self) -> Option<Color> {
68+
match self {
69+
NotificationLevel::Debug => Some(Color::Blue),
70+
NotificationLevel::Verbose => Some(Color::Magenta),
71+
NotificationLevel::Info => None,
72+
NotificationLevel::Warn => Some(Color::Yellow),
73+
NotificationLevel::Error => Some(Color::Red),
74+
}
7475
}
7576
}

src/currentprocess.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ pub fn with_runtime<'a, R>(
169169
mut runtime_builder: tokio::runtime::Builder,
170170
fut: impl Future<Output = R> + 'a,
171171
) -> R {
172+
use tracing::instrument::WithSubscriber;
173+
172174
ensure_hook();
173175

174176
let start_process = process.clone();
@@ -215,7 +217,7 @@ pub fn with_runtime<'a, R>(
215217
panic!("current process already set {old_p:?}");
216218
}
217219
*p.borrow_mut() = Some(process);
218-
let result = runtime.block_on(fut);
220+
let result = runtime.block_on(fut.with_subscriber(console_logger()));
219221
*p.borrow_mut() = None;
220222
result
221223
})

src/utils/notify.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
1+
use std::fmt;
2+
13
#[derive(Debug)]
24
pub(crate) enum NotificationLevel {
5+
Debug,
36
Verbose,
47
Info,
58
Warn,
69
Error,
7-
Debug,
10+
}
11+
12+
impl fmt::Display for NotificationLevel {
13+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
14+
f.write_str(match self {
15+
NotificationLevel::Debug => "debug",
16+
NotificationLevel::Verbose => "verbose",
17+
NotificationLevel::Info => "info",
18+
NotificationLevel::Warn => "warning",
19+
NotificationLevel::Error => "error",
20+
})
21+
}
22+
}
23+
24+
impl From<tracing::Level> for NotificationLevel {
25+
fn from(level: tracing::Level) -> Self {
26+
match level {
27+
tracing::Level::TRACE => Self::Debug,
28+
tracing::Level::DEBUG => Self::Verbose,
29+
tracing::Level::INFO => Self::Info,
30+
tracing::Level::WARN => Self::Warn,
31+
tracing::Level::ERROR => Self::Error,
32+
}
33+
}
834
}

0 commit comments

Comments
 (0)