diff --git a/vlib/log/common.v b/vlib/log/common.v index 8a90870437eabc..c1b0fa1640f784 100644 --- a/vlib/log/common.v +++ b/vlib/log/common.v @@ -19,8 +19,8 @@ pub enum LogTarget { both } -// tag_to_console returns the tag for log level `l` as a colored string. -fn tag_to_console(l Level, short_tag bool) string { +// tag_to_cli returns the tag for log level `l` as a colored string. +fn tag_to_cli(l Level, short_tag bool) string { if short_tag { return match l { .disabled { ' ' } diff --git a/vlib/log/log.v b/vlib/log/log.v index 1b3fd72e29f7bc..8901da6f909039 100644 --- a/vlib/log/log.v +++ b/vlib/log/log.v @@ -5,7 +5,6 @@ module log import os import time -import io // TimeFormat define the log time string format, come from time/format.v pub enum TimeFormat { @@ -37,7 +36,6 @@ mut: custom_time_format string = 'MMMM Do YY N kk:mm:ss A' // timestamp with custom format short_tag bool always_flush bool // flush after every single .fatal(), .error(), .warn(), .info(), .debug() call - output_stream io.Writer = os.stderr() pub mut: output_file_name string // log output to this file } @@ -79,11 +77,6 @@ pub fn (mut l Log) set_output_path(output_file_path string) { l.ofile = ofile } -// set_output_stream sets the output stream to write log e.g. stderr, stdout, etc. -pub fn (mut l Log) set_output_stream(stream io.Writer) { - l.output_stream = stream -} - // log_to_console_too turns on logging to the console too, in addition to logging to a file. // You have to call it *after* calling .set_output_path(output_file_path). pub fn (mut l Log) log_to_console_too() { @@ -138,19 +131,13 @@ fn (mut l Log) log_file(s string, level Level) { } } -// log_stream writes log line `s` with `level` to stderr or stderr depending on set output stream. -fn (mut l Log) log_stream(s string, level Level) { +// log_cli writes log line `s` with `level` to stdout. +fn (l &Log) log_cli(s string, level Level) { timestamp := l.time_format(time.utc()) - tag := tag_to_console(level, l.short_tag) - msg := '${timestamp} [${tag}] ${s}\n' - arr := msg.bytes() - l.output_stream.write(arr) or {} + e := tag_to_cli(level, l.short_tag) + println('${timestamp} [${e}] ${s}') if l.always_flush { - match (l.output_stream as os.File).fd { - 1 { flush_stdout() } - 2 { flush_stderr() } - else {} - } + flush_stdout() } } @@ -161,7 +148,7 @@ pub fn (mut l Log) send_output(s &string, level Level) { l.log_file(s, level) } if l.output_target == .console || l.output_target == .both { - l.log_stream(s, level) + l.log_cli(s, level) } } @@ -215,7 +202,6 @@ pub fn (mut f Log) free() { f.output_label.free() f.ofile.close() f.output_file_name.free() - f.output_stream.free() } } diff --git a/vlib/v/slow_tests/inout/dump_expression.out b/vlib/v/slow_tests/inout/dump_expression.out index 551bb1cd7381c5..dc681e7a89676c 100644 --- a/vlib/v/slow_tests/inout/dump_expression.out +++ b/vlib/v/slow_tests/inout/dump_expression.out @@ -1,28 +1,36 @@ -[vlib/v/slow_tests/inout/dump_expression.vv:4] 1: 1 -[vlib/v/slow_tests/inout/dump_expression.vv:9] 'a': a -[vlib/v/slow_tests/inout/dump_expression.vv:33] a: Aa{ - cmd: &os.Command{ - f: 0 - eof: false - exit_code: 0 - path: '' - redirect_stdout: false - } +[vlib/v/slow_tests/inout/dump_expression.vv:5] 1: 1 +[vlib/v/slow_tests/inout/dump_expression.vv:10] 'a': a +[vlib/v/slow_tests/inout/dump_expression.vv:34] a: Aa{ + log: &log.Logger(log.Log{ + level: disabled + output_label: '' + ofile: os.File{ + cfile: 0 + fd: 0 + is_opened: false + } + output_target: console + time_format: tf_rfc3339_micro + custom_time_format: 'MMMM Do YY N kk:mm:ss A' + short_tag: false + always_flush: false + output_file_name: '' + }) } -[vlib/v/slow_tests/inout/dump_expression.vv:34] p: Point{ +[vlib/v/slow_tests/inout/dump_expression.vv:35] p: Point{ x: 1 y: 2 z: 3 } -[vlib/v/slow_tests/inout/dump_expression.vv:35] p_mut: Point{ +[vlib/v/slow_tests/inout/dump_expression.vv:36] p_mut: Point{ x: 1 y: 2 z: 3 } -[vlib/v/slow_tests/inout/dump_expression.vv:36] p_ptr: &Point{ +[vlib/v/slow_tests/inout/dump_expression.vv:37] p_ptr: &Point{ x: 1 y: 2 z: 3 } -[vlib/v/slow_tests/inout/dump_expression.vv:47] os.file_name(vfile): dump_expression.vv -[vlib/v/slow_tests/inout/dump_expression.vv:50] f.read(mut buf): 10 +[vlib/v/slow_tests/inout/dump_expression.vv:48] os.file_name(vfile): dump_expression.vv +[vlib/v/slow_tests/inout/dump_expression.vv:51] f.read(mut buf): 10 diff --git a/vlib/v/slow_tests/inout/dump_expression.vv b/vlib/v/slow_tests/inout/dump_expression.vv index c5d2df77ac746d..4f52d5664032bd 100644 --- a/vlib/v/slow_tests/inout/dump_expression.vv +++ b/vlib/v/slow_tests/inout/dump_expression.vv @@ -1,4 +1,5 @@ import os +import log fn dump_of_int() { x := dump(1) + 1 @@ -18,16 +19,16 @@ mut: } struct Aa { - cmd &os.Command + log &log.Logger } fn dump_of_struct() { p := Point{1, 2, 3} mut p_mut := Point{1, 2, 3} p_ptr := &Point{1, 2, 3} - c := &os.Command{} + l := &log.Log{} a := Aa{ - cmd: c + log: l } dump(a)