Browse code

vendor: sirupsen/logrus v1.7.0

full diff: https://github.com/sirupsen/logrus/compare/v1.6.0...v1.7.0

removes dependency on github.com/konsorten/go-windows-terminal-sequences

Features:
* a new buffer pool management API has been added
* a set of `<LogLevel>Fn()` functions have been added

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2020/09/30 20:38:38
Showing 12 changed files
... ...
@@ -10,8 +10,7 @@ github.com/moby/locker                              281af2d563954745bea9d1487c96
10 10
 github.com/moby/term                                7f0af18e79f2784809e9cef63d0df5aa2c79d76e
11 11
 
12 12
 github.com/creack/pty                               3a6a957789163cacdfe0e291617a1c8e80612c11 # v1.1.9
13
-github.com/konsorten/go-windows-terminal-sequences  edb144dfd453055e1e49a3d8b410a660b5a87613 # v1.0.3
14
-github.com/sirupsen/logrus                          60c74ad9be0d874af0ab0daef6ab07c5c5911f0d # v1.6.0
13
+github.com/sirupsen/logrus                          6699a89a232f3db797f2e280639854bbc4b89725 # v1.7.0
15 14
 github.com/tchap/go-patricia                        a7f0089c6f496e8e70402f61733606daa326cac5 # v2.3.0
16 15
 golang.org/x/net                                    ab34263943818b32f575efc978a3d24e80b04bd7
17 16
 golang.org/x/sys                                    aee5d888a86055dc6ab0342f9cdc7b53aaeaec62
