Skip to content

Commit

Permalink
better logging on map fetch error
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorgimenez committed May 16, 2024
1 parent 388470a commit e371613
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions cmd/koolo/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func NewLogger(debug bool, logDir string) (*slog.Logger, error) {

func FlushLog() error {
if logFileHandler != nil {
logFileHandler.Sync()
return logFileHandler.Close()
}

Expand Down
15 changes: 13 additions & 2 deletions cmd/koolo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"context"
"fmt"
"log"
"log/slog"
"os"

_ "net/http/pprof"
"os"
"runtime/debug"

sloggger "github.com/hectorgimenez/koolo/cmd/koolo/log"
koolo "github.com/hectorgimenez/koolo/internal"
Expand All @@ -26,6 +27,7 @@ func main() {
if err != nil {
helper.ShowDialog("Error loading configuration", err.Error())
log.Fatalf("Error loading configuration: %s", err.Error())
return
}

logger, err := sloggger.NewLogger(config.Koolo.Debug.Log, config.Koolo.LogSaveDirectory)
Expand All @@ -34,6 +36,15 @@ func main() {
}
defer sloggger.FlushLog()

defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("fatal error detected, Koolo will close with the following error: %v\n Stacktrace: %s", r, debug.Stack())
logger.Error(err.Error())
sloggger.FlushLog()
helper.ShowDialog("Koolo error :(", fmt.Sprintf("Koolo will close due to an expected error, please check the latest log file for more info!\n %s", err.Error()))
}
}()

ctx, cancel := context.WithCancel(context.Background())
g, ctx := errgroup.WithContext(ctx)

Expand Down
9 changes: 3 additions & 6 deletions internal/game/map_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ import (
"github.com/hectorgimenez/koolo/internal/config"
)

func GetMapData(seed string, difficulty difficulty.Difficulty) MapData {
func GetMapData(seed string, difficulty difficulty.Difficulty) (MapData, error) {
cmd := exec.Command("./tools/koolo-map.exe", config.Koolo.D2LoDPath, "-s", seed, "-d", getDifficultyAsNum(difficulty))
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
stdout, err := cmd.Output()
if err != nil {
panic(fmt.Sprintf(
"Error detected fetching Map Data from Diablo II: LoD 1.13c game, please make sure you have the classic expansion game installed AND config.yaml D2LoDPath is pointing to the correct game path. Error: %s",
stdout,
))
return nil, fmt.Errorf("error fetching Map Data from Diablo II: LoD 1.13c game: %w", err)
}

stdoutLines := strings.Split(string(stdout), "\r\n")
Expand All @@ -38,7 +35,7 @@ func GetMapData(seed string, difficulty difficulty.Difficulty) MapData {
}
}

return lvls
return lvls, nil
}

func getDifficultyAsNum(df difficulty.Difficulty) string {
Expand Down
13 changes: 12 additions & 1 deletion internal/game/memory_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (

"github.com/hectorgimenez/d2go/pkg/memory"
"github.com/hectorgimenez/d2go/pkg/utils"
sloggger "github.com/hectorgimenez/koolo/cmd/koolo/log"
"github.com/hectorgimenez/koolo/internal/config"
"github.com/hectorgimenez/koolo/internal/game/map_client"
"github.com/hectorgimenez/koolo/internal/helper"
"github.com/lxn/win"
)

Expand Down Expand Up @@ -65,7 +67,16 @@ func (gd *MemoryReader) GetData(isNewGame bool) Data {
gd.CachedMapSeed, _ = gd.getMapSeed(d.PlayerUnit.Address)
t := time.Now()
gd.logger.Debug("Fetching map data...", slog.Uint64("seed", uint64(gd.CachedMapSeed)))
gd.CachedMapData = map_client.GetMapData(strconv.Itoa(int(gd.CachedMapSeed)), config.Characters[gd.supervisorName].Game.Difficulty)

mapData, err := map_client.GetMapData(strconv.Itoa(int(gd.CachedMapSeed)), config.Characters[gd.supervisorName].Game.Difficulty)
if err != nil {
// TODO: Refactor this crap with proper error handling
gd.logger.Error(fmt.Sprintf("Error fetching map data: %s", err.Error()))
sloggger.FlushLog()
helper.ShowDialog("Koolo error :(", fmt.Sprintf("Koolo will close due to an expected error, please check the latest log file for more info!\n %s", err.Error()))
panic(fmt.Sprintf("Error fetching map data: %s", err.Error()))
}
gd.CachedMapData = mapData
gd.logger.Debug("Fetch completed", slog.Int64("ms", time.Since(t).Milliseconds()))
}

Expand Down

0 comments on commit e371613

Please sign in to comment.