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