From 18be9e52be1075a3324db2f2995b50e877336ee1 Mon Sep 17 00:00:00 2001 From: wilesun Date: Wed, 11 Aug 2021 14:26:02 +0800 Subject: [PATCH] log: avoid using string__plus memory leak (#11128) --- vlib/term/colors.v | 5 ++--- vlib/time/format.v | 6 ++++-- vlib/v/tests/valgrind/logging.v | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 vlib/v/tests/valgrind/logging.v diff --git a/vlib/term/colors.v b/vlib/term/colors.v index f7662eea31..f89d56bff4 100644 --- a/vlib/term/colors.v +++ b/vlib/term/colors.v @@ -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 { diff --git a/vlib/time/format.v b/vlib/time/format.v index 6304cbcf67..6a77bb3acf 100644 --- a/vlib/time/format.v +++ b/vlib/time/format.v @@ -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) } diff --git a/vlib/v/tests/valgrind/logging.v b/vlib/v/tests/valgrind/logging.v new file mode 100644 index 0000000000..ec2f016953 --- /dev/null +++ b/vlib/v/tests/valgrind/logging.v @@ -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) + } +}