lexa_logger/stdout/
builder.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ Copyright: (c) 2023, Mike 'PhiSyX' S. (https://github.com/PhiSyX)         ┃
3// ┃ SPDX-License-Identifier: MPL-2.0                                          ┃
4// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
5// ┃                                                                           ┃
6// ┃  This Source Code Form is subject to the terms of the Mozilla Public      ┃
7// ┃  License, v. 2.0. If a copy of the MPL was not distributed with this      ┃
8// ┃  file, You can obtain one at https://mozilla.org/MPL/2.0/.                ┃
9// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
10
11use super::extension::LoggerStdoutBuilderExtension;
12use super::LoggerFilter;
13use crate::builder::LoggerFormatFn;
14use crate::{LoggerBuilder, LoggerStdout};
15
16// --------- //
17// Structure //
18// --------- //
19
20#[derive(Default)]
21pub struct LoggerStdoutBuilder
22{
23	colorized: bool,
24	timestamp: bool,
25	#[cfg(not(feature = "tracing"))]
26	level: Option<log::LevelFilter>,
27	#[cfg(feature = "tracing")]
28	level: Option<tracing::level_filters::LevelFilter>,
29	format_fn: Option<LoggerFormatFn>,
30	filter: LoggerFilter,
31}
32
33// -------------- //
34// Implémentation // -> Interface
35// -------------- //
36
37impl LoggerBuilder<LoggerStdout> for LoggerStdoutBuilder
38{
39	fn filter<F>(mut self, predicate: F, dependency: impl ToString) -> Self
40	where
41		F: 'static,
42		F: Send + Sync,
43		F: Fn(&log::Metadata) -> bool,
44	{
45		self.filter.push_callback(predicate);
46		self.filter.add_dependency(dependency);
47		self
48	}
49
50	fn with_color(mut self, colorized: impl Into<bool>) -> Self
51	{
52		self.colorized = colorized.into();
53		self
54	}
55
56	fn with_format(mut self, format: LoggerFormatFn) -> Self
57	{
58		self.format_fn.replace(format);
59		self
60	}
61
62	#[cfg(not(feature = "tracing"))]
63	fn with_level(mut self, level: impl Into<log::LevelFilter>) -> Self
64	{
65		self.level.replace(level.into());
66		self
67	}
68
69	#[cfg(feature = "tracing")]
70	fn with_level(mut self, level: impl Into<tracing::level_filters::LevelFilter>) -> Self
71	{
72		self.level.replace(level.into());
73		self
74	}
75
76	fn with_timestamp(mut self, b: impl Into<bool>) -> Self
77	{
78		self.timestamp = b.into();
79		self
80	}
81
82	fn build(self) -> LoggerStdout
83	{
84		LoggerStdout {
85			cache: Default::default(),
86			colorized: self.colorized,
87			filter: self.filter,
88			format_fn: self.format_fn.unwrap_or(LoggerStdout::default_format),
89			#[cfg(not(feature = "tracing"))]
90			level: self.level.unwrap_or(log::LevelFilter::Off),
91			#[cfg(feature = "tracing")]
92			level: self.level.unwrap_or(tracing_subscriber::filter::LevelFilter::OFF),
93			timestamp: self.timestamp,
94		}
95	}
96}
97
98impl LoggerStdoutBuilderExtension for LoggerStdoutBuilder {}