Skip to content

Commit 016eb96

Browse files
committed
Support logger::Record in C by String-ing the fmt::Arguments
This adds a new (non-feature) cfg argument `c_bindings` which will be set when building C bindings. With this, we can (slightly) tweak behavior and API based on whether we are being built for Rust or C users. Ideally we'd never need this, but as long as we can keep the API consistent-enough to avoid material code drift, this gives us a cheap way of doing the "right" thing for both C and Rust when the two are in tension. We also move lightning-background-processor to support the same MSRV as the main lightning crate, instead of only lightning-net-tokio's MSRV.
1 parent ba50dd5 commit 016eb96

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

.github/workflows/build.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ jobs:
115115
cargo test --verbose --color always -p lightning
116116
cargo test --verbose --color always -p lightning-invoice
117117
cargo build --verbose --color always -p lightning-persister
118+
cargo build --verbose --color always -p lightning-background-processor
119+
- name: Test C Bindings Modifications on Rust ${{ matrix.toolchain }}
120+
if: "! matrix.build-net-tokio"
121+
run: |
122+
RUSTFLAGS="--cfg=c_bindings" cargo test --verbose --color always -p lightning
123+
RUSTFLAGS="--cfg=c_bindings" cargo test --verbose --color always -p lightning-invoice
124+
RUSTFLAGS="--cfg=c_bindings" cargo build --verbose --color always -p lightning-persister
125+
RUSTFLAGS="--cfg=c_bindings" cargo build --verbose --color always -p lightning-background-processor
118126
- name: Test Block Sync Clients on Rust ${{ matrix.toolchain }} with features
119127
if: "matrix.build-net-tokio && !matrix.coverage"
120128
run: |

lightning/src/util/logger.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,32 +86,45 @@ impl Level {
8686

8787
/// A Record, unit of logging output with Metadata to enable filtering
8888
/// Module_path, file, line to inform on log's source
89-
/// (C-not exported) - we convert to a const char* instead
90-
#[derive(Clone,Debug)]
89+
#[derive(Clone, Debug)]
9190
pub struct Record<'a> {
9291
/// The verbosity level of the message.
9392
pub level: Level,
93+
#[cfg(not(c_bindings))]
9494
/// The message body.
9595
pub args: fmt::Arguments<'a>,
96+
#[cfg(c_bindings)]
97+
/// The message body.
98+
pub args: String,
9699
/// The module path of the message.
97-
pub module_path: &'a str,
100+
pub module_path: &'static str,
98101
/// The source file containing the message.
99-
pub file: &'a str,
102+
pub file: &'static str,
100103
/// The line containing the message.
101104
pub line: u32,
105+
106+
#[cfg(c_bindings)]
107+
/// We don't actually use the lifetime parameter in C bindings (as there is no good way to
108+
/// communicate a lifetime to a C, or worse, Java user).
109+
_phantom: core::marker::PhantomData<&'a ()>,
102110
}
103111

104112
impl<'a> Record<'a> {
105113
/// Returns a new Record.
106114
/// (C-not exported) as fmt can't be used in C
107115
#[inline]
108-
pub fn new(level: Level, args: fmt::Arguments<'a>, module_path: &'a str, file: &'a str, line: u32) -> Record<'a> {
116+
pub fn new(level: Level, args: fmt::Arguments<'a>, module_path: &'static str, file: &'static str, line: u32) -> Record<'a> {
109117
Record {
110118
level,
119+
#[cfg(not(c_bindings))]
111120
args,
121+
#[cfg(c_bindings)]
122+
args: format!("{}", args),
112123
module_path,
113124
file,
114-
line
125+
line,
126+
#[cfg(c_bindings)]
127+
_phantom: core::marker::PhantomData,
115128
}
116129
}
117130
}

0 commit comments

Comments
 (0)