/
xdi_logger.go
87 lines (74 loc) · 2.21 KB
/
xdi_logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package xdi
import (
"fmt"
"github.com/Aoi-hosizora/ahlib/xcolor"
)
type LogLevel int8
const (
LogName LogLevel = 1 << iota
LogType
LogImpl
LogInject
LogAll = LogName | LogType | LogImpl | LogInject
LogSilent = LogLevel(0)
)
// Logger represents xdi.DiContainer's logger function.
type Logger interface {
// LogName logs when DiContainer.ProvideName invoked.
LogName(name, typ string)
// LogType logs when DiContainer.ProvideType invoked.
LogType(typ string)
// LogImpl logs when DiContainer.ProvideImpl invoked.
LogImpl(interfaceTyp, implTyp string)
// LogInject logs when DiContainer.Inject invoked.
LogInject(parentTyp, fieldTyp, fieldName string)
}
// defaultLogger represents the default logger.
type defaultLogger struct {
level LogLevel
}
// DefaultLogger creates a defaultLogger.
func DefaultLogger(level LogLevel) Logger {
xcolor.ForceColor()
return &defaultLogger{
level: level,
}
}
// LogName logs like:
// [XDI] Name: a <- *xdi.ServiceA
func (d *defaultLogger) LogName(name, typ string) {
if d.level&LogName != 0 {
name = xcolor.Red.Sprint(name)
typ = xcolor.Yellow.Sprint(typ)
fmt.Printf("[XDI] %-8s %-30s <-- %s\n", "Name:", name, typ)
}
}
// LogType logs like:
// [XDI] Type: ~ <- *xdi.ServiceB
func (d *defaultLogger) LogType(typ string) {
if d.level&LogType != 0 {
auto := xcolor.Red.Sprint("~")
typ = xcolor.Yellow.Sprint(typ)
fmt.Printf("[XDI] %-8s %-30s <-- %s\n", "Type:", auto, typ)
}
}
// LogImpl logs like:
// [XDI] Impl: ~ <- xdi.IServiceA (*xdi.ServiceA)
func (d *defaultLogger) LogImpl(interfaceTyp, implTyp string) {
if d.level&LogImpl != 0 {
auto := xcolor.Red.Sprint("~")
interfaceTyp = xcolor.Yellow.Sprint(interfaceTyp)
implTyp = xcolor.Yellow.Sprint(implTyp)
fmt.Printf("[XDI] %-8s %-30s <-- %s (%s)\n", "Impl:", auto, interfaceTyp, implTyp)
}
}
// Inject logs like:
// [XDI] Inject: xdi.IServiceA -> (*xdi.ServiceB).SA
func (d *defaultLogger) LogInject(parentTyp, fieldTyp, fieldName string) {
if d.level&LogInject != 0 {
parentTyp = xcolor.Yellow.Sprint(parentTyp)
fieldTyp = xcolor.Yellow.Sprint(fieldTyp)
fieldName = xcolor.Red.Sprint(fieldName)
fmt.Printf("[XDI] %-8s %-30s --> (%s).%s\n", "Inject:", fieldTyp, parentTyp, fieldName)
}
}