From ecf122eaa4b618dcf9f30a874a0e42b8f381eacf Mon Sep 17 00:00:00 2001 From: Robert Krimen Date: Tue, 16 Apr 2013 19:06:39 -0700 Subject: [PATCH] Update dbg --- dbg.go | 4 +++- dbg/dbg.go | 40 +++++++++++++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/dbg.go b/dbg.go index 7a7dc1d..51fbdc2 100644 --- a/dbg.go +++ b/dbg.go @@ -1,7 +1,9 @@ +// This file was AUTOMATICALLY GENERATED by dbg-import (smuggol) for github.com/robertkrimen/dbg + package otto import ( - Dbg "github.com/robertkrimen/otto/dbg" + Dbg "github.com/robertkrimen/otto/dbg" ) var dbg, dbgf = Dbg.New() diff --git a/dbg/dbg.go b/dbg/dbg.go index ee5f64d..83bf6c5 100644 --- a/dbg/dbg.go +++ b/dbg/dbg.go @@ -1,3 +1,5 @@ +// This file was AUTOMATICALLY GENERATED by dbg-import (smuggol) from github.com/robertkrimen/dbg + /* Package dbg is a println/printf/log-debugging utility library. @@ -72,11 +74,12 @@ type _frmt struct { operandCount int panic bool fatal bool + check bool } var ( ctlTest = regexp.MustCompile(`^\s*%/`) - ctlScan = regexp.MustCompile(`%?/(panic|fatal)(?:\s|$)`) + ctlScan = regexp.MustCompile(`%?/(panic|fatal|check)(?:\s|$)`) ) func operandCount(format string) int { @@ -115,6 +118,8 @@ func parseFormat(format string) (frmt _frmt) { frmt.panic = true case "fatal": frmt.fatal = true + case "check": + frmt.check = true } } } @@ -179,6 +184,7 @@ func (self Dbgr) DbgDbgf() (dbg DbgFunction, dbgf DbgFunction) { } func (self Dbgr) dbgf(values ...interface{}) { + var frmt _frmt if len(values) > 0 { tmp := fmt.Sprint(values[0]) @@ -214,35 +220,55 @@ func (self Dbgr) dbgf(values ...interface{}) { at++ } + //values_f := append([]interface{}{}, values[0:frmt.operandCount]...) values_f := values[0:frmt.operandCount] values_dbg := values[frmt.operandCount:] if len(values_dbg) > 0 { // Adjust frmt.format: + // (%v instead of %s because: frmt.check) { tmp := format if len(tmp) > 0 { if unicode.IsSpace(rune(tmp[len(tmp)-1])) { - buffer_f.WriteString("%s") + buffer_f.WriteString("%v") } else { - buffer_f.WriteString(" %s") + buffer_f.WriteString(" %v") } + } else if frmt.check { + // Performing a check, so no output } else { - buffer_f.WriteString("%s") + buffer_f.WriteString("%v") } } - // Adjust vl_f: - { + // Adjust values_f: + if !frmt.check { tmp := []string{} for _, value := range values_dbg { tmp = append(tmp, fmt.Sprintf("%v", value)) } + // First, make a copy of values_f, so we avoid overwriting values_dbg when appending + values_f = append([]interface{}{}, values_f...) values_f = append(values_f, strings.Join(tmp, " ")) } } format = buffer_f.String() - self.getEmit().emit(frmt, format, values_f...) + if frmt.check { + // We do not actually emit to the log, but panic if + // a non-nil value is detected (e.g. a non-nil error) + for _, value := range values_dbg { + if value != nil { + if format == "" { + panic(value) + } else { + panic(fmt.Sprintf(format, append(values_f, value)...)) + } + } + } + } else { + self.getEmit().emit(frmt, format, values_f...) + } } // Idiot-proof &Dbgr{}, etc.