Debian Codegraph @

dbg/dbg.go

raw symbols SCIP
1 // This file was AUTOMATICALLY GENERATED by dbg-import (smuggol) from github.com/robertkrimen/dbg
2
3 /*
4 Package dbg is a println/printf/log-debugging utility library.
5
6 import (
7 Dbg "github.com/robertkrimen/dbg"
8 )
9
10 dbg, dbgf := Dbg.New()
11
12 dbg("Emit some debug stuff", []byte{120, 121, 122, 122, 121}, math.Pi)
13 # "2013/01/28 16:50:03 Emit some debug stuff [120 121 122 122 121] 3.141592653589793"
14
15 dbgf("With a %s formatting %.2f", "little", math.Pi)
16 # "2013/01/28 16:51:55 With a little formatting (3.14)"
17
18 dbgf("%/fatal//A fatal debug statement: should not be here")
19 # "A fatal debug statement: should not be here"
20 # ...and then, os.Exit(1)
21
22 dbgf("%/panic//Can also panic %s", "this")
23 # "Can also panic this"
24 # ...as a panic, equivalent to: panic("Can also panic this")
25
26 dbgf("Any %s arguments without a corresponding %%", "extra", "are treated like arguments to dbg()")
27 # "2013/01/28 17:14:40 Any extra arguments (without a corresponding %) are treated like arguments to dbg()"
28
29 dbgf("%d %d", 1, 2, 3, 4, 5)
30 # "2013/01/28 17:16:32 Another example: 1 2 3 4 5"
31
32 dbgf("%@: Include the function name for a little context (via %s)", "%@")
33 # "2013... github.com/robertkrimen/dbg.TestSynopsis: Include the function name for a little context (via %@)"
34
35 By default, dbg uses log (log.Println, log.Printf, log.Panic, etc.) for output.
36 However, you can also provide your own output destination by invoking dbg.New with
37 a customization function:
38
39 import (
40 "bytes"
41 Dbg "github.com/robertkrimen/dbg"
42 "os"
43 )
44
45 # dbg to os.Stderr
46 dbg, dbgf := Dbg.New(func(dbgr *Dbgr) {
47 dbgr.SetOutput(os.Stderr)
48 })
49
50 # A slightly contrived example:
51 var buffer bytes.Buffer
52 dbg, dbgf := New(func(dbgr *Dbgr) {
53 dbgr.SetOutput(&buffer)
54 })
55 */
56 package dbg
57
58 import (
59 "bytes"
60 "fmt"
61 "io"
62 "log"
63 "os"
64 "regexp"
65 goruntime "runtime"
66 "strings"
67 "unicode"
68 )
69
70 type _frmt struct {
71 ctl string
72 format string
73 operandCount int
74 panic bool
75 fatal bool
76 check bool
77 }
78
79 var (
80 ctlTest = regexp.MustCompile(`^\s*%/`)
81 ctlScan = regexp.MustCompile(`%?/(panic|fatal|check)(?:\s|$)`)
82 )
83
84 func operandCount(format string) int {
85 count := 0
86 end := len(format)
87 for at := 0; at < end; {
88 for at < end && format[at] != '%' {
89 at++
90 }
91 at++
92 if at < end {
93 if format[at] != '%' && format[at] != '@' {
94 count++
95 }
96 at++
97 }
98 }
99 return count
100 }
101
102 func parseFormat(format string) (frmt _frmt) {
103 if ctlTest.MatchString(format) {
104 format = strings.TrimLeftFunc(format, unicode.IsSpace)
105 index := strings.Index(format, "//")
106 if index != -1 {
107 frmt.ctl = format[0:index]
108 format = format[index+2:] // Skip the second slash via +2 (instead of +1)
109 } else {
110 frmt.ctl = format
111 format = ""
112 }
113 for _, tmp := range ctlScan.FindAllStringSubmatch(frmt.ctl, -1) {
114 for _, value := range tmp[1:] {
115 switch value {
116 case "panic":
117 frmt.panic = true
118 case "fatal":
119 frmt.fatal = true
120 case "check":
121 frmt.check = true
122 }
123 }
124 }
125 }
126 frmt.format = format
127 frmt.operandCount = operandCount(format)
128 return
129 }
130
131 type Dbgr struct {
132 emit emit
133 }
134
135 type DbgFunction func(values ...interface{})
136
137 func NewDbgr() *Dbgr {
138 return &Dbgr{}
139 }
140
141 /*
142 New will create and return a pair of debugging functions. You can customize where
143 they output to by passing in an (optional) customization function:
144
145 import (
146 Dbg "github.com/robertkrimen/dbg"
147 "os"
148 )
149
150 # dbg to os.Stderr
151 dbg, dbgf := Dbg.New(func(dbgr *Dbgr) {
152 dbgr.SetOutput(os.Stderr)
153 })
154 */
155 func New(options ...interface{}) (dbg DbgFunction, dbgf DbgFunction) {
156 dbgr := NewDbgr()
157 if len(options) > 0 {
158 if fn, ok := options[0].(func(*Dbgr)); ok {
159 fn(dbgr)
160 }
161 }
162 return dbgr.DbgDbgf()
163 }
164
165 func (d Dbgr) Dbg(values ...interface{}) {
166 d.getEmit().emit(_frmt{}, "", values...)
167 }
168
169 func (d Dbgr) Dbgf(values ...interface{}) {
170 d.dbgf(values...)
171 }
172
173 func (d Dbgr) DbgDbgf() (dbg DbgFunction, dbgf DbgFunction) {
174 dbg = func(vl ...interface{}) {
175 d.Dbg(vl...)
176 }
177 dbgf = func(vl ...interface{}) {
178 d.dbgf(vl...)
179 }
180 return dbg, dbgf // Redundant, but...
181 }
182
183 func (d Dbgr) dbgf(values ...interface{}) {
184 var frmt _frmt
185 if len(values) > 0 {
186 tmp := fmt.Sprint(values[0])
187 frmt = parseFormat(tmp)
188 values = values[1:]
189 }
190
191 buf := bytes.Buffer{}
192 format := frmt.format
193 end := len(format)
194 for at := 0; at < end; {
195 last := at
196 for at < end && format[at] != '%' {
197 at++
198 }
199 if at > last {
200 buf.WriteString(format[last:at])
201 }
202 if at >= end {
203 break
204 }
205 // format[at] == '%'
206 at++
207 // format[at] == ?
208 if format[at] == '@' {
209 depth := 2
210 pc, _, _, _ := goruntime.Caller(depth)
211 name := goruntime.FuncForPC(pc).Name()
212 buf.WriteString(name)
213 } else {
214 buf.WriteString(format[at-1 : at+1])
215 }
216 at++
217 }
218
219 //valuesF := append([]interface{}{}, values[0:frmt.operandCount]...)
220 valuesF := values[0:frmt.operandCount]
221 valuesDbg := values[frmt.operandCount:]
222 if len(valuesDbg) > 0 {
223 // Adjust frmt.format:
224 // (%v instead of %s because: frmt.check)
225 tmp := format
226 if len(tmp) > 0 {
227 if unicode.IsSpace(rune(tmp[len(tmp)-1])) {
228 buf.WriteString("%v")
229 } else {
230 buf.WriteString(" %v")
231 }
232 } else if frmt.check {
233 // Performing a check, so no output
234 } else {
235 buf.WriteString("%v")
236 }
237
238 // Adjust valuesF:
239 if !frmt.check {
240 tmp := []string{}
241 for _, value := range valuesDbg {
242 tmp = append(tmp, fmt.Sprintf("%v", value))
243 }
244 // First, make a copy of valuesF, so we avoid overwriting valuesDbg when appending
245 valuesF = append([]interface{}{}, valuesF...)
246 valuesF = append(valuesF, strings.Join(tmp, " "))
247 }
248 }
249
250 format = buf.String()
251 if frmt.check {
252 // We do not actually emit to the log, but panic if
253 // a non-nil value is detected (e.g. a non-nil error)
254 for _, value := range valuesDbg {
255 if value != nil {
256 if format == "" {
257 panic(value)
258 } else {
259 panic(fmt.Sprintf(format, append(valuesF, value)...))
260 }
261 }
262 }
263 } else {
264 d.getEmit().emit(frmt, format, valuesF...)
265 }
266 }
267
268 // Idiot-proof &Dbgr{}, etc.
269 func (d *Dbgr) getEmit() emit {
270 if d.emit == nil {
271 d.emit = standardEmit()
272 }
273 return d.emit
274 }
275
276 // SetOutput will accept the following as a destination for output:
277 //
278 // *log.Logger Print*/Panic*/Fatal* of the logger
279 // io.Writer -
280 // nil Reset to the default output (os.Stderr)
281 // "log" Print*/Panic*/Fatal* via the "log" package
282 func (d *Dbgr) SetOutput(output interface{}) {
283 if output == nil {
284 d.emit = standardEmit()
285 return
286 }
287 switch output := output.(type) {
288 case *log.Logger:
289 d.emit = emitLogger{
290 logger: output,
291 }
292 return
293 case io.Writer:
294 d.emit = emitWriter{
295 writer: output,
296 }
297 return
298 case string:
299 if output == "log" {
300 d.emit = emitLog{}
301 return
302 }
303 }
304 panic(output)
305 }
306
307 // ======== //
308 // = emit = //
309 // ======== //
310
311 func standardEmit() emit {
312 return emitWriter{
313 writer: os.Stderr,
314 }
315 }
316
317 func ln(tmp string) string {
318 length := len(tmp)
319 if length > 0 && tmp[length-1] != '\n' {
320 return tmp + "\n"
321 }
322 return tmp
323 }
324
325 type emit interface {
326 emit(_frmt, string, ...interface{})
327 }
328
329 type emitWriter struct {
330 writer io.Writer
331 }
332
333 func (ew emitWriter) emit(frmt _frmt, format string, values ...interface{}) {
334 if format == "" {
335 fmt.Fprintln(ew.writer, values...)
336 } else {
337 if frmt.panic {
338 panic(fmt.Sprintf(format, values...))
339 }
340 fmt.Fprintf(ew.writer, ln(format), values...)
341 if frmt.fatal {
342 os.Exit(1)
343 }
344 }
345 }
346
347 type emitLogger struct {
348 logger *log.Logger
349 }
350
351 func (el emitLogger) emit(frmt _frmt, format string, values ...interface{}) {
352 if format == "" {
353 el.logger.Println(values...)
354 } else {
355 if frmt.panic {
356 el.logger.Panicf(format, values...)
357 } else if frmt.fatal {
358 el.logger.Fatalf(format, values...)
359 } else {
360 el.logger.Printf(format, values...)
361 }
362 }
363 }
364
365 type emitLog struct {
366 }
367
368 func (el emitLog) emit(frmt _frmt, format string, values ...interface{}) {
369 if format == "" {
370 log.Println(values...)
371 } else {
372 if frmt.panic {
373 log.Panicf(format, values...)
374 } else if frmt.fatal {
375 log.Fatalf(format, values...)
376 } else {
377 log.Printf(format, values...)
378 }
379 }
380 }