Skip to content

Commit 31baf3a

Browse files
fjlkaralabe
authored andcommitted
log, internal/debug: delete RotatingFileHandler (#20586)
* log: delete RotatingFileHandler We added this for the dashboard, which is gone now. The handler never really worked well and had data race and file handling issues. * internal/debug: remove unused RotatingFileHandler setup code
1 parent ad2fc7c commit 31baf3a

4 files changed

Lines changed: 3 additions & 124 deletions

File tree

‎cmd/devp2p/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func init() {
4545
// Set up the CLI app.
4646
app.Flags = append(app.Flags, debug.Flags...)
4747
app.Before = func(ctx *cli.Context) error {
48-
return debug.Setup(ctx, "")
48+
return debug.Setup(ctx)
4949
}
5050
app.After = func(ctx *cli.Context) error {
5151
debug.Exit()

‎cmd/geth/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func init() {
233233
app.Flags = append(app.Flags, metricsFlags...)
234234

235235
app.Before = func(ctx *cli.Context) error {
236-
return debug.Setup(ctx, "")
236+
return debug.Setup(ctx)
237237
}
238238
app.After = func(ctx *cli.Context) error {
239239
debug.Exit()

‎internal/debug/flags.go‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,9 @@ func init() {
112112

113113
// Setup initializes profiling and logging based on the CLI flags.
114114
// It should be called as early as possible in the program.
115-
func Setup(ctx *cli.Context, logdir string) error {
115+
func Setup(ctx *cli.Context) error {
116116
// logging
117117
log.PrintOrigins(ctx.GlobalBool(debugFlag.Name))
118-
if logdir != "" {
119-
rfh, err := log.RotatingFileHandler(
120-
logdir,
121-
262144,
122-
log.JSONFormatOrderedEx(false, true),
123-
)
124-
if err != nil {
125-
return err
126-
}
127-
glogger.SetHandler(log.MultiHandler(ostream, rfh))
128-
}
129118
glogger.Verbosity(log.Lvl(ctx.GlobalInt(verbosityFlag.Name)))
130119
glogger.Vmodule(ctx.GlobalString(vmoduleFlag.Name))
131120
glogger.BacktraceAt(ctx.GlobalString(backtraceAtFlag.Name))

‎log/handler.go‎

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ import (
88
"reflect"
99
"sync"
1010

11-
"io/ioutil"
12-
"path/filepath"
13-
"regexp"
14-
"strings"
15-
1611
"github.com/go-stack/stack"
1712
)
1813

@@ -75,111 +70,6 @@ func FileHandler(path string, fmtr Format) (Handler, error) {
7570
return closingHandler{f, StreamHandler(f, fmtr)}, nil
7671
}
7772

78-
// countingWriter wraps a WriteCloser object in order to count the written bytes.
79-
type countingWriter struct {
80-
w io.WriteCloser // the wrapped object
81-
count uint // number of bytes written
82-
}
83-
84-
// Write increments the byte counter by the number of bytes written.
85-
// Implements the WriteCloser interface.
86-
func (w *countingWriter) Write(p []byte) (n int, err error) {
87-
n, err = w.w.Write(p)
88-
w.count += uint(n)
89-
return n, err
90-
}
91-
92-
// Close implements the WriteCloser interface.
93-
func (w *countingWriter) Close() error {
94-
return w.w.Close()
95-
}
96-
97-
// prepFile opens the log file at the given path, and cuts off the invalid part
98-
// from the end, because the previous execution could have been finished by interruption.
99-
// Assumes that every line ended by '\n' contains a valid log record.
100-
func prepFile(path string) (*countingWriter, error) {
101-
f, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND, 0600)
102-
if err != nil {
103-
return nil, err
104-
}
105-
_, err = f.Seek(-1, io.SeekEnd)
106-
if err != nil {
107-
return nil, err
108-
}
109-
buf := make([]byte, 1)
110-
var cut int64
111-
for {
112-
if _, err := f.Read(buf); err != nil {
113-
return nil, err
114-
}
115-
if buf[0] == '\n' {
116-
break
117-
}
118-
if _, err = f.Seek(-2, io.SeekCurrent); err != nil {
119-
return nil, err
120-
}
121-
cut++
122-
}
123-
fi, err := f.Stat()
124-
if err != nil {
125-
return nil, err
126-
}
127-
ns := fi.Size() - cut
128-
if err = f.Truncate(ns); err != nil {
129-
return nil, err
130-
}
131-
return &countingWriter{w: f, count: uint(ns)}, nil
132-
}
133-
134-
// RotatingFileHandler returns a handler which writes log records to file chunks
135-
// at the given path. When a file's size reaches the limit, the handler creates
136-
// a new file named after the timestamp of the first log record it will contain.
137-
func RotatingFileHandler(path string, limit uint, formatter Format) (Handler, error) {
138-
if err := os.MkdirAll(path, 0700); err != nil {
139-
return nil, err
140-
}
141-
files, err := ioutil.ReadDir(path)
142-
if err != nil {
143-
return nil, err
144-
}
145-
re := regexp.MustCompile(`\.log$`)
146-
last := len(files) - 1
147-
for last >= 0 && (!files[last].Mode().IsRegular() || !re.MatchString(files[last].Name())) {
148-
last--
149-
}
150-
var counter *countingWriter
151-
if last >= 0 && files[last].Size() < int64(limit) {
152-
// Open the last file, and continue to write into it until it's size reaches the limit.
153-
if counter, err = prepFile(filepath.Join(path, files[last].Name())); err != nil {
154-
return nil, err
155-
}
156-
}
157-
if counter == nil {
158-
counter = new(countingWriter)
159-
}
160-
h := StreamHandler(counter, formatter)
161-
162-
return FuncHandler(func(r *Record) error {
163-
if counter.count > limit {
164-
counter.Close()
165-
counter.w = nil
166-
}
167-
if counter.w == nil {
168-
f, err := os.OpenFile(
169-
filepath.Join(path, fmt.Sprintf("%s.log", strings.Replace(r.Time.Format("060102150405.00"), ".", "", 1))),
170-
os.O_CREATE|os.O_APPEND|os.O_WRONLY,
171-
0600,
172-
)
173-
if err != nil {
174-
return err
175-
}
176-
counter.w = f
177-
counter.count = 0
178-
}
179-
return h.Log(r)
180-
}), nil
181-
}
182-
18373
// NetHandler opens a socket to the given address and writes records
18474
// over the connection.
18575
func NetHandler(network, addr string, fmtr Format) (Handler, error) {

0 commit comments

Comments
 (0)