package argvpb // debug argv protobuf functions // enable with --autodebug or Setenv( "AUTOCOMPLETE_VERBOSE" ) import ( "fmt" "os" "strings" "go.wit.com/lib/cobol" ) func (pb *Argv) Debugf(fmts string, parts ...any) { Debugf(fmts, parts...) } // decides here to print to STDERR or not func Debugf(fmts string, parts ...any) { fmts = strings.TrimSpace(fmts) fmts += "\n" // me.pb.Stderr += fmt.Sprintf(fmts, parts...) fmt.Fprintf(Stddbg, fmts, parts...) } // print out auto complete debugging info func (pb *Argv) PrintDebug(last string) { pb.PrintDebugNew("ARGV", me.last) } func (pb *Argv) PrintDebugNew(msg string, last *Argv) { cmd := fmt.Sprintf("cmd='%s'", pb.GetCmd()) // var arglast string // arglast = fmt.Sprintf("last='%s'", last.GetCmd()) partial := fmt.Sprintf("p='%s'", pb.Partial) age := cobol.Duration(pb.Ctime) dur := cobol.Duration(pb.Duration) var dbg string if me.debug { dbg = "dbg=1" } else { dbg = "dbg=0" } sargv := fmt.Sprintf("argv(%v)", pb.Real) fast := fmt.Sprintf("F%-2dout%-2derr%-2d", pb.Fast, pb.OutCounter, pb.ErrCounter) top := fmt.Sprintf("%-4.4s age=(%s)dur(%s) %s %s %-12.12s", pb.Uuid, age, dur, dbg, fast, cmd) Debugf("%s: %s %-12.12s %s cmd=%s real='%v' len(%d)", msg, top, partial, pb.GetCmd(), sargv, pb.Real, me.all.Len()) } func (all *Argvs) PrintHistory(msg string) { counter := 0 var last *Argv // for pb := range all.Argv[0:10] { for pb := range all.IterAll() { counter += 1 pb.PrintDebugNew(msg, last) last = pb if counter > 30 { // sometimes I'm dumb break } } } func PrintStddbg() { if !me.debug { return } f, err := os.OpenFile("/tmp/argv.debug", os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644) if err != nil { panic("WTF openfile") } defer f.Close() lines := strings.Split(strings.TrimSpace(PB.Stddbg), "\n") if len(lines) == 0 { return } fmt.Fprintf(f, "\n") for _, line := range lines { fmt.Fprintf(f, "%s\n", line) } fmt.Fprintf(f, "\n") } func PrintStderr() { var lines []string for _, line := range strings.Split(strings.TrimSpace(PB.Stderr), "\n") { if strings.TrimSpace(line) == "" { continue } lines = append(lines, line) } if len(lines) == 0 { return } fmt.Fprintf(os.Stderr, "\n") for _, line := range lines { // line = strings.TrimSpace(line) // if line == "" { // continue // } fmt.Fprintf(os.Stderr, "%s\n", line) } fmt.Fprintf(os.Stderr, "\n") } func PrintStdout() { line := strings.TrimSpace(PB.Stdout) if line == "" { return } fmt.Fprintf(os.Stdout, " %s\n", line) }