@@ -22,7 +22,7 @@ use crate::{kv, Level, SourceLocation};
22
22
// possible to correct.
23
23
#[ derive( Clone , Debug ) ]
24
24
pub struct Record < ' a > {
25
- logger_name : Option < Cow < ' a , str > > ,
25
+ logger_name : Option < & ' a str > ,
26
26
payload : Cow < ' a , str > ,
27
27
kvs : Cow < ' a , [ kv:: Pair < ' a > ] > ,
28
28
inner : Cow < ' a , RecordInner > ,
@@ -46,7 +46,7 @@ impl<'a> Record<'a> {
46
46
kvs : & ' a [ ( kv:: Key < ' a > , kv:: Value < ' a > ) ] ,
47
47
) -> Record < ' a > {
48
48
Record {
49
- logger_name : logger_name . map ( Cow :: Borrowed ) ,
49
+ logger_name,
50
50
payload : payload. into ( ) ,
51
51
kvs : Cow :: Borrowed ( kvs) ,
52
52
inner : Cow :: Owned ( RecordInner {
@@ -62,7 +62,7 @@ impl<'a> Record<'a> {
62
62
#[ must_use]
63
63
pub fn to_owned ( & self ) -> RecordOwned {
64
64
RecordOwned {
65
- logger_name : self . logger_name . clone ( ) . map ( |n| n. into_owned ( ) ) ,
65
+ logger_name : self . logger_name . map ( |n| n. to_owned ( ) ) ,
66
66
payload : self . payload . to_string ( ) ,
67
67
kvs : self
68
68
. kvs
@@ -126,47 +126,6 @@ impl<'a> Record<'a> {
126
126
}
127
127
}
128
128
129
- #[ cfg( feature = "log" ) ]
130
- #[ must_use]
131
- pub ( crate ) fn from_log_crate_record (
132
- logger : & ' a crate :: Logger ,
133
- record : & ' a log:: Record ,
134
- time : SystemTime ,
135
- ) -> Self {
136
- let args = record. args ( ) ;
137
-
138
- Self {
139
- // If the logger has a name configured, use that name. Otherwise, the name can also be
140
- // given by the target of the log record.
141
- logger_name : logger. name ( ) . map ( Cow :: Borrowed ) . or_else ( || {
142
- let log_target = record. target ( ) ;
143
- if log_target. is_empty ( ) {
144
- None
145
- } else {
146
- Some ( Cow :: Owned ( String :: from ( log_target) ) )
147
- }
148
- } ) ,
149
- kvs : {
150
- let kvs = record. key_values ( ) ;
151
- let mut cvt = kv:: LogCrateConverter :: new ( kvs. count ( ) ) ;
152
- assert ! ( kvs. visit( & mut cvt) . is_ok( ) ) ;
153
- cvt. finalize ( )
154
- } ,
155
- payload : match args. as_str ( ) {
156
- Some ( literal_str) => literal_str. into ( ) ,
157
- None => args. to_string ( ) . into ( ) ,
158
- } ,
159
- inner : Cow :: Owned ( RecordInner {
160
- level : record. level ( ) . into ( ) ,
161
- source_location : SourceLocation :: from_log_crate_record ( record) ,
162
- time,
163
- // For records from `log` crate, they never seem to come from different threads, so
164
- // getting the current TID here should be correct
165
- tid : get_current_tid ( ) ,
166
- } ) ,
167
- }
168
- }
169
-
170
129
#[ cfg( test) ]
171
130
pub ( crate ) fn set_time ( & mut self , new : SystemTime ) {
172
131
self . inner . to_mut ( ) . time = new;
@@ -189,7 +148,7 @@ impl RecordOwned {
189
148
#[ must_use]
190
149
pub fn as_ref ( & self ) -> Record {
191
150
Record {
192
- logger_name : self . logger_name . as_deref ( ) . map ( Cow :: Borrowed ) ,
151
+ logger_name : self . logger_name . as_deref ( ) ,
193
152
payload : Cow :: Borrowed ( & self . payload ) ,
194
153
kvs : Cow :: Owned (
195
154
self . kvs
@@ -245,6 +204,65 @@ impl RecordOwned {
245
204
// When adding more getters, also add to `Record`
246
205
}
247
206
207
+ #[ cfg( feature = "log" ) ]
208
+ #[ derive( Clone , Debug ) ]
209
+ pub ( crate ) struct LogCrateRecord < ' a > {
210
+ logger_name : Option < & ' a str > ,
211
+ payload : Cow < ' a , str > ,
212
+ kvs : Vec < ( log:: kv:: Key < ' a > , kv:: ValueOwned ) > ,
213
+ inner : Cow < ' a , RecordInner > ,
214
+ }
215
+
216
+ #[ cfg( feature = "log" ) ]
217
+ impl < ' a > LogCrateRecord < ' a > {
218
+ #[ must_use]
219
+ pub ( crate ) fn new (
220
+ logger : & ' a crate :: Logger ,
221
+ record : & ' a log:: Record ,
222
+ time : SystemTime ,
223
+ ) -> Self {
224
+ let args = record. args ( ) ;
225
+
226
+ Self {
227
+ // If the logger has a name configured, use that name. Otherwise, the name can also be
228
+ // given by the target of the log record.
229
+ logger_name : logger. name ( ) . or_else ( || Some ( record. target ( ) ) ) ,
230
+ kvs : {
231
+ let kvs = record. key_values ( ) ;
232
+ let mut cvt = kv:: LogCrateConverter :: new ( kvs. count ( ) ) ;
233
+ assert ! ( kvs. visit( & mut cvt) . is_ok( ) ) ;
234
+ cvt. finalize ( )
235
+ } ,
236
+ payload : match args. as_str ( ) {
237
+ Some ( literal_str) => literal_str. into ( ) ,
238
+ None => args. to_string ( ) . into ( ) ,
239
+ } ,
240
+ inner : Cow :: Owned ( RecordInner {
241
+ level : record. level ( ) . into ( ) ,
242
+ source_location : SourceLocation :: from_log_crate_record ( record) ,
243
+ time,
244
+ // For records from `log` crate, they never seem to come from different threads, so
245
+ // getting the current TID here should be correct
246
+ tid : get_current_tid ( ) ,
247
+ } ) ,
248
+ }
249
+ }
250
+
251
+ #[ must_use]
252
+ pub ( crate ) fn as_record ( & self ) -> Record {
253
+ Record {
254
+ logger_name : self . logger_name ,
255
+ payload : self . payload . clone ( ) ,
256
+ kvs : self
257
+ . kvs
258
+ . iter ( )
259
+ . map ( |( k, v) | ( kv:: Key :: from_str ( k. as_str ( ) ) , v. by_ref ( ) ) )
260
+ . collect ( ) ,
261
+ inner : self . inner . clone ( ) ,
262
+ }
263
+ }
264
+ }
265
+
248
266
fn get_current_tid ( ) -> u64 {
249
267
#[ cfg( target_os = "linux" ) ]
250
268
#[ must_use]
0 commit comments