updated logging
This commit is contained in:
106
modules/ui/ui.go
106
modules/ui/ui.go
@@ -4,75 +4,101 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"fes/modules/config"
|
||||
"fes/modules/version"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
const (
|
||||
hint_color = 0xbda02a
|
||||
hintColor = 0xbda02a
|
||||
)
|
||||
|
||||
/* print out the current path (route) and relevant error */
|
||||
func Path(path string, err error) {
|
||||
path = strings.TrimPrefix(path, "/")
|
||||
func formatTimestamp() string {
|
||||
return time.Now().Format("02 Jan 2006 15:04")
|
||||
}
|
||||
|
||||
if path == "" {
|
||||
path = "(null)"
|
||||
}
|
||||
|
||||
fmt.Printf(" > %s ", path)
|
||||
if err == nil {
|
||||
OK("ok")
|
||||
return
|
||||
} else if errors.Is(err, config.ErrRouteMiss) {
|
||||
WARN(config.ErrRouteMiss.Error())
|
||||
func logMessage(prefix string, msg string, args ...any) {
|
||||
formatted := fmt.Sprintf(msg, args...)
|
||||
if prefix == "" {
|
||||
fmt.Printf("%s * %s\n", formatTimestamp(), formatted)
|
||||
} else {
|
||||
ERROR("bad")
|
||||
fmt.Printf("%s * %s: %s\n", formatTimestamp(), prefix, formatted)
|
||||
}
|
||||
}
|
||||
|
||||
/* print general system warning */
|
||||
func Warning(msg string, err error) error {
|
||||
fmt.Printf("%s: %s: %v\n", version.PROGRAM_NAME, color.MagentaString("warning"), err)
|
||||
return err
|
||||
// Generic log
|
||||
func Log(msg string, args ...any) {
|
||||
logMessage("", msg, args...)
|
||||
}
|
||||
|
||||
/* print general system error */
|
||||
func Error(msg string, err error) error {
|
||||
fmt.Printf("%s: %s: %v\n", version.PROGRAM_NAME, color.RedString("error"), err)
|
||||
return err
|
||||
// OK message (green)
|
||||
func OK(msg string, args ...any) {
|
||||
formatted := fmt.Sprintf(msg, args...)
|
||||
color.Green("%s * %s\n", formatTimestamp(), formatted)
|
||||
}
|
||||
|
||||
/* print fatality and panic */
|
||||
func Fatal(msg string, err error) error {
|
||||
fmt.Printf("%s: %s: %v\n", version.PROGRAM_NAME, color.RedString("fatal"), err)
|
||||
panic(err)
|
||||
// Warning message (magenta)
|
||||
func WARN(msg string, args ...any) {
|
||||
formatted := fmt.Sprintf(msg, args...)
|
||||
color.Magenta("%s # %s\n", formatTimestamp(), formatted)
|
||||
}
|
||||
|
||||
/* print a useful hint to the user */
|
||||
func Hint(format string, args ...any) {
|
||||
// Error message (red)
|
||||
func ERROR(msg string, args ...any) {
|
||||
formatted := fmt.Sprintf(msg, args...)
|
||||
color.Red("%s ! %s\n", formatTimestamp(), formatted)
|
||||
}
|
||||
|
||||
// Fatal message and panic
|
||||
func FATAL(msg string, args ...any) {
|
||||
formatted := fmt.Sprintf(msg, args...)
|
||||
color.Red("%s % %s\n", formatTimestamp(), formatted)
|
||||
panic(formatted)
|
||||
}
|
||||
|
||||
// Hint message (custom color)
|
||||
func Hint(msg string, args ...any) {
|
||||
formatted := fmt.Sprintf(msg, args...)
|
||||
color.RGB(func(hex int) (r, g, b int) {
|
||||
r = (hex >> 16) & 0xFF
|
||||
g = (hex >> 8) & 0xFF
|
||||
b = hex & 0xFF
|
||||
return
|
||||
}(hint_color)).Printf("hint: %s\n", fmt.Sprintf(format, args...))
|
||||
}(hintColor)).Printf("%s * hint: %s\n", formatTimestamp(), formatted)
|
||||
}
|
||||
|
||||
/* print message using the ok status color */
|
||||
func OK(msg string) {
|
||||
color.Green(msg)
|
||||
// Path logging: prints route and status
|
||||
func Path(path string, err error) {
|
||||
path = strings.TrimPrefix(path, "/")
|
||||
if path == "" {
|
||||
path = "(null)"
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
OK("Route: %s - ok", path)
|
||||
} else if errors.Is(err, config.ErrRouteMiss) {
|
||||
WARN("Route: %s - %s", path, config.ErrRouteMiss.Error())
|
||||
} else {
|
||||
ERROR("Route: %s - bad", path)
|
||||
}
|
||||
}
|
||||
|
||||
/* print message using the warning status color */
|
||||
func WARN(msg string) {
|
||||
color.Magenta(msg)
|
||||
// System warning with prefix
|
||||
func Warning(msg string, err error) error {
|
||||
WARN("%s: %v", msg, err)
|
||||
return err
|
||||
}
|
||||
|
||||
/* print message using the error status color */
|
||||
func ERROR(msg string) {
|
||||
color.Red(msg)
|
||||
// System error with prefix
|
||||
func Error(msg string, err error) error {
|
||||
ERROR("%s: %v", msg, err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Fatal system error
|
||||
func Fatal(msg string, err error) error {
|
||||
FATAL("%s: %v", msg, err)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user