aboutsummaryrefslogtreecommitdiff
path: root/log.go
blob: bf8d68a29b55400ece8178af916d9345697b0d19 (plain) (blame)
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
package proxy

import (
	"log"
	"os"
	"path/filepath"
)

type LogWriter struct {
	f *os.File
}

// Write writes the input data to os.Stderr and the log file.
// It returns the number of bytes written and an error.
func (lw *LogWriter) Write(p []byte) (n int, err error) {
	n, err = os.Stderr.Write(p)
	if err != nil {
		return
	}

	return lw.f.Write(p)
}

func init() {
	executable, err := os.Executable()
	if err != nil {
		log.Fatal("{←|⇶} ", err)
	}

	path := filepath.Dir(executable) + "/latest.log"
	f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
	if err != nil {
		log.Fatal("{←|⇶} ", err)
	}

	go func() {
		defer f.Close()
		select {}
	}()

	lw := &LogWriter{f}
	log.SetOutput(lw)
}