feat(quality): enhance logger
Warnings are flushed after the generation to help the user to find issues. Colors are better defined.
This commit is contained in:
@@ -4,6 +4,7 @@ package logger
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Icon is a unicode icon
|
||||
@@ -27,6 +28,18 @@ const (
|
||||
|
||||
const reset = "\033[0m"
|
||||
|
||||
const (
|
||||
ColorGreen = "\033[38;5;34m"
|
||||
ColorRed = "\033[38;5;196m"
|
||||
ColorOrange = "\033[38;5;214m"
|
||||
ColorWarning = "\033[38;5;214m"
|
||||
)
|
||||
|
||||
var (
|
||||
warnings []string
|
||||
mu sync.Mutex
|
||||
)
|
||||
|
||||
// Print prints a message without icon.
|
||||
func Print(msg ...any) {
|
||||
fmt.Print(msg...)
|
||||
@@ -49,38 +62,38 @@ func Infof(format string, msg ...any) {
|
||||
|
||||
// Warn prints a warning message.
|
||||
func Warn(msg ...any) {
|
||||
orange := "\033[38;5;214m"
|
||||
message(orange, IconWarning, msg...)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
warning := fmt.Sprint(msg...)
|
||||
warnings = append(warnings, warning)
|
||||
}
|
||||
|
||||
// Warnf prints a formatted warning message.
|
||||
func Warnf(format string, msg ...any) {
|
||||
orange := "\033[38;5;214m"
|
||||
message(orange, IconWarning, fmt.Sprintf(format, msg...))
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
warning := fmt.Sprintf(format, msg...)
|
||||
warnings = append(warnings, warning)
|
||||
}
|
||||
|
||||
// Success prints a success message.
|
||||
func Success(msg ...any) {
|
||||
green := "\033[38;5;34m"
|
||||
message(green, IconSuccess, msg...)
|
||||
message(ColorGreen, IconSuccess, msg...)
|
||||
}
|
||||
|
||||
// Successf prints a formatted success message.
|
||||
func Successf(format string, msg ...any) {
|
||||
green := "\033[38;5;34m"
|
||||
message(green, IconSuccess, fmt.Sprintf(format, msg...))
|
||||
message(ColorGreen, IconSuccess, fmt.Sprintf(format, msg...))
|
||||
}
|
||||
|
||||
// Failure prints a failure message.
|
||||
func Failure(msg ...any) {
|
||||
red := "\033[38;5;196m"
|
||||
message(red, IconFailure, msg...)
|
||||
message(ColorRed, IconFailure, msg...)
|
||||
}
|
||||
|
||||
// Failuref prints a formatted failure message.
|
||||
func Failuref(format string, msg ...any) {
|
||||
red := "\033[38;5;196m"
|
||||
message(red, IconFailure, fmt.Sprintf(format, msg...))
|
||||
message(ColorRed, IconFailure, fmt.Sprintf(format, msg...))
|
||||
}
|
||||
|
||||
// Log prints a message with a custom icon.
|
||||
@@ -106,12 +119,26 @@ func fatalf(red string, icon Icon, format string, msg ...any) {
|
||||
|
||||
// Fatal prints a fatal error message and exits with code 1.
|
||||
func Fatal(msg ...any) {
|
||||
red := "\033[38;5;196m"
|
||||
fatal(red, IconFailure, msg...)
|
||||
fatal(ColorRed, IconFailure, msg...)
|
||||
}
|
||||
|
||||
// Fatalf prints a fatal error message with formatting and exits with code 1.
|
||||
func Fatalf(format string, msg ...any) {
|
||||
red := "\033[38;5;196m"
|
||||
fatalf(red, IconFailure, format, msg...)
|
||||
fatalf(ColorRed, IconFailure, format, msg...)
|
||||
}
|
||||
|
||||
// FlushWarnings prints all collected warnings at the end of the conversion.
|
||||
func FlushWarnings() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if len(warnings) > 0 {
|
||||
fmt.Println()
|
||||
fmt.Print(ColorWarning, IconWarning, " The following issues may need attention:", reset)
|
||||
fmt.Println()
|
||||
for _, warning := range warnings {
|
||||
fmt.Println(" •", warning)
|
||||
}
|
||||
fmt.Println()
|
||||
warnings = nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,3 +77,25 @@ func TestLog(t *testing.T) {
|
||||
}()
|
||||
Log(IconInfo, "test log")
|
||||
}
|
||||
|
||||
func TestWarningsCollection(t *testing.T) {
|
||||
// Clear any existing warnings
|
||||
warnings = nil
|
||||
|
||||
// Add some warnings
|
||||
Warn("test warning 1")
|
||||
Warnf("test warning 2: %s", "value")
|
||||
|
||||
// Check that warnings were collected
|
||||
if len(warnings) != 2 {
|
||||
t.Errorf("expected 2 warnings, got %d", len(warnings))
|
||||
}
|
||||
|
||||
// Check the content of warnings
|
||||
if warnings[0] != "test warning 1" {
|
||||
t.Errorf("expected 'test warning 1', got '%s'", warnings[0])
|
||||
}
|
||||
if warnings[1] != "test warning 2: value" {
|
||||
t.Errorf("expected 'test warning 2: value', got '%s'", warnings[1])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user