Skip to content

Commit e15c361

Browse files
committed
refactor: remove fmt::Builder, and retool Builder::format into build_with_format_fn
1 parent 4ed7fb4 commit e15c361

File tree

2 files changed

+35
-65
lines changed

2 files changed

+35
-65
lines changed

src/fmt/mod.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use std::cell::RefCell;
6161
use std::fmt::Display;
6262
use std::io::prelude::Write;
6363
use std::rc::Rc;
64-
use std::{fmt, io, mem};
64+
use std::{fmt, io};
6565

6666
#[cfg(feature = "color")]
6767
use log::Level;
@@ -214,38 +214,6 @@ where
214214

215215
pub(crate) type FormatFn = Box<dyn RecordFormat + Sync + Send>;
216216

217-
#[derive(Default)]
218-
pub(crate) struct Builder {
219-
pub(crate) default_format: ConfigurableFormat,
220-
pub(crate) custom_format: Option<FormatFn>,
221-
built: bool,
222-
}
223-
224-
impl Builder {
225-
/// Convert the format into a callable function.
226-
///
227-
/// If the `custom_format` is `Some`, then any `default_format` switches are ignored.
228-
/// If the `custom_format` is `None`, then a default format is returned.
229-
/// Any `default_format` switches set to `false` won't be written by the format.
230-
pub(crate) fn build(&mut self) -> FormatFn {
231-
assert!(!self.built, "attempt to re-use consumed builder");
232-
233-
let built = mem::replace(
234-
self,
235-
Builder {
236-
built: true,
237-
..Default::default()
238-
},
239-
);
240-
241-
if let Some(fmt) = built.custom_format {
242-
fmt
243-
} else {
244-
Box::new(built.default_format)
245-
}
246-
}
247-
}
248-
249217
#[cfg(feature = "color")]
250218
type SubtleStyle = StyledValue<&'static str>;
251219
#[cfg(not(feature = "color"))]

src/logger.rs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub const DEFAULT_WRITE_STYLE_ENV: &str = "RUST_LOG_STYLE";
3838
pub struct Builder {
3939
filter: env_filter::Builder,
4040
writer: writer::Builder,
41-
format: fmt::Builder,
41+
format: fmt::ConfigurableFormat,
4242
built: bool,
4343
}
4444

@@ -211,7 +211,8 @@ impl Builder {
211211
self.parse_env(Env::default())
212212
}
213213

