mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v test v => v test-compiler
This commit is contained in:
committed by
Alexander Medvednikov
parent
854309a7d8
commit
ec15bfb7d1
128
vlib/log/log.v
128
vlib/log/log.v
@@ -3,11 +3,12 @@ module log
|
||||
import os
|
||||
import time
|
||||
import term
|
||||
import filepath
|
||||
|
||||
pub enum LogLevel {
|
||||
fatal
|
||||
fatal = 1
|
||||
error
|
||||
warning
|
||||
warn
|
||||
info
|
||||
debug
|
||||
}
|
||||
@@ -16,8 +17,8 @@ fn tag(l LogLevel) string {
|
||||
return match l {
|
||||
.fatal { term.red('F') }
|
||||
.error { term.red('E') }
|
||||
.warning { term.yellow('W') }
|
||||
.info { term.white('I') }
|
||||
.warn { term.yellow('W') }
|
||||
.info { term.white('I') }
|
||||
.debug { term.blue('D') }
|
||||
else { ' ' }
|
||||
}
|
||||
@@ -40,17 +41,21 @@ interface Logger {
|
||||
}
|
||||
|
||||
pub struct Log {
|
||||
mut:
|
||||
mut:
|
||||
level LogLevel
|
||||
output_label string
|
||||
|
||||
ofile os.File
|
||||
output_to_file bool
|
||||
pub:
|
||||
output_file_name string
|
||||
}
|
||||
|
||||
pub fn (l mut Log) set_level(level int){
|
||||
l.level = match level {
|
||||
FATAL { LogLevel.fatal }
|
||||
ERROR { LogLevel.error }
|
||||
WARN { LogLevel.warning }
|
||||
WARN { LogLevel.warn }
|
||||
INFO { LogLevel.info }
|
||||
DEBUG { LogLevel.debug }
|
||||
else { .debug }
|
||||
@@ -61,79 +66,74 @@ pub fn (l mut Log) set_output_level(level LogLevel){
|
||||
l.level = level
|
||||
}
|
||||
|
||||
pub fn (l mut Log) set_output_label(label string) {
|
||||
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 ) )
|
||||
}
|
||||
|
||||
pub fn (l mut Log) set_output_label(label string){
|
||||
l.output_label = label
|
||||
}
|
||||
|
||||
pub fn (l mut Log) set_output(output string){
|
||||
l.output_label = output
|
||||
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 )
|
||||
mut ofile := os.open_append( l.output_file_name ) or {
|
||||
panic('error while opening log file ${l.output_file_name} for appending')
|
||||
}
|
||||
l.ofile = ofile
|
||||
}
|
||||
|
||||
fn (l Log) log_file(s string, level LogLevel) {
|
||||
filename := '${l.output_label}.log'.replace(' ', '')
|
||||
f := os.open_append(filename) or {
|
||||
panic('error reading file $filename')
|
||||
}
|
||||
pub fn (l mut Log) close(){
|
||||
l.ofile.close()
|
||||
}
|
||||
|
||||
fn (l mut Log) log_file(s string, level LogLevel) {
|
||||
timestamp := time.now().format_ss()
|
||||
e := tag(level)
|
||||
f.writeln('$timestamp [$e] $s')
|
||||
l.ofile.writeln('$timestamp [$e] $s')
|
||||
}
|
||||
|
||||
fn (l Log) log_cli(s string, level LogLevel) {
|
||||
fn (l mut Log) log_cli(s string, level LogLevel) {
|
||||
f := tag(level)
|
||||
t := time.now()
|
||||
println('[$f ${t.format_ss()}] $s')
|
||||
}
|
||||
|
||||
pub fn (l Log) fatal(s string){
|
||||
if l.level == .fatal {
|
||||
if l.output_to_file {
|
||||
l.log_file(s, .fatal)
|
||||
} else {
|
||||
l.log_cli(s, .fatal)
|
||||
}
|
||||
panic('$l.output_label: $s')
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (l Log) error(s string){
|
||||
if l.level in [.info, .debug, .warning, .error] {
|
||||
if l.output_to_file {
|
||||
l.log_file(s, .error)
|
||||
} else {
|
||||
l.log_cli(s, .error)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (l Log) warn(s string){
|
||||
if l.level in [.info, .debug, .warning] {
|
||||
if l.output_to_file {
|
||||
l.log_file(s, .warning)
|
||||
} else {
|
||||
l.log_cli(s, .warning)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (l Log) info(s string){
|
||||
if l.level in [.info, .debug] {
|
||||
if l.output_to_file {
|
||||
l.log_file(s, .info)
|
||||
} else {
|
||||
l.log_cli(s, .info)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (l Log) debug(s string){
|
||||
if l.level != .debug {
|
||||
return
|
||||
}
|
||||
fn (l mut Log) send_output(s &string, level LogLevel){
|
||||
if l.output_to_file {
|
||||
l.log_file(s, .debug)
|
||||
l.log_file(s, level)
|
||||
} else {
|
||||
l.log_cli(s, .debug)
|
||||
l.log_cli(s, level)
|
||||
}
|
||||
}
|
||||
|
||||
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')
|
||||
}
|
||||
|
||||
pub fn (l mut Log) error(s string){
|
||||
if l.level < .error { return }
|
||||
l.send_output(s, .error)
|
||||
}
|
||||
|
||||
pub fn (l mut Log) warn(s string){
|
||||
if l.level < .warn { return }
|
||||
l.send_output(s, .warn)
|
||||
}
|
||||
|
||||
pub fn (l mut Log) info(s string){
|
||||
if l.level < .info { return }
|
||||
l.send_output(s, .info)
|
||||
}
|
||||
|
||||
pub fn (l mut Log) debug(s string){
|
||||
if l.level < .debug { return }
|
||||
l.send_output(s, .debug)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user