From ad8f7003098f5195d3305ca78d884ff2223c1150 Mon Sep 17 00:00:00 2001 From: Kacper Malachowski Date: Thu, 28 Mar 2024 10:34:08 +0100 Subject: [PATCH 1/2] Add logging solution --- app.go | 2 + go.mod | 3 +- go.sum | 3 ++ internal/logger/logger.go | 84 +++++++++++++++++++++++++++++++++++++++ main.go | 51 ++++++++++++++---------- 5 files changed, 120 insertions(+), 23 deletions(-) create mode 100644 internal/logger/logger.go diff --git a/app.go b/app.go index 51bca01..ce4e246 100644 --- a/app.go +++ b/app.go @@ -25,6 +25,7 @@ func NewApp() *App { func (a *App) startup(ctx context.Context) { a.ctx = ctx + runtime.LogInfo(ctx, fmt.Sprintf("Version: %s", "test")) } func (a *App) domReady(ctx context.Context) { @@ -59,6 +60,7 @@ func (a *App) LoadStationFile() station.Definition { a.station = station a.stationHash = calculateSHA256(data) a.client = td2.New(a.ctx, fmt.Sprintf("%x", a.stationHash)) + runtime.LogInfof(a.ctx, "Loaded station file with hash: %s", a.stationHash) return station } diff --git a/go.mod b/go.mod index 871636d..7c2f9e3 100644 --- a/go.mod +++ b/go.mod @@ -24,9 +24,10 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect - github.com/pkg/errors v0.9.1 // indirect + github.com/pkg/errors v0.9.1 github.com/rivo/uniseg v0.4.4 // indirect github.com/samber/lo v1.38.1 // indirect + github.com/sirupsen/logrus v1.9.3 github.com/tkrajina/go-reflector v0.5.6 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect diff --git a/go.sum b/go.sum index 4570f9a..1760dfd 100644 --- a/go.sum +++ b/go.sum @@ -46,6 +46,8 @@ github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= @@ -78,6 +80,7 @@ golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= diff --git a/internal/logger/logger.go b/internal/logger/logger.go new file mode 100644 index 0000000..d3a5a67 --- /dev/null +++ b/internal/logger/logger.go @@ -0,0 +1,84 @@ +package logger + +import ( + "fmt" + "os" + "path/filepath" + "time" + + "github.com/pkg/errors" + "github.com/sirupsen/logrus" +) + +type Logger struct { + LogDir string + file *os.File +} + +type Options = func(*Logger) + +func New(options ...Options) (*Logger, error) { + + logger := &Logger{ + LogDir: "logs", + } + for _, option := range options { + option(logger) + } + + var path string + if filepath.IsAbs(logger.LogDir) { + path = filepath.Join(logger.LogDir, fmt.Sprintf("log-%s", time.Now().Format("01-02-2016_HHmmss"))) + } else { + ex, err := os.Executable() + if err != nil { + return nil, errors.WithMessage(err, "cannot find executable path") + } + path = filepath.Join(filepath.Dir(ex), logger.LogDir, fmt.Sprintf("log-%s.log", time.Now().Format("01-02-2016_HHmmss"))) + } + + err := os.MkdirAll(filepath.Dir(path), os.ModePerm) + if err != nil { + return nil, errors.WithMessage(err, "cannot make directory") + } + + file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return nil, errors.WithMessage(err, "cannot open log file") + } + + logger.file = file + logrus.SetOutput(file) + logrus.SetFormatter(&logrus.JSONFormatter{}) + logrus.SetLevel(logrus.TraceLevel) + + return logger, nil +} + +func (l *Logger) Print(message string) { + logrus.Print(message) +} + +func (l *Logger) Trace(message string) { + logrus.Trace(message) +} + +func (l *Logger) Debug(message string) { + logrus.Debug(message) +} + +func (l *Logger) Info(message string) { + logrus.Info(message) +} + +func (l *Logger) Warning(message string) { + logrus.Warning(message) +} + +func (l *Logger) Error(message string) { + logrus.Error(message) +} + +func (l *Logger) Fatal(message string) { + logrus.Fatal(message) +} diff --git a/main.go b/main.go index 2eefb43..8f4eb74 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "embed" "log" + customlogger "github.com/kacpermalachowski/marshal-controller/internal/logger" "github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2/pkg/logger" "github.com/wailsapp/wails/v2/pkg/options" @@ -22,28 +23,34 @@ var icon []byte func main() { app := NewApp() - err := wails.Run(&options.App{ - Title: "marshal-controller", - Width: 900, - Height: 600, - MinWidth: 900, - MinHeight: 600, - MaxWidth: 1200, - MaxHeight: 800, - DisableResize: false, - Fullscreen: false, - Frameless: false, - StartHidden: false, - HideWindowOnClose: false, - BackgroundColour: &options.RGBA{R: 255, G: 255, B: 255, A: 0}, - Menu: nil, - Logger: nil, - LogLevel: logger.DEBUG, - OnStartup: app.startup, - OnDomReady: app.domReady, - OnBeforeClose: app.beforeClose, - OnShutdown: app.shutdown, - WindowStartState: options.Normal, + appLogger, err := customlogger.New() + if err != nil { + log.Fatal(err) + } + + err = wails.Run(&options.App{ + Title: "marshal-controller", + Width: 900, + Height: 600, + MinWidth: 900, + MinHeight: 600, + MaxWidth: 1200, + MaxHeight: 800, + DisableResize: false, + Fullscreen: false, + Frameless: false, + StartHidden: false, + HideWindowOnClose: false, + BackgroundColour: &options.RGBA{R: 255, G: 255, B: 255, A: 0}, + Menu: nil, + Logger: appLogger, + LogLevel: logger.DEBUG, + LogLevelProduction: logger.INFO, + OnStartup: app.startup, + OnDomReady: app.domReady, + OnBeforeClose: app.beforeClose, + OnShutdown: app.shutdown, + WindowStartState: options.Normal, AssetServer: &assetserver.Options{ Assets: assets, Handler: nil, From 6a90d81c540f1931e21abaab04200ef52e6956c1 Mon Sep 17 00:00:00 2001 From: Kacper Malachowski Date: Thu, 28 Mar 2024 19:34:48 +0100 Subject: [PATCH 2/2] Add logger --- app.go | 6 ++--- frontend/src/App.vue | 45 +++++++++++++++++-------------- frontend/wailsjs/go/main/App.d.ts | 2 +- internal/logger/logger.go | 8 ++++-- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/app.go b/app.go index ce4e246..c4c0596 100644 --- a/app.go +++ b/app.go @@ -65,11 +65,11 @@ func (a *App) LoadStationFile() station.Definition { return station } -func (a *App) Connect(address string) string { +func (a *App) Connect(address string) error { err := a.client.Connect(address) if err != nil { runtime.LogError(a.ctx, fmt.Sprint(err, address)) - return fmt.Sprintf("%s", err) + return err } go func() { @@ -85,7 +85,7 @@ func (a *App) Connect(address string) string { } }() - return "" + return nil } func (a *App) Disconnect() { diff --git a/frontend/src/App.vue b/frontend/src/App.vue index ffd9004..ab420e3 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -2,17 +2,22 @@ import { ref } from "vue"; import { Connect, Disconnect } from "../wailsjs/go/main/App"; import { useStationStore } from "./stores/station"; +import { LogError, LogInfo } from "../wailsjs/runtime/runtime"; const connected = ref(false); -const stationStore = useStationStore() +const stationStore = useStationStore(); function handleConnect() { if (connected.value) { - connected.value = false - Disconnect() + connected.value = false; + Disconnect(); } else { Connect("127.0.0.1:7424") - connected.value = true + .then(() => { + LogInfo("Connected!"); + connected.value = true; + }) + .catch(LogError); } } @@ -32,7 +37,7 @@ function handleConnect() { :disabled="stationStore.hills.length === 0" class="btn btn-main" > - {{ connected ? 'Disconnect' : 'Connect' }} + {{ connected ? "Disconnect" : "Connect" }}