-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathper_module_with_ctor.rs
More file actions
105 lines (88 loc) · 2.85 KB
/
per_module_with_ctor.rs
File metadata and controls
105 lines (88 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2024 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::BTreeMap;
use std::sync::OnceLock;
use std::sync::RwLock;
use logforth::append::Stdout;
use logforth::filter::EnvFilter;
use logforth::filter::env_filter::EnvFilterBuilder;
use logforth::record::Level;
use logforth::starter_log;
static FILTER: OnceLock<Filter> = OnceLock::new();
mod foo {
// This is how you can allow debug level logs for this module while keeping the default log
// level for everything else at info.
#[cfg(debug_assertions)]
#[ctor::ctor]
unsafe fn set_log_filter() {
use logforth::record::Level;
use crate::Filter;
super::FILTER
.get_or_init(|| Filter::new(Level::Info))
.set_module_level(module_path!(), Level::Debug);
}
pub fn run() {
log::debug!("foo debug");
log::info!("foo info");
}
}
mod bar {
pub fn run() {
log::debug!("bar debug");
log::info!("bar info");
}
}
fn main() {
starter_log::builder()
.dispatch(|dispatch_builder| {
dispatch_builder
.filter(
FILTER
.get_or_init(|| Filter::new(Level::Info))
.build_env_filter(),
)
.append(Stdout::default())
})
.apply();
foo::run();
bar::run();
}
#[derive(Debug)]
struct Filter {
default_level: Level,
module_levels: RwLock<BTreeMap<String, Level>>,
}
impl Filter {
fn new(default_level: Level) -> Self {
Self {
default_level,
module_levels: RwLock::new(BTreeMap::new()),
}
}
fn set_module_level(&self, module_path: &str, level: Level) {
let mut module_levels = self
.module_levels
.write()
.expect("filter write is poisoned");
module_levels.insert(module_path.to_string(), level);
}
pub fn build_env_filter(&self) -> EnvFilter {
let module_levels = self.module_levels.read().expect("filter read is poisoned");
let mut directives = vec![self.default_level.name().to_string()];
for (module_path, level) in module_levels.iter() {
directives.push(format!("{module_path}={}", level.name()));
}
EnvFilterBuilder::from_spec(directives.join(",")).build()
}
}