add error handler
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Verbosity int
|
||||
@@ -16,46 +17,50 @@ const (
|
||||
VerbosityTrace
|
||||
)
|
||||
|
||||
func writeError(n int, err error) {
|
||||
fmt.Fprint(os.Stderr, err)
|
||||
}
|
||||
|
||||
type Output func(ctx context.Context, verb Verbosity, msg string, args ...KeyValue) (int, error)
|
||||
|
||||
func (o Output) Print(ctx context.Context, args ...interface{}) {
|
||||
o(ctx, VerbosityNorm, fmt.Sprint(args...))
|
||||
writeError(o(ctx, VerbosityNorm, fmt.Sprint(args...)))
|
||||
}
|
||||
|
||||
func (o Output) PrintKV(ctx context.Context, msg string, kv ...KeyValue) {
|
||||
o(ctx, VerbosityNorm, msg, kv...)
|
||||
writeError(o(ctx, VerbosityNorm, msg, kv...))
|
||||
}
|
||||
|
||||
func (o Output) Printf(ctx context.Context, format string, args ...interface{}) {
|
||||
o(ctx, VerbosityNorm, fmt.Sprintf(format, args...))
|
||||
writeError(o(ctx, VerbosityNorm, fmt.Sprintf(format, args...)))
|
||||
}
|
||||
|
||||
func (o Output) Println(ctx context.Context, args ...interface{}) {
|
||||
o(ctx, VerbosityNorm, fmt.Sprintln(args...))
|
||||
writeError(o(ctx, VerbosityNorm, fmt.Sprintln(args...)))
|
||||
}
|
||||
|
||||
func (o Output) Info(ctx context.Context, args ...interface{}) {
|
||||
o(ctx, VerbosityInfo, fmt.Sprint(args...))
|
||||
writeError(o(ctx, VerbosityInfo, fmt.Sprint(args...)))
|
||||
}
|
||||
|
||||
func (o Output) InfoKV(ctx context.Context, msg string, kv ...KeyValue) {
|
||||
o(ctx, VerbosityInfo, msg, kv...)
|
||||
writeError(o(ctx, VerbosityInfo, msg, kv...))
|
||||
}
|
||||
|
||||
func (o Output) Debug(ctx context.Context, args ...interface{}) {
|
||||
o(ctx, VerbosityDebug, fmt.Sprint(args...))
|
||||
writeError(o(ctx, VerbosityDebug, fmt.Sprint(args...)))
|
||||
}
|
||||
|
||||
func (o Output) DebugKV(ctx context.Context, msg string, kv ...KeyValue) {
|
||||
o(ctx, VerbosityDebug, msg, kv...)
|
||||
writeError(o(ctx, VerbosityDebug, msg, kv...))
|
||||
}
|
||||
|
||||
func (o Output) Trace(ctx context.Context, args ...interface{}) {
|
||||
o(ctx, VerbosityTrace, fmt.Sprint(args...))
|
||||
writeError(o(ctx, VerbosityTrace, fmt.Sprint(args...)))
|
||||
}
|
||||
|
||||
func (o Output) TraceKV(ctx context.Context, msg string, kv ...KeyValue) {
|
||||
o(ctx, VerbosityTrace, msg, kv...)
|
||||
writeError(o(ctx, VerbosityTrace, msg, kv...))
|
||||
}
|
||||
|
||||
func (o Output) Write(b []byte) (int, error) {
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
)
|
||||
|
||||
const newline = "\n"
|
||||
|
||||
func Stderr() output.Output {
|
||||
return New(os.Stderr, String)
|
||||
}
|
||||
@@ -25,13 +27,12 @@ func Buffer(buf *bytes.Buffer) output.Output {
|
||||
|
||||
func String(_ output.Verbosity, msg string, kv ...output.KeyValue) string {
|
||||
if len(kv) > 0 {
|
||||
newline := ""
|
||||
if msg[len(msg)-1:] == "\n" {
|
||||
newline = "\n"
|
||||
nline := ""
|
||||
if msg[len(msg)-1:] == newline {
|
||||
nline = newline
|
||||
}
|
||||
|
||||
return "msg=\"" + strings.TrimSpace(msg) + "\", " + output.KeyValues(kv).String() + newline
|
||||
|
||||
return "msg=\"" + strings.TrimSpace(msg) + "\", " + output.KeyValues(kv).String() + nline
|
||||
}
|
||||
|
||||
return msg
|
||||
|
||||
Reference in New Issue
Block a user