-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Use "tracing" for logging and loading indicators (#216)
This is preparation for the implementation of the TEACH ME mode. Command with `RUST_LOG=info`: ![Screenshot 2024-09-11 at 10 51 35](https://github.com/user-attachments/assets/5a3591b0-7ebf-4cb7-8fa3-6a7e0fa2c4ea) Command with `RUST_LOG=trace`: ![Screenshot 2024-09-11 at 10 55 54](https://github.com/user-attachments/assets/bfa40594-6736-445e-9f19-c12bfba925c8) --------- Co-authored-by: FroVolod <frol_off@meta.ua>
- Loading branch information
Showing
15 changed files
with
310 additions
and
230 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
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
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,2 +1,3 @@ | ||
pub mod color_preference_cli; | ||
pub mod my_formatter; | ||
pub mod utf8_path_buf; |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use colored::Colorize; | ||
use tracing::Level; | ||
use tracing_core::{Event, Subscriber}; | ||
use tracing_log::NormalizeEvent; | ||
use tracing_subscriber::fmt::{ | ||
format::{self, FormatEvent, FormatFields}, | ||
FmtContext, | ||
}; | ||
use tracing_subscriber::registry::LookupSpan; | ||
|
||
#[derive(Debug)] | ||
pub struct MyFormatter { | ||
environment: colored::ColoredString, | ||
} | ||
|
||
impl MyFormatter { | ||
pub fn from_environment(environment: colored::ColoredString) -> Self { | ||
Self { environment } | ||
} | ||
} | ||
|
||
impl<S, N> FormatEvent<S, N> for MyFormatter | ||
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<'_>, | ||
) -> std::fmt::Result { | ||
let normalized_meta = event.normalized_metadata(); | ||
let metadata = normalized_meta.as_ref().unwrap_or_else(|| event.metadata()); | ||
|
||
let level = format!("[{}]", metadata.level()); | ||
let fmt_level = match *metadata.level() { | ||
Level::ERROR => level.red(), | ||
Level::WARN => level.yellow(), | ||
Level::INFO => level.cyan(), | ||
Level::DEBUG => level.truecolor(100, 100, 100), | ||
Level::TRACE => level.truecolor(200, 200, 200), | ||
}; | ||
|
||
write!( | ||
&mut writer, | ||
"{}-[{}] {}:{} - ", | ||
fmt_level, | ||
self.environment, | ||
metadata.file().unwrap_or("log"), | ||
metadata.line().unwrap_or_default() | ||
)?; | ||
|
||
ctx.field_format().format_fields(writer.by_ref(), event)?; | ||
|
||
writeln!(writer) | ||
} | ||
} |