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-06-26 03:14:38 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
FATAL = 1
|
|
|
|
ERROR = 2
|
|
|
|
WARN = 3
|
|
|
|
INFO = 4
|
|
|
|
DEBUG =5
|
|
|
|
)
|
|
|
|
|
|
|
|
struct Log{
|
|
|
|
mut:
|
|
|
|
level int
|
2019-07-24 18:50:29 +03:00
|
|
|
output string
|
2019-06-26 03:14:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn (l mut Log) set_level(level int){
|
|
|
|
l.level = level
|
|
|
|
}
|
|
|
|
|
2019-07-24 18:50:29 +03:00
|
|
|
pub fn (l mut Log) set_output(output string) {
|
|
|
|
l.output = output
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (l Log) log_file(s string, e string) {
|
|
|
|
filename := l.output
|
|
|
|
f := os.open_append(l.output) or {
|
|
|
|
panic('error reading file $filename')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
timestamp := time.now().format_ss()
|
|
|
|
f.writeln('$timestamp [$e] $s')
|
|
|
|
}
|
|
|
|
|
2019-06-26 03:14:38 +03:00
|
|
|
pub fn (l Log) fatal(s string){
|
|
|
|
panic(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (l Log) error(s string){
|
|
|
|
if l.level >= ERROR{
|
2019-07-24 18:50:29 +03:00
|
|
|
switch l.output {
|
|
|
|
case 'terminal':
|
|
|
|
f := term.red('E')
|
|
|
|
println('[$f]$s')
|
|
|
|
|
|
|
|
default:
|
|
|
|
l.log_file(s, 'E')
|
|
|
|
}
|
2019-06-26 03:14:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (l Log) warn(s string){
|
|
|
|
if l.level >= WARN{
|
2019-07-24 18:50:29 +03:00
|
|
|
switch l.output {
|
|
|
|
case 'terminal':
|
|
|
|
f := term.yellow('W')
|
|
|
|
println('[$f]$s')
|
|
|
|
|
|
|
|
default:
|
|
|
|
l.log_file(s, 'W')
|
|
|
|
}
|
|
|
|
}
|
2019-06-26 03:14:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (l Log) info(s string){
|
|
|
|
if l.level >= INFO{
|
2019-07-24 18:50:29 +03:00
|
|
|
switch l.output {
|
|
|
|
case 'terminal':
|
|
|
|
f := term.white('I')
|
|
|
|
println('[$f]$s')
|
|
|
|
|
|
|
|
default:
|
|
|
|
l.log_file(s, 'I')
|
|
|
|
}
|
2019-06-26 03:14:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (l Log) debug(s string){
|
|
|
|
if l.level >= DEBUG{
|
2019-07-24 18:50:29 +03:00
|
|
|
switch l.output {
|
|
|
|
case 'terminal':
|
|
|
|
f := term.blue('D')
|
|
|
|
println('[$f]$s')
|
|
|
|
|
|
|
|
default:
|
|
|
|
l.log_file(s, 'D')
|
|
|
|
}
|
2019-06-26 03:14:38 +03:00
|
|
|
}
|
2019-07-16 18:59:07 +03:00
|
|
|
}
|