Skip to content

Commit

Permalink
log: implement set_always_flush/1 for log.Log, log.ThreadSafeLog and …
Browse files Browse the repository at this point in the history
…log.Logger (#20698)
  • Loading branch information
spytheman committed Feb 1, 2024
1 parent 98e0293 commit 68bd9a9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vlib/log/default.v
Expand Up @@ -53,3 +53,8 @@ pub fn info(s string) {
pub fn debug(s string) {
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)
}
24 changes: 24 additions & 0 deletions vlib/log/file_log_test.v
Expand Up @@ -40,3 +40,27 @@ fn test_reopen() {

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')
}
13 changes: 13 additions & 0 deletions vlib/log/log.v
Expand Up @@ -34,6 +34,7 @@ mut:
time_format TimeFormat
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
pub mut:
output_file_name string // log output to this file
}
Expand Down Expand Up @@ -118,13 +119,19 @@ fn (mut l Log) log_file(s string, level Level) {
timestamp := l.time_format(time.now())
e := tag_to_file(level, l.short_tag)
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.
fn (l &Log) log_cli(s string, level Level) {
timestamp := l.time_format(time.now())
e := tag_to_cli(level, l.short_tag)
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
Expand Down Expand Up @@ -244,6 +251,12 @@ pub fn (mut l Log) set_time_format(f TimeFormat) {
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
pub fn (l Log) get_time_format() TimeFormat {
return l.time_format
Expand Down
1 change: 1 addition & 0 deletions vlib/log/logger_interface.v
Expand Up @@ -11,5 +11,6 @@ mut:
debug(s string)
// utility methods:
set_level(level Level)
set_always_flush(should_flush bool)
free()
}
8 changes: 8 additions & 0 deletions vlib/log/safe_log.v
Expand Up @@ -37,6 +37,14 @@ pub fn (mut x ThreadSafeLog) set_level(level Level) {
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
pub fn (mut x ThreadSafeLog) debug(s string) {
x.mu.@lock()
Expand Down
1 change: 1 addition & 0 deletions vlib/v/slow_tests/inout/dump_expression.out
Expand Up @@ -13,6 +13,7 @@
time_format: tf_ss_micro
custom_time_format: 'MMMM Do YY N kk:mm:ss A'
short_tag: false
always_flush: false
output_file_name: ''
})
}
Expand Down

0 comments on commit 68bd9a9

Please sign in to comment.