You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.0 KiB

4 years ago
package output
import (
"context"
"fmt"
"io"
"os"
4 years ago
4 years ago
"gitoa.ru/go-4devs/console/output/label"
"gitoa.ru/go-4devs/console/output/verbosity"
4 years ago
)
func writeError(_ int, err error) {
if err != nil {
fmt.Fprint(os.Stderr, err)
}
}
4 years ago
type Output func(ctx context.Context, verb verbosity.Verbosity, msg string, args ...label.KeyValue) (int, error)
4 years ago
func (o Output) Print(ctx context.Context, args ...interface{}) {
writeError(o(ctx, verbosity.Norm, fmt.Sprint(args...)))
4 years ago
}
4 years ago
func (o Output) PrintKV(ctx context.Context, msg string, kv ...label.KeyValue) {
writeError(o(ctx, verbosity.Norm, msg, kv...))
4 years ago
}
func (o Output) Printf(ctx context.Context, format string, args ...interface{}) {
writeError(o(ctx, verbosity.Norm, fmt.Sprintf(format, args...)))
4 years ago
}
func (o Output) Println(ctx context.Context, args ...interface{}) {
writeError(o(ctx, verbosity.Norm, fmt.Sprintln(args...)))
4 years ago
}
func (o Output) Info(ctx context.Context, args ...interface{}) {
writeError(o(ctx, verbosity.Info, fmt.Sprint(args...)))
4 years ago
}
4 years ago
func (o Output) InfoKV(ctx context.Context, msg string, kv ...label.KeyValue) {
writeError(o(ctx, verbosity.Info, msg, kv...))
4 years ago
}
func (o Output) Debug(ctx context.Context, args ...interface{}) {
writeError(o(ctx, verbosity.Debug, fmt.Sprint(args...)))
4 years ago
}
4 years ago
func (o Output) DebugKV(ctx context.Context, msg string, kv ...label.KeyValue) {
writeError(o(ctx, verbosity.Debug, msg, kv...))
4 years ago
}
func (o Output) Trace(ctx context.Context, args ...interface{}) {
writeError(o(ctx, verbosity.Trace, fmt.Sprint(args...)))
4 years ago
}
4 years ago
func (o Output) TraceKV(ctx context.Context, msg string, kv ...label.KeyValue) {
writeError(o(ctx, verbosity.Trace, msg, kv...))
4 years ago
}
func (o Output) Write(b []byte) (int, error) {
return o(context.Background(), verbosity.Norm, string(b))
4 years ago
}
func (o Output) Writer(ctx context.Context, verb verbosity.Verbosity) io.Writer {
return verbosityWriter(func(b []byte) (int, error) {
return o(ctx, verb, string(b))
})
4 years ago
}
type verbosityWriter func(b []byte) (int, error)
4 years ago
func (w verbosityWriter) Write(b []byte) (int, error) {
return w(b)
4 years ago
}