log: implement .log_to_console_too() method

pull/9539/head^2
Delyan Angelov 2021-04-02 10:12:52 +03:00
parent 897cd4cec2
commit bcb3992406
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 26 additions and 9 deletions

View File

@ -5,6 +5,7 @@ fn main() {
l.set_level(.info) l.set_level(.info)
// Make a new file called info.log in the current folder // Make a new file called info.log in the current folder
l.set_full_logpath('./info.log') l.set_full_logpath('./info.log')
l.log_to_console_too()
println('Please check the file: $l.output_file_name after this example crashes.') println('Please check the file: $l.output_file_name after this example crashes.')
l.info('info') l.info('info')

View File

@ -16,6 +16,12 @@ pub enum Level {
debug debug
} }
pub enum LogTarget {
console
file
both
}
// tag returns the tag for log level `l` as a string. // tag returns the tag for log level `l` as a string.
fn tag_to_cli(l Level) string { fn tag_to_cli(l Level) string {
return match l { return match l {
@ -49,10 +55,10 @@ interface Logger {
// Log represents a logging object // Log represents a logging object
pub struct Log { pub struct Log {
mut: mut:
level Level level Level
output_label string output_label string
ofile os.File ofile os.File
output_to_file bool // if true output to file else use stdout/stderr. output_target LogTarget // if true output to file else use stdout/stderr.
pub mut: pub mut:
output_file_name string // log output to this file output_file_name string // log output to this file
} }
@ -84,7 +90,7 @@ pub fn (mut l Log) set_output_path(output_file_path string) {
if l.ofile.is_opened { if l.ofile.is_opened {
l.ofile.close() l.ofile.close()
} }
l.output_to_file = true l.output_target = .file
l.output_file_name = os.join_path(os.real_path(output_file_path), l.output_label) l.output_file_name = os.join_path(os.real_path(output_file_path), l.output_label)
ofile := os.open_append(l.output_file_name) or { ofile := os.open_append(l.output_file_name) or {
panic('error while opening log file $l.output_file_name for appending') panic('error while opening log file $l.output_file_name for appending')
@ -92,6 +98,15 @@ pub fn (mut l Log) set_output_path(output_file_path string) {
l.ofile = ofile l.ofile = ofile
} }
// 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() {
if l.output_target != .file {
panic('log_to_console_too should be called *after* .set_output_path')
}
l.output_target = .both
}
// flush writes the log file content to disk. // flush writes the log file content to disk.
pub fn (mut l Log) flush() { pub fn (mut l Log) flush() {
l.ofile.flush() l.ofile.flush()
@ -116,12 +131,13 @@ fn (l &Log) log_cli(s string, level Level) {
println('[$f $t.format_ss()] $s') println('[$f $t.format_ss()] $s')
} }
// send_output writes log line `s` with `level` to either the log file or stdout // send_output writes log line `s` with `level` to either the log file or the console
// according to the value of the `.output_to_file` field. // according to the value of the `.output_target` field.
fn (mut l Log) send_output(s &string, level Level) { fn (mut l Log) send_output(s &string, level Level) {
if l.output_to_file { if l.output_target == .file || l.output_target == .both {
l.log_file(s, level) l.log_file(s, level)
} else { }
if l.output_target == .console || l.output_target == .both {
l.log_cli(s, level) l.log_cli(s, level)
} }
} }