From 914781cd004eac4b774d27acc5631bb3fa9752fa Mon Sep 17 00:00:00 2001 From: Melora Hugues Date: Sun, 13 Aug 2023 16:20:55 +0200 Subject: [PATCH] Add base structure and logger --- config/.gitignore | 1 + config/Makefile | 5 +++ config/go.mod | 3 ++ config/logger/logger.go | 86 +++++++++++++++++++++++++++++++++++++++++ config/main.go | 14 +++++++ 5 files changed, 109 insertions(+) create mode 100644 config/.gitignore create mode 100644 config/Makefile create mode 100644 config/go.mod create mode 100644 config/logger/logger.go create mode 100644 config/main.go diff --git a/config/.gitignore b/config/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/config/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/config/Makefile b/config/Makefile new file mode 100644 index 0000000..58373ce --- /dev/null +++ b/config/Makefile @@ -0,0 +1,5 @@ +.PHONY: build + +build: + mkdir -p build/ + go build -o build/ diff --git a/config/go.mod b/config/go.mod new file mode 100644 index 0000000..598eec5 --- /dev/null +++ b/config/go.mod @@ -0,0 +1,3 @@ +module git.faercol.me/faercol/http-boot-config/config + +go 1.20 diff --git a/config/logger/logger.go b/config/logger/logger.go new file mode 100644 index 0000000..8b0c335 --- /dev/null +++ b/config/logger/logger.go @@ -0,0 +1,86 @@ +package logger + +import ( + "fmt" + "io" + "os" +) + +const ( + NC = "\033[0m" + red = "\033[0;31m" + yellow = "\033[0;33m" +) + +const ( + errorPrefix = "Error: " + fatalPrefix = "Fatal: " + warningprefix = "Warning: " +) + +type SimpleLogger struct { + enableColour bool +} + +func (sl *SimpleLogger) printMsg(msg string, dest io.Writer) { + if msg[len(msg)-1:] != "\n" { + msg = msg + "\n" + } + + n, err := fmt.Fprint(dest, msg) + if err != nil { + panic(fmt.Errorf("failed to write message to %v: %w", dest, err)) + } + if n != len(msg) { + panic(fmt.Errorf("failed to write the entire message (%d/%d)", n, len(msg))) + } +} + +func (sl *SimpleLogger) colourText(msg string, colour string) string { + if !sl.enableColour { + return msg + } + return colour + msg + NC +} + +func (sl *SimpleLogger) Info(msg string) { + sl.printMsg(msg, os.Stdout) +} + +func (sl *SimpleLogger) Infof(format string, a ...any) { + sl.printMsg(fmt.Sprintf(format, a...), os.Stdout) +} + +func (sl *SimpleLogger) Error(msg string) { + sl.printMsg(sl.colourText(errorPrefix+msg, red), os.Stderr) +} + +func (sl *SimpleLogger) Errorf(format string, a ...any) { + sl.printMsg(sl.colourText(errorPrefix+fmt.Sprintf(format, a...), red), os.Stderr) +} + +func (sl *SimpleLogger) Warning(msg string) { + sl.printMsg(sl.colourText(warningprefix+msg, yellow), os.Stdout) +} + +func (sl *SimpleLogger) Warningf(format string, a ...any) { + sl.printMsg(sl.colourText(warningprefix+fmt.Sprintf(format, a...), yellow), os.Stdout) +} + +func (sl *SimpleLogger) Fatal(msg string) { + sl.printMsg(sl.colourText(fatalPrefix+msg, red), os.Stderr) + os.Exit(1) +} + +func (sl *SimpleLogger) Fatalf(format string, a ...any) { + sl.printMsg(sl.colourText(fatalPrefix+fmt.Sprintf(format, a...), red), os.Stderr) + os.Exit(1) +} + +func WithColour() *SimpleLogger { + return &SimpleLogger{enableColour: true} +} + +func NoColour() *SimpleLogger { + return &SimpleLogger{enableColour: false} +} diff --git a/config/main.go b/config/main.go new file mode 100644 index 0000000..8ab1cf9 --- /dev/null +++ b/config/main.go @@ -0,0 +1,14 @@ +package main + +import "git.faercol.me/faercol/http-boot-config/config/logger" + +func main() { + l := logger.NoColour() + + l.Info("test message") + l.Warning("this is a warning") + l.Error("this is an error") + + l.Fatal("this is fatal") + l.Info("This should not be displayed") +}