forked from fergusstrange/embedded-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
55 lines (44 loc) · 963 Bytes
/
logging.go
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
package embeddedpostgres
import (
"fmt"
"io"
"os"
)
type syncedLogger struct {
offset int64
logger io.Writer
file *os.File
}
func newSyncedLogger(dir string, logger io.Writer) (*syncedLogger, error) {
file, err := os.CreateTemp(dir, "embedded_postgres_log")
if err != nil {
return nil, err
}
s := syncedLogger{
logger: logger,
file: file,
}
return &s, nil
}
func (s *syncedLogger) flush() error {
if s.logger != nil {
file, err := os.Open(s.file.Name())
if err != nil {
return fmt.Errorf("unable to process postgres logs: %s", err)
}
defer func() {
if err := file.Close(); err != nil {
panic(err)
}
}()
if _, err = file.Seek(s.offset, io.SeekStart); err != nil {
return fmt.Errorf("unable to process postgres logs: %s", err)
}
readBytes, err := io.Copy(s.logger, file)
if err != nil {
return fmt.Errorf("unable to process postgres logs: %s", err)
}
s.offset += readBytes
}
return nil
}