log: implement .log_to_console_too() method
parent
897cd4cec2
commit
bcb3992406
|
@ -5,6 +5,7 @@ fn main() {
|
|||
l.set_level(.info)
|
||||
// Make a new file called info.log in the current folder
|
||||
l.set_full_logpath('./info.log')
|
||||
l.log_to_console_too()
|
||||
println('Please check the file: $l.output_file_name after this example crashes.')
|
||||
|
||||
l.info('info')
|
||||
|
|
|
@ -16,6 +16,12 @@ pub enum Level {
|
|||
debug
|
||||
}
|
||||
|
||||
pub enum LogTarget {
|
||||
console
|
||||
file
|
||||
both
|
||||
}
|
||||
|
||||
// tag returns the tag for log level `l` as a string.
|
||||
fn tag_to_cli(l Level) string {
|
||||
return match l {
|
||||
|
@ -49,10 +55,10 @@ interface Logger {
|
|||
// Log represents a logging object
|
||||
pub struct Log {
|
||||
mut:
|
||||
level Level
|
||||
output_label string
|
||||
ofile os.File
|
||||
output_to_file bool // if true output to file else use stdout/stderr.
|
||||
level Level
|
||||
output_label string
|
||||
ofile os.File
|
||||
output_target LogTarget // if true output to file else use stdout/stderr.
|
||||
pub mut:
|
||||
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 {
|
||||
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)
|
||||
ofile := os.open_append(l.output_file_name) or {
|
||||
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
|
||||
}
|
||||
|
||||
// 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.
|
||||
pub fn (mut l Log) flush() {
|
||||
l.ofile.flush()
|
||||
|
@ -116,12 +131,13 @@ fn (l &Log) log_cli(s string, level Level) {
|
|||
println('[$f $t.format_ss()] $s')
|
||||
}
|
||||
|
||||
// send_output writes log line `s` with `level` to either the log file or stdout
|
||||
// according to the value of the `.output_to_file` field.
|
||||
// send_output writes log line `s` with `level` to either the log file or the console
|
||||
// according to the value of the `.output_target` field.
|
||||
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)
|
||||
} else {
|
||||
}
|
||||
if l.output_target == .console || l.output_target == .both {
|
||||
l.log_cli(s, level)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue