Repository navigation
Expand file tree
/
Copy pathlogging.go
More file actions
218 lines (194 loc) · 5.24 KB
/
Copy pathlogging.go
File metadata and controls
218 lines (194 loc) · 5.24 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//go:build windows
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
// errLog receives errors: they go to the log file even without -log.
// Logging is process-wide by nature, like the standard logger.
var errLog = log.New(os.Stderr, "ERROR ", log.Ltime)
func errorf(format string, args ...any) { errLog.Printf(format, args...) }
// setupLogging sends informational messages to the console and, if full,
// to the file; errors always go to both (file == "" means no file). The
// returned func flushes and closes everything.
func setupLogging(file string, full bool) func() {
log.SetFlags(log.Ltime)
infoOut, errOut := teeWriter{os.Stderr}, teeWriter{os.Stderr}
var f *rotatingFile
if file != "" {
var err error
if f, err = openRotating(file, maxLogSize); err != nil {
fatalf("cannot open log: %v", err)
}
errOut = append(errOut, f)
if full {
infoOut = append(infoOut, f)
}
}
infoW, errW := newAsyncWriter(infoOut), newAsyncWriter(errOut)
log.SetOutput(infoW)
errLog.SetOutput(errW)
return func() {
infoW.Close()
errW.Close()
if f != nil {
f.Close()
}
}
}
// asyncWriter writes the log in the background. Console writes block while
// text is selected in the console (QuickEdit) — that must not stall the app.
type asyncWriter struct {
ch chan []byte
done chan struct{}
}
func newAsyncWriter(w io.Writer) *asyncWriter {
a := &asyncWriter{ch: make(chan []byte, 256), done: make(chan struct{})}
go func() {
for b := range a.ch {
w.Write(b)
}
close(a.done)
}()
return a
}
func (a *asyncWriter) Write(p []byte) (int, error) {
select {
case a.ch <- append([]byte(nil), p...):
default: // queue is full — drop the log line rather than block
}
return len(p), nil
}
// Close flushes the queued lines; the writer must not be used afterwards.
func (a *asyncWriter) Close() {
close(a.ch)
select {
case <-a.done:
case <-time.After(time.Second):
}
}
// teeWriter writes to every destination regardless of errors. Unlike
// io.MultiWriter it doesn't stop at the first failure: a GUI build has no
// valid stderr, and that must not prevent writing the log file.
type teeWriter []io.Writer
func (t teeWriter) Write(p []byte) (int, error) {
for _, w := range t {
w.Write(p)
}
return len(p), nil
}
// guiBuild reports whether this exe is the GUI build. Only that one shows
// message boxes: it has no stderr anyone could read. The console build
// always writes to stderr — it may run without a console window (scripts,
// redirected output), where a modal box would just hang it.
var guiBuild = sync.OnceValue(func() bool {
exe, err := os.Executable()
if err != nil {
return false
}
s, _ := peSubsystem(exe)
return s == subsystemGUI
})
// notify tells the user something important: a message box in the GUI
// build, stderr in the console build.
func notify(msg string) {
if guiBuild() {
messageBox(msg)
return
}
fmt.Fprintln(os.Stderr, msg)
}
// exitHooks run on fatalf, which exits without running deferred calls.
var exitHooks []func()
func atExit(f func()) { exitHooks = append(exitHooks, f) }
// fatalf reports a startup error and exits: to stderr and the log file
// (errorf), and in the GUI build also in a message box.
func fatalf(format string, args ...any) {
msg := fmt.Sprintf(format, args...)
errorf("%s", msg)
for i := len(exitHooks) - 1; i >= 0; i-- {
exitHooks[i]()
}
time.Sleep(100 * time.Millisecond) // let the async log writers flush
if guiBuild() {
messageBox("KeyLayoutPin cannot start:\n\n" + msg)
}
os.Exit(1)
}
// maxLogSize keeps the log from growing forever and piling up history.
const maxLogSize = 1 << 20
// rotatingFile is a log file capped at max bytes: when a write would exceed
// it, the file is renamed to .old (replacing the previous .old) and a new one
// is started. So at most two files' worth of history stay on disk, however
// long the app runs. Safe for concurrent use: the info and error writers
// share one file.
type rotatingFile struct {
mu sync.Mutex
path string
max int64
f *os.File
size int64
}
func openRotating(path string, max int64) (*rotatingFile, error) {
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return nil, err
}
r := &rotatingFile{path: path, max: max}
if st, err := os.Stat(path); err == nil && st.Size() >= max {
r.rotate()
}
return r, r.open()
}
func (r *rotatingFile) open() error {
f, err := os.OpenFile(r.path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
if err != nil {
return err
}
r.f, r.size = f, 0
if st, err := f.Stat(); err == nil {
r.size = st.Size()
}
return nil
}
func (r *rotatingFile) oldPath() string {
ext := filepath.Ext(r.path)
return strings.TrimSuffix(r.path, ext) + ".old" + ext
}
func (r *rotatingFile) rotate() {
os.Remove(r.oldPath())
os.Rename(r.path, r.oldPath())
}
func (r *rotatingFile) Write(p []byte) (int, error) {
r.mu.Lock()
defer r.mu.Unlock()
if r.f == nil {
return 0, os.ErrClosed
}
if r.size > 0 && r.size+int64(len(p)) > r.max {
r.f.Close()
r.rotate()
if err := r.open(); err != nil {
r.f = nil
return 0, err
}
}
n, err := r.f.Write(p)
r.size += int64(n)
return n, err
}
func (r *rotatingFile) Close() error {
r.mu.Lock()
defer r.mu.Unlock()
if r.f == nil {
return nil
}
err := r.f.Close()
r.f = nil
return err
}