| 1 | // Copyright (c) The EfficientGo Authors. |
| 2 | // Licensed under the Apache License 2.0. |
| 3 | |
| 4 | // Initially copied from Thanos |
| 5 | // |
| 6 | // Copyright (c) The Thanos Authors. |
| 7 | // Licensed under the Apache License 2.0. |
| 8 | |
| 9 | package logerrcapturescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/ |
| 10 | |
| 11 | import ( |
| 12 | "fmtscip-go gomod github.com/golang/go/src go1.17 fmt/" |
| 13 | "ioscip-go gomod github.com/golang/go/src go1.17 io/" |
| 14 | "osscip-go gomod github.com/golang/go/src go1.17 os/" |
| 15 | |
| 16 | "github.com/efficientgo/core/errorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/" |
| 17 | ) |
| 18 | |
| 19 | // Logger interface compatible with go-kit/logger. |
| 20 | type Loggerscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/Logger# interface { |
| 21 | Logscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/Logger#Log.(keyvals ...interface{}) error |
| 22 | } |
| 23 | |
| 24 | type doFuncscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/doFunc# func() error |
| 25 | |
| 26 | // Do is making sure we log every error, even those from best effort tiny functions. |
| 27 | func Doscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/Do().(logger Loggerscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/Logger#, doer doFuncscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/doFunc#, format string, a ...interface{}) { |
| 28 | derr := doer() |
| 29 | if derr == nil { |
| 30 | return |
| 31 | } |
| 32 | |
| 33 | // For os closers, it's a common case to double close. From reliability purpose this is not a problem it may only indicate |
| 34 | // surprising execution path. |
| 35 | if errorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/.Isscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/Is().(derr, osscip-go gomod github.com/golang/go/src go1.17 os/.ErrClosedscip-go gomod github.com/golang/go/src go1.17 os/ErrClosed.) { |
| 36 | return |
| 37 | } |
| 38 | |
| 39 | _ = logger.Logscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/Logger#Log.("msg", "detected do error", "err", errorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/.Wrapscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/Wrap().(derr, fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.17 fmt/Sprintf().(format, a...))) |
| 40 | } |
| 41 | |
| 42 | // ExhaustClose closes the io.ReadCloser with a log message on error but exhausts the reader before. |
| 43 | func ExhaustClosescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/ExhaustClose().(logger Loggerscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/Logger#, r ioscip-go gomod github.com/golang/go/src go1.17 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.17 io/ReadCloser#, format string, a ...interface{}) { |
| 44 | _, err := ioscip-go gomod github.com/golang/go/src go1.17 io/.Copyscip-go gomod github.com/golang/go/src go1.17 io/Copy().(ioscip-go gomod github.com/golang/go/src go1.17 io/.Discardscip-go gomod github.com/golang/go/src go1.17 io/Discard., r) |
| 45 | if err != nil { |
| 46 | _ = logger.Logscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/Logger#Log.("msg", "failed to exhaust reader, performance may be impeded", "err", err) |
| 47 | } |
| 48 | |
| 49 | Doscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/Do().(logger, r.Closescip-go gomod github.com/golang/go/src go1.17 io/Closer#Close()., format, a...) |
| 50 | } |