// This file was AUTOMATICALLY GENERATED by dbg-import (smuggol) from github.com/robertkrimen/dbg

/*
Package dbg is a println/printf/log-debugging utility library.

	import (
	    Dbg "github.com/robertkrimen/dbg"
	)

	dbg, dbgf := Dbg.New()

	dbg("Emit some debug stuff", []byte{120, 121, 122, 122, 121}, math.Pi)
	# "2013/01/28 16:50:03 Emit some debug stuff [120 121 122 122 121] 3.141592653589793"

	dbgf("With a %s formatting %.2f", "little", math.Pi)
	# "2013/01/28 16:51:55 With a little formatting (3.14)"

	dbgf("%/fatal//A fatal debug statement: should not be here")
	# "A fatal debug statement: should not be here"
	# ...and then, os.Exit(1)

	dbgf("%/panic//Can also panic %s", "this")
	# "Can also panic this"
	# ...as a panic, equivalent to: panic("Can also panic this")

	dbgf("Any %s arguments without a corresponding %%", "extra", "are treated like arguments to dbg()")
	# "2013/01/28 17:14:40 Any extra arguments (without a corresponding %) are treated like arguments to dbg()"

	dbgf("%d %d", 1, 2, 3, 4, 5)
	# "2013/01/28 17:16:32 Another example: 1 2 3 4 5"

	dbgf("%@: Include the function name for a little context (via %s)", "%@")
	# "2013... github.com/robertkrimen/dbg.TestSynopsis: Include the function name for a little context (via %@)"

By default, dbg uses log (log.Println, log.Printf, log.Panic, etc.) for output.
However, you can also provide your own output destination by invoking dbg.New with
a customization function:

	import (
	    "bytes"
	    Dbg "github.com/robertkrimen/dbg"
	    "os"
	)

	# dbg to os.Stderr
	dbg, dbgf := Dbg.New(func(dbgr *Dbgr) {
	    dbgr.SetOutput(os.Stderr)
	})

	# A slightly contrived example:
	var buffer bytes.Buffer
	dbg, dbgf := New(func(dbgr *Dbgr) {
	    dbgr.SetOutput(&buffer)
	})
*/
package dbg

import (
	"bytes"
	"fmt"
	"io"
	"log"
	"os"
	"regexp"
	goruntime "runtime"
	"strings"
	"unicode"
)

type _frmt struct {
	ctl          string
	format       string
	operandCount int
	panic        bool
	fatal        bool
	check        bool
}

var (
	ctlTest = regexp.MustCompile(`^\s*%/`)
	ctlScan = regexp.MustCompile(`%?/(panic|fatal|check)(?:\s|$)`)
)

func operandCount(format string) int {
	count := 0
	end := len(format)
	for at := 0; at < end; {
		for at < end && format[at] != '%' {
			at++
		}
		at++
		if at < end {
			if format[at] != '%' && format[at] != '@' {
				count++
			}
			at++
		}
	}
	return count
}

func parseFormat(format string) (frmt _frmt) {
	if ctlTest.MatchString(format) {
		format = strings.TrimLeftFunc(format, unicode.IsSpace)
		index := strings.Index(format, "//")
		if index != -1 {
			frmt.ctl = format[0:index]
			format = format[index+2:] // Skip the second slash via +2 (instead of +1)
		} else {
			frmt.ctl = format
			format = ""
		}
		for _, tmp := range ctlScan.FindAllStringSubmatch(frmt.ctl, -1) {
			for _, value := range tmp[1:] {
				switch value {
				case "panic":
					frmt.panic = true
				case "fatal":
					frmt.fatal = true
				case "check":
					frmt.check = true
				}
			}
		}
	}
	frmt.format = format
	frmt.operandCount = operandCount(format)
	return
}

type Dbgr struct {
	emit emit
}

type DbgFunction func(values ...interface{})

func NewDbgr() *Dbgr {
	return &Dbgr{}
}

/*
New will create and return a pair of debugging functions. You can customize where
they output to by passing in an (optional) customization function:

	import (
	    Dbg "github.com/robertkrimen/dbg"
	    "os"
	)

	# dbg to os.Stderr
	dbg, dbgf := Dbg.New(func(dbgr *Dbgr) {
	    dbgr.SetOutput(os.Stderr)
	})
*/
func New(options ...interface{}) (dbg DbgFunction, dbgf DbgFunction) {
	dbgr := NewDbgr()
	if len(options) > 0 {
		if fn, ok := options[0].(func(*Dbgr)); ok {
			fn(dbgr)
		}
	}
	return dbgr.DbgDbgf()
}

func (d Dbgr) Dbg(values ...interface{}) {
	d.getEmit().emit(_frmt{}, "", values...)
}

func (d Dbgr) Dbgf(values ...interface{}) {
	d.dbgf(values...)
}

