lexa_logger/initiator.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 crate::LoggerBuilder;
12
13// --------- //
14// Structure //
15// --------- //
16
17pub struct LoggerInitiator;
18
19// -------------- //
20// Implémentation //
21// -------------- //
22
23impl LoggerInitiator
24{
25 /// Initialise le logger STDOUT à partir du builder.
26 #[cfg(not(feature = "tracing"))]
27 pub fn stdout(builder: impl LoggerBuilder<crate::LoggerStdout>) -> Result<(), log::SetLoggerError>
28 {
29 let stdout = builder.build();
30 let level = stdout.level();
31
32 log::set_max_level(level);
33
34 if log::LevelFilter::Off == log::max_level() {
35 const NO: crate::noop::NopeLogger = crate::noop::NopeLogger;
36 log::set_logger(&NO)
37 } else {
38 log::set_boxed_logger(Box::new(stdout))
39 }
40 }
41
42 #[cfg(feature = "tracing")]
43 pub fn stdout(builder: impl LoggerBuilder<crate::LoggerStdout>) -> Result<(), &'static str>
44 {
45 let stdout = builder.build();
46 let level = stdout.level();
47
48 let trsb = tracing_subscriber::fmt()
49 .with_max_level(level)
50 .with_ansi(stdout.colorized)
51 .with_line_number(true);
52
53 if stdout.timestamp {
54 trsb.init();
55 } else {
56 trsb.without_time().init();
57 }
58
59 Ok(())
60 }
61
62 // NOTE: Initialiser d'autres types de logger ici...
63}