Debian Codegraph @

util/logging.go

raw symbols SCIP
1 package util
2
3 import (
4 "fmt"
5 "io"
6 "sync"
7
8 "github.com/mattn/go-colorable"
9 "github.com/mgutz/ansi"
10 )
11
12 var cyan func(string) string
13 var red func(string) string
14 var yellow func(string) string
15 var redInverse func(string) string
16 var gray func(string) string
17 var magenta func(string) string
18
19 var colorfulMap = map[string]int{}
20 var colorfulMutex = &sync.Mutex{}
21 var colorfulFormats = []func(string) string{
22 ansi.ColorFunc("+h"),
23 ansi.ColorFunc("green"),
24 ansi.ColorFunc("yellow"),
25 ansi.ColorFunc("magenta"),
26 ansi.ColorFunc("green+h"),
27 ansi.ColorFunc("yellow+h"),
28 ansi.ColorFunc("magenta+h"),
29 }
30
31 // LogWriter is the writer to which the logs are written
32 var LogWriter io.Writer
33
34 func init() {
35 ansi.DisableColors(false)
36 cyan = ansi.ColorFunc("cyan")
37 red = ansi.ColorFunc("red+b")
38 yellow = ansi.ColorFunc("yellow+b")
39 redInverse = ansi.ColorFunc("white:red")
40 gray = ansi.ColorFunc("black+h")
41 magenta = ansi.ColorFunc("magenta+h")
42 LogWriter = colorable.NewColorableStdout()
43 }
44
45 // Debug writes a debug statement to stdout.
46 func Debug(group string, format string, any ...interface{}) {
47 fmt.Fprint(LogWriter, gray(group)+" ")
48 fmt.Fprintf(LogWriter, gray(format), any...)
49 }
50
51 // Info writes an info statement to stdout.
52 func Info(group string, format string, any ...interface{}) {
53 fmt.Fprint(LogWriter, cyan(group)+" ")
54 fmt.Fprintf(LogWriter, format, any...)
55 }
56
57 // InfoColorful writes an info statement to stdout changing colors
58 // on succession.
59 func InfoColorful(group string, format string, any ...interface{}) {
60 colorfulMutex.Lock()
61 colorfulMap[group]++
62 colorFn := colorfulFormats[colorfulMap[group]%len(colorfulFormats)]
63 colorfulMutex.Unlock()
64
65 fmt.Fprint(LogWriter, cyan(group)+" ")
66 s := colorFn(fmt.Sprintf(format, any...))
67 fmt.Fprint(LogWriter, s)
68 }
69
70 // Error writes an error statement to stdout.
71 func Error(group string, format string, any ...interface{}) error {
72 fmt.Fprintf(LogWriter, red(group)+" ")
73 fmt.Fprintf(LogWriter, red(format), any...)
74 return fmt.Errorf(format, any...)
75 }
76
77 // Panic writes an error statement to stdout.
78 func Panic(group string, format string, any ...interface{}) {
79 fmt.Fprintf(LogWriter, redInverse(group)+" ")
80 fmt.Fprintf(LogWriter, redInverse(format), any...)
81 panic("")
82 }
83
84 // Deprecate writes a deprecation warning.
85 func Deprecate(message string) {
86 fmt.Fprintf(LogWriter, yellow("godo")+" "+message)
87 }