Skip to content

Commit

Permalink
redirect logging to a file on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jan 4, 2025
1 parent e61f91b commit 08fae68
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions backend/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package backend

import (
"context"
"debug/pe"
"errors"
"fmt"
"log"
"os"
"path"
"path/filepath"
"reflect"
"runtime"
"slices"
"time"

Expand Down Expand Up @@ -60,6 +62,8 @@ type App struct {
cancel context.CancelFunc

lastWrittenCfg Config

logFile *os.File
}

func (a *App) VersionTag() string {
Expand All @@ -81,7 +85,17 @@ func StartupApp(appName, displayAppName, appVersion, appVersionTag, latestReleas
configdir.MakePath(confDir)
configdir.MakePath(cacheDir)

var logFile *os.File
if isWindowsGUI() {
// Can't log to console in Windows GUI app so log to file instead
if f, err := os.Create(filepath.Join(confDir, "supersonic.log")); err == nil {
log.SetOutput(f)
logFile = f
}
}

a := &App{
logFile: logFile,
appName: appName,
appVersionTag: appVersionTag,
configDir: confDir,
Expand Down Expand Up @@ -336,6 +350,9 @@ func (a *App) DeleteServerCacheDir(serverID uuid.UUID) error {
}

func (a *App) Shutdown() {
if a.logFile != nil {
a.logFile.Close()
}
repeatMode := "None"
switch a.PlaybackManager.GetLoopMode() {
case LoopOne:
Expand Down Expand Up @@ -439,3 +456,30 @@ func clamp(i, min, max int) int {
}
return i
}

func isWindowsGUI() bool {
if runtime.GOOS != "windows" {
return false
}

// check executable for windows GUI flag
// https://stackoverflow.com/questions/58813512/is-it-possible-to-detect-if-go-binary-was-compiled-with-h-windowsgui-at-runtime
fileName, err := os.Executable()
if err != nil {
return false
}
fl, err := pe.Open(fileName)
if err != nil {
return false
}
defer fl.Close()

var subsystem uint16
if header, ok := fl.OptionalHeader.(*pe.OptionalHeader64); ok {
subsystem = header.Subsystem
} else if header, ok := fl.OptionalHeader.(*pe.OptionalHeader32); ok {
subsystem = header.Subsystem
}

return subsystem == 2 /*IMAGE_SUBSYSTEM_WINDOWS_GUI*/
}

0 comments on commit 08fae68

Please sign in to comment.