Skip to content

Commit 0347e04

Browse files
committed
feat: address costly log record re-allocation
- allocates Strings only when necessary with `uniffi` feature, otherwise, keeps LogRecord fileds as lifetime references.
1 parent e968562 commit 0347e04

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/logger.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,29 @@ pub use lightning::util::logger::Level as LogLevel;
1515
use chrono::Utc;
1616
use log::{debug, error, info, trace, warn};
1717

18+
#[cfg(not(feature = "uniffi"))]
19+
use core::fmt;
1820
use std::fmt::Debug;
1921
use std::fs;
2022
use std::io::Write;
2123
use std::path::Path;
2224
use std::sync::Arc;
2325

24-
/// A unit of logging output with Metadata to enable filtering Module_path,
26+
/// A unit of logging output with Metadata to enable filtering module_path,
2527
/// file, line to inform on log's source.
28+
#[cfg(not(feature = "uniffi"))]
29+
pub struct LogRecord<'a> {
30+
/// The verbosity level of the message.
31+
pub level: LogLevel,
32+
/// The message body.
33+
pub args: fmt::Arguments<'a>,
34+
/// The module path of the message.
35+
pub module_path: &'a str,
36+
/// The line containing the message.
37+
pub line: u32,
38+
}
39+
40+
#[cfg(feature = "uniffi")]
2641
pub struct LogRecord {
2742
/// The verbosity level of the message.
2843
pub level: LogLevel,
@@ -34,6 +49,7 @@ pub struct LogRecord {
3449
pub line: u32,
3550
}
3651

52+
#[cfg(feature = "uniffi")]
3753
impl<'a> From<Record<'a>> for LogRecord {
3854
fn from(record: Record) -> Self {
3955
Self {
@@ -45,8 +61,27 @@ impl<'a> From<Record<'a>> for LogRecord {
4561
}
4662
}
4763

64+
#[cfg(not(feature = "uniffi"))]
65+
impl<'a> From<Record<'a>> for LogRecord<'a> {
66+
fn from(record: Record<'a>) -> Self {
67+
Self {
68+
level: record.level,
69+
args: record.args,
70+
module_path: record.module_path,
71+
line: record.line,
72+
}
73+
}
74+
}
75+
4876
/// LogWriter trait encapsulating the operations required of a
4977
/// logger's writer.
78+
#[cfg(not(feature = "uniffi"))]
79+
pub trait LogWriter: Send + Sync + Debug {
80+
/// Log the record.
81+
fn log<'a>(&self, record: LogRecord<'a>);
82+
}
83+
84+
#[cfg(feature = "uniffi")]
5085
pub trait LogWriter: Send + Sync + Debug {
5186
/// Log the record.
5287
fn log(&self, record: LogRecord);

0 commit comments

Comments
 (0)