mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
log: improve the most common use case (#19242)
This commit is contained in:
parent
55575fd7bd
commit
6fb4a481f8
16 changed files with 576 additions and 150 deletions
69
vlib/log/common.v
Normal file
69
vlib/log/common.v
Normal file
|
@ -0,0 +1,69 @@
|
|||
module log
|
||||
|
||||
import term
|
||||
|
||||
// Level defines the possible log levels, used by Log.set_level()
|
||||
pub enum Level {
|
||||
disabled = 0 // lowest level, disables everything else
|
||||
fatal // disables error, warn, info and debug
|
||||
error // disables warn, info and debug
|
||||
warn // disables info and debug
|
||||
info // disables debug
|
||||
debug
|
||||
}
|
||||
|
||||
// LogTarget defines the possible log targets, that Log supports
|
||||
pub enum LogTarget {
|
||||
console
|
||||
file
|
||||
both
|
||||
}
|
||||
|
||||
// tag_to_cli returns the tag for log level `l` as a colored string.
|
||||
fn tag_to_cli(l Level) string {
|
||||
return match l {
|
||||
.disabled { '' }
|
||||
.fatal { term.red('FATAL') }
|
||||
.error { term.red('ERROR') }
|
||||
.warn { term.yellow('WARN ') }
|
||||
.info { term.white('INFO ') }
|
||||
.debug { term.magenta('DEBUG') }
|
||||
}
|
||||
}
|
||||
|
||||
// tag_to_file returns the tag for log level `l` as a string.
|
||||
fn tag_to_file(l Level) string {
|
||||
return match l {
|
||||
.disabled { ' ' }
|
||||
.fatal { 'FATAL' }
|
||||
.error { 'ERROR' }
|
||||
.warn { 'WARN ' }
|
||||
.info { 'INFO ' }
|
||||
.debug { 'DEBUG' }
|
||||
}
|
||||
}
|
||||
|
||||
// level_from_tag returns the log level from the given string.
|
||||
// It returns `none` when it does not find a match.
|
||||
pub fn level_from_tag(tag string) ?Level {
|
||||
return match tag {
|
||||
'DISABLED' { Level.disabled }
|
||||
'FATAL' { Level.fatal }
|
||||
'ERROR' { Level.error }
|
||||
'WARN' { Level.warn }
|
||||
'INFO' { Level.info }
|
||||
'DEBUG' { Level.debug }
|
||||
else { none }
|
||||
}
|
||||
}
|
||||
|
||||
// target_from_label returns the log target from the given string.
|
||||
// It returns `none` when it does not find a match.
|
||||
pub fn target_from_label(label string) ?LogTarget {
|
||||
return match label {
|
||||
'console' { LogTarget.console }
|
||||
'file' { LogTarget.file }
|
||||
'both' { LogTarget.both }
|
||||
else { none }
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue