2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2020-12-18 20:04:49 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
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-12-18 20:04:49 +03:00
|
|
|
// Level defines possible log levels used by `Log`
|
2020-04-06 18:22:53 +03:00
|
|
|
pub enum Level {
|
2021-12-18 13:38:43 +03:00
|
|
|
disabled = 0
|
|
|
|
fatal
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-18 13:38:43 +03:00
|
|
|
// LogTarget defines possible log targets
|
2021-04-02 10:12:52 +03:00
|
|
|
pub enum LogTarget {
|
|
|
|
console
|
|
|
|
file
|
|
|
|
both
|
|
|
|
}
|
|
|
|
|
2021-12-18 13:38:43 +03:00
|
|
|
// tag_to_cli returns the tag for log level `l` as a colored string.
|
2021-01-08 13:26:37 +03:00
|
|
|
fn tag_to_cli(l Level) string {
|
2019-11-25 07:50:59 +03:00
|
|
|
return match l {
|
2021-12-18 13:38:43 +03:00
|
|
|
.disabled { '' }
|
2020-02-29 16:31:59 +03:00
|
|
|
.fatal { term.red('FATAL') }
|
|
|
|
.error { term.red('ERROR') }
|
2020-07-23 08:02:53 +03:00
|
|
|
.warn { term.yellow('WARN ') }
|
|
|
|
.info { term.white('INFO ') }
|
2020-02-29 16:31:59 +03:00
|
|
|
.debug { term.blue('DEBUG') }
|
2019-11-25 07:50:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-18 13:38:43 +03:00
|
|
|
// tag_to_file returns the tag for log level `l` as a string.
|
2021-01-08 13:26:37 +03:00
|
|
|
fn tag_to_file(l Level) string {
|
|
|
|
return match l {
|
2021-12-18 13:38:43 +03:00
|
|
|
.disabled { ' ' }
|
2021-01-08 13:26:37 +03:00
|
|
|
.fatal { 'FATAL' }
|
|
|
|
.error { 'ERROR' }
|
|
|
|
.warn { 'WARN ' }
|
|
|
|
.info { 'INFO ' }
|
|
|
|
.debug { 'DEBUG' }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 07:09:48 +03:00
|
|
|
// level_from_tag returns the log level from the given string if it matches.
|
2021-12-18 13:38:43 +03:00
|
|
|
pub fn level_from_tag(tag string) ?Level {
|
|
|
|
return match tag {
|
|
|
|
'DISABLED' { Level.disabled }
|
|
|
|
'FATAL' { Level.fatal }
|
|
|
|
'ERROR' { Level.error }
|
|
|
|
'WARN' { Level.warn }
|
|
|
|
'INFO' { Level.info }
|
|
|
|
'DEBUG' { Level.debug }
|
|
|
|
else { none }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 07:09:48 +03:00
|
|
|
// target_from_label returns the log target from the given string if it matches.
|
|
|
|
pub fn target_from_label(label string) ?LogTarget {
|
|
|
|
return match label {
|
|
|
|
'console' { LogTarget.console }
|
|
|
|
'file' { LogTarget.file }
|
|
|
|
'both' { LogTarget.both }
|
|
|
|
else { none }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-18 13:38:43 +03:00
|
|
|
// Logger is an interface that describes a generic Logger
|
|
|
|
pub interface Logger {
|
2022-02-05 23:09:55 +03:00
|
|
|
mut:
|
2019-11-25 07:50:59 +03:00
|
|
|
fatal(s string)
|
|
|
|
error(s string)
|
|
|
|
warn(s string)
|
|
|
|
info(s string)
|
|
|
|
debug(s string)
|
2022-08-01 15:09:32 +03:00
|
|
|
set_level(level Level)
|
2019-08-07 08:44:43 +03:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:04:49 +03:00
|
|
|
// Log represents a logging object
|
2019-10-28 18:53:02 +03:00
|
|
|
pub struct Log {
|
2019-12-01 12:50:13 +03:00
|
|
|
mut:
|
2021-04-02 10:12:52 +03:00
|
|
|
level Level
|
|
|
|
output_label string
|
|
|
|
ofile os.File
|
2021-12-18 13:38:43 +03:00
|
|
|
output_target LogTarget // output to console (stdout/stderr) or file or both.
|
2019-12-13 20:09:11 +03:00
|
|
|
pub mut:
|
2020-12-18 20:04:49 +03:00
|
|
|
output_file_name string // log output to this file
|
2019-06-26 03:14:38 +03:00
|
|
|
}
|
|
|
|
|
2021-09-23 12:05:11 +03:00
|
|
|
// get_level gets the internal logging level.
|
|
|
|
pub fn (mut l Log) get_level() Level {
|
|
|
|
return l.level
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:04:49 +03:00
|
|
|
// set_level sets the internal logging to `level`.
|
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-12-18 20:04:49 +03:00
|
|
|
// set_output_level sets the internal logging output to `level`.
|
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-12-18 20:04:49 +03:00
|
|
|
// set_full_logpath sets the output label and output path from `full_log_path`.
|
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))
|
2020-10-01 02:30:22 +03:00
|
|
|
l.set_output_path(os.dir(rlog_file))
|
2019-12-06 15:24:53 +03:00
|
|
|
}
|
2019-12-01 12:50:13 +03:00
|
|
|
|
2020-12-18 20:04:49 +03:00
|
|
|
// set_output_label sets the `label` for the output.
|
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-12-18 20:04:49 +03:00
|
|
|
// set_output_path sets the file to which output is logged to.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut l Log) set_output_path(output_file_path string) {
|
2020-08-03 02:35:08 +03:00
|
|
|
if l.ofile.is_opened {
|
2020-07-23 08:02:53 +03:00
|
|
|
l.ofile.close()
|
|
|
|
}
|
2021-04-02 10:12:52 +03:00
|
|
|
l.output_target = .file
|
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
|
|
|
}
|
2019-12-06 15:24:53 +03:00
|
|
|
l.ofile = ofile
|
2019-11-25 07:50:59 +03:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:12:52 +03:00
|
|
|
// log_to_console_too turns on logging to the console too, in addition to logging to a file.
|
|
|
|
// You have to call it *after* calling .set_output_path(output_file_path).
|
|
|
|
pub fn (mut l Log) log_to_console_too() {
|
|
|
|
if l.output_target != .file {
|
|
|
|
panic('log_to_console_too should be called *after* .set_output_path')
|
|
|
|
}
|
|
|
|
l.output_target = .both
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:04:49 +03:00
|
|
|
// flush writes the log file content to disk.
|
2020-07-23 08:02:53 +03:00
|
|
|
pub fn (mut l Log) flush() {
|
|
|
|
l.ofile.flush()
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:04:49 +03:00
|
|
|
// close closes the log file.
|
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-12-18 20:04:49 +03:00
|
|
|
// log_file writes log line `s` with `level` to the log file.
|
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()
|
2021-01-08 13:26:37 +03:00
|
|
|
e := tag_to_file(level)
|
2021-03-01 02:18:14 +03:00
|
|
|
l.ofile.writeln('$timestamp [$e] $s') or { panic(err) }
|
2019-11-25 07:50:59 +03:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:04:49 +03:00
|
|
|
// log_cli writes log line `s` with `level` to stdout.
|
2020-04-06 18:22:53 +03:00
|
|
|
fn (l &Log) log_cli(s string, level Level) {
|
2021-04-12 09:38:47 +03:00
|
|
|
timestamp := time.now().format_ss()
|
|
|
|
e := tag_to_cli(level)
|
|
|
|
println('$timestamp [$e] $s')
|
2019-07-24 18:50:29 +03:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:12:52 +03:00
|
|
|
// send_output writes log line `s` with `level` to either the log file or the console
|
|
|
|
// according to the value of the `.output_target` field.
|
2021-04-02 13:53:29 +03:00
|
|
|
pub fn (mut l Log) send_output(s &string, level Level) {
|
2021-04-02 10:12:52 +03:00
|
|
|
if l.output_target == .file || l.output_target == .both {
|
2019-12-01 12:50:13 +03:00
|
|
|
l.log_file(s, level)
|
2021-04-02 10:12:52 +03:00
|
|
|
}
|
|
|
|
if l.output_target == .console || l.output_target == .both {
|
2019-12-01 12:50:13 +03:00
|
|
|
l.log_cli(s, level)
|
2019-11-25 07:50:59 +03:00
|
|
|
}
|
2019-06-26 03:14:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:04:49 +03:00
|
|
|
// fatal logs line `s` via `send_output` if `Log.level` is greater than or equal to the `Level.fatal` category.
|
2021-12-18 13:38:43 +03:00
|
|
|
// Note that this method performs a panic at the end, even if log level is not enabled.
|
2020-07-23 08:02:53 +03:00
|
|
|
pub fn (mut l Log) fatal(s string) {
|
2021-12-18 13:38:43 +03:00
|
|
|
if int(l.level) >= int(Level.fatal) {
|
|
|
|
l.send_output(s, .fatal)
|
|
|
|
l.ofile.close()
|
2020-07-23 08:02:53 +03:00
|
|
|
}
|
2019-12-01 12:50:13 +03:00
|
|
|
panic('$l.output_label: $s')
|
|
|
|
}
|
2019-11-25 07:50:59 +03:00
|
|
|
|
2020-12-18 20:04:49 +03:00
|
|
|
// error logs line `s` via `send_output` if `Log.level` is greater than or equal to the `Level.error` category.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut l Log) error(s string) {
|
2021-01-10 13:26:31 +03:00
|
|
|
if int(l.level) < int(Level.error) {
|
2020-07-23 08:02:53 +03:00
|
|
|
return
|
|
|
|
}
|
2019-12-01 12:50:13 +03:00
|
|
|
l.send_output(s, .error)
|
2019-06-26 03:14:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:04:49 +03:00
|
|
|
// warn logs line `s` via `send_output` if `Log.level` is greater than or equal to the `Level.warn` category.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut l Log) warn(s string) {
|
2021-01-10 13:26:31 +03:00
|
|
|
if int(l.level) < int(Level.warn) {
|
2020-07-23 08:02:53 +03:00
|
|
|
return
|
|
|
|
}
|
2019-12-01 12:50:13 +03:00
|
|
|
l.send_output(s, .warn)
|
2019-06-26 03:14:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:04:49 +03:00
|
|
|
// info logs line `s` via `send_output` if `Log.level` is greater than or equal to the `Level.info` category.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut l Log) info(s string) {
|
2021-01-10 13:26:31 +03:00
|
|
|
if int(l.level) < int(Level.info) {
|
2020-07-23 08:02:53 +03:00
|
|
|
return
|
|
|
|
}
|
2019-12-01 12:50:13 +03:00
|
|
|
l.send_output(s, .info)
|
2019-06-26 03:14:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-18 20:04:49 +03:00
|
|
|
// debug logs line `s` via `send_output` if `Log.level` is greater than or equal to the `Level.debug` category.
|
2020-05-17 14:51:18 +03:00
|
|
|
pub fn (mut l Log) debug(s string) {
|
2021-01-10 13:26:31 +03:00
|
|
|
if int(l.level) < int(Level.debug) {
|
2020-07-23 08:02:53 +03:00
|
|
|
return
|
|
|
|
}
|
2019-12-01 12:50:13 +03:00
|
|
|
l.send_output(s, .debug)
|
2019-07-16 18:59:07 +03:00
|
|
|
}
|