forked from elastos/Elastos.ELA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
188 lines (158 loc) · 4.22 KB
/
log.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"fmt"
"io"
"path/filepath"
"time"
"github.com/elastos/Elastos.ELA/common/log"
"github.com/elastos/Elastos.ELA/dpos/state"
"github.com/elastos/Elastos.ELA/elanet"
"github.com/elastos/Elastos.ELA/elanet/netsync"
"github.com/elastos/Elastos.ELA/elanet/peer"
"github.com/elastos/Elastos.ELA/elanet/routes"
"github.com/elastos/Elastos.ELA/p2p/addrmgr"
"github.com/elastos/Elastos.ELA/p2p/connmgr"
"github.com/elastos/Elastos.ELA/utils/elalog"
"github.com/urfave/cli"
"gopkg.in/cheggaaa/pb.v1"
)
type logWrapper struct {
level elalog.Level
logger elalog.Logger
}
func (l *logWrapper) Debug(a ...interface{}) {
if l.level > elalog.LevelDebug {
return
}
l.logger.Debug(a...)
}
func (l *logWrapper) Debugf(format string, a ...interface{}) {
if l.level > elalog.LevelDebug {
return
}
l.logger.Debugf(format, a...)
}
func (l *logWrapper) Info(a ...interface{}) {
if l.level > elalog.LevelInfo {
return
}
l.logger.Info(a...)
}
func (l *logWrapper) Infof(format string, a ...interface{}) {
if l.level > elalog.LevelInfo {
return
}
l.logger.Infof(format, a...)
}
func (l *logWrapper) Warn(a ...interface{}) {
if l.level > elalog.LevelWarn {
return
}
l.logger.Warn(a...)
}
func (l *logWrapper) Warnf(format string, a ...interface{}) {
if l.level > elalog.LevelWarn {
return
}
l.logger.Warnf(format, a...)
}
func (l *logWrapper) Error(a ...interface{}) {
if l.level > elalog.LevelError {
return
}
l.logger.Error(a...)
}
func (l *logWrapper) Errorf(format string, a ...interface{}) {
if l.level > elalog.LevelError {
return
}
l.logger.Errorf(format, a...)
}
func (l *logWrapper) Fatal(a ...interface{}) {
if l.level > elalog.LevelFatal {
return
}
l.logger.Fatal(a...)
}
func (l *logWrapper) Fatalf(format string, a ...interface{}) {
if l.level > elalog.LevelFatal {
return
}
l.logger.Fatalf(format, a...)
}
// Level returns the current logging level.
func (l *logWrapper) Level() elalog.Level {
return l.level
}
// SetLevel changes the logging level to the passed level.
func (l *logWrapper) SetLevel(level elalog.Level) {
l.level = level
}
func wrap(logger *log.Logger, level elalog.Level) *logWrapper {
return &logWrapper{logger: logger, level: level}
}
const (
// progressRefreshRate indicates the duration between refresh progress.
progressRefreshRate = time.Millisecond * 500
// startString defines the print out message when start progress.
startString = "[ ========== BLOCKCHAIN INITIALIZE STARTED ========== ]"
// finishString defines the print out message when finish progress.
finishString = "[ ========== BLOCKCHAIN INITIALIZE FINISHED ========== ]"
)
// progress shows a progress bar in the terminal and print blockchain initialize
// progress into log file.
type progress struct {
w io.Writer
pb *pb.ProgressBar
}
func (p *progress) Start(total uint32) {
fmt.Fprintln(p.w, startString)
p.pb = pb.New64(int64(total))
p.pb.Output = p.w
p.pb.ShowTimeLeft = false
p.pb.ShowFinalTime = true
p.pb.SetRefreshRate(progressRefreshRate)
p.pb.Start()
}
func (p *progress) Increase() {
if p.pb != nil {
p.pb.Increment()
}
}
func (p *progress) Stop() {
if p.pb != nil {
p.pb.FinishPrint(finishString)
}
}
// newProgress creates a progress instance.
func newProgress(w io.Writer) *progress {
return &progress{w: w}
}
// log is a logger that is initialized with no output filters. This
// means the package will not perform any logging by default until the caller
// requests it.
var (
logger *log.Logger
pgBar *progress
)
// The default amount of logging is none.
func setupLog(c *cli.Context) {
flagDataDir := c.String("datadir")
path := filepath.Join(flagDataDir, nodeLogPath)
logger = log.NewDefault(path, uint8(cfg.PrintLevel), cfg.MaxPerLogSize, cfg.MaxLogsSize)
pgBar = newProgress(logger.Writer())
admrlog := wrap(logger, elalog.LevelOff)
cmgrlog := wrap(logger, elalog.LevelOff)
synclog := wrap(logger, cfg.PrintLevel)
peerlog := wrap(logger, cfg.PrintLevel)
routlog := wrap(logger, cfg.PrintLevel)
elanlog := wrap(logger, cfg.PrintLevel)
statlog := wrap(logger, cfg.PrintLevel)
addrmgr.UseLogger(admrlog)
connmgr.UseLogger(cmgrlog)
netsync.UseLogger(synclog)
peer.UseLogger(peerlog)
routes.UseLogger(routlog)
elanet.UseLogger(elanlog)
state.UseLogger(statlog)
}