forked from tmrts/boilr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
164 lines (130 loc) · 3.87 KB
/
log.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Package tlog implements logging utilities for boilr
package tlog
import (
"fmt"
"strings"
"github.com/fatih/color"
)
// TODO default to ASCII if Unicode is not supported
const (
// DebugMark character indicates debug message.
DebugMark = "☹"
// CheckMark character indicates success message.
CheckMark = "✔"
// InfoMark character indicates information message.
InfoMark = "i"
// WarnMark character indicates warning message.
WarnMark = "!"
// ErrorMark character indicates error message.
ErrorMark = "✘"
// QuestionMark character indicates prompt message.
QuestionMark = "?"
)
var (
logLevel Level
)
// Level is a 16-bit set holding the enabled log levels.
type Level uint16
const (
LevelDebug = 1 << 5
LevelFatal = 1 << 4
LevelWarn = 1 << 3
LevelError = 1 << 2
LevelInfo = 1 << 1
LevelSuccess = 1 << 0
)
// Set enables the levels upto and including the given log level.
func (lvl *Level) Set(newLvl Level) {
*lvl = (newLvl << 1) - 1
}
// Permits queries whether the given log level is enabled or not.
func (lvl Level) Permits(queryLvl Level) bool {
return lvl&queryLvl > 0
}
// SetLogLevel sets the global logging level.
func SetLogLevel(LogLevelString string) {
levels := map[string]Level{
"debug": LevelDebug,
"fatal": LevelFatal,
"warn": LevelWarn,
"error": LevelError,
"info": LevelInfo,
"success": LevelSuccess,
}
newLevel, ok := levels[strings.ToLower(LogLevelString)]
if !ok {
Error(fmt.Sprintf("unknown log level %s", LogLevelString))
return
}
logLevel.Set(newLevel)
}
// TODO add log levels
func coloredPrintMsg(icon string, msg string, iC color.Attribute, mC color.Attribute) {
fmt.Println(
color.New(mC).SprintFunc()("["+icon+"]"),
color.New(color.Bold, iC).SprintFunc()(msg))
}
// Debug logs the given message as a debug message.
func Debug(msg string) {
if !logLevel.Permits(LevelDebug) {
return
}
coloredPrintMsg(DebugMark, msg, color.FgYellow, color.FgYellow)
}
// Success logs the given message as a success message.
func Success(msg string) {
if !logLevel.Permits(LevelSuccess) {
return
}
coloredPrintMsg(CheckMark, msg, color.FgWhite, color.FgGreen)
}
// Info logs the given message as a info message.
func Info(msg string) {
if !logLevel.Permits(LevelInfo) {
return
}
coloredPrintMsg(InfoMark, msg, color.FgWhite, color.FgBlue)
}
// Warn logs the given message as a warn message.
func Warn(msg string) {
if !logLevel.Permits(LevelWarn) {
return
}
coloredPrintMsg(WarnMark, msg, color.FgMagenta, color.FgMagenta)
}
// Error logs the given message as a error message.
func Error(msg string) {
if !logLevel.Permits(LevelError) {
return
}
coloredPrintMsg(ErrorMark, msg, color.FgRed, color.FgRed)
}
// Fatal logs the given message as a fatal message.
func Fatal(msg string) {
// Fatal level is being deprecated
Error(msg)
}
// Prompt outputs the given message as a question along with a default value.
func Prompt(msg string, defval interface{}) {
tokens := []string{
color.New(color.FgBlue).SprintFunc()("[" + QuestionMark + "]"),
color.New(color.Bold, color.FgWhite).SprintFunc()(msg),
}
// TODO refactor & eliminate duplication
switch val := defval.(type) {
case []interface{}:
tokens = append(tokens, "\n")
for i, v := range val {
tokens = append(tokens, color.New(color.Bold, color.FgWhite).SprintFunc()(fmt.Sprintf(" %v - %#v\n", i+1, v)))
}
tokens = append(tokens, color.New(color.Bold, color.FgWhite).SprintFunc()(fmt.Sprintf(" Choose from %v..%v", 1, len(val))))
tokens = append(tokens, color.New(color.Bold, color.FgBlue).SprintFunc()(fmt.Sprintf("[default: %v]: ", 1)))
default:
tokens = append(tokens, color.New(color.Bold, color.FgBlue).SprintFunc()(fmt.Sprintf("[default: %#v]: ", defval)))
}
fmt.Print(strings.Join(tokens, " "))
}
// TODO use dependency injection wrapper for fmt.Print usage in the code base
func init() {
logLevel.Set(LevelError)
}