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

140 lines
2.5 KiB
V
Raw Normal View History

2019-06-26 03:14:38 +03:00
module log
2019-07-24 18:50:29 +03:00
import os
import time
2019-07-01 18:09:22 +03:00
import term
2019-12-01 12:50:13 +03:00
import filepath
2019-06-26 03:14:38 +03:00
2019-11-25 07:50:59 +03:00
pub enum LogLevel {
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
}
fn tag(l LogLevel) string {
return match l {
.fatal { term.red('F') }
.error { term.red('E') }
2019-12-01 12:50:13 +03:00
.warn { term.yellow('W') }
.info { term.white('I') }
2019-11-25 07:50:59 +03:00
.debug { term.blue('D') }
else { ' ' }
}
}
2019-10-28 18:53:02 +03:00
pub const (
2019-11-25 07:50:59 +03:00
FATAL = 1
ERROR = 2
WARN = 3
INFO = 4
DEBUG = 5
2019-06-26 03:14:38 +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:
2019-11-25 07:50:59 +03:00
level LogLevel
output_label string
2019-12-01 12:50:13 +03:00
ofile os.File
2019-11-25 07:50:59 +03:00
output_to_file bool
2019-12-01 12:50:13 +03:00
pub:
output_file_name string
2019-06-26 03:14:38 +03:00
}
pub fn (l mut Log) set_level(level int){
2019-11-25 07:50:59 +03:00
l.level = match level {
FATAL { LogLevel.fatal }
ERROR { LogLevel.error }
2019-12-01 12:50:13 +03:00
WARN { LogLevel.warn }
2019-11-25 07:50:59 +03:00
INFO { LogLevel.info }
DEBUG { LogLevel.debug }
else { .debug }
}
}
pub fn (l mut Log) set_output_level(level LogLevel){
l.level = level
2019-06-26 03:14:38 +03:00
}
2019-12-01 12:50:13 +03:00
pub fn (l mut Log) set_full_logpath(full_log_path string) {
rlog_file := os.realpath( full_log_path )
l.set_output_label( os.filename( rlog_file ) )
l.set_output_path( os.basedir( rlog_file ) )
}
2019-12-01 12:50:13 +03:00
pub fn (l mut 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
}
2019-12-01 12:50:13 +03:00
pub fn (l mut Log) set_output_path(output_file_path string) {
if l.ofile.is_opened() { l.ofile.close() }
l.output_to_file = true
l.output_file_name = filepath.join( os.realpath( output_file_path ) , l.output_label )
ofile := os.open_append( l.output_file_name ) or {
2019-12-01 12:50:13 +03:00
panic('error while opening log file ${l.output_file_name} for appending')
}
l.ofile = ofile
2019-11-25 07:50:59 +03:00
}
2019-12-01 12:50:13 +03:00
pub fn (l mut Log) close(){
l.ofile.close()
}
fn (l mut Log) log_file(s string, level LogLevel) {
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
}
fn (l &Log) log_cli(s string, level LogLevel) {
2019-11-25 07:50:59 +03:00
f := tag(level)
t := time.now()
println('[$f ${t.format_ss()}] $s')
2019-07-24 18:50:29 +03:00
}
2019-12-01 12:50:13 +03:00
fn (l mut Log) send_output(s &string, level LogLevel){
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
}
2019-12-01 12:50:13 +03:00
pub fn (l mut Log) fatal(s string){
if l.level < .fatal { return }
l.send_output(s, .fatal)
l.ofile.close()
panic('$l.output_label: $s')
}
2019-11-25 07:50:59 +03:00
2019-12-01 12:50:13 +03:00
pub fn (l mut Log) error(s string){
if l.level < .error { return }
l.send_output(s, .error)
2019-06-26 03:14:38 +03:00
}
2019-12-01 12:50:13 +03:00
pub fn (l mut Log) warn(s string){
if l.level < .warn { return }
l.send_output(s, .warn)
2019-06-26 03:14:38 +03:00
}
2019-12-01 12:50:13 +03:00
pub fn (l mut Log) info(s string){
if l.level < .info { return }
l.send_output(s, .info)
2019-06-26 03:14:38 +03:00
}
2019-12-01 12:50:13 +03:00
pub fn (l mut Log) debug(s string){
if l.level < .debug { return }
l.send_output(s, .debug)
2019-07-16 18:59:07 +03:00
}
2019-12-01 12:50:13 +03:00