214-
/// Sets the format function for formatting the log output.
214+
/// Sets the format function for formatting the log output,
215+
/// and builds the Logger.
215216
///
216217
/// This function is called on each record logged and should format the
217218
/// log record and output it to the given [`Formatter`].
@@ -234,45 +235,43 @@ impl Builder {
234235
///
235236
/// let mut builder = Builder::new();
236237
///
237-
/// builder.format(|buf, record| writeln!(buf, "{}", record.args()));
238+
/// builder.build_with_format_fn(|buf, record| writeln!(buf, "{}", record.args()));
238239
/// ```
239240
///
240241
/// [`Formatter`]: fmt/struct.Formatter.html
241242
/// [`String`]: https://doc.rust-lang.org/stable/std/string/struct.String.html
242243
/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
243-
pub fn format<F>(&mut self, format: F) -> &mut Self
244+
pub fn build_with_format_fn<F>(&mut self, format: F) -> Logger
244245
where
245246
F: Fn(&mut Formatter, &Record<'_>) -> io::Result<()> + Sync + Send + 'static,
246247
{
247-
self.format.custom_format = Some(Box::new(format));
248-
self
249-
}
248+
assert!(!self.built, "attempt to re-use consumed builder");
249+
self.built = true;
250250

251-
/// Use the default format.
252-
///
253-
/// This method will clear any custom format set on the builder.
254-
pub fn default_format(&mut self) -> &mut Self {
255-
self.format = Default::default();
256-
self
251+
Logger {
252+
writer: self.writer.build(),
253+
filter: self.filter.build(),
254+
format: Box::new(format),
255+
}
257256
}
258257

259258
/// Whether or not to write the level in the default format.
260259
pub fn format_level(&mut self, write: bool) -> &mut Self {
261-
self.format.default_format.level(write);
260+
self.format.level(write);
262261
self
263262
}
264263

265264
/// Whether or not to write the source file path in the default format.
266265
pub fn format_file(&mut self, write: bool) -> &mut Self {
267-
self.format.default_format.file(write);
266+
self.format.file(write);
268267
self
269268
}
270269

271270
/// Whether or not to write the source line number path in the default format.
272271
///
273272
/// Only has effect if `format_file` is also enabled
274273
pub fn format_line_number(&mut self, write: bool) -> &mut Self {
275-
self.format.default_format.line_number(write);
274+
self.format.line_number(write);
276275
self
277276
}
278277

@@ -287,26 +286,26 @@ impl Builder {
287286

288287
/// Whether or not to write the module path in the default format.
289288
pub fn format_module_path(&mut self, write: bool) -> &mut Self {
290-
self.format.default_format.module_path(write);
289+
self.format.module_path(write);
291290
self
292291
}
293292

294293
/// Whether or not to write the target in the default format.
295294
pub fn format_target(&mut self, write: bool) -> &mut Self {
296-
self.format.default_format.target(write);
295+
self.format.target(write);
297296
self
298297
}
299298

300299
/// Configures the amount of spaces to use to indent multiline log records.
301300
/// A value of `None` disables any kind of indentation.
302301
pub fn format_indent(&mut self, indent: Option<usize>) -> &mut Self {
303-
self.format.default_format.indent(indent);
302+
self.format.indent(indent);
304303
self
305304
}
306305

307306
/// Configures if timestamp should be included and in what precision.
308307
pub fn format_timestamp(&mut self, timestamp: Option<fmt::TimestampPrecision>) -> &mut Self {
309-
self.format.default_format.timestamp(timestamp);
308+
self.format.timestamp(timestamp);
310309
self
311310
}
312311

@@ -332,7 +331,7 @@ impl Builder {
332331

333332
/// Configures the end of line suffix.
334333
pub fn format_suffix(&mut self, suffix: &'static str) -> &mut Self {
335-
self.format.default_format.suffix(suffix);
334+
self.format.suffix(suffix);
336335
self
337336
}
338337

@@ -351,7 +350,7 @@ impl Builder {
351350
where
352351
F: Fn(&mut Formatter, &dyn log::kv::Source) -> io::Result<()> + Sync + Send + 'static,
353352
{
354-
self.format.default_format.key_values(format);
353+
self.format.key_values(format);
355354
self
356355
}
357356

@@ -497,15 +496,7 @@ impl Builder {
497496
/// library has already initialized a global logger.
498497
pub fn try_init(&mut self) -> Result<(), SetLoggerError> {
499498
let logger = self.build();
500-
501-
let max_level = logger.filter();
502-
let r = log::set_boxed_logger(Box::new(logger));
503-
504-
if r.is_ok() {
505-
log::set_max_level(max_level);
506-
}
507-
508-
r
499+
logger.try_init()
509500
}
510501

511502
/// Initializes the global logger with the built env logger.
@@ -533,7 +524,7 @@ impl Builder {
533524
Logger {
534525
writer: self.writer.build(),
535526
filter: self.filter.build(),
536-
format: self.format.build(),
527+
format: Box::new(std::mem::take(&mut self.format)),
537528
}
538529
}
539530
}
@@ -641,6 +632,17 @@ impl Logger {
641632
pub fn matches(&self, record: &Record<'_>) -> bool {
642633
self.filter.matches(record)
643634
}
635+
636+
pub fn try_init(self) -> Result<(), SetLoggerError> {
637+
let max_level = self.filter();
638+
let r = log::set_boxed_logger(Box::new(self));
639+
640+
if r.is_ok() {
641+
log::set_max_level(max_level);
642+
}
643+
644+
r
645+
}
644646
}
645647

646648
impl Log for Logger {

0 commit comments

Comments
 (0)