zlog: Add env var to enable line number logging (#41905)
Release Notes: - N/A *or* Added/Fixed/Improved ...
This commit is contained in:
+14
-1
@@ -39,6 +39,7 @@ pub struct Record<'a> {
|
||||
pub level: log::Level,
|
||||
pub message: &'a std::fmt::Arguments<'a>,
|
||||
pub module_path: Option<&'a str>,
|
||||
pub line: Option<u32>,
|
||||
}
|
||||
|
||||
pub fn init_output_stdout() {
|
||||
@@ -105,7 +106,11 @@ static LEVEL_ANSI_COLORS: [&str; 6] = [
|
||||
];
|
||||
|
||||
// PERF: batching
|
||||
pub fn submit(record: Record) {
|
||||
pub fn submit(mut record: Record) {
|
||||
if record.module_path.is_none_or(|p| !p.ends_with(".rs")) {
|
||||
// Only render line numbers for actual rust files emitted by `log_err` and friends
|
||||
record.line.take();
|
||||
}
|
||||
if ENABLED_SINKS_STDOUT.load(Ordering::Acquire) {
|
||||
let mut stdout = std::io::stdout().lock();
|
||||
_ = writeln!(
|
||||
@@ -117,6 +122,7 @@ pub fn submit(record: Record) {
|
||||
SourceFmt {
|
||||
scope: record.scope,
|
||||
module_path: record.module_path,
|
||||
line: record.line,
|
||||
ansi: true,
|
||||
},
|
||||
record.message
|
||||
@@ -132,6 +138,7 @@ pub fn submit(record: Record) {
|
||||
SourceFmt {
|
||||
scope: record.scope,
|
||||
module_path: record.module_path,
|
||||
line: record.line,
|
||||
ansi: true,
|
||||
},
|
||||
record.message
|
||||
@@ -167,6 +174,7 @@ pub fn submit(record: Record) {
|
||||
SourceFmt {
|
||||
scope: record.scope,
|
||||
module_path: record.module_path,
|
||||
line: record.line,
|
||||
ansi: false,
|
||||
},
|
||||
record.message
|
||||
@@ -202,6 +210,7 @@ pub fn flush() {
|
||||
struct SourceFmt<'a> {
|
||||
scope: Scope,
|
||||
module_path: Option<&'a str>,
|
||||
line: Option<u32>,
|
||||
ansi: bool,
|
||||
}
|
||||
|
||||
@@ -225,6 +234,10 @@ impl std::fmt::Display for SourceFmt<'_> {
|
||||
f.write_str(subscope)?;
|
||||
}
|
||||
}
|
||||
if let Some(line) = self.line {
|
||||
f.write_char(':')?;
|
||||
line.fmt(f)?;
|
||||
}
|
||||
if self.ansi {
|
||||
f.write_str(ANSI_RESET)?;
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ impl log::Log for Zlog {
|
||||
None => (private::scope_new(&[]), private::scope_new(&["*unknown*"])),
|
||||
};
|
||||
let level = record.metadata().level();
|
||||
if !filter::is_scope_enabled(&crate_name_scope, record.module_path(), level) {
|
||||
if !filter::is_scope_enabled(&crate_name_scope, Some(record.target()), level) {
|
||||
return;
|
||||
}
|
||||
sink::submit(sink::Record {
|
||||
@@ -89,6 +89,7 @@ impl log::Log for Zlog {
|
||||
message: record.args(),
|
||||
// PERF(batching): store non-static paths in a cache + leak them and pass static str here
|
||||
module_path: record.module_path().or(record.file()),
|
||||
line: record.line(),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -109,6 +110,7 @@ macro_rules! log {
|
||||
level,
|
||||
message: &format_args!($($arg)+),
|
||||
module_path: Some(module_path!()),
|
||||
line: Some(line!()),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -291,7 +293,7 @@ impl log::Log for Logger {
|
||||
return;
|
||||
}
|
||||
let level = record.metadata().level();
|
||||
if !filter::is_scope_enabled(&self.scope, record.module_path(), level) {
|
||||
if !filter::is_scope_enabled(&self.scope, Some(record.target()), level) {
|
||||
return;
|
||||
}
|
||||
sink::submit(sink::Record {
|
||||
@@ -299,6 +301,7 @@ impl log::Log for Logger {
|
||||
level,
|
||||
message: record.args(),
|
||||
module_path: record.module_path(),
|
||||
line: record.line(),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user