-
Notifications
You must be signed in to change notification settings - Fork 893
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(log): reimplement
log
using tracing
- Loading branch information
Showing
3 changed files
with
85 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,71 @@ | ||
use std::fmt; | ||
use std::io::Write; | ||
use std::{fmt, io::Write}; | ||
|
||
use crate::currentprocess::{ | ||
filesource::StderrSource, process, terminalsource, varsource::VarSource, | ||
use termcolor::{Color, ColorSpec, WriteColor}; | ||
use tracing::{Event, Subscriber}; | ||
use tracing_subscriber::fmt::{ | ||
format::{self, FormatEvent, FormatFields}, | ||
FmtContext, | ||
}; | ||
use tracing_subscriber::registry::LookupSpan; | ||
|
||
macro_rules! warn { | ||
( $ ( $ arg : tt ) * ) => ( $crate::cli::log::warn_fmt ( format_args ! ( $ ( $ arg ) * ) ) ) | ||
} | ||
macro_rules! err { | ||
( $ ( $ arg : tt ) * ) => ( $crate::cli::log::err_fmt ( format_args ! ( $ ( $ arg ) * ) ) ) | ||
} | ||
macro_rules! info { | ||
( $ ( $ arg : tt ) * ) => ( $crate::cli::log::info_fmt ( format_args ! ( $ ( $ arg ) * ) ) ) | ||
use crate::utils::notify::NotificationLevel; | ||
|
||
macro_rules! debug { | ||
( $ ( $ arg : tt ) * ) => ( tracing::trace ! ( $ ( $ arg ) * ) ) | ||
} | ||
|
||
macro_rules! verbose { | ||
( $ ( $ arg : tt ) * ) => ( $crate::cli::log::verbose_fmt ( format_args ! ( $ ( $ arg ) * ) ) ) | ||
( $ ( $ arg : tt ) * ) => ( tracing::debug ! ( $ ( $ arg ) * ) ) | ||
} | ||
|
||
macro_rules! debug { | ||
( $ ( $ arg : tt ) * ) => ( $crate::cli::log::debug_fmt ( format_args ! ( $ ( $ arg ) * ) ) ) | ||
macro_rules! info { | ||
( $ ( $ arg : tt ) * ) => ( tracing::info ! ( $ ( $ arg ) * ) ) | ||
} | ||
|
||
pub(crate) fn warn_fmt(args: fmt::Arguments<'_>) { | ||
let mut t = process().stderr().terminal(); | ||
let _ = t.fg(terminalsource::Color::Yellow); | ||
let _ = t.attr(terminalsource::Attr::Bold); | ||
let _ = write!(t.lock(), "warning: "); | ||
let _ = t.reset(); | ||
let _ = t.lock().write_fmt(args); | ||
let _ = writeln!(t.lock()); | ||
macro_rules! warn { | ||
( $ ( $ arg : tt ) * ) => ( tracing::warn ! ( $ ( $ arg ) * ) ) | ||
} | ||
|
||
pub(crate) fn err_fmt(args: fmt::Arguments<'_>) { | ||
let mut t = process().stderr().terminal(); | ||
let _ = t.fg(terminalsource::Color::Red); | ||
let _ = t.attr(terminalsource::Attr::Bold); | ||
let _ = write!(t.lock(), "error: "); | ||
let _ = t.reset(); | ||
let _ = t.lock().write_fmt(args); | ||
let _ = writeln!(t.lock()); | ||
macro_rules! err { | ||
( $ ( $ arg : tt ) * ) => ( tracing::error ! ( $ ( $ arg ) * ) ) | ||
} | ||
|
||
pub(crate) fn info_fmt(args: fmt::Arguments<'_>) { | ||
let mut t = process().stderr().terminal(); | ||
let _ = t.attr(terminalsource::Attr::Bold); | ||
let _ = write!(t.lock(), "info: "); | ||
let _ = t.reset(); | ||
let _ = t.lock().write_fmt(args); | ||
let _ = writeln!(t.lock()); | ||
} | ||
// Adapted from | ||
// https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/trait.FormatEvent.html#examples | ||
pub struct EventFormatter; | ||
|
||
pub(crate) fn verbose_fmt(args: fmt::Arguments<'_>) { | ||
let mut t = process().stderr().terminal(); | ||
let _ = t.fg(terminalsource::Color::Magenta); | ||
let _ = t.attr(terminalsource::Attr::Bold); | ||
let _ = write!(t.lock(), "verbose: "); | ||
let _ = t.reset(); | ||
let _ = t.lock().write_fmt(args); | ||
let _ = writeln!(t.lock()); | ||
impl<S, N> FormatEvent<S, N> for EventFormatter | ||
where | ||
S: Subscriber + for<'a> LookupSpan<'a>, | ||
N: for<'a> FormatFields<'a> + 'static, | ||
{ | ||
fn format_event( | ||
&self, | ||
ctx: &FmtContext<'_, S, N>, | ||
mut writer: format::Writer<'_>, | ||
event: &Event<'_>, | ||
) -> fmt::Result { | ||
let level = NotificationLevel::from(*event.metadata().level()); | ||
{ | ||
let mut buf = termcolor::Buffer::ansi(); | ||
_ = buf.set_color(ColorSpec::new().set_bold(true).set_fg(level.fg_color())); | ||
_ = write!(buf, "{level}: "); | ||
_ = buf.reset(); | ||
writer.write_str(std::str::from_utf8(buf.as_slice()).unwrap())?; | ||
} | ||
ctx.field_format().format_fields(writer.by_ref(), event)?; | ||
writeln!(writer) | ||
} | ||
} | ||
|
||
pub(crate) fn debug_fmt(args: fmt::Arguments<'_>) { | ||
if process().var("RUSTUP_DEBUG").is_ok() { | ||
let mut t = process().stderr().terminal(); | ||
let _ = t.fg(terminalsource::Color::Blue); | ||
let _ = t.attr(terminalsource::Attr::Bold); | ||
let _ = write!(t.lock(), "debug: "); | ||
let _ = t.reset(); | ||
let _ = t.lock().write_fmt(args); | ||
let _ = writeln!(t.lock()); | ||
impl NotificationLevel { | ||
fn fg_color(&self) -> Option<Color> { | ||
match self { | ||
NotificationLevel::Debug => Some(Color::Blue), | ||
NotificationLevel::Verbose => Some(Color::Magenta), | ||
NotificationLevel::Info => None, | ||
NotificationLevel::Warn => Some(Color::Yellow), | ||
NotificationLevel::Error => Some(Color::Red), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,34 @@ | ||
use std::fmt; | ||
|
||
#[derive(Debug)] | ||
pub(crate) enum NotificationLevel { | ||
Debug, | ||
Verbose, | ||
Info, | ||
Warn, | ||
Error, | ||
Debug, | ||
} | ||
|
||
impl fmt::Display for NotificationLevel { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result { | ||
f.write_str(match self { | ||
NotificationLevel::Debug => "debug", | ||
NotificationLevel::Verbose => "verbose", | ||
NotificationLevel::Info => "info", | ||
NotificationLevel::Warn => "warn", | ||
NotificationLevel::Error => "error", | ||
}) | ||
} | ||
} | ||
|
||
impl From<tracing::Level> for NotificationLevel { | ||
fn from(level: tracing::Level) -> Self { | ||
match level { | ||
tracing::Level::TRACE => Self::Debug, | ||
tracing::Level::DEBUG => Self::Verbose, | ||
tracing::Level::INFO => Self::Info, | ||
tracing::Level::WARN => Self::Warn, | ||
tracing::Level::ERROR => Self::Error, | ||
} | ||
} | ||
} |