2024-07-03 15:11:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-07-10 13:39:05 +00:00
|
|
|
"errors"
|
2024-07-03 15:11:29 +00:00
|
|
|
"os"
|
2024-07-09 01:33:49 +00:00
|
|
|
"path/filepath"
|
2024-07-03 15:11:29 +00:00
|
|
|
|
|
|
|
"github.com/charmbracelet/log"
|
2024-07-09 01:33:49 +00:00
|
|
|
gap "github.com/muesli/go-app-paths"
|
2024-07-03 15:11:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getLogFilePath() (string, error) {
|
2024-07-09 01:33:49 +00:00
|
|
|
dir, err := gap.NewScope(gap.User, "glow").CacheDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(dir, "glow.log"), nil
|
2024-07-03 15:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupLog() (func() error, error) {
|
|
|
|
// Log to file, if set
|
|
|
|
logFile, err := getLogFilePath()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-10 13:35:52 +00:00
|
|
|
if err := os.MkdirAll(filepath.Dir(logFile), 0o644); err != nil {
|
2024-07-10 13:39:05 +00:00
|
|
|
if errors.Is(err, os.ErrPermission) {
|
|
|
|
// log disabled
|
|
|
|
return nil, nil
|
|
|
|
}
|
2024-07-10 13:35:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-03 15:11:29 +00:00
|
|
|
f, err := os.OpenFile(logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.SetOutput(f)
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
return f.Close, nil
|
|
|
|
}
|