18 17
deleted file mode 100644
... ...
@@ -1,9 +0,0 @@
1
-(The MIT License)
2
-
3
-Copyright (c) 2017 marvin + konsorten GmbH (open-source@konsorten.de)
4
-
5
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
-
7
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
-
9
-THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 1
deleted file mode 100644
... ...
@@ -1,42 +0,0 @@
1
-# Windows Terminal Sequences
2
-
3
-This library allow for enabling Windows terminal color support for Go.
4
-
5
-See [Console Virtual Terminal Sequences](https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences) for details.
6
-
7
-## Usage
8
-
9
-```go
10
-import (
11
-	"syscall"
12
-	
13
-	sequences "github.com/konsorten/go-windows-terminal-sequences"
14
-)
15
-
16
-func main() {
17
-	sequences.EnableVirtualTerminalProcessing(syscall.Stdout, true)
18
-}
19
-
20
-```
21
-
22
-## Authors
23
-
24
-The tool is sponsored by the [marvin + konsorten GmbH](http://www.konsorten.de).
25
-
26
-We thank all the authors who provided code to this library:
27
-
28
-* Felix Kollmann
29
-* Nicolas Perraut
30
-* @dirty49374
31
-
32
-## License
33
-
34
-(The MIT License)
35
-
36
-Copyright (c) 2018 marvin + konsorten GmbH (open-source@konsorten.de)
37
-
38
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
39
-
40
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
41
-
42
-THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 1
deleted file mode 100644
... ...
@@ -1 +0,0 @@
1
-module github.com/konsorten/go-windows-terminal-sequences
2 1
deleted file mode 100644
... ...
@@ -1,35 +0,0 @@
1
-// +build windows
2
-
3
-package sequences
4
-
5
-import (
6
-	"syscall"
7
-)
8
-
9
-var (
10
-	kernel32Dll    *syscall.LazyDLL  = syscall.NewLazyDLL("Kernel32.dll")
11
-	setConsoleMode *syscall.LazyProc = kernel32Dll.NewProc("SetConsoleMode")
12
-)
13
-
14
-func EnableVirtualTerminalProcessing(stream syscall.Handle, enable bool) error {
15
-	const ENABLE_VIRTUAL_TERMINAL_PROCESSING uint32 = 0x4
16
-
17
-	var mode uint32
18
-	err := syscall.GetConsoleMode(syscall.Stdout, &mode)
19
-	if err != nil {
20
-		return err
21
-	}
22
-
23
-	if enable {
24
-		mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING
25
-	} else {
26
-		mode &^= ENABLE_VIRTUAL_TERMINAL_PROCESSING
27
-	}
28
-
29
-	ret, _, err := setConsoleMode.Call(uintptr(stream), uintptr(mode))
30
-	if ret == 0 {
31
-		return err
32
-	}
33
-
34
-	return nil
35
-}
36 1
deleted file mode 100644
... ...
@@ -1,11 +0,0 @@
1
-// +build linux darwin
2
-
3
-package sequences
4
-
5
-import (
6
-	"fmt"
7
-)
8
-
9
-func EnableVirtualTerminalProcessing(stream uintptr, enable bool) error {
10
-	return fmt.Errorf("windows only package")
11
-}
12 1
new file mode 100644
... ...
@@ -0,0 +1,52 @@
0
+package logrus
1
+
2
+import (
3
+	"bytes"
4
+	"sync"
5
+)
6
+
7
+var (
8
+	bufferPool BufferPool
9
+)
10
+
11
+type BufferPool interface {
12
+	Put(*bytes.Buffer)
13
+	Get() *bytes.Buffer
14
+}
15
+
16
+type defaultPool struct {
17
+	pool *sync.Pool
18
+}
19
+
20
+func (p *defaultPool) Put(buf *bytes.Buffer) {
21
+	p.pool.Put(buf)
22
+}
23
+
24
+func (p *defaultPool) Get() *bytes.Buffer {
25
+	return p.pool.Get().(*bytes.Buffer)
26
+}
27
+
28
+func getBuffer() *bytes.Buffer {
29
+	return bufferPool.Get()
30
+}
31
+
32
+func putBuffer(buf *bytes.Buffer) {
33
+	buf.Reset()
34
+	bufferPool.Put(buf)
35
+}
36
+
37
+// SetBufferPool allows to replace the default logrus buffer pool
38
+// to better meets the specific needs of an application.
39
+func SetBufferPool(bp BufferPool) {
40
+	bufferPool = bp
41
+}
42
+
43
+func init() {
44
+	SetBufferPool(&defaultPool{
45
+		pool: &sync.Pool{
46
+			New: func() interface{} {
47
+				return new(bytes.Buffer)
48
+			},
49
+		},
50
+	})
51
+}
... ...
@@ -13,7 +13,6 @@ import (
13 13
 )
14 14
 
15 15
 var (
16
-	bufferPool *sync.Pool
17 16
 
18 17
 	// qualified package name, cached at first use
19 18
 	logrusPackage string
... ...
@@ -31,12 +30,6 @@ const (
31 31
 )
32 32
 
33 33
 func init() {
34
-	bufferPool = &sync.Pool{
35
-		New: func() interface{} {
36
-			return new(bytes.Buffer)
37
-		},
38
-	}
39
-
40 34
 	// start at the bottom of the stack before the package-name cache is primed
41 35
 	minimumCallerDepth = 1
42 36
 }
... ...
@@ -243,9 +236,12 @@ func (entry Entry) log(level Level, msg string) {
243 243
 
244 244
 	entry.fireHooks()
245 245
 
246
-	buffer = bufferPool.Get().(*bytes.Buffer)
246
+	buffer = getBuffer()
247
+	defer func() {
248
+		entry.Buffer = nil
249
+		putBuffer(buffer)
250
+	}()
247 251
 	buffer.Reset()
248
-	defer bufferPool.Put(buffer)
249 252
 	entry.Buffer = buffer
250 253
 
251 254
 	entry.write()
... ...
@@ -134,6 +134,51 @@ func Fatal(args ...interface{}) {
134 134
 	std.Fatal(args...)
135 135
 }
136 136
 
137
+// TraceFn logs a message from a func at level Trace on the standard logger.
138
+func TraceFn(fn LogFunction) {
139
+	std.TraceFn(fn)
140
+}
141
+
142
+// DebugFn logs a message from a func at level Debug on the standard logger.
143
+func DebugFn(fn LogFunction) {
144
+	std.DebugFn(fn)
145
+}
146
+
147
+// PrintFn logs a message from a func at level Info on the standard logger.
148
+func PrintFn(fn LogFunction) {
149
+	std.PrintFn(fn)
150
+}
151
+
152
+// InfoFn logs a message from a func at level Info on the standard logger.
153
+func InfoFn(fn LogFunction) {
154
+	std.InfoFn(fn)
155
+}
156
+
157
+// WarnFn logs a message from a func at level Warn on the standard logger.
158
+func WarnFn(fn LogFunction) {
159
+	std.WarnFn(fn)
160
+}
161
+
162
+// WarningFn logs a message from a func at level Warn on the standard logger.
163
+func WarningFn(fn LogFunction) {
164
+	std.WarningFn(fn)
165
+}
166
+
167
+// ErrorFn logs a message from a func at level Error on the standard logger.
168
+func ErrorFn(fn LogFunction) {
169
+	std.ErrorFn(fn)
170
+}
171
+
172
+// PanicFn logs a message from a func at level Panic on the standard logger.
173
+func PanicFn(fn LogFunction) {
174
+	std.PanicFn(fn)
175
+}
176
+
177
+// FatalFn logs a message from a func at level Fatal on the standard logger then the process will exit with status set to 1.
178
+func FatalFn(fn LogFunction) {
179
+	std.FatalFn(fn)
180
+}
181
+
137 182
 // Tracef logs a message at level Trace on the standard logger.
138 183
 func Tracef(format string, args ...interface{}) {
139 184
 	std.Tracef(format, args...)
... ...
@@ -2,10 +2,9 @@ module github.com/sirupsen/logrus
2 2
 
3 3
 require (
4 4
 	github.com/davecgh/go-spew v1.1.1 // indirect
5
-	github.com/konsorten/go-windows-terminal-sequences v1.0.3
6 5
 	github.com/pmezard/go-difflib v1.0.0 // indirect
7 6
 	github.com/stretchr/testify v1.2.2
8
-	golang.org/x/sys v0.0.0-20190422165155-953cdadca894
7
+	golang.org/x/sys v0.0.0-20191026070338-33540a1f6037
9 8
 )
10 9
 
11 10
 go 1.13
... ...
@@ -9,6 +9,11 @@ import (
9 9
 	"time"
10 10
 )
11 11
 
12
+// LogFunction For big messages, it can be more efficient to pass a function
13
+// and only call it if the log level is actually enables rather than
14
+// generating the log message and then checking if the level is enabled
15
+type LogFunction func()[]interface{}
16
+
12 17
 type Logger struct {
13 18
 	// The logs are `io.Copy`'d to this in a mutex. It's common to set this to a
14 19
 	// file, or leave it default which is `os.Stderr`. You can also set this to
... ...
@@ -70,7 +75,7 @@ func (mw *MutexWrap) Disable() {
70 70
 //
71 71
 //    var log = &logrus.Logger{
72 72
 //      Out: os.Stderr,
73
-//      Formatter: new(logrus.JSONFormatter),
73
+//      Formatter: new(logrus.TextFormatter),
74 74
 //      Hooks: make(logrus.LevelHooks),
75 75
 //      Level: logrus.DebugLevel,
76 76
 //    }
... ...
@@ -195,6 +200,14 @@ func (logger *Logger) Log(level Level, args ...interface{}) {
195 195
 	}
196 196
 }
197 197
 
198
+func (logger *Logger) LogFn(level Level, fn LogFunction) {
199
+	if logger.IsLevelEnabled(level) {
200
+		entry := logger.newEntry()
201
+		entry.Log(level, fn()...)
202
+		logger.releaseEntry(entry)
203
+	}
204
+}
205
+
198 206
 func (logger *Logger) Trace(args ...interface{}) {
199 207
 	logger.Log(TraceLevel, args...)
200 208
 }
... ...
@@ -234,6 +247,45 @@ func (logger *Logger) Panic(args ...interface{}) {
234 234
 	logger.Log(PanicLevel, args...)
235 235
 }
236 236
 
237
+func (logger *Logger) TraceFn(fn LogFunction) {
238
+	logger.LogFn(TraceLevel, fn)
239
+}
240
+
241
+func (logger *Logger) DebugFn(fn LogFunction) {
242
+	logger.LogFn(DebugLevel, fn)
243
+}
244
+
245
+func (logger *Logger) InfoFn(fn LogFunction) {
246
+	logger.LogFn(InfoLevel, fn)
247
+}
248
+
249
+func (logger *Logger) PrintFn(fn LogFunction) {
250
+	entry := logger.newEntry()
251
+	entry.Print(fn()...)
252
+	logger.releaseEntry(entry)
253
+}
254
+
255
+func (logger *Logger) WarnFn(fn LogFunction) {
256
+	logger.LogFn(WarnLevel, fn)
257
+}
258
+
259
+func (logger *Logger) WarningFn(fn LogFunction) {
260
+	logger.WarnFn(fn)
261
+}
262
+
263
+func (logger *Logger) ErrorFn(fn LogFunction) {
264
+	logger.LogFn(ErrorLevel, fn)
265
+}
266
+
267
+func (logger *Logger) FatalFn(fn LogFunction) {
268
+	logger.LogFn(FatalLevel, fn)
269
+	logger.Exit(1)
270
+}
271
+
272
+func (logger *Logger) PanicFn(fn LogFunction) {
273
+	logger.LogFn(PanicLevel, fn)
274
+}
275
+
237 276
 func (logger *Logger) Logln(level Level, args ...interface{}) {
238 277
 	if logger.IsLevelEnabled(level) {
239 278
 		entry := logger.newEntry()
... ...
@@ -5,30 +5,23 @@ package logrus
5 5
 import (
6 6
 	"io"
7 7
 	"os"
8
-	"syscall"
9 8
 
10
-	sequences "github.com/konsorten/go-windows-terminal-sequences"
9
+	"golang.org/x/sys/windows"
11 10
 )
12 11
 
13
-func initTerminal(w io.Writer) {
14
-	switch v := w.(type) {
15
-	case *os.File:
16
-		sequences.EnableVirtualTerminalProcessing(syscall.Handle(v.Fd()), true)
17
-	}
18
-}
19
-
20 12
 func checkIfTerminal(w io.Writer) bool {
21
-	var ret bool
22 13
 	switch v := w.(type) {
23 14
 	case *os.File:
15
+		handle := windows.Handle(v.Fd())
24 16
 		var mode uint32
25
-		err := syscall.GetConsoleMode(syscall.Handle(v.Fd()), &mode)
26
-		ret = (err == nil)
27
-	default:
28
-		ret = false
29
-	}
30
-	if ret {
31
-		initTerminal(w)
17
+		if err := windows.GetConsoleMode(handle, &mode); err != nil {
18
+			return false
19
+		}
20
+		mode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
21
+		if err := windows.SetConsoleMode(handle, mode); err != nil {
22
+			return false
23
+		}
24
+		return true
32 25
 	}
33
-	return ret
26
+	return false
34 27
 }