Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logger #13

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -59,15 +60,16 @@ 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
}

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() {
Expand All @@ -83,7 +85,7 @@ func (a *App) Connect(address string) string {
}
}()

return ""
return nil
}

func (a *App) Disconnect() {
Expand Down
45 changes: 25 additions & 20 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
</script>
Expand All @@ -32,7 +37,7 @@ function handleConnect() {
:disabled="stationStore.hills.length === 0"
class="btn btn-main"
>
{{ connected ? 'Disconnect' : 'Connect' }}
{{ connected ? "Disconnect" : "Connect" }}
</button>
<button
@click="() => stationStore.loadStation()"
Expand All @@ -47,19 +52,19 @@ function handleConnect() {
</template>

<style lang="scss">
.btn {
@apply font-bold py-2 px-4 rounded mr-2 ml-2;
}
.btn-main {
@apply bg-gray-300;
}
.btn-main:hover {
@apply bg-gray-700 text-white;
}
.btn-secondary {
@apply bg-transparent border border-gray-700 hover:border-transparent;
}
.btn-secondary:hover {
@apply bg-gray-300;
}
.btn {
@apply font-bold py-2 px-4 rounded mr-2 ml-2;
}
.btn-main {
@apply bg-gray-300;
}
.btn-main:hover {
@apply bg-gray-700 text-white;
}
.btn-secondary {
@apply bg-transparent border border-gray-700 hover:border-transparent;
}
.btn-secondary:hover {
@apply bg-gray-300;
}
</style>
2 changes: 1 addition & 1 deletion frontend/wailsjs/go/main/App.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This file is automatically generated. DO NOT EDIT
import {station} from '../models';

export function Connect(arg1:string):Promise<string>;
export function Connect(arg1:string):Promise<void>;

export function Disconnect():Promise<void>;

Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
88 changes: 88 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package logger

import (
"fmt"
"os"
"path/filepath"
"strings"
"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)
}

ts := time.Now().Format(time.RFC3339)
filename := fmt.Sprintf("log-%s.log", strings.Replace(strings.Replace(ts, ":", "", -1), "-", "", -1))

var path string
if filepath.IsAbs(logger.LogDir) {
path = filepath.Join(logger.LogDir, filename)
} 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, filename)
}

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)
}
51 changes: 29 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down