go vendor

This commit is contained in:
lealife
2017-11-30 19:55:33 +08:00
parent 2856da6888
commit 0fb92efbf3
670 changed files with 199010 additions and 0 deletions

109
vendor/github.com/revel/log15/ext/ext_test.go generated vendored Normal file
View File

@ -0,0 +1,109 @@
package ext
import (
"errors"
log "github.com/inconshreveable/log15"
"math"
"testing"
)
func testHandler() (log.Handler, *log.Record) {
rec := new(log.Record)
return log.FuncHandler(func(r *log.Record) error {
*rec = *r
return nil
}), rec
}
func TestHotSwapHandler(t *testing.T) {
t.Parallel()
h1, r1 := testHandler()
l := log.New()
h := HotSwapHandler(h1)
l.SetHandler(h)
l.Info("to h1")
if r1.Msg != "to h1" {
t.Fatalf("didn't get expected message to h1")
}
h2, r2 := testHandler()
h.Swap(h2)
l.Info("to h2")
if r2.Msg != "to h2" {
t.Fatalf("didn't get expected message to h2")
}
}
func TestSpeculativeHandler(t *testing.T) {
t.Parallel()
// test with an even multiple of the buffer size, less than full buffer size
// and not a multiple of the buffer size
for _, count := range []int{10000, 50, 432} {
recs := make(chan *log.Record)
done := make(chan int)
spec := SpeculativeHandler(100, log.ChannelHandler(recs))
go func() {
defer close(done)
expectedCount := int(math.Min(float64(count), float64(100)))
expectedIdx := count - expectedCount
for r := range recs {
if r.Ctx[1] != expectedIdx {
t.Errorf("Bad ctx 'i', got %d expected %d", r.Ctx[1], expectedIdx)
return
}
expectedIdx++
expectedCount--
if expectedCount == 0 {
// got everything we expected
break
}
}
select {
case <-recs:
t.Errorf("got an extra record we shouldn't have!")
default:
}
}()
lg := log.New()
lg.SetHandler(spec)
for i := 0; i < count; i++ {
lg.Debug("test speculative", "i", i)
}
go spec.Flush()
// wait for the go routine to finish
<-done
}
}
func TestErrorHandler(t *testing.T) {
t.Parallel()
h, r := testHandler()
lg := log.New()
lg.SetHandler(EscalateErrHandler(
log.LvlFilterHandler(log.LvlError, h)))
lg.Debug("some function result", "err", nil)
if r.Msg != "" {
t.Fatalf("Expected debug level message to be filtered")
}
lg.Debug("some function result", "err", errors.New("failed operation"))
if r.Msg != "some function result" {
t.Fatalf("Expected debug level message to be escalated and pass lvlfilter")
}
if r.Lvl != log.LvlError {
t.Fatalf("Expected debug level message to be escalated to LvlError")
}
}

130
vendor/github.com/revel/log15/ext/handler.go generated vendored Normal file
View File

