Files
katenary/internal/logger/logger.go

145 lines
3.2 KiB
Go
Raw Permalink Normal View History

// Package logger provides simple logging functions with icons and colors.
package logger
2026-03-08 22:38:03 +01:00
import (
"fmt"
"os"
"sync"
2026-03-08 22:38:03 +01:00
)
// Icon is a unicode icon
type Icon string
// Icons used in katenary.
const (
IconSuccess Icon = "✅"
IconFailure Icon = "❌"
IconWarning Icon = "❕"
IconNote Icon = "📝"
IconWorld Icon = "🌐"
IconPlug Icon = "🔌"
IconPackage Icon = "📦"
IconCabinet Icon = "🗄️"
IconInfo Icon = "🔵"
IconSecret Icon = "🔒"
IconConfig Icon = "🔧"
IconDependency Icon = "🔗"
)
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
)
2026-03-08 22:46:26 +01:00
// Print prints a message without icon.
func Print(msg ...any) {
fmt.Print(msg...)
}
// Printf prints a formatted message without icon.
func Printf(format string, msg ...any) {
fmt.Printf(format, msg...)
}
2025-09-15 13:36:22 +02:00
// Info prints an informational message.
func Info(msg ...any) {
message("", IconInfo, msg...)
}
2026-03-08 22:46:26 +01:00
// Infof prints a formatted informational message.
func Infof(format string, msg ...any) {
message("", IconInfo, fmt.Sprintf(format, msg...))
}
2025-09-15 13:36:22 +02:00
// Warn prints a warning message.
func Warn(msg ...any) {
mu.Lock()
defer mu.Unlock()
warning := fmt.Sprint(msg...)
warnings = append(warnings, warning)
}
2026-03-08 22:46:26 +01:00
// Warnf prints a formatted warning message.
func Warnf(format string, msg ...any) {
mu.Lock()
defer mu.Unlock()
warning := fmt.Sprintf(format, msg...)
warnings = append(warnings, warning)
2026-03-08 22:46:26 +01:00
}
2025-09-15 13:36:22 +02:00
// Success prints a success message.
func Success(msg ...any) {
message(ColorGreen, IconSuccess, msg...)
}
2026-03-08 22:46:26 +01:00
// Successf prints a formatted success message.
func Successf(format string, msg ...any) {
message(ColorGreen, IconSuccess, fmt.Sprintf(format, msg...))
2026-03-08 22:46:26 +01:00
}
2025-09-15 13:36:22 +02:00
// Failure prints a failure message.
func Failure(msg ...any) {
message(ColorRed, IconFailure, msg...)
}
2026-03-08 22:46:26 +01:00
// Failuref prints a formatted failure message.
func Failuref(format string, msg ...any) {
message(ColorRed, IconFailure, fmt.Sprintf(format, msg...))
2026-03-08 22:46:26 +01:00
}
2025-09-15 13:36:22 +02:00
// Log prints a message with a custom icon.
func Log(icon Icon, msg ...any) {
message("", icon, msg...)
}
2026-03-08 22:38:03 +01:00
2026-03-08 22:46:26 +01:00
// Logf prints a formatted message with a custom icon.
func Logf(icon Icon, format string, msg ...any) {
message("", icon, fmt.Sprintf(format, msg...))
}
2026-03-08 22:38:03 +01:00
func fatal(red string, icon Icon, msg ...any) {
fmt.Print(icon, " ", red)
fmt.Print(msg...)
fmt.Println(reset)
os.Exit(1)
}
func fatalf(red string, icon Icon, format string, msg ...any) {
fatal(red, icon, fmt.Sprintf(format, msg...))
}
// Fatal prints a fatal error message and exits with code 1.
func Fatal(msg ...any) {
fatal(ColorRed, IconFailure, msg...)
2026-03-08 22:38:03 +01:00
}
// Fatalf prints a fatal error message with formatting and exits with code 1.
func Fatalf(format string, msg ...any) {
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
}
2026-03-08 22:38:03 +01:00
}