progress on go rewrite: logger and config work now
This commit is contained in:
120
utils/logger.go
Normal file
120
utils/logger.go
Normal file
@ -0,0 +1,120 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/wailsapp/wails/v2/pkg/logger"
|
||||
)
|
||||
|
||||
// multiLogger fans every log call out to multiple logger.Logger targets with timestamps.
|
||||
type multiLogger struct {
|
||||
targets []logger.Logger
|
||||
}
|
||||
|
||||
// timestamped prefixes each message with a timestamp.
|
||||
func timestamped(message string) string {
|
||||
return time.Now().Format("2006-01-02 15:04:05") + " " + message
|
||||
}
|
||||
|
||||
func (m *multiLogger) Print(message string) {
|
||||
msg := timestamped(message)
|
||||
for _, l := range m.targets {
|
||||
l.Print(msg)
|
||||
}
|
||||
}
|
||||
func (m *multiLogger) Trace(message string) {
|
||||
msg := timestamped(message)
|
||||
for _, l := range m.targets {
|
||||
l.Trace(msg)
|
||||
}
|
||||
}
|
||||
func (m *multiLogger) Debug(message string) {
|
||||
msg := timestamped(message)
|
||||
for _, l := range m.targets {
|
||||
l.Debug(msg)
|
||||
}
|
||||
}
|
||||
func (m *multiLogger) Info(message string) {
|
||||
msg := timestamped(message)
|
||||
for _, l := range m.targets {
|
||||
l.Info(msg)
|
||||
}
|
||||
}
|
||||
func (m *multiLogger) Warning(message string) {
|
||||
msg := timestamped(message)
|
||||
for _, l := range m.targets {
|
||||
l.Warning(msg)
|
||||
}
|
||||
}
|
||||
func (m *multiLogger) Error(message string) {
|
||||
msg := timestamped(message)
|
||||
for _, l := range m.targets {
|
||||
l.Error(msg)
|
||||
}
|
||||
}
|
||||
func (m *multiLogger) Fatal(message string) {
|
||||
msg := timestamped(message)
|
||||
for _, l := range m.targets {
|
||||
l.Fatal(msg)
|
||||
}
|
||||
}
|
||||
|
||||
// NewRuntimeFileLogger returns a logger that writes all output both to
|
||||
//
|
||||
// $XDG_CONFIG_HOME/FanslySync/logs/runtime_latest.log
|
||||
//
|
||||
// and to a timestamped file
|
||||
//
|
||||
// $XDG_CONFIG_HOME/FanslySync/logs/runtime_YYYY-MM-DD_HH-MM-SS.log
|
||||
//
|
||||
// It also deletes any timestamped logs older than 14 days.
|
||||
//
|
||||
// The returned logger implements github.com/wailsapp/wails/v2/pkg/logger.Logger
|
||||
// and will be used by Wails for all Go-side logging.
|
||||
func NewRuntimeFileLogger() (logger.Logger, error) {
|
||||
// 1) Ensure log directory exists
|
||||
cfgDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot determine user config dir: %w", err)
|
||||
}
|
||||
logDir := filepath.Join(cfgDir, "FanslySync", "logs")
|
||||
if err := os.MkdirAll(logDir, 0o755); err != nil {
|
||||
return nil, fmt.Errorf("cannot create log directory: %w", err)
|
||||
}
|
||||
|
||||
// 2) Prune old timestamped logs (>14 days)
|
||||
cutoff := time.Now().Add(-14 * 24 * time.Hour)
|
||||
entries, _ := os.ReadDir(logDir)
|
||||
for _, e := range entries {
|
||||
if e.IsDir() || e.Name() == "runtime_latest.log" {
|
||||
continue
|
||||
}
|
||||
info, err := e.Info()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if info.ModTime().Before(cutoff) {
|
||||
_ = os.Remove(filepath.Join(logDir, e.Name()))
|
||||
}
|
||||
}
|
||||
|
||||
// 3) Build paths for timestamped + latest
|
||||
ts := time.Now().Format("2006-01-02_15-04-05")
|
||||
tsPath := filepath.Join(logDir, fmt.Sprintf("runtime_%s.log", ts))
|
||||
latestPath := filepath.Join(logDir, "runtime_latest.log")
|
||||
|
||||
// 4) Create both loggers
|
||||
tsLogger := logger.NewFileLogger(tsPath)
|
||||
latestLogger := logger.NewFileLogger(latestPath)
|
||||
termLogger := logger.NewDefaultLogger()
|
||||
|
||||
// 5) Fan-out into a multiLogger
|
||||
multi := &multiLogger{
|
||||
targets: []logger.Logger{tsLogger, latestLogger, termLogger},
|
||||
}
|
||||
|
||||
return multi, nil
|
||||
}
|
57
utils/utils.go
Normal file
57
utils/utils.go
Normal file
@ -0,0 +1,57 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
|
||||
// A base type for all message box options.
|
||||
// This is a functional option pattern, so you can pass in any number of
|
||||
// options to customize the message box.
|
||||
type MessageBoxOption func(*runtime.MessageDialogOptions)
|
||||
|
||||
// WithDialogType allows you to set the dialog type for the message box.
|
||||
// The default is InfoDialog.
|
||||
func WithDialogType(dt runtime.DialogType) MessageBoxOption {
|
||||
return func(o *runtime.MessageDialogOptions) {
|
||||
o.Type = dt
|
||||
}
|
||||
}
|
||||
|
||||
// Allows you to build a custom set of buttons for the dialog.
|
||||
// The default is a single “OK” button.
|
||||
func WithButtons(btns []string) MessageBoxOption {
|
||||
return func(o *runtime.MessageDialogOptions) {
|
||||
o.Buttons = btns
|
||||
}
|
||||
}
|
||||
|
||||
// BuildMessageBoxOpts builds the options struct, defaulting to
|
||||
// an Info dialog and a single “OK” button. Pass WithDialogType()
|
||||
// and/or WithButtons() to customize.
|
||||
func BuildMessageBoxOpts(title, message string, opts ...MessageBoxOption) runtime.MessageDialogOptions {
|
||||
// defaults:
|
||||
o := runtime.MessageDialogOptions{
|
||||
Title: title,
|
||||
Message: message,
|
||||
Type: runtime.InfoDialog,
|
||||
Buttons: []string{"OK"},
|
||||
}
|
||||
|
||||
// apply any overrides:
|
||||
for _, opt := range opts {
|
||||
opt(&o)
|
||||
}
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
// ShowMessageBox shows a message box with the given options.
|
||||
func ShowMessageBox(ctx context.Context, title, message string, opts ...MessageBoxOption) {
|
||||
// Build the options
|
||||
options := BuildMessageBoxOpts(title, message, opts...)
|
||||
|
||||
// Show the message box
|
||||
runtime.MessageDialog(ctx, options)
|
||||
}
|
Reference in New Issue
Block a user