This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Don't log with colors when we are writing to a tty #7525
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a8e3140
Don't log with colors when we are writing to a tty
bkchr 8f6e195
Remove accidentally added crate
bkchr a628e4a
Review feedback
bkchr 7ffa84d
More feedback
bkchr dadea48
Update client/cli/src/logging.rs
bkchr 26969b9
Update client/cli/src/logging.rs
bkchr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -16,8 +16,8 @@ | |
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| use std::fmt::{self, Write}; | ||
| use ansi_term::Colour; | ||
| use std::fmt; | ||
| use tracing::{span::Attributes, Event, Id, Level, Subscriber}; | ||
| use tracing_log::NormalizeEvent; | ||
| use tracing_subscriber::{ | ||
|
|
@@ -29,16 +29,62 @@ use tracing_subscriber::{ | |
| registry::LookupSpan, | ||
| Layer, | ||
| }; | ||
| use regex::Regex; | ||
|
|
||
| /// Span name used for the logging prefix. See macro `sc_cli::prefix_logs_with!` | ||
| pub const PREFIX_LOG_SPAN: &str = "substrate-log-prefix"; | ||
|
|
||
| /// A writer that may write to `inner_writer` with colors. | ||
| /// | ||
| /// This is used by [`EventFormat`] to kill colors when `enable_color` is `false`. | ||
| /// | ||
| /// It is required to call [`MaybeColorWriter::write`] after all writes are done, | ||
| /// because the content of these writes is buffered and will only be written to the | ||
| /// `inner_writer` at that point. | ||
| struct MaybeColorWriter<'a> { | ||
| enable_color: bool, | ||
| buffer: String, | ||
| inner_writer: &'a mut dyn fmt::Write, | ||
| } | ||
|
|
||
| impl<'a> fmt::Write for MaybeColorWriter<'a> { | ||
| fn write_str(&mut self, buf: &str) -> fmt::Result { | ||
| self.buffer.push_str(buf); | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl<'a> MaybeColorWriter<'a> { | ||
| /// Creates a new instance. | ||
| fn new(enable_color: bool, inner_writer: &'a mut dyn fmt::Write) -> Self { | ||
| Self { | ||
| enable_color, | ||
| inner_writer, | ||
| buffer: String::new(), | ||
| } | ||
| } | ||
|
|
||
| /// Write the buffered content to the `inner_writer`. | ||
| fn write(&mut self) -> fmt::Result { | ||
| lazy_static::lazy_static! { | ||
| static ref RE: Regex = Regex::new("\x1b\\[[^m]+m").expect("Error initializing color regex"); | ||
| } | ||
|
|
||
| if !self.enable_color { | ||
| let replaced = RE.replace_all(&self.buffer, ""); | ||
| self.inner_writer.write_str(&replaced) | ||
| } else { | ||
| self.inner_writer.write_str(&self.buffer) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(crate) struct EventFormat<T = SystemTime> { | ||
| pub(crate) timer: T, | ||
| pub(crate) ansi: bool, | ||
| pub(crate) display_target: bool, | ||
| pub(crate) display_level: bool, | ||
| pub(crate) display_thread_name: bool, | ||
| pub(crate) enable_color: bool, | ||
|
Comment on lines
-38
to
+87
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not too fan of renaming this because it makes us going away from tracing's original implementation: https://github.com/tokio-rs/tracing/blob/2f59b32/tracing-subscriber/src/fmt/format/mod.rs#L180 |
||
| } | ||
|
|
||
| // NOTE: the following code took inspiration from tracing-subscriber | ||
|
|
@@ -56,12 +102,13 @@ where | |
| writer: &mut dyn fmt::Write, | ||
| event: &Event, | ||
| ) -> fmt::Result { | ||
| let writer = &mut MaybeColorWriter::new(self.enable_color, writer); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I forgot you could do that 🤯 nice!! |
||
| let normalized_meta = event.normalized_metadata(); | ||
| let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata()); | ||
| time::write(&self.timer, writer, self.ansi)?; | ||
| time::write(&self.timer, writer, self.enable_color)?; | ||
|
|
||
| if self.display_level { | ||
| let fmt_level = { FmtLevel::new(meta.level(), self.ansi) }; | ||
| let fmt_level = { FmtLevel::new(meta.level(), self.enable_color) }; | ||
| write!(writer, "{} ", fmt_level)?; | ||
| } | ||
|
|
||
|
|
@@ -94,7 +141,9 @@ where | |
| write!(writer, "{}:", meta.target())?; | ||
| } | ||
| ctx.format_fields(writer, event)?; | ||
| writeln!(writer) | ||
| writeln!(writer)?; | ||
|
|
||
| writer.write() | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.