-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
94 lines (78 loc) · 1.52 KB
/
util.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package raft
import (
"fmt"
"math/rand"
"os"
"time"
log "github.com/sirupsen/logrus"
)
const Debug = 0
var (
NANO_TO_MILLI = int64(1000000)
Black = Color("\033[1;30m%s\033[0m")
Red = Color("\033[1;31m%s\033[0m")
Blue = Color("\033[0;31m%s\033[0m")
Green = Color("\033[1;32m%s\033[0m")
Yellow = Color("\033[1;33m%s\033[0m")
Purple = Color("\033[1;34m%s\033[0m")
Magenta = Color("\033[1;35m%s\033[0m")
Teal = Color("\033[1;36m%s\033[0m")
White = Color("\033[1;37m%s\033[0m")
fp *os.File
seq int
)
func init() {
fn := os.Getenv("LOG_FILE")
if fn == "" {
fn = "raft.log"
}
if Debug == 0 {
fp, _ = os.OpenFile(fn, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
}
}
func Dlog(format string, a ...interface{}) (n int, err error) {
s := fmt.Sprintf(format, a...)
if Debug > 0 {
log.Infof(s)
} else {
fmt.Fprintf(fp, s)
}
return
}
func MicroSecondNow() int64 {
return time.Now().UnixNano() / NANO_TO_MILLI
}
func randTs(from, to int) int64 {
return int64(rand.Intn(to-from) + from)
}
func Color(colorString string) func(...interface{}) string {
sprint := func(args ...interface{}) string {
return fmt.Sprintf(colorString,
fmt.Sprint(args...))
}
return sprint
}
func copyEntries(s []Entry) []Entry {
r := make([]Entry, len(s))
for i, v := range s {
r[i] = v
}
return r
}
func clearLog() {
if Debug == 0 {
fp.Truncate(int64(0))
fp.Seek(int64(0), 0)
}
}
func seqInt() int {
ret := seq
seq++
return ret
}
func minInt(a, b int) int {
if a > b {
return b
}
return a
}