forked from malashin/hoi4treesnap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
113 lines (96 loc) · 2.84 KB
/
logger.go
File metadata and controls
113 lines (96 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"time"
)
const (
diagnosticDateLayout = "02-01-2006"
diagnosticTimeLayout = "03-04-05"
)
// initLogger opens (or creates) the application log file under
// <output base>/logs/hoi4treesnap.log and installs it as the default slog
// handler. The returned close function must be called on exit.
// On failure the default slog handler (stderr text) is kept and a warning is
// printed, so the application always continues to run.
func initLogger() func() {
logDir, err := getLogsOutputDir()
if err != nil {
fmt.Fprintf(os.Stderr, "log: could not determine logs dir: %v\n", err)
return func() {}
}
logsPath = logDir
logPath := filepath.Join(logDir, "hoi4treesnap.log")
appLogPath = logPath
f, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
if err != nil {
fmt.Fprintf(os.Stderr, "log: could not open log file: %v\n", err)
return func() {}
}
h := slog.NewTextHandler(f, &slog.HandlerOptions{Level: slog.LevelDebug})
slog.SetDefault(slog.New(h))
fmt.Fprintf(os.Stderr, "log: %s\n", logPath)
return func() { _ = f.Close() }
}
func writeDiagnosticLog(fileName, content string) (string, error) {
return writeDiagnosticLogAt(time.Now(), fileName, content)
}
func writeDiagnosticLogAt(now time.Time, fileName, content string) (string, error) {
logDir, err := getLogsOutputDir()
if err != nil {
return "", err
}
logsPath = logDir
dayDir := filepath.Join(logDir, now.Format(diagnosticDateLayout))
if err := os.MkdirAll(dayDir, 0o755); err != nil {
return "", err
}
path, err := uniqueDiagnosticLogPath(dayDir, now, fileName)
if err != nil {
return "", err
}
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
if err != nil {
return "", err
}
defer f.Close()
_, err = fmt.Fprintf(f, "[%s]\n%s\n\n", now.Format(time.RFC3339), content)
if err != nil {
return "", err
}
return path, nil
}
func uniqueDiagnosticLogPath(dayDir string, now time.Time, fileName string) (string, error) {
base := filepath.Base(fileName)
ext := filepath.Ext(base)
name := strings.TrimSuffix(base, ext)
prefix := fmt.Sprintf("%s-%s-%s", now.Format(diagnosticTimeLayout), now.Format(diagnosticDateLayout), name)
path := filepath.Join(dayDir, prefix+ext)
if _, err := os.Stat(path); os.IsNotExist(err) {
return path, nil
} else if err != nil {
return "", err
}
for i := 1; ; i++ {
candidate := filepath.Join(dayDir, fmt.Sprintf("%s-%02d%s", prefix, i, ext))
if _, err := os.Stat(candidate); os.IsNotExist(err) {
return candidate, nil
} else if err != nil {
return "", err
}
}
}
func removeDiagnosticLog(fileName string) error {
logDir, err := getLogsOutputDir()
if err != nil {
return err
}
path := filepath.Join(logDir, fileName)
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
return err
}
return nil
}