-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepedencies.go
64 lines (52 loc) · 1.27 KB
/
depedencies.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
59
60
61
62
63
64
package cbtransaction
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"runtime"
"strings"
)
type ReadWriteSeekCloser interface {
io.ReadWriteSeeker
io.Closer
}
type ErrorHandler interface {
Error(e error)
Recover()
}
type DefaultErrorHandler struct{}
func (d DefaultErrorHandler) Error(e error) {
buf := make([]byte, 1000000)
runtime.Stack(buf, false)
buf = bytes.Trim(buf, "\x00")
stack := string(buf)
stackParts := strings.Split(stack, "\n")
newStackParts := []string{stackParts[0]}
newStackParts = append(newStackParts, stackParts[3:]...)
stack = strings.Join(newStackParts, "\n")
log.Println("ERROR", e.Error()+"\n"+stack)
}
func (d DefaultErrorHandler) Recover() {
e := recover()
if e != nil {
err, ok := e.(error)
if ok {
d.Error(err)
} else {
d.Error(errors.New(fmt.Sprint(e)))
}
}
}
type Logger interface {
InfoF(category string, message string, args ...interface{})
DebugF(category string, message string, args ...interface{})
}
type defaultLogger struct{}
func (d defaultLogger) InfoF(category string, message string, args ...interface{}) {
log.Println(category+":", fmt.Sprintf(message, args...))
}
func (d defaultLogger) DebugF(category string, message string, args ...interface{}) {
log.Println(category+":", fmt.Sprintf(message, args...))
}