-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
72 lines (62 loc) · 1.75 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package krater
import (
"fmt"
"io"
"io/ioutil"
"log"
"gopkg.in/Shopify/sarama.v1"
)
func sequentialIntGen() func() int {
i := 0
return func() int {
i++
return i
}
}
// LogOutput is the writer used by krater's loggers
var LogOutput io.Writer = ioutil.Discard
// newLogger creates a new log.Logger using either "out" or, if it's nil, LogOutput for writing.
func newLogger(prefix string, out io.Writer) StdLogger {
if out == nil {
out = LogOutput
}
return log.New(out, fmt.Sprintf("[%s] ", prefix), log.Ldate|log.Lmicroseconds)
}
// LogTo makes krater and sarama loggers output to the given writer by replacing sarama.Logger and LogOutput. It returns a function that
// sets LogOutput and sarama.Logger to whatever values they had before the call to LogTo.
//
// As an example
// defer LogTo(os.Stderr)()
// would start logging to stderr immediately and defer restoring the loggers.
func LogTo(w io.Writer) func() {
oldLogger := sarama.Logger
oldOutput := LogOutput
LogOutput = w
sarama.Logger = newLogger("Sarama", LogOutput)
return func() {
LogOutput = oldOutput
sarama.Logger = oldLogger
}
}
// StdLogger is the interface for log.Logger-compatible loggers
type StdLogger interface {
Print(v ...interface{})
Printf(format string, v ...interface{})
Println(v ...interface{})
Panic(v ...interface{})
Panicf(format string, v ...interface{})
}
func withRecover(fn func()) {
defer func() {
handler := PanicHandler
if handler != nil {
if err := recover(); err != nil {
handler(err)
}
}
}()
fn()
}
// PanicHandler is called for recovering from panics spawned internally to the library (and thus
// not recoverable by the caller's goroutine). Defaults to nil, which means panics are not recovered.
var PanicHandler func(interface{})