-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
180 lines (158 loc) · 3.33 KB
/
message.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package golog
import (
"fmt"
"runtime"
"strings"
"time"
"github.com/BOOMfinity/go-utils/gpool"
"github.com/BOOMfinity/go-utils/inlineif"
)
type Level uint
const (
LevelPanic Level = iota + 1
LevelError
LevelWarning
LevelInfo
LevelDebug
LevelTrace
)
func (l Level) String() string {
switch l {
case LevelPanic:
return "PANIC"
case LevelError:
return "ERROR"
case LevelWarning:
return "WARNING"
case LevelInfo:
return "INFO"
case LevelDebug:
return "DEBUG"
case LevelTrace:
return "TRACE"
default:
panic("undefined logging level")
}
}
func (l Level) Color() string {
switch l {
case LevelPanic:
return errorColorCode
case LevelError:
return errorColorCode
case LevelWarning:
return warningColorCode
case LevelInfo:
return infoColorCode
case LevelDebug:
return debugColorCode
case LevelTrace:
return traceColorCode
default:
panic("undefined logging level")
}
}
func levelFromString(str string) Level {
switch strings.ToUpper(str) {
case "PANIC":
return LevelPanic
case "ERROR":
return LevelError
case "WARNING":
return LevelWarning
case "INFO":
return LevelInfo
case "DEBUG":
return LevelDebug
case "TRACE":
return LevelTrace
default:
return LevelInfo
}
}
type Message interface {
Internal() MessageInfo
Details(v any) Message
Param(name, value any) Message
Duration(d time.Duration) Message
Stack() Message
Send(format string, args ...any)
Throw(err error)
}
type MessageInfo struct {
Details any
Logger Logger
Level Level
Stack []byte
ExitCode int
Duration time.Duration
Message []byte
Params Params
}
type message struct {
info MessageInfo
}
func (m *message) Stack() Message {
l := runtime.Stack(m.info.Stack[:cap(m.info.Stack)], false)
m.info.Stack = m.info.Stack[:l]
return m
}
func (m *message) Throw(err error) {
m.Stack().Send(err.Error())
}
func (m *message) Internal() MessageInfo {
return m.info
}
func (m *message) Details(v any) Message {
m.info.Details = v
return m
}
func (m *message) Param(name, value any) Message {
arr := paramsPool.Get()
(*arr)[0] = name
(*arr)[1] = value
m.info.Params = append(m.info.Params, arr)
return m
}
func (m *message) Duration(d time.Duration) Message {
m.info.Duration = d
return m
}
func (m *message) Send(format string, args ...any) {
defer messagePool.Put(m)
if m.Internal().Level > m.Internal().Logger.Internal().Level {
return
}
m.info.Message = fmt.Appendf(m.info.Message, format, args...)
m.info.Logger.Internal().Engine(m)
}
var messagePool = gpool.New[message](gpool.OnInit[message](func(m *message) {
m.info.Stack = make([]byte, 1024*4)
m.info.Params = make(Params, 0, 25)
m.info.Message = make([]byte, 0, 1024*4)
}), gpool.OnPut[message](func(m *message) {
m.info.Message = m.info.Message[:0]
for _, param := range m.info.Params {
paramsPool.Put(param)
}
m.info.Params = m.info.Params[:0]
m.info.Details = nil
m.info.ExitCode = 0
m.info.Stack = m.info.Stack[:0]
m.info.Duration = 0
}))
func newMessage(log Logger, level Level) Message {
msg := messagePool.Get()
msg.info.Level = level
msg.info.Logger = log
return msg
}
func newErrorMessage(log Logger, exitCode ...int) Message {
msg := messagePool.Get()
msg.info.Level = inlineif.IfElse(len(exitCode) > 0, LevelPanic, LevelError)
if len(exitCode) > 0 {
msg.info.ExitCode = exitCode[0]
}
msg.info.Logger = log
return msg
}