func (d Dbgr) DbgDbgf() (dbg DbgFunction, dbgf DbgFunction) {
	dbg = func(vl ...interface{}) {
		d.Dbg(vl...)
	}
	dbgf = func(vl ...interface{}) {
		d.dbgf(vl...)
	}
	return dbg, dbgf // Redundant, but...
}

func (d Dbgr) dbgf(values ...interface{}) {
	var frmt _frmt
	if len(values) > 0 {
		tmp := fmt.Sprint(values[0])
		frmt = parseFormat(tmp)
		values = values[1:]
	}

	buf := bytes.Buffer{}
	format := frmt.format
	end := len(format)
	for at := 0; at < end; {
		last := at
		for at < end && format[at] != '%' {
			at++
		}
		if at > last {
			buf.WriteString(format[last:at])
		}
		if at >= end {
			break
		}
		// format[at] == '%'
		at++
		// format[at] == ?
		if format[at] == '@' {
			depth := 2
			pc, _, _, _ := goruntime.Caller(depth)
			name := goruntime.FuncForPC(pc).Name()
			buf.WriteString(name)
		} else {
			buf.WriteString(format[at-1 : at+1])
		}
		at++
	}

	//valuesF := append([]interface{}{}, values[0:frmt.operandCount]...)
	valuesF := values[0:frmt.operandCount]
	valuesDbg := values[frmt.operandCount:]
	if len(valuesDbg) > 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])) {
				buf.WriteString("%v")
			} else {
				buf.WriteString(" %v")
			}
		} else if frmt.check {
			// Performing a check, so no output
		} else {
			buf.WriteString("%v")
		}

		// Adjust valuesF:
		if !frmt.check {
			tmp := []string{}
			for _, value := range valuesDbg {
				tmp = append(tmp, fmt.Sprintf("%v", value))
			}
			// First, make a copy of valuesF, so we avoid overwriting valuesDbg when appending
			valuesF = append([]interface{}{}, valuesF...)
			valuesF = append(valuesF, strings.Join(tmp, " "))
		}
	}

	format = buf.String()
	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 valuesDbg {
			if value != nil {
				if format == "" {
					panic(value)
				} else {
					panic(fmt.Sprintf(format, append(valuesF, value)...))
				}
			}
		}
	} else {
		d.getEmit().emit(frmt, format, valuesF...)
	}
}

// Idiot-proof &Dbgr{}, etc.
func (d *Dbgr) getEmit() emit {
	if d.emit == nil {
		d.emit = standardEmit()
	}
	return d.emit
}

// SetOutput will accept the following as a destination for output:
//
//	*log.Logger         Print*/Panic*/Fatal* of the logger
//	io.Writer           -
//	nil                 Reset to the default output (os.Stderr)
//	"log"               Print*/Panic*/Fatal* via the "log" package
func (d *Dbgr) SetOutput(output interface{}) {
	if output == nil {
		d.emit = standardEmit()
		return
	}
	switch output := output.(type) {
	case *log.Logger:
		d.emit = emitLogger{
			logger: output,
		}
		return
	case io.Writer:
		d.emit = emitWriter{
			writer: output,
		}
		return
	case string:
		if output == "log" {
			d.emit = emitLog{}
			return
		}
	}
	panic(output)
}

// ======== //
// = emit = //
// ======== //

func standardEmit() emit {
	return emitWriter{
		writer: os.Stderr,
	}
}

func ln(tmp string) string {
	length := len(tmp)
	if length > 0 && tmp[length-1] != '\n' {
		return tmp + "\n"
	}
	return tmp
}

type emit interface {
	emit(_frmt, string, ...interface{})
}

type emitWriter struct {
	writer io.Writer
}

func (ew emitWriter) emit(frmt _frmt, format string, values ...interface{}) {
	if format == "" {
		fmt.Fprintln(ew.writer, values...)
	} else {
		if frmt.panic {
			panic(fmt.Sprintf(format, values...))
		}
		fmt.Fprintf(ew.writer, ln(format), values...)
		if frmt.fatal {
			os.Exit(1)
		}
	}
}

type emitLogger struct {
	logger *log.Logger
}

func (el emitLogger) emit(frmt _frmt, format string, values ...interface{}) {
	if format == "" {
		el.logger.Println(values...)
	} else {
		if frmt.panic {
			el.logger.Panicf(format, values...)
		} else if frmt.fatal {
			el.logger.Fatalf(format, values...)
		} else {
			el.logger.Printf(format, values...)
		}
	}
}

type emitLog struct {
}

func (el emitLog) emit(frmt _frmt, format string, values ...interface{}) {
	if format == "" {
		log.Println(values...)
	} else {
		if frmt.panic {
			log.Panicf(format, values...)
		} else if frmt.fatal {
			log.Fatalf(format, values...)
		} else {
			log.Printf(format, values...)
		}
	}
}
