1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

log: add target_from_label and unit tests (#15538)

This commit is contained in:
Subhomoy Haldar 2022-08-26 09:39:48 +05:30 committed by GitHub
parent b0e7ddfd97
commit 3af12271fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View File

@ -48,7 +48,7 @@ fn tag_to_file(l Level) string {
}
}
// level_from_tag returns the log level from the given string if matches.
// level_from_tag returns the log level from the given string if it matches.
pub fn level_from_tag(tag string) ?Level {
return match tag {
'DISABLED' { Level.disabled }
@ -61,6 +61,16 @@ pub fn level_from_tag(tag string) ?Level {
}
}
// target_from_label returns the log target from the given string if it matches.
pub fn target_from_label(label string) ?LogTarget {
return match label {
'console' { LogTarget.console }
'file' { LogTarget.file }
'both' { LogTarget.both }
else { none }
}
}
// Logger is an interface that describes a generic Logger
pub interface Logger {
mut:

View File

@ -80,3 +80,33 @@ fn test_logger_mutable_reference() {
assert true
println(@FN + ' end')
}
fn test_level_from_tag() ? {
assert level_from_tag('INFO')? == .info
assert level_from_tag('FATAL')? == .fatal
assert level_from_tag('WARN')? == .warn
assert level_from_tag('ERROR')? == .error
assert level_from_tag('DEBUG')? == .debug
invalid := ['', 'FOO', 'nope']
for value in invalid {
mut passed := false
level_from_tag(value) or { passed = true }
assert passed
}
}
fn test_target_from_label() ? {
assert target_from_label('console')? == .console
assert target_from_label('file')? == .file
assert target_from_label('both')? == .both
invalid := ['', 'FOO', 'nope']
for value in invalid {
mut passed := false
target_from_label(value) or { passed = true }
assert passed
}
}