add log replace color

pull/560/head
nzlov 2019-06-25 23:52:34 +08:00 committed by Alex Medvednikov
parent 354e9ff1f8
commit 8ef27f0bb3
4 changed files with 90 additions and 27 deletions

BIN
examples/log 100755

Binary file not shown.

12
examples/log.v 100644
View File

@ -0,0 +1,12 @@
import log
fn main(){
mut l := log.Log{level:log.INFO}
l.i('info')
l.w('warn')
l.e('error')
l.d('no debug')
l.set_level(log.DEBUG)
l.d('debug')
l.f('fatal')
}

View File

@ -2,108 +2,108 @@
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module colors module log
fn format(msg, open, close string) string { pub fn format(msg, open, close string) string {
return '\x1b[' + open + 'm' + msg + '\x1b[' + close + 'm' return '\x1b[' + open + 'm' + msg + '\x1b[' + close + 'm'
} }
fn bg_black(msg string) string { pub fn bg_black(msg string) string {
return format(msg, '40', '49') return format(msg, '40', '49')
} }
fn bg_blue(msg string) string { pub fn bg_blue(msg string) string {
return format(msg, '44', '49') return format(msg, '44', '49')
} }
fn bg_cyan(msg string) string { pub fn bg_cyan(msg string) string {
return format(msg, '46', '49') return format(msg, '46', '49')
} }
fn bg_green(msg string) string { pub fn bg_green(msg string) string {
return format(msg, '42', '49') return format(msg, '42', '49')
} }
fn bg_magenta(msg string) string { pub fn bg_magenta(msg string) string {
return format(msg, '45', '49') return format(msg, '45', '49')
} }
fn bg_red(msg string) string { pub fn bg_red(msg string) string {
return format(msg, '41', '49') return format(msg, '41', '49')
} }
fn bg_white(msg string) string { pub fn bg_white(msg string) string {
return format(msg, '47', '49') return format(msg, '47', '49')
} }
fn bg_yellow(msg string) string { pub fn bg_yellow(msg string) string {
return format(msg, '43', '49') return format(msg, '43', '49')
} }
fn black(msg string) string { pub fn black(msg string) string {
return format(msg, '30', '39') return format(msg, '30', '39')
} }
fn blue(msg string) string { pub fn blue(msg string) string {
return format(msg, '34', '39') return format(msg, '34', '39')
} }
fn bold(msg string) string { pub fn bold(msg string) string {
return format(msg, '1', '22') return format(msg, '1', '22')
} }
fn cyan(msg string) string { pub fn cyan(msg string) string {
return format(msg, '36', '39') return format(msg, '36', '39')
} }
fn dim(msg string) string { pub fn dim(msg string) string {
return format(msg, '2', '22') return format(msg, '2', '22')
} }
fn green(msg string) string { pub fn green(msg string) string {
return format(msg, '32', '39') return format(msg, '32', '39')
} }
fn gray(msg string) string { pub fn gray(msg string) string {
return format(msg, '90', '39') return format(msg, '90', '39')
} }
fn hidden(msg string) string { pub fn hidden(msg string) string {
return format(msg, '8', '28') return format(msg, '8', '28')
} }
fn italic(msg string) string { pub fn italic(msg string) string {
return format(msg, '3', '23') return format(msg, '3', '23')
} }
fn inverse(msg string) string { pub fn inverse(msg string) string {
return format(msg, '7', '27') return format(msg, '7', '27')
} }
fn magenta(msg string) string { pub fn magenta(msg string) string {
return format(msg, '35', '39') return format(msg, '35', '39')
} }
fn reset(msg string) string { pub fn reset(msg string) string {
return format(msg, '0', '0') return format(msg, '0', '0')
} }
fn red(msg string) string { pub fn red(msg string) string {
return format(msg, '31', '39') return format(msg, '31', '39')
} }
fn strikethrough(msg string) string { pub fn strikethrough(msg string) string {
return format(msg, '9', '29') return format(msg, '9', '29')
} }
fn underline(msg string) string { pub fn underline(msg string) string {
return format(msg, '4', '24') return format(msg, '4', '24')
} }
fn white(msg string) string { pub fn white(msg string) string {
return format(msg, '37', '39') return format(msg, '37', '39')
} }
fn yellow(msg string) string { pub fn yellow(msg string) string {
return format(msg, '33', '39') return format(msg, '33', '39')
} }

51
log/log.v 100644
View File

@ -0,0 +1,51 @@
module log
const (
FATAL = 1
ERROR = 2
WARN = 3
INFO = 4
DEBUG =5
)
struct Log{
mut:
level int
}
pub fn (l mut Log) set_level(level int){
l.level = level
}
pub fn (l Log) f(s string){
panic(s)
}
pub fn (l Log) e(s string){
if l.level >= ERROR{
f := red('E')
println('[$f]$s')
}
}
pub fn (l Log) w(s string){
if l.level >= WARN{
f := yellow('W')
println('[$f]$s')
}
}
pub fn (l Log) i(s string){
if l.level >= INFO{
f := white('I')
println('[$f]$s')
}
}
pub fn (l Log) d(s string){
if l.level >= DEBUG{
f := blue('D')
println('[$f]$s')
}
}