1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/log/log.v

138 lines
2.4 KiB
V
Raw Normal View History

2019-06-26 03:14:38 +03:00
module log
2020-04-26 14:49:31 +03:00
import os
import time
import term
2019-06-26 03:14:38 +03:00
2020-04-06 18:22:53 +03:00
pub enum Level {
2019-12-01 12:50:13 +03:00
fatal = 1
2019-11-25 07:50:59 +03:00
error
2019-12-01 12:50:13 +03:00
warn
2019-11-25 07:50:59 +03:00
info
debug
}
2020-04-06 18:22:53 +03:00
fn tag(l Level) string {
2019-11-25 07:50:59 +03:00
return match l {
.fatal { term.red('FATAL') }
.error { term.red('ERROR') }
2020-07-23 08:02:53 +03:00
.warn { term.yellow('WARN ') }
.info { term.white('INFO ') }
.debug { term.blue('DEBUG') }
2019-11-25 07:50:59 +03:00
}
}
interface Logger {
2019-11-25 07:50:59 +03:00
fatal(s string)
error(s string)
warn(s string)
info(s string)
debug(s string)
}
2019-10-28 18:53:02 +03:00
pub struct Log {
2019-12-01 12:50:13 +03:00
mut:
2020-04-06 18:22:53 +03:00
level Level
output_label string
ofile os.File
output_to_file bool
pub mut:
2019-12-01 12:50:13 +03:00
output_file_name string
2019-06-26 03:14:38 +03:00
}
2020-05-17 14:51:18 +03:00
pub fn (mut l Log) set_level(level Level) {
2020-04-06 18:22:53 +03:00
l.level = level
2019-11-25 07:50:59 +03:00
}
2020-05-17 14:51:18 +03:00
pub fn (mut l Log) set_output_level(level Level) {
2019-11-25 07:50:59 +03:00
l.level = level
2019-06-26 03:14:38 +03:00
}
2020-05-17 14:51:18 +03:00
pub fn (mut l Log) set_full_logpath(full_log_path string) {
2020-07-23 08:02:53 +03:00
rlog_file := os.real_path(full_log_path)
l.set_output_label(os.file_name(rlog_file))
l.set_output_path(os.base_dir(rlog_file))
}
2019-12-01 12:50:13 +03:00
2020-07-23 08:02:53 +03:00
pub fn (mut l Log) set_output_label(label string) {
2019-11-25 07:50:59 +03:00
l.output_label = label
2019-07-24 18:50:29 +03:00
}
2020-05-17 14:51:18 +03:00
pub fn (mut l Log) set_output_path(output_file_path string) {
2020-07-23 08:02:53 +03:00
if l.ofile.is_opened() {
l.ofile.close()
}
2019-12-01 12:50:13 +03:00
l.output_to_file = true
2020-07-23 08:02:53 +03:00
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')
2019-12-01 12:50:13 +03:00
}
l.ofile = ofile
2019-11-25 07:50:59 +03:00
}
2020-07-23 08:02:53 +03:00
// Writes the log file content to disk
pub fn (mut l Log) flush() {
l.ofile.flush()
}
2020-05-17 14:51:18 +03:00
pub fn (mut l Log) close() {
2020-07-23 08:02:53 +03:00
l.ofile.close()
2019-12-01 12:50:13 +03:00
}
2020-05-17 14:51:18 +03:00
fn (mut l Log) log_file(s string, level Level) {
2019-11-25 07:50:59 +03:00
timestamp := time.now().format_ss()
e := tag(level)
2019-12-01 12:50:13 +03:00
l.ofile.writeln('$timestamp [$e] $s')
2019-11-25 07:50:59 +03:00
}
2020-04-06 18:22:53 +03:00
fn (l &Log) log_cli(s string, level Level) {
2019-11-25 07:50:59 +03:00
f := tag(level)
t := time.now()
2020-07-23 08:02:53 +03:00
println('[$f $t.format_ss()] $s')
2019-07-24 18:50:29 +03:00
}
2020-05-17 14:51:18 +03:00
fn (mut l Log) send_output(s &string, level Level) {
2019-12-01 12:50:13 +03:00
if l.output_to_file {
l.log_file(s, level)
} else {
l.log_cli(s, level)
2019-11-25 07:50:59 +03:00
}
2019-06-26 03:14:38 +03:00
}
2020-07-23 08:02:53 +03:00
pub fn (mut l Log) fatal(s string) {
if l.level < .fatal {
return
}
2019-12-01 12:50:13 +03:00
l.send_output(s, .fatal)
l.ofile.close()
panic('$l.output_label: $s')
}
2019-11-25 07:50:59 +03:00
2020-05-17 14:51:18 +03:00
pub fn (mut l Log) error(s string) {
2020-07-23 08:02:53 +03:00
if l.level < .error {
return
}
2019-12-01 12:50:13 +03:00
l.send_output(s, .error)
2019-06-26 03:14:38 +03:00
}
2020-05-17 14:51:18 +03:00
pub fn (mut l Log) warn(s string) {
2020-07-23 08:02:53 +03:00
if l.level < .warn {
return
}
2019-12-01 12:50:13 +03:00
l.send_output(s, .warn)
2019-06-26 03:14:38 +03:00
}
2020-05-17 14:51:18 +03:00
pub fn (mut l Log) info(s string) {
2020-07-23 08:02:53 +03:00
if l.level < .info {
return
}
2019-12-01 12:50:13 +03:00
l.send_output(s, .info)
2019-06-26 03:14:38 +03:00
}
2020-05-17 14:51:18 +03:00
pub fn (mut l Log) debug(s string) {
2020-07-23 08:02:53 +03:00
if l.level < .debug {
return
}
2019-12-01 12:50:13 +03:00
l.send_output(s, .debug)
2019-07-16 18:59:07 +03:00
}