Skip to content

Commit 3f2926c

Browse files
committed
f: differentiate test and prod log output
1 parent e069743 commit 3f2926c

File tree

1 file changed

+50
-16
lines changed

1 file changed

+50
-16
lines changed

lightning/src/util/logger.rs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,25 +162,59 @@ impl<$($args)?> Display for Record<$($args)?> {
162162
write!(&mut context_formatter, "{:<5} [{}:{}]", self.level, self.module_path, self.line)?;
163163
context_formatter.pad_remaining()?;
164164

165-
let mut peer_formatter = SubstringFormatter::new(8, f);
166-
if let Some(peer_id) = self.peer_id {
167-
write!(peer_formatter, "p:{}", peer_id)?;
168-
}
169-
peer_formatter.pad_remaining()?;
170-
171-
let mut channel_formatter = SubstringFormatter::new(10, f);
165+
let mut channel_formatter = SubstringFormatter::new(9, f);
172166
if let Some(channel_id) = self.channel_id {
173-
write!(channel_formatter, " ch:{}", channel_id)?;
167+
write!(channel_formatter, "ch:{}", channel_id)?;
174168
}
175169
channel_formatter.pad_remaining()?;
176170

177-
let mut payment_formatter = SubstringFormatter::new(9, f);
178-
if let Some(payment_hash) = self.payment_hash {
179-
write!(payment_formatter, " h:{}", payment_hash)?;
171+
#[cfg(not(test))]
172+
{
173+
let mut peer_formatter = SubstringFormatter::new(9, f);
174+
if let Some(peer_id) = self.peer_id {
175+
write!(peer_formatter, " p:{}", peer_id)?;
176+
}
177+
peer_formatter.pad_remaining()?;
178+
179+
let mut payment_formatter = SubstringFormatter::new(9, f);
180+
if let Some(payment_hash) = self.payment_hash {
181+
write!(payment_formatter, " h:{}", payment_hash)?;
182+
}
183+
payment_formatter.pad_remaining()?;
184+
185+
write!(f, " {}", self.args)
180186
}
181-
payment_formatter.pad_remaining()?;
182187

183-
write!(f, " {}", self.args)
188+
#[cfg(test)]
189+
{
190+
write!(f, " {}", self.args)?;
191+
192+
let mut open_bracket_written = false;
193+
if let Some(peer_id) = self.peer_id {
194+
write!(f, " [")?;
195+
open_bracket_written = true;
196+
let mut peer_formatter = SubstringFormatter::new(8, f);
197+
write!(peer_formatter, "p:{}", peer_id)?;
198+
}
199+
200+
if let Some(payment_hash) = self.payment_hash {
201+
if !open_bracket_written {
202+
write!(f, " [")?;
203+
open_bracket_written = true;
204+
} else {
205+
write!(f, " ")?;
206+
}
207+
208+
let mut payment_formatter = SubstringFormatter::new(8, f);
209+
write!(payment_formatter, "h:{}", payment_hash)?;
210+
}
211+
212+
if open_bracket_written {
213+
write!(f, "]")?;
214+
}
215+
216+
Ok(())
217+
}
184218
}
185219
}
186220
} }
@@ -191,19 +225,19 @@ impl_record!(, 'a);
191225

192226
// Writes only up to a certain number of unicode characters to the underlying formatter. This handles multi-byte Unicode
193227
// characters safely.
194-
struct SubstringFormatter<'fmt: 'r, 'r> {
228+
pub(crate) struct SubstringFormatter<'fmt: 'r, 'r> {
195229
remaining_chars: usize,
196230
fmt: &'r mut fmt::Formatter<'fmt>,
197231
}
198232

199233
impl<'fmt: 'r, 'r> SubstringFormatter<'fmt, 'r> {
200-
fn new(length: usize, formatter: &'r mut fmt::Formatter<'fmt>) -> Self {
234+
pub(crate) fn new(length: usize, formatter: &'r mut fmt::Formatter<'fmt>) -> Self {
201235
debug_assert!(length <= 100);
202236
SubstringFormatter { remaining_chars: length, fmt: formatter }
203237
}
204238

205239
// Pads the underlying formatter with spaces until the remaining character count.
206-
fn pad_remaining(&mut self) -> fmt::Result {
240+
pub(crate) fn pad_remaining(&mut self) -> fmt::Result {
207241
// Use a constant string to avoid allocations.
208242
const PAD100: &str = " "; // 100 spaces
209243

0 commit comments

Comments
 (0)