-
Notifications
You must be signed in to change notification settings - Fork 1
/
level_map.v
51 lines (45 loc) · 1.18 KB
/
level_map.v
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
module slog
import os
pub struct ModLevelMapString {
pub mut:
lv_map map[string]Level
}
// See `ModLevelMapString#set_level_from_str`
pub fn (mut lg ModLevelMapString) set_level_from_default_env() {
val := os.getenv(default_env)
lg.set_level_from_str(val)
}
// See `ModLevelMapString#set_level_from_str`
pub fn (mut lg ModLevelMapString) set_level_from_envvar(name string) {
val := os.getenv(name)
$if slog_envcheck ? {
if val.len == 0 {
panic("[WARN] envvar $name, is not set!!")
}
}
lg.set_level_from_str(val)
}
// ```v
// lg.set_level_from_str('info,net.websocket=debug,mylib=info')
// // is equivalent to
// set_max_level(.info)
// lg.lv_map['net.websocket'] = .debug
// lg.lv_map['mylib'] = .info
// ```
[inline]
pub fn (mut lg ModLevelMapString) set_level_from_str(val string) {
if val.len > 0 {
for setting in val.split(',').map(it.trim_space()) {
if idx := setting.index('=') {
lg.lv_map[setting[..idx]] = level___from_str(setting[idx + 1..])
} else {
set_max_level(level___from_str(setting))
}
}
}
}
[inline]
pub fn (lg &ModLevelMapString) enabled(target string, lv Level) bool {
level := lg.lv_map[target] or { max_level }
return int(lv) <= int(level)
}