Skip to content

Commit 6169a5a

Browse files
committed
refactor(fmt): Pull out DefaultFormat
1 parent 1e955bc commit 6169a5a

File tree

2 files changed

+57
-47
lines changed

2 files changed

+57
-47
lines changed

src/fmt/mod.rs

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -202,18 +202,10 @@ impl fmt::Debug for Formatter {
202202

203203
pub(crate) type FormatFn = Box<dyn Fn(&mut Formatter, &Record<'_>) -> io::Result<()> + Sync + Send>;
204204

205+
#[derive(Default)]
205206
pub(crate) struct Builder {
206-
pub(crate) format_timestamp: Option<TimestampPrecision>,
207-
pub(crate) format_module_path: bool,
208-
pub(crate) format_target: bool,
209-
pub(crate) format_level: bool,
210-
pub(crate) format_indent: Option<usize>,
207+
pub(crate) default_format: DefaultFormat,
211208
pub(crate) custom_format: Option<FormatFn>,
212-
pub(crate) format_suffix: &'static str,
213-
pub(crate) format_file: bool,
214-
pub(crate) format_line_number: bool,
215-
#[cfg(feature = "kv")]
216-
pub(crate) kv_format: Option<Box<KvFormatFn>>,
217209
built: bool,
218210
}
219211

@@ -239,17 +231,21 @@ impl Builder {
239231
} else {
240232
Box::new(move |buf, record| {
241233
let fmt = DefaultFormatWriter {
242-
timestamp: built.format_timestamp,
243-
module_path: built.format_module_path,
244-
target: built.format_target,
245-
level: built.format_level,
234+
timestamp: built.default_format.timestamp,
235+
module_path: built.default_format.module_path,
236+
target: built.default_format.target,
237+
level: built.default_format.level,
246238
written_header_value: false,
247-
indent: built.format_indent,
248-
suffix: built.format_suffix,
249-
source_file: built.format_file,
250-
source_line_number: built.format_line_number,
239+
indent: built.default_format.indent,
240+
suffix: built.default_format.suffix,
241+
source_file: built.default_format.source_file,
242+
source_line_number: built.default_format.source_line_number,
251243
#[cfg(feature = "kv")]
252-
kv_format: built.kv_format.as_deref().unwrap_or(&default_kv_format),
244+
kv_format: built
245+
.default_format
246+
.kv_format
247+
.as_deref()
248+
.unwrap_or(&default_kv_format),
253249
buf,
254250
};
255251

@@ -259,25 +255,6 @@ impl Builder {
259255
}
260256
}
261257

262-
impl Default for Builder {
263-
fn default() -> Self {
264-
Builder {
265-
format_timestamp: Some(Default::default()),
266-
format_module_path: false,
267-
format_target: true,
268-
format_level: true,
269-
format_file: false,
270-
format_line_number: false,
271-
format_indent: Some(4),
272-
custom_format: None,
273-
format_suffix: "\n",
274-
#[cfg(feature = "kv")]
275-
kv_format: None,
276-
built: false,
277-
}
278-
}
279-
}
280-
281258
#[cfg(feature = "color")]
282259
type SubtleStyle = StyledValue<&'static str>;
283260
#[cfg(not(feature = "color"))]
@@ -307,6 +284,39 @@ impl<T: Display> Display for StyledValue<T> {
307284
#[cfg(not(feature = "color"))]
308285
type StyledValue<T> = T;
309286

287+
/// The default format.
288+
///
289+
/// This format needs to work with any combination of crate features.
290+
pub(crate) struct DefaultFormat {
291+
pub(crate) timestamp: Option<TimestampPrecision>,
292+
pub(crate) module_path: bool,
293+
pub(crate) target: bool,
294+
pub(crate) level: bool,
295+
pub(crate) source_file: bool,
296+
pub(crate) source_line_number: bool,
297+
pub(crate) indent: Option<usize>,
298+
pub(crate) suffix: &'static str,
299+
#[cfg(feature = "kv")]
300+
pub(crate) kv_format: Option<Box<KvFormatFn>>,
301+
}
302+
303+
impl Default for DefaultFormat {
304+
fn default() -> Self {
305+
Self {
306+
timestamp: Some(Default::default()),
307+
module_path: false,
308+
target: true,
309+
level: true,
310+
source_file: false,
311+
source_line_number: false,
312+
indent: Some(4),
313+
suffix: "\n",
314+
#[cfg(feature = "kv")]
315+
kv_format: None,
316+
}
317+
}
318+
}
319+
310320
/// The default format.
311321
///
312322
/// This format needs to work with any combination of crate features.

src/logger.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,21 +258,21 @@ impl Builder {
258258

259259
/// Whether or not to write the level in the default format.
260260
pub fn format_level(&mut self, write: bool) -> &mut Self {
261-
self.format.format_level = write;
261+
self.format.default_format.level = write;
262262
self
263263
}
264264

265265
/// Whether or not to write the source file path in the default format.
266266
pub fn format_file(&mut self, write: bool) -> &mut Self {
267-
self.format.format_file = write;
267+
self.format.default_format.source_file = write;
268268
self
269269
}
270270

271271
/// Whether or not to write the source line number path in the default format.
272272
///
273273
/// Only has effect if `format_file` is also enabled
274274
pub fn format_line_number(&mut self, write: bool) -> &mut Self {
275-
self.format.format_line_number = write;
275+
self.format.default_format.source_line_number = write;
276276
self
277277
}
278278

@@ -287,26 +287,26 @@ impl Builder {
287287

288288
/// Whether or not to write the module path in the default format.
289289
pub fn format_module_path(&mut self, write: bool) -> &mut Self {
290-
self.format.format_module_path = write;
290+
self.format.default_format.module_path = write;
291291
self
292292
}
293293

294294
/// Whether or not to write the target in the default format.
295295
pub fn format_target(&mut self, write: bool) -> &mut Self {
296-
self.format.format_target = write;
296+
self.format.default_format.target = write;
297297
self
298298
}
299299

300300
/// Configures the amount of spaces to use to indent multiline log records.
301301
/// A value of `None` disables any kind of indentation.
302302
pub fn format_indent(&mut self, indent: Option<usize>) -> &mut Self {
303-
self.format.format_indent = indent;
303+
self.format.default_format.indent = indent;
304304
self
305305
}
306306

307307
/// Configures if timestamp should be included and in what precision.
308308
pub fn format_timestamp(&mut self, timestamp: Option<fmt::TimestampPrecision>) -> &mut Self {
309-
self.format.format_timestamp = timestamp;
309+
self.format.default_format.timestamp = timestamp;
310310
self
311311
}
312312

@@ -332,7 +332,7 @@ impl Builder {
332332

333333
/// Configures the end of line suffix.
334334
pub fn format_suffix(&mut self, suffix: &'static str) -> &mut Self {
335-
self.format.format_suffix = suffix;
335+
self.format.default_format.suffix = suffix;
336336
self
337337
}
338338

@@ -351,7 +351,7 @@ impl Builder {
351351
where
352352
F: Fn(&mut Formatter, &dyn log::kv::Source) -> io::Result<()> + Sync + Send + 'static,
353353
{
354-
self.format.kv_format = Some(Box::new(format));
354+
self.format.default_format.kv_format = Some(Box::new(format));
355355
self
356356
}
357357

0 commit comments

Comments
 (0)