-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
58 lines (48 loc) · 1.2 KB
/
utils.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
56
57
58
package main
import (
"fmt"
"io"
"log"
"os"
"runtime/debug"
"go.bug.st/json"
)
// NewReadWriteCloser create an io.ReadWriteCloser from given io.ReadCloser and io.WriteCloser.
func NewReadWriteCloser(in io.ReadCloser, out io.WriteCloser) io.ReadWriteCloser {
return &combinedReadWriteCloser{in, out}
}
// OpenLogFileAs creates a log file in GlobalLogDirectory.
func openLogFileAs(filename string) *os.File {
path := "./logs/" + filename
res, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Fatalf("Error opening log file: %s", err)
}
res.WriteString("\n\n\nStarted logging.\n")
return res
}
func LogReadWriteCloserAs(upstream io.ReadWriteCloser, filename string) io.ReadWriteCloser {
return &dumper{
upstream: upstream,
logfile: openLogFileAs(filename),
}
}
func catchAndLogPanic(callback func()) {
if r := recover(); r != nil {
reason := fmt.Sprintf("%v", r)
LogError(fmt.Errorf("Panic: %s\n\n%s", reason, string(debug.Stack())))
go callback()
}
}
func readConfig() {
f, err := os.Open("config.json")
if err != nil {
panic(err)
}
defer f.Close()
decoder := json.NewDecoder(f)
err = decoder.Decode(&config)
if err != nil {
panic(err)
}
}