1package kbchat
2
3import (
4	"fmt"
5	"log"
6	"time"
7)
8
9func ErrToOK(err *error) string {
10	if err == nil || *err == nil {
11		return "ok"
12	}
13	return fmt.Sprintf("ERROR: %v", *err)
14}
15
16type DebugOutput struct {
17	name string
18}
19
20func NewDebugOutput(name string) *DebugOutput {
21	return &DebugOutput{
22		name: name,
23	}
24}
25
26func (d *DebugOutput) Debug(format string, args ...interface{}) {
27	msg := fmt.Sprintf(format, args...)
28	log.Printf("%s: %s\n", d.name, msg)
29}
30
31func (d *DebugOutput) Trace(err *error, format string, args ...interface{}) func() {
32	msg := fmt.Sprintf(format, args...)
33	start := time.Now()
34	log.Printf("+ %s: %s\n", d.name, msg)
35	return func() {
36		log.Printf("- %s: %s -> %s [time=%v]\n", d.name, msg, ErrToOK(err), time.Since(start))
37	}
38}
39