package argvpb // debug argv protobuf functions // enable with --autodebug or Setenv( "AUTOCOMPLETE_VERBOSE" ) import ( "fmt" "os" "strings" "go.wit.com/lib/cobol" "go.wit.com/lib/config" ) 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" // 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) { // var arglast string // arglast = fmt.Sprintf("last='%s'", last.GetCmd()) partial := fmt.Sprintf("p='%s'", pb.Partial) age := cobol.Duration(pb.Ctime) pdur := cobol.Duration(pb.Duration) argvdur := cobol.Duration(pb.ArgvDuration) var dbg string if me.debug { dbg = "dbg=1" } else { dbg = "dbg=0" } lens := fmt.Sprintf("len=%-2d,%-2d,%-2d", me.all.Len(), len(strings.Fields(pb.Stdout)), len(strings.Split(pb.Stderr, "\n"))) fast := fmt.Sprintf("F%-2dout%-2derr%-2d %s", pb.Fast, pb.OutCounter, pb.ErrCounter, lens) top := fmt.Sprintf("%-4.4s age=(%s)(%s)dur(%s) %s %s cmd=%-12.12s", pb.Uuid, age, pdur, argvdur, dbg, fast, pb.GetCmd()) debugf("%s: %s %-12.12s real='%v'", msg, top, partial, pb.Real) } 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 } if config.Exists("/tmp/argv.debug") { printStddbgFile() return } lines := strings.Split(strings.TrimSpace(PB.Stddbg), "\n") if len(lines) == 0 { return } fmt.Fprintf(os.Stderr, "\n") for _, line := range lines { fmt.Fprintf(os.Stderr, "%s\n", line) } fmt.Fprintf(os.Stderr, "\n") } func printStddbgFile() { if !me.debug { return } f, err := os.OpenFile("/tmp/argv.debug", os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644) if err != nil { return } 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) }