Этот коммит содержится в:
Christopher Speller
2017-06-21 19:06:17 -07:00
коммит произвёл Corey Hulen
родитель 6b39c308d8
Коммит 42f28ab8e3
714 изменённых файлов: 54430 добавлений и 51180 удалений

1
vendor/github.com/spf13/jwalterweatherman/log_counter.go сгенерированный поставляемый
Просмотреть файл

@@ -27,7 +27,6 @@ func (c *logCounter) getCount() uint64 {
func (c *logCounter) Write(p []byte) (n int, err error) {
c.incr()
return len(p), nil
}

72
vendor/github.com/spf13/jwalterweatherman/notepad.go сгенерированный поставляемый
Просмотреть файл

@@ -9,7 +9,6 @@ import (
"fmt"
"io"
"log"
"os"
)
type Threshold int
@@ -38,11 +37,7 @@ var prefixes map[Threshold]string = map[Threshold]string{
LevelFatal: "FATAL",
}
func prefix(t Threshold) string {
return t.String() + " "
}
// Notepad is where you leave a note !
// Notepad is where you leave a note!
type Notepad struct {
TRACE *log.Logger
DEBUG *log.Logger
@@ -55,7 +50,7 @@ type Notepad struct {
LOG *log.Logger
FEEDBACK *Feedback
loggers []**log.Logger
loggers [7]**log.Logger
logHandle io.Writer
outHandle io.Writer
logThreshold Threshold
@@ -71,11 +66,11 @@ type Notepad struct {
func NewNotepad(outThreshold Threshold, logThreshold Threshold, outHandle, logHandle io.Writer, prefix string, flags int) *Notepad {
n := &Notepad{}
n.loggers = append(n.loggers, &n.TRACE, &n.DEBUG, &n.INFO, &n.WARN, &n.ERROR, &n.CRITICAL, &n.FATAL)
n.logHandle = logHandle
n.loggers = [7]**log.Logger{&n.TRACE, &n.DEBUG, &n.INFO, &n.WARN, &n.ERROR, &n.CRITICAL, &n.FATAL}
n.outHandle = outHandle
n.logThreshold = logThreshold
n.logHandle = logHandle
n.stdoutThreshold = outThreshold
n.logThreshold = logThreshold
if len(prefix) != 0 {
n.prefix = "[" + prefix + "] "
@@ -88,47 +83,48 @@ func NewNotepad(outThreshold Threshold, logThreshold Threshold, outHandle, logHa
n.LOG = log.New(n.logHandle,
"LOG: ",
n.flags)
n.FEEDBACK = &Feedback{n}
n.FEEDBACK = &Feedback{out: log.New(outHandle, "", 0), log: n.LOG}
n.init()
return n
}
// init create the loggers for each level depending on the notepad thresholds
// init creates the loggers for each level depending on the notepad thresholds.
func (n *Notepad) init() {
bothHandle := io.MultiWriter(n.outHandle, n.logHandle)
logAndOut := io.MultiWriter(n.outHandle, n.logHandle)
for t, logger := range n.loggers {
threshold := Threshold(t)
counter := &logCounter{}
n.logCounters[t] = counter
prefix := n.prefix + threshold.String() + " "
switch {
case threshold >= n.logThreshold && threshold >= n.stdoutThreshold:
*logger = log.New(io.MultiWriter(counter, bothHandle), n.prefix+prefix(threshold), n.flags)
*logger = log.New(io.MultiWriter(counter, logAndOut), prefix, n.flags)
case threshold >= n.logThreshold:
*logger = log.New(io.MultiWriter(counter, n.logHandle), n.prefix+prefix(threshold), n.flags)
*logger = log.New(io.MultiWriter(counter, n.logHandle), prefix, n.flags)
case threshold >= n.stdoutThreshold:
*logger = log.New(io.MultiWriter(counter, os.Stdout), n.prefix+prefix(threshold), n.flags)
*logger = log.New(io.MultiWriter(counter, n.outHandle), prefix, n.flags)
default:
*logger = log.New(counter, n.prefix+prefix(threshold), n.flags)
// counter doesn't care about prefix and flags, so don't use them
// for performance.
*logger = log.New(counter, "", 0)
}
}
}
// SetLogThreshold change the threshold above which messages are written to the
// log file
// SetLogThreshold changes the threshold above which messages are written to the
// log file.
func (n *Notepad) SetLogThreshold(threshold Threshold) {
n.logThreshold = threshold
n.init()
}
// SetLogOutput change the file where log messages are written
// SetLogOutput changes the file where log messages are written.
func (n *Notepad) SetLogOutput(handle io.Writer) {
n.logHandle = handle
n.init()
@@ -139,8 +135,8 @@ func (n *Notepad) GetLogThreshold() Threshold {
return n.logThreshold
}
// SetStdoutThreshold change the threshold above which messages are written to the
// standard output
// SetStdoutThreshold changes the threshold above which messages are written to the
// standard output.
func (n *Notepad) SetStdoutThreshold(threshold Threshold) {
n.stdoutThreshold = threshold
n.init()
@@ -151,7 +147,7 @@ func (n *Notepad) GetStdoutThreshold() Threshold {
return n.stdoutThreshold
}
// SetPrefix change the prefix used by the notepad. Prefixes are displayed between
// SetPrefix changes the prefix used by the notepad. Prefixes are displayed between
// brackets at the begining of the line. An empty prefix won't be displayed at all.
func (n *Notepad) SetPrefix(prefix string) {
if len(prefix) != 0 {
@@ -169,26 +165,30 @@ func (n *Notepad) SetFlags(flags int) {
n.init()
}
// Feedback is special. It writes plainly to the output while
// Feedback writes plainly to the outHandle while
// logging with the standard extra information (date, file, etc).
type Feedback struct {
*Notepad
out *log.Logger
log *log.Logger
}
func (fb *Feedback) Println(v ...interface{}) {
s := fmt.Sprintln(v...)
fmt.Print(s)
fb.LOG.Output(2, s)
fb.output(fmt.Sprintln(v...))
}
func (fb *Feedback) Printf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
fmt.Print(s)
fb.LOG.Output(2, s)
fb.output(fmt.Sprintf(format, v...))
}
func (fb *Feedback) Print(v ...interface{}) {
s := fmt.Sprint(v...)
fmt.Print(s)
fb.LOG.Output(2, s)
fb.output(fmt.Sprint(v...))
}
func (fb *Feedback) output(s string) {
if fb.out != nil {
fb.out.Output(2, s)
}
if fb.log != nil {
fb.log.Output(2, s)
}
}

11
vendor/github.com/spf13/jwalterweatherman/notepad_test.go сгенерированный поставляемый
Просмотреть файл

@@ -13,7 +13,6 @@ import (
)
func TestNotepad(t *testing.T) {
var logHandle, outHandle bytes.Buffer
n := NewNotepad(LevelCritical, LevelError, &outHandle, &logHandle, "TestNotePad", 0)
@@ -39,3 +38,13 @@ func TestThresholdString(t *testing.T) {
require.Equal(t, LevelError.String(), "ERROR")
require.Equal(t, LevelTrace.String(), "TRACE")
}
func BenchmarkLogPrintOnlyToCounter(b *testing.B) {
var logHandle, outHandle bytes.Buffer
n := NewNotepad(LevelCritical, LevelCritical, &outHandle, &logHandle, "TestNotePad", 0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
n.INFO.Print("Test")
}
}