package main

import (
//	"bufio"
	"log"
	"os"
	"runtime"
	"runtime/debug"
	"runtime/pprof"
)
import "git.wit.org/wit/gui"

func golangDebug(w *gui.Node) {
	if (w == nil) {
		gui.Config.Title = "golangDebug Window"
		gui.Config.Width = 100
		gui.Config.Height = 100
		gui.Config.Exit = customExit
		w = gui.NewWindow()
	}

	tab := w.AddTab("golangDebug()", nil)

	///////////////////////////////  Column DEBUG GOLANG   //////////////////////
	gNode := tab.AddGroup("golang")

	gNode.AddButton("runtime.Stack() dump", func (*gui.Node) {
		log.Println("\tSTART")
		buf := make([]byte, 1<<16)
		runtime.Stack(buf, true)
		log.Printf("%s", buf)
		log.Println("\tEND")
	})
	gNode.AddButton("dumpModuleInfo", func (*gui.Node) {
		log.Println("\tSTART")
		dumpModuleInfo()
		log.Println("\tEND")
	})
	gNode.AddButton("debug.PrintStack", func (*gui.Node) {
		log.Println("\tSTART")
		debug.PrintStack()
		log.Println("\tEND")
	})
	gNode.AddButton("pprof.Lookup goroutine", func (*gui.Node) {
		log.Println("\tSTART")
		pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
		log.Println("\tEND")
	})
	gNode.AddButton("pprof.Lookup", func (*gui.Node) {
		log.Println("\tSTART")
		pprof.Lookup("heap").WriteTo(os.Stdout, 1)
		log.Println("\tEND")
	})
	gNode.AddButton("pprof.Lookup block", func (*gui.Node) {
		log.Println("\tSTART")
		pprof.Lookup("block").WriteTo(os.Stdout, 1)
		log.Println("\tEND")
	})
	gNode.AddButton("pprof.Lookup threadcreate", func (*gui.Node) {
		log.Println("\tSTART")
		pprof.Lookup("threadcreate").WriteTo(os.Stdout, 1)
		log.Println("\tEND")
	})
	gNode.AddButton("runtime.ReadMemStats", func (*gui.Node) {
		var s runtime.MemStats
		runtime.ReadMemStats(&s)
		log.Printf("alloc: %v bytes\n", s.Alloc)
		log.Printf("total-alloc: %v bytes\n", s.TotalAlloc)
		log.Printf("sys: %v bytes\n", s.Sys)
		log.Printf("lookups: %v\n", s.Lookups)
		log.Printf("mallocs: %v\n", s.Mallocs)
		log.Printf("frees: %v\n", s.Frees)
		log.Printf("heap-alloc: %v bytes\n", s.HeapAlloc)
		log.Printf("heap-sys: %v bytes\n", s.HeapSys)
		log.Printf("heap-idle: %v bytes\n", s.HeapIdle)
		log.Printf("heap-in-use: %v bytes\n", s.HeapInuse)
		log.Printf("heap-released: %v bytes\n", s.HeapReleased)
		log.Printf("heap-objects: %v\n", s.HeapObjects)
		log.Printf("stack-in-use: %v bytes\n", s.StackInuse)
		log.Printf("stack-sys: %v bytes\n", s.StackSys)
		log.Printf("next-gc: when heap-alloc >= %v bytes\n", s.NextGC)
		log.Printf("last-gc: %v ns\n", s.LastGC)
		log.Printf("gc-pause: %v ns\n", s.PauseTotalNs)
		log.Printf("num-gc: %v\n", s.NumGC)
		log.Printf("enable-gc: %v\n", s.EnableGC)
		log.Printf("debug-gc: %v\n", s.DebugGC)
	})
}
