From 60c1edb5fe8fcf24a57d8d8057c632ae33a20c08 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Tue, 16 Feb 2021 23:39:09 -0600 Subject: [PATCH 1/3] Fix legacy code Signed-off-by: jolheiser --- color/basic.go | 6 ++++++ color/basic_colors.go | 21 +++++++++++++++++++++ color/color.go | 1 + color/extended.go | 6 ++++++ color/true.go | 6 ++++++ 5 files changed, 40 insertions(+) diff --git a/color/basic.go b/color/basic.go index 99247a1..b2b3ca4 100644 --- a/color/basic.go +++ b/color/basic.go @@ -82,9 +82,15 @@ func (b *Basic) unformat() string { return fmt.Sprintf("%s[%dm", escape, Reset) } +// Format returns a string wrapped in the basic color func (b *Basic) Format(text string) string { if len(b.Attrs) > 0 { return b.wrap(text) } return text } + +// Formatf returns a formatted string wrapped in the basic color +func (b *Basic) Formatf(format string, v ...interface{}) string { + return b.Format(fmt.Sprintf(format, v...)) +} diff --git a/color/basic_colors.go b/color/basic_colors.go index 0a9ca24..f44ebcc 100644 --- a/color/basic_colors.go +++ b/color/basic_colors.go @@ -47,3 +47,24 @@ const ( BgHiCyan BgHiWhite ) + +var attrCache = make(map[BasicAttribute]*Basic) + +func attr(a BasicAttribute) *Basic { + if c, ok := attrCache[a]; ok { + return c + } + c := New(a) + attrCache[a] = c + return c +} + +// Format is a quick way to format a string using a single attribute +func (a BasicAttribute) Format(text string) string { + return attr(a).Format(text) +} + +// Format is a quick way to format a formatted string using a single attribute +func (a BasicAttribute) Formatf(text string, v ...interface{}) string { + return attr(a).Formatf(text, v...) +} diff --git a/color/color.go b/color/color.go index 2cd4e02..df5d90f 100644 --- a/color/color.go +++ b/color/color.go @@ -8,6 +8,7 @@ const escape = "\x1b" type Color interface { Format(text string) string + Formatf(text string, v ...interface{}) string } // ParseLevel parses a string and returns a Beaver Level's Color, defaulting to Info diff --git a/color/extended.go b/color/extended.go index 0a08b84..a8a7798 100644 --- a/color/extended.go +++ b/color/extended.go @@ -43,6 +43,12 @@ func (e *Extended) unformat() string { return fmt.Sprintf("%s[%dm", escape, Reset) } +// Format returns a string wrapped in the extended color func (e *Extended) Format(text string) string { return e.wrap(text) } + +// Formatf returns a formatted string wrapped in the extended color +func (e *Extended) Formatf(text string, v ...interface{}) string { + return e.wrap(fmt.Sprintf(text, v...)) +} diff --git a/color/true.go b/color/true.go index 3d6aa5e..5601db1 100644 --- a/color/true.go +++ b/color/true.go @@ -40,6 +40,12 @@ func (t *True) unformat() string { return fmt.Sprintf("%s[%dm", escape, Reset) } +// Format returns a string wrapped in the true color func (t *True) Format(text string) string { return t.wrap(text) } + +// Formatf returns a formatted string wrapped in the true color +func (t *True) Formatf(text string, v ...interface{}) string { + return t.wrap(fmt.Sprintf(text, v...)) +} -- 2.41.0 From 262effd971740480b2e2a47daa39c36549fbcd0b Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Wed, 17 Feb 2021 13:40:21 +0800 Subject: [PATCH 2/3] Fix legacy code (#4) Co-authored-by: jolheiser Reviewed-on: https://gitea.com/jolheiser/beaver/pulls/4 Co-authored-by: John Olheiser Co-committed-by: John Olheiser --- color/basic.go | 6 ++++++ color/basic_colors.go | 21 +++++++++++++++++++++ color/color.go | 1 + color/extended.go | 6 ++++++ color/true.go | 6 ++++++ 5 files changed, 40 insertions(+) diff --git a/color/basic.go b/color/basic.go index 99247a1..b2b3ca4 100644 --- a/color/basic.go +++ b/color/basic.go @@ -82,9 +82,15 @@ func (b *Basic) unformat() string { return fmt.Sprintf("%s[%dm", escape, Reset) } +// Format returns a string wrapped in the basic color func (b *Basic) Format(text string) string { if len(b.Attrs) > 0 { return b.wrap(text) } return text } + +// Formatf returns a formatted string wrapped in the basic color +func (b *Basic) Formatf(format string, v ...interface{}) string { + return b.Format(fmt.Sprintf(format, v...)) +} diff --git a/color/basic_colors.go b/color/basic_colors.go index 0a9ca24..f44ebcc 100644 --- a/color/basic_colors.go +++ b/color/basic_colors.go @@ -47,3 +47,24 @@ const ( BgHiCyan BgHiWhite ) + +var attrCache = make(map[BasicAttribute]*Basic) + +func attr(a BasicAttribute) *Basic { + if c, ok := attrCache[a]; ok { + return c + } + c := New(a) + attrCache[a] = c + return c +} + +// Format is a quick way to format a string using a single attribute +func (a BasicAttribute) Format(text string) string { + return attr(a).Format(text) +} + +// Format is a quick way to format a formatted string using a single attribute +func (a BasicAttribute) Formatf(text string, v ...interface{}) string { + return attr(a).Formatf(text, v...) +} diff --git a/color/color.go b/color/color.go index 2cd4e02..df5d90f 100644 --- a/color/color.go +++ b/color/color.go @@ -8,6 +8,7 @@ const escape = "\x1b" type Color interface { Format(text string) string + Formatf(text string, v ...interface{}) string } // ParseLevel parses a string and returns a Beaver Level's Color, defaulting to Info diff --git a/color/extended.go b/color/extended.go index 0a08b84..a8a7798 100644 --- a/color/extended.go +++ b/color/extended.go @@ -43,6 +43,12 @@ func (e *Extended) unformat() string { return fmt.Sprintf("%s[%dm", escape, Reset) } +// Format returns a string wrapped in the extended color func (e *Extended) Format(text string) string { return e.wrap(text) } + +// Formatf returns a formatted string wrapped in the extended color +func (e *Extended) Formatf(text string, v ...interface{}) string { + return e.wrap(fmt.Sprintf(text, v...)) +} diff --git a/color/true.go b/color/true.go index 3d6aa5e..5601db1 100644 --- a/color/true.go +++ b/color/true.go @@ -40,6 +40,12 @@ func (t *True) unformat() string { return fmt.Sprintf("%s[%dm", escape, Reset) } +// Format returns a string wrapped in the true color func (t *True) Format(text string) string { return t.wrap(text) } + +// Formatf returns a formatted string wrapped in the true color +func (t *True) Formatf(text string, v ...interface{}) string { + return t.wrap(fmt.Sprintf(text, v...)) +} -- 2.41.0 From 0b1a1e4bd0f8f5a149d4313e7b0d75dbe86ee0bd Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Wed, 17 Feb 2021 13:58:17 +0800 Subject: [PATCH 3/3] Time format (#5) Co-authored-by: jolheiser Reviewed-on: https://gitea.com/jolheiser/beaver/pulls/5 Co-authored-by: John Olheiser Co-committed-by: John Olheiser --- level.go | 36 ------------------------------------ logger.go | 2 +- 2 files changed, 1 insertion(+), 37 deletions(-) diff --git a/level.go b/level.go index baf8887..7b1004f 100644 --- a/level.go +++ b/level.go @@ -3,7 +3,6 @@ package beaver import ( "fmt" "strings" - "time" "go.jolheiser.com/beaver/color" ) @@ -105,38 +104,3 @@ func ParseLevel(level string) Level { } return INFO } - -func timePrefix(t time.Time) string { - var buf = &[]byte{} - year, month, day := t.Date() - itoa(buf, int(month), 2) - *buf = append(*buf, '/') - itoa(buf, day, 2) - *buf = append(*buf, '/') - itoa(buf, year, 4) - *buf = append(*buf, ' ') - - hour, min, sec := t.Clock() - itoa(buf, hour, 2) - *buf = append(*buf, ':') - itoa(buf, min, 2) - *buf = append(*buf, ':') - itoa(buf, sec, 2) - - return string(*buf) -} - -func itoa(buf *[]byte, i int, wid int) { - var b [20]byte - bp := len(b) - 1 - for i >= 10 || wid > 1 { - wid-- - q := i / 10 - b[bp] = byte('0' + i - q*10) - bp-- - i = q - } - // i < 10 - b[bp] = byte('0' + i) - *buf = append(*buf, b[bp:]...) -} diff --git a/logger.go b/logger.go index d586d9b..39c096d 100644 --- a/logger.go +++ b/logger.go @@ -42,7 +42,7 @@ func (l *Logger) write(level Level, text string) { if l.Level <= level { var message string if l.Format.TimePrefix { - message += color.Time.Format(timePrefix(time.Now())) + " " + message += color.Time.Format(time.Now().Format("01/02/2006 15:04:05")) + " " } if l.Format.StackPrefix { _, file, line, _ := runtime.Caller(l.skip) -- 2.41.0