mirror of
https://github.com/vlang/v.git
synced 2025-09-08 14:51:53 -04:00
log: implement set_always_flush/1 for log.Log, log.ThreadSafeLog and log.Logger (#20698)
This commit is contained in:
parent
98e0293ec0
commit
68bd9a9549
@ -53,3 +53,8 @@ pub fn info(s string) {
|
|||||||
pub fn debug(s string) {
|
pub fn debug(s string) {
|
||||||
default_logger.debug(s)
|
default_logger.debug(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set_always_flush called with true, will make the log flush after every single .fatal(), .error(), .warn(), .info(), .debug() call.
|
||||||
|
pub fn set_always_flush(should_flush_on_every_message bool) {
|
||||||
|
default_logger.set_always_flush(should_flush_on_every_message)
|
||||||
|
}
|
||||||
|
@ -40,3 +40,27 @@ fn test_reopen() {
|
|||||||
|
|
||||||
os.rmdir_all(lfolder) or {}
|
os.rmdir_all(lfolder) or {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_set_always_flush() {
|
||||||
|
lfolder := os.join_path(os.vtmp_dir(), rand.ulid())
|
||||||
|
lpath1 := os.join_path(lfolder, 'current.log')
|
||||||
|
os.mkdir_all(lfolder)!
|
||||||
|
defer {
|
||||||
|
os.rmdir_all(lfolder) or {}
|
||||||
|
}
|
||||||
|
dump(lfolder)
|
||||||
|
mut l := log.Log{
|
||||||
|
level: .info
|
||||||
|
}
|
||||||
|
l.set_full_logpath(lpath1)
|
||||||
|
l.set_always_flush(true)
|
||||||
|
l.warn('one warning')
|
||||||
|
l.info('one info message')
|
||||||
|
l.error('one error')
|
||||||
|
l.close()
|
||||||
|
lcontent1 := os.read_file(lpath1)!
|
||||||
|
assert lcontent1.len > 0
|
||||||
|
assert lcontent1.contains('one warning')
|
||||||
|
assert lcontent1.contains('one error')
|
||||||
|
assert lcontent1.contains('one info message')
|
||||||
|
}
|
||||||
|
@ -34,6 +34,7 @@ mut:
|
|||||||
time_format TimeFormat
|
time_format TimeFormat
|
||||||
custom_time_format string = 'MMMM Do YY N kk:mm:ss A' // timestamp with custom format
|
custom_time_format string = 'MMMM Do YY N kk:mm:ss A' // timestamp with custom format
|
||||||
short_tag bool
|
short_tag bool
|
||||||
|
always_flush bool // flush after every single .fatal(), .error(), .warn(), .info(), .debug() call
|
||||||
pub mut:
|
pub mut:
|
||||||
output_file_name string // log output to this file
|
output_file_name string // log output to this file
|
||||||
}
|
}
|
||||||
@ -118,6 +119,9 @@ fn (mut l Log) log_file(s string, level Level) {
|
|||||||
timestamp := l.time_format(time.now())
|
timestamp := l.time_format(time.now())
|
||||||
e := tag_to_file(level, l.short_tag)
|
e := tag_to_file(level, l.short_tag)
|
||||||
l.ofile.writeln('${timestamp} [${e}] ${s}') or { panic(err) }
|
l.ofile.writeln('${timestamp} [${e}] ${s}') or { panic(err) }
|
||||||
|
if l.always_flush {
|
||||||
|
l.flush()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// log_cli writes log line `s` with `level` to stdout.
|
// log_cli writes log line `s` with `level` to stdout.
|
||||||
@ -125,6 +129,9 @@ fn (l &Log) log_cli(s string, level Level) {
|
|||||||
timestamp := l.time_format(time.now())
|
timestamp := l.time_format(time.now())
|
||||||
e := tag_to_cli(level, l.short_tag)
|
e := tag_to_cli(level, l.short_tag)
|
||||||
println('${timestamp} [${e}] ${s}')
|
println('${timestamp} [${e}] ${s}')
|
||||||
|
if l.always_flush {
|
||||||
|
flush_stdout()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// send_output writes log line `s` with `level` to either the log file or the console
|
// send_output writes log line `s` with `level` to either the log file or the console
|
||||||
@ -244,6 +251,12 @@ pub fn (mut l Log) set_time_format(f TimeFormat) {
|
|||||||
l.time_format = f
|
l.time_format = f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set_always_flush called with true, will make the log flush after every single .fatal(), .error(), .warn(), .info(), .debug() call.
|
||||||
|
// That can be much slower, if you plan to do lots of frequent calls, but if your program exits early or crashes, your logs will be more complete.
|
||||||
|
pub fn (mut l Log) set_always_flush(should_flush_every_time bool) {
|
||||||
|
l.always_flush = should_flush_every_time
|
||||||
|
}
|
||||||
|
|
||||||
// get_time_format will get the log time format
|
// get_time_format will get the log time format
|
||||||
pub fn (l Log) get_time_format() TimeFormat {
|
pub fn (l Log) get_time_format() TimeFormat {
|
||||||
return l.time_format
|
return l.time_format
|
||||||
|
@ -11,5 +11,6 @@ mut:
|
|||||||
debug(s string)
|
debug(s string)
|
||||||
// utility methods:
|
// utility methods:
|
||||||
set_level(level Level)
|
set_level(level Level)
|
||||||
|
set_always_flush(should_flush bool)
|
||||||
free()
|
free()
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,14 @@ pub fn (mut x ThreadSafeLog) set_level(level Level) {
|
|||||||
x.mu.unlock()
|
x.mu.unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set_always_flush called with true, will make the log flush after every single .fatal(), .error(), .warn(), .info(), .debug() call.
|
||||||
|
// That can be much slower, if you plan to do lots of frequent calls, but if your program exits early or crashes, your logs will be more complete.
|
||||||
|
pub fn (mut x ThreadSafeLog) set_always_flush(should_flush bool) {
|
||||||
|
x.mu.@lock()
|
||||||
|
x.Log.set_always_flush(should_flush)
|
||||||
|
x.mu.unlock()
|
||||||
|
}
|
||||||
|
|
||||||
// debug logs a debug message
|
// debug logs a debug message
|
||||||
pub fn (mut x ThreadSafeLog) debug(s string) {
|
pub fn (mut x ThreadSafeLog) debug(s string) {
|
||||||
x.mu.@lock()
|
x.mu.@lock()
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
time_format: tf_ss_micro
|
time_format: tf_ss_micro
|
||||||
custom_time_format: 'MMMM Do YY N kk:mm:ss A'
|
custom_time_format: 'MMMM Do YY N kk:mm:ss A'
|
||||||
short_tag: false
|
short_tag: false
|
||||||
|
always_flush: false
|
||||||
output_file_name: ''
|
output_file_name: ''
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user