From 321fe31260aebde95ec75defa625967893cd8860 Mon Sep 17 00:00:00 2001 From: Nao Yonashiro Date: Fri, 25 Mar 2022 05:09:22 +0900 Subject: [PATCH] feat: add DebugWith option --- encode.go | 2 ++ encode_test.go | 3 ++- internal/encoder/option.go | 6 +++++- internal/encoder/vm/debug_vm.go | 21 ++++++++++---------- internal/encoder/vm_color/debug_vm.go | 21 ++++++++++---------- internal/encoder/vm_color_indent/debug_vm.go | 21 ++++++++++---------- internal/encoder/vm_indent/debug_vm.go | 21 ++++++++++---------- option.go | 9 +++++++++ 8 files changed, 62 insertions(+), 42 deletions(-) diff --git a/encode.go b/encode.go index c9527c0e..4bd899f3 100644 --- a/encode.go +++ b/encode.go @@ -3,6 +3,7 @@ package json import ( "context" "io" + "os" "unsafe" "github.com/goccy/go-json/internal/encoder" @@ -62,6 +63,7 @@ func (e *Encoder) encodeWithOption(ctx *encoder.RuntimeContext, v interface{}, o ctx.Option.Flag |= encoder.HTMLEscapeOption } ctx.Option.Flag |= encoder.NormalizeUTF8Option + ctx.Option.DebugOut = os.Stdout for _, optFunc := range optFuncs { optFunc(ctx.Option) } diff --git a/encode_test.go b/encode_test.go index 469b6551..92f43043 100644 --- a/encode_test.go +++ b/encode_test.go @@ -477,7 +477,8 @@ func TestDebugMode(t *testing.T) { t.Fatal("expected error") } }() - json.MarshalWithOption(mustErrTypeForDebug{}, json.Debug()) + var buf bytes.Buffer + json.MarshalWithOption(mustErrTypeForDebug{}, json.Debug(), json.DebugWith(&buf)) } func TestIssue116(t *testing.T) { diff --git a/internal/encoder/option.go b/internal/encoder/option.go index dcec8f20..82d5ce3e 100644 --- a/internal/encoder/option.go +++ b/internal/encoder/option.go @@ -1,6 +1,9 @@ package encoder -import "context" +import ( + "context" + "io" +) type OptionFlag uint8 @@ -19,6 +22,7 @@ type Option struct { Flag OptionFlag ColorScheme *ColorScheme Context context.Context + DebugOut io.Writer } type EncodeFormat struct { diff --git a/internal/encoder/vm/debug_vm.go b/internal/encoder/vm/debug_vm.go index 05509fed..fbbc0de4 100644 --- a/internal/encoder/vm/debug_vm.go +++ b/internal/encoder/vm/debug_vm.go @@ -16,16 +16,17 @@ func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) } if err := recover(); err != nil { - fmt.Println("=============[DEBUG]===============") - fmt.Println("* [TYPE]") - fmt.Println(codeSet.Type) - fmt.Printf("\n") - fmt.Println("* [ALL OPCODE]") - fmt.Println(code.Dump()) - fmt.Printf("\n") - fmt.Println("* [CONTEXT]") - fmt.Printf("%+v\n", ctx) - fmt.Println("===================================") + w := ctx.Option.DebugOut + fmt.Fprintln(w, "=============[DEBUG]===============") + fmt.Fprintln(w, "* [TYPE]") + fmt.Fprintln(w, codeSet.Type) + fmt.Fprintf(w, "\n") + fmt.Fprintln(w, "* [ALL OPCODE]") + fmt.Fprintln(w, code.Dump()) + fmt.Fprintf(w, "\n") + fmt.Fprintln(w, "* [CONTEXT]") + fmt.Fprintf(w, "%+v\n", ctx) + fmt.Fprintln(w, "===================================") panic(err) } }() diff --git a/internal/encoder/vm_color/debug_vm.go b/internal/encoder/vm_color/debug_vm.go index 6a6a33d2..925f61ed 100644 --- a/internal/encoder/vm_color/debug_vm.go +++ b/internal/encoder/vm_color/debug_vm.go @@ -16,16 +16,17 @@ func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) defer func() { if err := recover(); err != nil { - fmt.Println("=============[DEBUG]===============") - fmt.Println("* [TYPE]") - fmt.Println(codeSet.Type) - fmt.Printf("\n") - fmt.Println("* [ALL OPCODE]") - fmt.Println(code.Dump()) - fmt.Printf("\n") - fmt.Println("* [CONTEXT]") - fmt.Printf("%+v\n", ctx) - fmt.Println("===================================") + w := ctx.Option.DebugOut + fmt.Fprintln(w, "=============[DEBUG]===============") + fmt.Fprintln(w, "* [TYPE]") + fmt.Fprintln(w, codeSet.Type) + fmt.Fprintf(w, "\n") + fmt.Fprintln(w, "* [ALL OPCODE]") + fmt.Fprintln(w, code.Dump()) + fmt.Fprintf(w, "\n") + fmt.Fprintln(w, "* [CONTEXT]") + fmt.Fprintf(w, "%+v\n", ctx) + fmt.Fprintln(w, "===================================") panic(err) } }() diff --git a/internal/encoder/vm_color_indent/debug_vm.go b/internal/encoder/vm_color_indent/debug_vm.go index a68bbf6b..dd4cd489 100644 --- a/internal/encoder/vm_color_indent/debug_vm.go +++ b/internal/encoder/vm_color_indent/debug_vm.go @@ -16,16 +16,17 @@ func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) defer func() { if err := recover(); err != nil { - fmt.Println("=============[DEBUG]===============") - fmt.Println("* [TYPE]") - fmt.Println(codeSet.Type) - fmt.Printf("\n") - fmt.Println("* [ALL OPCODE]") - fmt.Println(code.Dump()) - fmt.Printf("\n") - fmt.Println("* [CONTEXT]") - fmt.Printf("%+v\n", ctx) - fmt.Println("===================================") + w := ctx.Option.DebugOut + fmt.Fprintln(w, "=============[DEBUG]===============") + fmt.Fprintln(w, "* [TYPE]") + fmt.Fprintln(w, codeSet.Type) + fmt.Fprintf(w, "\n") + fmt.Fprintln(w, "* [ALL OPCODE]") + fmt.Fprintln(w, code.Dump()) + fmt.Fprintf(w, "\n") + fmt.Fprintln(w, "* [CONTEXT]") + fmt.Fprintf(w, "%+v\n", ctx) + fmt.Fprintln(w, "===================================") panic(err) } }() diff --git a/internal/encoder/vm_indent/debug_vm.go b/internal/encoder/vm_indent/debug_vm.go index 4cfd17ab..99395388 100644 --- a/internal/encoder/vm_indent/debug_vm.go +++ b/internal/encoder/vm_indent/debug_vm.go @@ -16,16 +16,17 @@ func DebugRun(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet) defer func() { if err := recover(); err != nil { - fmt.Println("=============[DEBUG]===============") - fmt.Println("* [TYPE]") - fmt.Println(codeSet.Type) - fmt.Printf("\n") - fmt.Println("* [ALL OPCODE]") - fmt.Println(code.Dump()) - fmt.Printf("\n") - fmt.Println("* [CONTEXT]") - fmt.Printf("%+v\n", ctx) - fmt.Println("===================================") + w := ctx.Option.DebugOut + fmt.Fprintln(w, "=============[DEBUG]===============") + fmt.Fprintln(w, "* [TYPE]") + fmt.Fprintln(w, codeSet.Type) + fmt.Fprintf(w, "\n") + fmt.Fprintln(w, "* [ALL OPCODE]") + fmt.Fprintln(w, code.Dump()) + fmt.Fprintf(w, "\n") + fmt.Fprintln(w, "* [CONTEXT]") + fmt.Fprintf(w, "%+v\n", ctx) + fmt.Fprintln(w, "===================================") panic(err) } }() diff --git a/option.go b/option.go index 0138d777..af400a45 100644 --- a/option.go +++ b/option.go @@ -1,6 +1,8 @@ package json import ( + "io" + "github.com/goccy/go-json/internal/decoder" "github.com/goccy/go-json/internal/encoder" ) @@ -39,6 +41,13 @@ func Debug() EncodeOptionFunc { } } +// DebugWith sets the destination to write debug messages. +func DebugWith(w io.Writer) EncodeOptionFunc { + return func(opt *EncodeOption) { + opt.DebugOut = w + } +} + // Colorize add an identifier for coloring to the string of the encoded result. func Colorize(scheme *ColorScheme) EncodeOptionFunc { return func(opt *EncodeOption) {