From 973f6411f4a59dcf21a8151d21578775deb340e0 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 3 Jan 2024 22:10:13 -0600 Subject: [PATCH] more learning/debugging code Signed-off-by: Jeff Carr --- args.go | 2 +- chan.go | 124 +++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 17 +++++++ go.sum | 31 +++++++++++++ logSettings.go | 10 ++-- mainWindow.go | 53 +++++++++++---------- 6 files changed, 208 insertions(+), 29 deletions(-) create mode 100644 chan.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/args.go b/args.go index 5ff1f58..65a2cb0 100644 --- a/args.go +++ b/args.go @@ -4,7 +4,7 @@ package debugger import ( arg "github.com/alexflint/go-arg" - log "go.wit.com/log" + "go.wit.com/log" ) var INFO log.LogFlag diff --git a/chan.go b/chan.go new file mode 100644 index 0000000..0956c48 --- /dev/null +++ b/chan.go @@ -0,0 +1,124 @@ +package debugger + +// channel communication to the plugins +// https://github.com/sourcegraph/conc +// https://www.reddit.com/r/golang/comments/11x1oek/hello_gophers_show_me_your_concurrent_code/ + +import ( + // "regexp" + // "go.wit.com/gui/toolkit" + "sync" + "runtime" + "github.com/sourcegraph/conc" + "github.com/sourcegraph/conc/stream" + "github.com/sourcegraph/conc/panics" + + "go.wit.com/log" +) + +// this should never exit +// TODO: clean up all this poorly named code +func makeConc() { + var wg conc.WaitGroup + defer wg.Wait() + + startTheThing(&wg) + log.Warn("panic?") + log.Sleep(2) + log.Warn("panic? after sleep(5)") +} + +func startTheThing(wg *conc.WaitGroup) { + f := func() { + log.Warn("startTheThing() == about to panic now") + panic("test conc.WaitGroup") + } + wg.Go(func() { + ExampleCatcher(f) + }) +} + +func ExampleCatcher(f func()) { + var pc panics.Catcher + i := 0 + pc.Try(func() { i += 1 }) + pc.Try(f) + pc.Try(func() { i += 1 }) + + recovered := pc.Recovered() + + log.Warn("panic.Recovered():", recovered.Value.(string)) + frames := runtime.CallersFrames(recovered.Callers) + for { + frame, more := frames.Next() + log.Warn("\t", frame.Function) + + if !more { + break + } + } +} + +func mapStream( + in chan int, + out chan int, + f func(int) int, +) { + tasks := make(chan func()) + taskResults := make(chan chan int) + + // Worker goroutines + var workerWg sync.WaitGroup + for i := 0; i < 10; i++ { + workerWg.Add(1) + go func() { + defer workerWg.Done() + for task := range tasks { + task() + } + }() + } + + // Ordered reader goroutines + var readerWg sync.WaitGroup + readerWg.Add(1) + go func() { + defer readerWg.Done() + for result := range taskResults { + item := <-result + out <- item + } + }() + + // Feed the workers with tasks + for elem := range in { + resultCh := make(chan int, 1) + taskResults <- resultCh + tasks <- func() { + resultCh <- f(elem) + } + } + + // We've exhausted input. + // Wait for everything to finish + close(tasks) + workerWg.Wait() + close(taskResults) + readerWg.Wait() +} + +func mapStream2( + in chan int, + out chan int, + f func(int) int, +) { + s := stream.New().WithMaxGoroutines(10) + for elem := range in { + elem := elem + s.Go(func() stream.Callback { + res := f(elem) + return func() { out <- res } + }) + } + s.Wait() +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9ff4f2d --- /dev/null +++ b/go.mod @@ -0,0 +1,17 @@ +module go.wit.com/gui/debugger + +go 1.21.4 + +require ( + github.com/alexflint/go-arg v1.4.3 + go.wit.com/gui/gui v0.9.8 + go.wit.com/log v0.0.0-20240102010317-907893ba7b4b +) + +require ( + github.com/alexflint/go-scalar v1.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + go.uber.org/atomic v1.7.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + go.wit.com/spew v0.0.0-20240101141411-c7b8e91573c9 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6608ee8 --- /dev/null +++ b/go.sum @@ -0,0 +1,31 @@ +github.com/alexflint/go-arg v1.4.3 h1:9rwwEBpMXfKQKceuZfYcwuc/7YY7tWJbFsgG5cAU/uo= +github.com/alexflint/go-arg v1.4.3/go.mod h1:3PZ/wp/8HuqRZMUUgu7I+e1qcpUbvmS258mRXkFH4IA= +github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4PnltbhM= +github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= +go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +go.wit.com/gui/gui v0.9.8 h1:oMqM4sfucMnZxh9e2F0DKxNTuVxl2JZGXcuTRnGW+xI= +go.wit.com/gui/gui v0.9.8/go.mod h1:H2+uDT6qoQ8UkV6QUNIC1MQsgy6/aAop0zWBHnwACso= +go.wit.com/log v0.0.0-20240102010317-907893ba7b4b h1:YqDB6AChqjmt5jYN4F79UrjIDoUt58pfCgXJwp+G2wg= +go.wit.com/log v0.0.0-20240102010317-907893ba7b4b/go.mod h1:GmsggfsKrqdZdAj26fEOlcTz6qEIazbV33uyuuktvB8= +go.wit.com/spew v0.0.0-20240101141411-c7b8e91573c9 h1:UEX2EzLQPzLTfy/kUFQD7OXtvKn8wk/+jpDOkbl4ff4= +go.wit.com/spew v0.0.0-20240101141411-c7b8e91573c9/go.mod h1:qBpgJXThMMT15vym7/E4Ur9y8oOo2nP7t2RP52QHUNw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/logSettings.go b/logSettings.go index 898de90..fee9bf4 100644 --- a/logSettings.go +++ b/logSettings.go @@ -21,7 +21,8 @@ type LogSettings struct { } func (ls *LogSettings) Set(b bool) { - log.Set(ls.name, b) + // log.Set(ls.name, b) + log.Warn("log.Set() FIXME: not working here anymore") ls.checkbox.Set(b) } @@ -33,9 +34,10 @@ func NewLogFlag(p *gui.Node, name string) *LogSettings { ls.checkbox = p.NewCheckbox(name) ls.label = p.NewLabel("Enable log." + name) - ls.checkbox.Set(log.Get(name)) + // ls.checkbox.Set(log.Get(name)) ls.checkbox.Custom = func() { - log.Set(name, ls.checkbox.B) + // log.Set(name, ls.checkbox.B) + log.Warn("log.Set() FIXME: not working here anymore") } return ls } @@ -89,8 +91,10 @@ func DebugFlags(n *gui.Node) { g = newB.NewGroup("List") g = g.NewGrid("flags grid", 2, 2) + /* logGadgets["INFO"] = NewLogFlag(g, "INFO") logGadgets["WARN"] = NewLogFlag(g, "WARN") logGadgets["SPEW"] = NewLogFlag(g, "SPEW") logGadgets["ERROR"] = NewLogFlag(g, "ERROR") + */ } diff --git a/mainWindow.go b/mainWindow.go index 8f688c8..460b76b 100644 --- a/mainWindow.go +++ b/mainWindow.go @@ -23,7 +23,7 @@ func DebugWindow(p *gui.Node) { } func DebugWindow2(n *gui.Node, title string) *gui.Node { - var newW, newB, gog, g1 *gui.Node + var newW, newB, gr *gui.Node // var logSettings *gadgets.LogSettings // time.Sleep(1 * time.Second) @@ -32,56 +32,50 @@ func DebugWindow2(n *gui.Node, title string) *gui.Node { newB = newW.NewBox("hBox", true) //////////////////////// main debug things ////////////////////////////////// - gog = newB.NewGroup("Debugging Windows:") + gr = newB.NewGroup("Debugging Windows:") - gog.NewButton("logging", func () { + gr.NewButton("logging", func () { DebugFlags(myGui) }) - gog.NewButton("Debug Widgets", func () { + gr.NewButton("Debug Widgets", func () { DebugWidgetWindow(myGui) }) - gog.NewButton("GO Language Internals", func () { - DebugGolangWindow(bugWin) - }) - gog.NewButton("GO Channels debug", func () { - DebugGoChannels(bugWin) - }) - gog.NewLabel("Force Quit:") + gr.NewLabel("Force Quit:") - gog.NewButton("os.Exit()", func () { + gr.NewButton("os.Exit()", func () { os.Exit(0) }) //////////////////////// window debugging things ////////////////////////////////// - g1 = newB.NewGroup("list things") + gr = newB.NewGroup("list things") - g1.NewButton("List toolkits", func () { - dropdownWindow(g1) + gr.NewButton("List toolkits", func () { + dropdownWindow(gr) bugWin.ListToolkits() }) - g1.NewButton("List Windows", func () { - dropdownWindow(g1) + gr.NewButton("List Windows", func () { + dropdownWindow(gr) }) - g1.NewButton("List Window Widgets", func () { - dropdownWindowWidgets(g1) + gr.NewButton("List Window Widgets", func () { + dropdownWindowWidgets(gr) }) - g2 := newB.NewGroup("more things") + gr = newB.NewGroup("more things") - g2.NewButton("Node.ListChildren(true)", func () { + gr.NewButton("Node.ListChildren(true)", func () { if (activeWidget == nil) { activeWidget = bugWin } activeWidget.ListChildren(true) }) - g2.NewButton("test conc", func () { + gr.NewButton("test conc", func () { log.Log(true, "TODO: fix me") // makeConc() }) - g2.NewButton("List Plugins", func () { + gr.NewButton("List Plugins", func () { log.Log(true, "TODO: fix me") /* for _, aplug := range allPlugins { @@ -90,14 +84,23 @@ func DebugWindow2(n *gui.Node, title string) *gui.Node { */ }) - g2.NewButton("load toolkit 'gocui'", func () { + gr.NewButton("load toolkit 'gocui'", func () { bugWin.LoadToolkit("gocui") }) - g2.NewButton("load toolkit 'andlabs'", func () { + gr.NewButton("load toolkit 'andlabs'", func () { bugWin.LoadToolkit("andlabs") }) + gr = newB.NewGroup("Learn GO") + + gr.NewButton("GO Language Internals", func () { + DebugGolangWindow(myGui) + }) + gr.NewButton("GO Channels debug", func () { + DebugGoChannels(myGui) + }) + return newB }