@@ -33,12 +33,15 @@ static ENABLED_SINKS_STDERR: AtomicBool = AtomicBool::new(false);
3333static SINK_FILE_SIZE_BYTES : AtomicU64 = AtomicU64 :: new ( 0 ) ;
3434/// Maximum size of the log file before it will be rotated, in bytes.
3535const 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
3739pub 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
4447pub 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+
5461pub 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() {
202215struct 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 }
0 commit comments