Skip to content

Commit 2ff50f8

Browse files
committed
refactor(log): reimplement log using tracing
1 parent c98f64e commit 2ff50f8

File tree

3 files changed

+85
-60
lines changed

3 files changed

+85
-60
lines changed

src/bin/rustup-init.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,12 @@ fn maybe_trace_rustup() -> Result<utils::ExitCode> {
101101
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("INFO"));
102102
logger.compact().with_filter(env_filter).boxed()
103103
} else {
104-
let env_filter = EnvFilter::new("DEBUG");
105-
// FIXME: Add "classical" formatting
106-
logger.with_filter(env_filter).boxed()
104+
// Receive log lines from Rustup only.
105+
let env_filter = EnvFilter::new("rustup=DEBUG");
106+
logger
107+
.event_format(rustup::cli::log::EventFormatter)
108+
.with_filter(env_filter)
109+
.boxed()
107110
}
108111
};
109112
let subscriber = {

src/cli/log.rs

+52-56
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,71 @@
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 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+
}
6359
}
6460

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+
}
7470
}
7571
}

src/utils/notify.rs

+27-1
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 => "warn",
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)