From 4ceb550e8879880c7f57ce33ee37f5a953b8241f Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Thu, 25 Jan 2024 16:47:00 +0800 Subject: [PATCH] log: add support for l.set_short_tag/1 (#20652) --- vlib/log/common.v | 66 ++++++++++++++------- vlib/log/default_test.v | 1 + vlib/log/log.v | 16 ++++- vlib/log/log_test.v | 15 +++++ vlib/v/slow_tests/inout/dump_expression.out | 1 + 5 files changed, 75 insertions(+), 24 deletions(-) diff --git a/vlib/log/common.v b/vlib/log/common.v index 6349affb9c..29d60eea5b 100644 --- a/vlib/log/common.v +++ b/vlib/log/common.v @@ -20,26 +20,48 @@ pub enum LogTarget { } // 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') } +fn tag_to_cli(l Level, short_tag bool) string { + if short_tag { + return match l { + .disabled { ' ' } + .fatal { term.red('F') } + .error { term.red('E') } + .warn { term.yellow('W') } + .info { term.white('I') } + .debug { term.magenta('D') } + } + } else { + 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' } +fn tag_to_file(l Level, short_tag bool) string { + if short_tag { + return match l { + .disabled { ' ' } + .fatal { 'F' } + .error { 'E' } + .warn { 'W' } + .info { 'I' } + .debug { 'D' } + } + } else { + return match l { + .disabled { ' ' } + .fatal { 'FATAL' } + .error { 'ERROR' } + .warn { 'WARN ' } + .info { 'INFO ' } + .debug { 'DEBUG' } + } } } @@ -47,12 +69,12 @@ fn tag_to_file(l Level) 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 } + 'DISABLED', ' ' { Level.disabled } + 'FATAL', 'F' { Level.fatal } + 'ERROR', 'E' { Level.error } + 'WARN', 'W' { Level.warn } + 'INFO', 'I' { Level.info } + 'DEBUG', 'D' { Level.debug } else { none } } } diff --git a/vlib/log/default_test.v b/vlib/log/default_test.v index d6b1076fb0..7fe99d3c4d 100644 --- a/vlib/log/default_test.v +++ b/vlib/log/default_test.v @@ -13,6 +13,7 @@ fn test_default_log_instance() { println('^^^ there should be `debug now` shown above') log.set_level(log.level_from_tag('INFO') or { log.Level.disabled }) log.info('info again') + log.debug('no output for debug') log.set_level(log.level_from_tag('') or { log.Level.disabled }) log.error('no output anymore') println('^^^ there should be no `no output anymore` shown above') diff --git a/vlib/log/log.v b/vlib/log/log.v index 1337910fa5..992908bd66 100644 --- a/vlib/log/log.v +++ b/vlib/log/log.v @@ -33,6 +33,7 @@ mut: output_target LogTarget // output to console (stdout/stderr) or file or both. time_format TimeFormat custom_time_format string = 'MMMM Do YY N kk:mm:ss A' // timestamp with custom format + short_tag bool pub mut: output_file_name string // log output to this file } @@ -115,14 +116,14 @@ pub fn (mut l Log) reopen() ! { // log_file writes log line `s` with `level` to the log file. fn (mut l Log) log_file(s string, level Level) { timestamp := l.time_format(time.now()) - e := tag_to_file(level) + e := tag_to_file(level, l.short_tag) l.ofile.writeln('${timestamp} [${e}] ${s}') or { panic(err) } } // log_cli writes log line `s` with `level` to stdout. fn (l &Log) log_cli(s string, level Level) { timestamp := l.time_format(time.now()) - e := tag_to_cli(level) + e := tag_to_cli(level, l.short_tag) println('${timestamp} [${e}] ${s}') } @@ -260,3 +261,14 @@ pub fn (mut l Log) set_custom_time_format(f string) { pub fn (l Log) get_custom_time_format() string { return l.custom_time_format } + +// set_short_tag will set the log tag to it's short version +// eg. '[FATAL]'=>'[F]', '[ERROR]'=>'[E]','[WARN ]'=>'[W]','[INFO ]'=>'[I]','[DEBUG]'=>'[D]' +pub fn (mut l Log) set_short_tag(enabled bool) { + l.short_tag = enabled +} + +// get_short_tag will get the log short tag enable state +pub fn (l Log) get_short_tag() bool { + return l.short_tag +} diff --git a/vlib/log/log_test.v b/vlib/log/log_test.v index e415751f49..aedb37e6e9 100644 --- a/vlib/log/log_test.v +++ b/vlib/log/log_test.v @@ -75,12 +75,27 @@ fn test_logger_mutable_reference() { println(@FN + ' end') } +fn test_log_mutable_short_tag() { + println(@FN + ' start') + mut l := Log{} + l.set_level(.info) + l.set_short_tag(true) + log_mutable_statements(mut l) + 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 + assert level_from_tag('I')? == .info + assert level_from_tag('F')? == .fatal + assert level_from_tag('W')? == .warn + assert level_from_tag('E')? == .error + assert level_from_tag('D')? == .debug invalid := ['', 'FOO', 'nope'] diff --git a/vlib/v/slow_tests/inout/dump_expression.out b/vlib/v/slow_tests/inout/dump_expression.out index 3fa64d6594..c7280fcfc1 100644 --- a/vlib/v/slow_tests/inout/dump_expression.out +++ b/vlib/v/slow_tests/inout/dump_expression.out @@ -12,6 +12,7 @@ output_target: console time_format: tf_ss_micro custom_time_format: 'MMMM Do YY N kk:mm:ss A' + short_tag: false output_file_name: '' }) }