Skip to content

Commit 99a139e

Browse files
committed
zlog: Add env var to enable line number logging
1 parent 6a38d69 commit 99a139e

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

crates/zed/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ pub fn main() {
242242
zlog::init_output_stdout();
243243
};
244244
}
245+
if std::env::var_os("ZED_LOG_LINE_NUMBERS").is_some() {
246+
zlog::enable_line_numbers();
247+
}
245248

246249
let app_version = AppVersion::load(env!("CARGO_PKG_VERSION"));
247250
let app_commit_sha =

crates/zlog/src/sink.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ static ENABLED_SINKS_STDERR: AtomicBool = AtomicBool::new(false);
3333
static SINK_FILE_SIZE_BYTES: AtomicU64 = AtomicU64::new(0);
3434
/// Maximum size of the log file before it will be rotated, in bytes.
3535
const SINK_FILE_SIZE_BYTES_MAX: u64 = 1024 * 1024; // 1 MB
36+
/// Whether line numbers are enabled.
37+
static ENABLED_LINE_NUMBERS: AtomicBool = AtomicBool::new(false);
3638

3739
pub struct Record<'a> {
3840
pub scope: Scope,
3941
pub level: log::Level,
4042
pub message: &'a std::fmt::Arguments<'a>,
4143
pub module_path: Option<&'a str>,
44+
pub line: Option<u32>,
4245
}
4346

4447
pub fn init_output_stdout() {
@@ -51,6 +54,10 @@ pub fn init_output_stderr() {
5154
ENABLED_SINKS_STDERR.store(true, Ordering::Release);
5255
}
5356

57+
pub fn enable_line_numbers() {
58+
ENABLED_LINE_NUMBERS.store(true, Ordering::Release);
59+
}
60+
5461
pub fn init_output_file(
5562
path: &'static PathBuf,
5663
path_rotate: Option<&'static PathBuf>,
@@ -105,7 +112,10 @@ static LEVEL_ANSI_COLORS: [&str; 6] = [
105112
];
106113

107114
// PERF: batching
108-
pub fn submit(record: Record) {
115+
pub fn submit(mut record: Record) {
116+
if ENABLED_LINE_NUMBERS.load(Ordering::Acquire) {
117+
record.line.take();
118+
}
109119
if ENABLED_SINKS_STDOUT.load(Ordering::Acquire) {
110120
let mut stdout = std::io::stdout().lock();
111121
_ = writeln!(
@@ -117,6 +127,7 @@ pub fn submit(record: Record) {
117127
SourceFmt {
118128
scope: record.scope,
119129
module_path: record.module_path,
130+
line: record.line,
120131
ansi: true,
121132
},
122133
record.message
@@ -132,6 +143,7 @@ pub fn submit(record: Record) {
132143
SourceFmt {
133144
scope: record.scope,
134145
module_path: record.module_path,
146+
line: record.line,
135147
ansi: true,
136148
},
137149
record.message
@@ -167,6 +179,7 @@ pub fn submit(record: Record) {
167179
SourceFmt {
168180
scope: record.scope,
169181
module_path: record.module_path,
182+
line: record.line,
170183
ansi: false,
171184
},
172185
record.message
@@ -202,6 +215,7 @@ pub fn flush() {
202215
struct SourceFmt<'a> {
203216
scope: Scope,
204217
module_path: Option<&'a str>,
218+
line: Option<u32>,
205219
ansi: bool,
206220
}
207221

@@ -225,6 +239,10 @@ impl std::fmt::Display for SourceFmt<'_> {
225239
f.write_str(subscope)?;
226240
}
227241
}
242+
if let Some(line) = self.line {
243+
f.write_char(':')?;
244+
line.fmt(f)?;
245+
}
228246
if self.ansi {
229247
f.write_str(ANSI_RESET)?;
230248
}

crates/zlog/src/zlog.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ mod env_config;
55
pub mod filter;
66
pub mod sink;
77

8-
pub use sink::{flush, init_output_file, init_output_stderr, init_output_stdout};
8+
pub use sink::{
9+
enable_line_numbers, flush, init_output_file, init_output_stderr, init_output_stdout,
10+
};
911

1012
pub const SCOPE_DEPTH_MAX: usize = 4;
1113

@@ -89,6 +91,7 @@ impl log::Log for Zlog {
8991
message: record.args(),
9092
// PERF(batching): store non-static paths in a cache + leak them and pass static str here
9193
module_path: record.module_path().or(record.file()),
94+
line: record.line(),
9295
});
9396
}
9497

@@ -109,6 +112,7 @@ macro_rules! log {
109112
level,
110113
message: &format_args!($($arg)+),
111114
module_path: Some(module_path!()),
115+
line: Some(line!()),
112116
});
113117
}
114118
}
@@ -299,6 +303,7 @@ impl log::Log for Logger {
299303
level,
300304
message: record.args(),
301305
module_path: record.module_path(),
306+
line: record.line(),
302307
});
303308
}
304309

0 commit comments

Comments
 (0)