log: avoid using string__plus memory leak (#11128)

pull/11139/head
wilesun 2021-08-11 14:26:02 +08:00 committed by GitHub
parent 70124d2d23
commit 18be9e52be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View File

@ -4,12 +4,11 @@
module term
pub fn format(msg string, open string, close string) string {
return '\x1b[' + open + 'm' + msg + '\x1b[' + close + 'm'
return '\x1b[${open}m$msg\x1b[${close}m'
}
pub fn format_rgb(r int, g int, b int, msg string, open string, close string) string {
return '\x1b[' + open + ';2;' + r.str() + ';' + g.str() + ';' + b.str() + 'm' + msg + '\x1b[' +
close + 'm'
return '\x1b[$open;2;$r;$g;${b}m$msg\x1b[${close}m'
}
pub fn rgb(r int, g int, b int, msg string) string {

View File

@ -117,7 +117,7 @@ pub fn (t Time) get_fmt_date_str(fmt_dlmtr FormatDelimiter, fmt_date FormatDate)
if fmt_date == .no_date {
return ''
}
month := '$t.smonth()'
month := t.smonth()
year := '${(t.year % 100):02d}'
mut res := match fmt_date {
.ddmmyy { '${t.day:02d}|${t.month:02d}|$year' }
@ -156,7 +156,9 @@ pub fn (t Time) get_fmt_str(fmt_dlmtr FormatDelimiter, fmt_time FormatTime, fmt_
}
} else {
if fmt_time != .no_time {
return t.get_fmt_date_str(fmt_dlmtr, fmt_date) + ' ' + t.get_fmt_time_str(fmt_time)
dstr := t.get_fmt_date_str(fmt_dlmtr, fmt_date)
tstr := t.get_fmt_time_str(fmt_time)
return '$dstr $tstr'
} else {
return t.get_fmt_date_str(fmt_dlmtr, fmt_date)
}

View File

@ -0,0 +1,18 @@
import log
import time
fn main() {
mut l := log.Log{}
defer {
l.close()
}
l.set_level(.info)
l.info('info')
l.warn('warn')
l.error('an error')
l.debug('some debug info')
for i := 0; i < 100; i++ {
l.info('123456')
time.sleep(1 * time.millisecond)
}
}