Files
VRCWT-OSC/internal/app/runtime.go
messypy dd165d9301
Some checks failed
build-windows-exe / build (push) Failing after 1m13s
Clean project layout and fix release workflow
2026-07-27 23:01:24 +09:00

154 lines
3.6 KiB
Go

package app
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"sync"
"time"
"vrc_osc_go/internal/common"
)
type runtimeLogWriter struct {
mu sync.Mutex
file *os.File
}
func (w *runtimeLogWriter) Write(p []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
if w.file == nil {
return len(p), nil
}
return w.file.Write(p)
}
func (w *runtimeLogWriter) ensureFile(path string) error {
w.mu.Lock()
defer w.mu.Unlock()
if w.file != nil {
return nil
}
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
return err
}
w.file = f
return nil
}
func (w *runtimeLogWriter) appendf(path, format string, args ...any) error {
w.mu.Lock()
defer w.mu.Unlock()
if w.file == nil {
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
return err
}
w.file = f
}
_, err := fmt.Fprintf(w.file, format, args...)
return err
}
var runtimeLogFileWriter runtimeLogWriter
var joinLeaveLogFileWriter runtimeLogWriter
var desktopJoinLogMu sync.Mutex
var desktopJoinLogLastPath string
var desktopJoinLogLastBody string
func setupRuntimeLogger() error {
dir := filepath.Join(common.RootDir(), "runtime")
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
path := filepath.Join(dir, "runtime.log")
if err := runtimeLogFileWriter.ensureFile(path); err != nil {
return err
}
log.SetOutput(io.MultiWriter(os.Stderr, &runtimeLogFileWriter))
return nil
}
func appendRuntimeLog(title, text string) error {
dir := filepath.Join(common.RootDir(), "runtime")
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
path := filepath.Join(dir, "runtime.log")
body := text
if body == "" {
body = "(empty)"
}
return runtimeLogFileWriter.appendf(path, "\n[%s] %s\n%s\n", time.Now().Format("2006-01-02 15:04:05"), title, body)
}
func AppendRuntimeLog(title, text string) error {
return appendRuntimeLog(title, text)
}
func appendJoinLeaveLog(text string) error {
dir := filepath.Join(common.RootDir(), "runtime")
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
path := filepath.Join(dir, "join_leave.log")
body := text
if body == "" {
body = "(empty)"
}
return joinLeaveLogFileWriter.appendf(path, "\n[%s] VRC JOIN/LEAVE\n%s\n", time.Now().Format("2006-01-02 15:04:05"), body)
}
func appendDesktopJoinLog(title, worldLabel, text string) error {
dir := filepath.Join(os.Getenv("USERPROFILE"), "Desktop")
if _, err := os.Stat(dir); err != nil {
if home, herr := os.UserHomeDir(); herr == nil {
dir = filepath.Join(home, "Desktop")
}
}
if err := os.MkdirAll(dir, 0o755); err != nil {
dir = filepath.Join(common.RootDir(), "runtime")
if mkErr := os.MkdirAll(dir, 0o755); mkErr != nil {
return err
}
}
path := filepath.Join(dir, "join.txt")
body := text
if body == "" {
body = "(empty)"
}
desktopJoinLogMu.Lock()
defer desktopJoinLogMu.Unlock()
if desktopJoinLogLastPath == path && desktopJoinLogLastBody == body {
return nil
}
f, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
if err != nil {
fallbackDir := filepath.Join(common.RootDir(), "runtime")
if mkErr := os.MkdirAll(fallbackDir, 0o755); mkErr != nil {
return err
}
fallbackPath := filepath.Join(fallbackDir, "join.txt")
f, err = os.OpenFile(fallbackPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
if err != nil {
return err
}
path = fallbackPath
}
defer f.Close()
if _, err = fmt.Fprintf(f, "%s\n", body); err != nil {
return err
}
desktopJoinLogLastPath = path
desktopJoinLogLastBody = body
return nil
}
func AppendDesktopJoinLog(title, worldLabel, text string) error {
return appendDesktopJoinLog(title, worldLabel, text)
}