@ -0,0 +1,130 @@
package ext
import (
"os"
"sync"
"sync/atomic"
"unsafe"
log "github.com/inconshreveable/log15"
)
// EscalateErrHandler wraps another handler and passes all records through
// unchanged except if the logged context contains a non-nil error
// value in its context. In that case, the record's level is raised
// to LvlError unless it was already more serious (LvlCrit).
//
// This allows you to log the result of all functions for debugging
// and still capture error conditions when in production with a single
// log line. As an example, the following the log record will be written
// out only if there was an error writing a value to redis:
//
// logger := logext.EscalateErrHandler(
// log.LvlFilterHandler(log.LvlInfo, log.StdoutHandler))
//
// reply, err := redisConn.Do("SET", "foo", "bar")
// logger.Debug("Wrote value to redis", "reply", reply, "err", err)
// if err != nil {
// return err
// }
//
func EscalateErrHandler(h log.Handler) log.Handler {
return log.FuncHandler(func(r *log.Record) error {
if r.Lvl > log.LvlError {
for i := 1; i < len(r.Ctx); i++ {
if v, ok := r.Ctx[i].(error); ok && v != nil {
r.Lvl = log.LvlError
break
}
}
}
return h.Log(r)
})
}
// SpeculativeHandler is a handler for speculative logging. It
// keeps a ring buffer of the given size full of the last events
// logged into it. When Flush is called, all buffered log records
// are written to the wrapped handler. This is extremely for
// continuosly capturing debug level output, but only flushing those
// log records if an exceptional condition is encountered.
func SpeculativeHandler(size int, h log.Handler) *Speculative {
return &Speculative{
handler: h,
recs: make([]*log.Record, size),
}
}
type Speculative struct {
mu sync.Mutex
idx int
recs []*log.Record
handler log.Handler
full bool
}
func (h *Speculative) Log(r *log.Record) error {
h.mu.Lock()
defer h.mu.Unlock()
h.recs[h.idx] = r
h.idx = (h.idx + 1) % len(h.recs)
h.full = h.full || h.idx == 0
return nil
}
func (h *Speculative) Flush() {
recs := make([]*log.Record, 0)
func() {
h.mu.Lock()
defer h.mu.Unlock()
if h.full {
recs = append(recs, h.recs[h.idx:]...)
}
recs = append(recs, h.recs[:h.idx]...)
// reset state
h.full = false
h.idx = 0
}()
// don't hold the lock while we flush to the wrapped handler
for _, r := range recs {
h.handler.Log(r)
}
}
// HotSwapHandler wraps another handler that may swapped out
// dynamically at runtime in a thread-safe fashion.
// HotSwapHandler is the same functionality
// used to implement the SetHandler method for the default
// implementation of Logger.
func HotSwapHandler(h log.Handler) *HotSwap {
hs := new(HotSwap)
hs.Swap(h)
return hs
}
type HotSwap struct {
handler unsafe.Pointer
}
func (h *HotSwap) Log(r *log.Record) error {
return (*(*log.Handler)(atomic.LoadPointer(&h.handler))).Log(r)
}
func (h *HotSwap) Swap(newHandler log.Handler) {
atomic.StorePointer(&h.handler, unsafe.Pointer(&newHandler))
}
// FatalHandler makes critical errors exit the program
// immediately, much like the log.Fatal* methods from the
// standard log package
func FatalHandler(h log.Handler) log.Handler {
return log.FuncHandler(func(r *log.Record) error {
err := h.Log(r)
if r.Lvl == log.LvlCrit {
os.Exit(1)
}
return err
})
}

47
vendor/github.com/revel/log15/ext/id.go generated vendored Normal file
View File

@ -0,0 +1,47 @@
package ext
import (
"fmt"
"math/rand"
"sync"
"time"
)
var r = rand.New(&lockedSource{src: rand.NewSource(time.Now().Unix())})
// RandId creates a random identifier of the requested length.
// Useful for assigning mostly-unique identifiers for logging
// and identification that are unlikely to collide because of
// short lifespan or low set cardinality
func RandId(idlen int) string {
b := make([]byte, idlen)
var randVal uint32
for i := 0; i < idlen; i++ {
byteIdx := i % 4
if byteIdx == 0 {
randVal = r.Uint32()
}
b[i] = byte((randVal >> (8 * uint(byteIdx))) & 0xFF)
}
return fmt.Sprintf("%x", b)
}
// lockedSource is a wrapper to allow a rand.Source to be used
// concurrently (same type as the one used internally in math/rand).
type lockedSource struct {
lk sync.Mutex
src rand.Source
}
func (r *lockedSource) Int63() (n int64) {
r.lk.Lock()
n = r.src.Int63()
r.lk.Unlock()
return
}
func (r *lockedSource) Seed(seed int64) {
r.lk.Lock()
r.src.Seed(seed)
r.lk.Unlock()
}