1// +build codegen
2
3package api
4
5import (
6	"fmt"
7	"io"
8	"sync"
9)
10
11var debugLogger *logger
12var initDebugLoggerOnce sync.Once
13
14// logger provides a basic logging
15type logger struct {
16	w io.Writer
17}
18
19// LogDebug initialize's the debug logger for the components in the api
20// package to log debug lines to.
21//
22// Panics if called multiple times.
23//
24// Must be used prior to any model loading or code gen.
25func LogDebug(w io.Writer) {
26	var initialized bool
27	initDebugLoggerOnce.Do(func() {
28		debugLogger = &logger{
29			w: w,
30		}
31		initialized = true
32	})
33
34	if !initialized && debugLogger != nil {
35		panic("LogDebug called multiple times. Can only be called once")
36	}
37}
38
39// Logf logs using the fmt printf pattern. Appends a new line to the end of the
40// logged statement.
41func (l *logger) Logf(format string, args ...interface{}) {
42	if l == nil {
43		return
44	}
45	fmt.Fprintf(l.w, format+"\n", args...)
46}
47
48// Logln logs using the fmt println pattern.
49func (l *logger) Logln(args ...interface{}) {
50	if l == nil {
51		return
52	}
53	fmt.Fprintln(l.w, args...)
54}
55