Skip to content

Commit ce32f29

Browse files
Veykriltomatitito
authored andcommitted
zlog: Add env var to enable line number logging (zed-industries#41905)
Release Notes: - N/A *or* Added/Fixed/Improved ...
1 parent d7bca3c commit ce32f29

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

crates/gpui/src/executor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ impl BackgroundExecutor {
281281
});
282282
let mut cx = std::task::Context::from_waker(&waker);
283283

284-
let duration = Duration::from_secs(500);
284+
let duration = Duration::from_secs(180);
285285
let mut test_should_end_by = Instant::now() + duration;
286286

287287
loop {

crates/util/src/util.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -611,17 +611,21 @@ where
611611
let file = caller.file().replace('\\', "/");
612612
// In this codebase all crates reside in a `crates` directory,
613613
// so discard the prefix up to that segment to find the crate name
614-
let target = file
615-
.split_once("crates/")
616-
.and_then(|(_, s)| s.split_once("/src/"));
614+
let file = file.split_once("crates/");
615+
let target = file.as_ref().and_then(|(_, s)| s.split_once("/src/"));
617616

618617
let module_path = target.map(|(krate, module)| {
619-
krate.to_owned() + "::" + &module.trim_end_matches(".rs").replace('/', "::")
618+
if module.starts_with(krate) {
619+
module.trim_end_matches(".rs").replace('/', "::")
620+
} else {
621+
krate.to_owned() + "::" + &module.trim_end_matches(".rs").replace('/', "::")
622+
}
620623
});
624+
let file = file.map(|(_, file)| format!("crates/{file}"));
621625
log::logger().log(
622626
&log::Record::builder()
623-
.target(target.map_or("", |(krate, _)| krate))
624-
.module_path(module_path.as_deref())
627+
.target(module_path.as_deref().unwrap_or(""))
628+
.module_path(file.as_deref())
625629
.args(format_args!("{:?}", error))
626630
.file(Some(caller.file()))
627631
.line(Some(caller.line()))

crates/zlog/src/sink.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub struct Record<'a> {
3939
pub level: log::Level,
4040
pub message: &'a std::fmt::Arguments<'a>,
4141
pub module_path: Option<&'a str>,
42+
pub line: Option<u32>,
4243
}
4344

4445
pub fn init_output_stdout() {
@@ -105,7 +106,11 @@ static LEVEL_ANSI_COLORS: [&str; 6] = [
105106
];
106107

107108
// PERF: batching
108-
pub fn submit(record: Record) {
109+
pub fn submit(mut record: Record) {
110+
if record.module_path.is_none_or(|p| !p.ends_with(".rs")) {
111+
// Only render line numbers for actual rust files emitted by `log_err` and friends
112+
record.line.take();
113+
}
109114
if ENABLED_SINKS_STDOUT.load(Ordering::Acquire) {
110115
let mut stdout = std::io::stdout().lock();
111116
_ = writeln!(
@@ -117,6 +122,7 @@ pub fn submit(record: Record) {
117122
SourceFmt {
118123
scope: record.scope,
119124
module_path: record.module_path,
125+
line: record.line,
120126
ansi: true,
121127
},
122128
record.message
@@ -132,6 +138,7 @@ pub fn submit(record: Record) {
132138
SourceFmt {
133139
scope: record.scope,
134140
module_path: record.module_path,
141+
line: record.line,
135142
ansi: true,
136143
},
137144
record.message
@@ -167,6 +174,7 @@ pub fn submit(record: Record) {
167174
SourceFmt {
168175
scope: record.scope,
169176
module_path: record.module_path,
177+
line: record.line,
170178
ansi: false,
171179
},
172180
record.message
@@ -202,6 +210,7 @@ pub fn flush() {
202210
struct SourceFmt<'a> {
203211
scope: Scope,
204212
module_path: Option<&'a str>,
213+
line: Option<u32>,
205214
ansi: bool,
206215
}
207216

@@ -225,6 +234,10 @@ impl std::fmt::Display for SourceFmt<'_> {
225234
f.write_str(subscope)?;
226235
}
227236
}
237+
if let Some(line) = self.line {
238+
f.write_char(':')?;
239+
line.fmt(f)?;
240+
}
228241
if self.ansi {
229242
f.write_str(ANSI_RESET)?;
230243
}

crates/zlog/src/zlog.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl log::Log for Zlog {
8080
None => (private::scope_new(&[]), private::scope_new(&["*unknown*"])),
8181
};
8282
let level = record.metadata().level();
83-
if !filter::is_scope_enabled(&crate_name_scope, record.module_path(), level) {
83+
if !filter::is_scope_enabled(&crate_name_scope, Some(record.target()), level) {
8484
return;
8585
}
8686
sink::submit(sink::Record {
@@ -89,6 +89,7 @@ impl log::Log for Zlog {
8989
message: record.args(),
9090
// PERF(batching): store non-static paths in a cache + leak them and pass static str here
9191
module_path: record.module_path().or(record.file()),
92+
line: record.line(),
9293
});
9394
}
9495

@@ -109,6 +110,7 @@ macro_rules! log {
109110
level,
110111
message: &format_args!($($arg)+),
111112
module_path: Some(module_path!()),
113+
line: Some(line!()),
112114
});
113115
}
114116
}
@@ -291,14 +293,15 @@ impl log::Log for Logger {
291293
return;
292294
}
293295
let level = record.metadata().level();
294-
if !filter::is_scope_enabled(&self.scope, record.module_path(), level) {
296+
if !filter::is_scope_enabled(&self.scope, Some(record.target()), level) {
295297
return;
296298
}
297299
sink::submit(sink::Record {
298300
scope: self.scope,
299301
level,
300302
message: record.args(),
301303
module_path: record.module_path(),
304+
line: record.line(),
302305
});
303306
}
304307

0 commit comments

Comments
 (0)