Skip to content

Commit 06b308f

Browse files
tomakadpc
authored andcommitted
Expose the serialization capabilities
1 parent 337b9e5 commit 06b308f

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

lib.rs

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -280,22 +280,7 @@ where
280280
let join = builder.spawn(move || loop {
281281
match rx.recv().unwrap() {
282282
AsyncMsg::Record(r) => {
283-
let rs = RecordStatic {
284-
location: &*r.location,
285-
level: r.level,
286-
tag: &r.tag,
287-
};
288-
289-
drain
290-
.log(
291-
&Record::new(
292-
&rs,
293-
&format_args!("{}", r.msg),
294-
BorrowedKV(&r.kv),
295-
),
296-
&r.logger_values,
297-
)
298-
.unwrap();
283+
r.to_drain(&drain).unwrap();
299284
}
300285
AsyncMsg::Finish => return,
301286
}
@@ -461,18 +446,12 @@ impl Drain for AsyncCore {
461446
.serialize(record, &mut ser)
462447
.expect("`ToSendSerializer` can't fail");
463448

464-
self.send(AsyncRecord {
465-
msg: fmt::format(*record.msg()),
466-
level: record.level(),
467-
location: Box::new(*record.location()),
468-
tag: String::from(record.tag()),
469-
logger_values: logger_values.clone(),
470-
kv: ser.finish(),
471-
})
449+
self.send(AsyncRecord::from(record, logger_values))
472450
}
473451
}
474452

475-
struct AsyncRecord {
453+
/// Serialized record.
454+
pub struct AsyncRecord {
476455
msg: String,
477456
level: Level,
478457
location: Box<slog::RecordLocation>,
@@ -481,6 +460,45 @@ struct AsyncRecord {
481460
kv: Box<dyn KV + Send>,
482461
}
483462

463+
impl AsyncRecord {
464+
/// Serializes a `Record` and an `OwnedKVList`.
465+
pub fn from(record: &Record, logger_values: &OwnedKVList) -> Self {
466+
let mut ser = ToSendSerializer::new();
467+
record
468+
.kv()
469+
.serialize(record, &mut ser)
470+
.expect("`ToSendSerializer` can't fail");
471+
472+
AsyncRecord {
473+
msg: fmt::format(*record.msg()),
474+
level: record.level(),
475+
location: Box::new(*record.location()),
476+
tag: String::from(record.tag()),
477+
logger_values: logger_values.clone(),
478+
kv: ser.finish(),
479+
}
480+
}
481+
482+
/// Writes the record to a `Drain`.
483+
pub fn to_drain<D: Drain>(self, drain: &D) -> Result<D::Ok, D::Err> {
484+
let rs = RecordStatic {
485+
location: &*self.location,
486+
level: self.level,
487+
tag: &self.tag,
488+
};
489+
490+
drain
491+
.log(
492+
&Record::new(
493+
&rs,
494+
&format_args!("{}", self.msg),
495+
BorrowedKV(&self.kv),
496+
),
497+
&self.logger_values,
498+
)
499+
}
500+
}
501+
484502
enum AsyncMsg {
485503
Record(AsyncRecord),
486504
Finish,

0 commit comments

Comments
 (0)