-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
281 lines (241 loc) · 7.63 KB
/
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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// This file is part of arduino-language-server.
//
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU Affero General Public License version 3,
// which covers the main part of arduino-language-server.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/agpl-3.0.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
package main
import (
"fmt"
"log"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/tectiv3/go-lsp/jsonrpc"
"go.bug.st/json"
)
// Logger is a lsp logger
type Logger struct {
IncomingPrefix, OutgoingPrefix string
HiColor, LoColor func(format string, a ...interface{}) string
ErrorColor func(format string, a ...interface{}) string
}
func init() {
log.SetFlags(log.LstdFlags)
}
// LogOutgoingRequest prints an outgoing request into the log
func (l *Logger) LogOutgoingRequest(id string, method string, params json.RawMessage) {
if config.EnableLogging {
log.Print(l.decorate(l.HiColor("%s REQU %s %s %s", l.OutgoingPrefix, method, id, string(params))))
}
}
// LogOutgoingCancelRequest prints an outgoing cancel request into the log
func (l *Logger) LogOutgoingCancelRequest(id string) {
if config.EnableLogging {
log.Print(l.LoColor("%s CANCEL %s", l.OutgoingPrefix, id))
}
}
// LogIncomingResponse prints an incoming response into the log if there is no error
func (l *Logger) LogIncomingResponse(id string, method string, resp json.RawMessage, respErr *jsonrpc.ResponseError) {
e := ""
if respErr != nil {
e = l.ErrorColor(" ERROR: %s", respErr.AsError())
} else {
e = string(resp)
}
if config.EnableLogging {
log.Print(l.decorate(l.LoColor("%s RESP %s %s %s", l.IncomingPrefix, method, id, e)))
}
}
// LogOutgoingNotification prints an outgoing notification into the log
func (l *Logger) LogOutgoingNotification(method string, params json.RawMessage) {
if config.EnableLogging {
log.Print(l.decorate(l.HiColor("%s NOTIF %s", l.OutgoingPrefix, method)))
}
}
// LogIncomingRequest prints an incoming request into the log
func (l *Logger) LogIncomingRequest(id string, method string, params json.RawMessage) jsonrpc.FunctionLogger {
spaces := " "
if config.EnableLogging {
log.Print(l.decorate(l.HiColor(fmt.Sprintf("%s REQU %s %s", l.IncomingPrefix, method, id))))
}
return &FunctionLogger{
colorFunc: l.HiColor,
prefix: fmt.Sprintf("%s %s %s", spaces[:len(l.IncomingPrefix)], method, id),
}
}
// LogIncomingCancelRequest prints an incoming cancel request into the log
func (l *Logger) LogIncomingCancelRequest(id string) {
if config.EnableLogging {
log.Print(l.LoColor("%s CANCEL %s", l.IncomingPrefix, id))
}
}
// LogOutgoingResponse prints an outgoing response into the log if there is no error
func (l *Logger) LogOutgoingResponse(id string, method string, resp json.RawMessage, respErr *jsonrpc.ResponseError) {
e := ""
if respErr != nil {
e = l.ErrorColor(" ERROR: %s", respErr.AsError())
}
if config.EnableLogging {
log.Print(l.decorate(l.LoColor("%s RESP %s %s %s", l.OutgoingPrefix, method, id, e)))
}
}
// LogIncomingNotification prints an incoming notification into the log
func (l *Logger) LogIncomingNotification(method string, params json.RawMessage) jsonrpc.FunctionLogger {
spaces := " "
return &FunctionLogger{
colorFunc: l.HiColor,
prefix: fmt.Sprintf("%s %s", spaces[:len(l.IncomingPrefix)], method),
}
}
// LogIncomingDataDelay prints the delay of incoming data into the log
func (l *Logger) LogIncomingDataDelay(delay time.Duration) {
//log.Printf("IN Elapsed: %v", delay)
}
// LogOutgoingDataDelay prints the delay of outgoing data into the log
func (l *Logger) LogOutgoingDataDelay(delay time.Duration) {
//log.Printf("OUT Elapsed: %v", delay)
}
func (l *Logger) decorate(format string) string {
file_info := ""
pc, file, line, ok := runtime.Caller(2)
if ok {
// Get caller function name.
fn := runtime.FuncForPC(pc)
var fnName string
if fn == nil {
fnName = "?()"
} else {
fnName = strings.TrimLeft(filepath.Ext(fn.Name()), ".")
}
fileName := filepath.Join(filepath.Base(filepath.Dir(file)), filepath.Base(file))
file_info = fmt.Sprintf("[%s:%d %s] ", fileName, line, fnName)
}
return file_info + format
}
// FunctionLogger is a lsp function logger
type FunctionLogger struct {
colorFunc func(format string, a ...interface{}) string
prefix string
}
// NewLSPFunctionLogger creates a new function logger
func NewLSPFunctionLogger(colorFunction func(format string, a ...interface{}) string, prefix string) *FunctionLogger {
return &FunctionLogger{
colorFunc: colorFunction,
prefix: prefix,
}
}
// Foreground Hi-Intensity text colors
const (
FgHiBlack int = iota + 90
FgHiRed
FgHiGreen
FgHiYellow
FgHiBlue
FgHiMagenta
FgHiCyan
FgHiWhite
)
// Foreground text colors
const (
FgBlack int = iota + 30
FgRed
FgGreen
FgYellow
FgBlue
FgMagenta
FgCyan
FgWhite
)
// Background Hi-Intensity text colors
const (
BgHiBlack int = iota + 100
BgHiRed
BgHiGreen
BgHiYellow
BgHiBlue
BgHiMagenta
BgHiCyan
BgHiWhite
)
const escape = "\x1b"
const (
Reset int = iota
)
func Log(format string, a ...interface{}) {
logger.Logf(format, a...)
}
func LogError(err error) {
logger.Logf(errorString("Error: %v", err))
}
// Logf logs the given message
func (l *FunctionLogger) Logf(format string, a ...interface{}) {
log.Print(l.decorate(l.colorFunc(l.prefix+": "+format, a...)))
}
func (l *FunctionLogger) decorate(format string) string {
file_info := ""
pc, file, line, ok := runtime.Caller(3)
if ok {
// Get caller function name.
fn := runtime.FuncForPC(pc)
var fnName string
if fn == nil {
fnName = "?()"
} else {
fnName = strings.TrimLeft(filepath.Ext(fn.Name()), ".")
}
fileName := filepath.Join(filepath.Base(filepath.Dir(file)), filepath.Base(file))
file_info = fmt.Sprintf("[%s:%d %s] ", fileName, line, fnName)
}
return file_info + format
}
func c_format(colors ...int) string {
return fmt.Sprintf("%s[%sm", escape, c_sequence(colors...))
}
func c_unformat() string {
return fmt.Sprintf("%s[%dm", escape, Reset)
}
func c_sequence(colors ...int) string {
format := make([]string, len(colors))
for i, v := range colors {
format[i] = strconv.Itoa(v)
}
return strings.Join(format, ";")
}
func hiRedString(format string, a ...interface{}) string {
return colorFormat(format, FgHiRed, a...)
}
func hiGreenString(format string, a ...interface{}) string {
return colorFormat(format, FgHiGreen, a...)
}
func hiMagentaString(format string, a ...interface{}) string {
return colorFormat(format, FgHiMagenta, a...)
}
func hiBlueString(format string, a ...interface{}) string {
return colorFormat(format, FgHiBlue, a...)
}
func redString(format string, a ...interface{}) string {
return colorFormat(format, FgRed, a...)
}
func greenString(format string, a ...interface{}) string {
return colorFormat(format, FgGreen, a...)
}
func blueString(format string, a ...interface{}) string {
return colorFormat(format, FgBlue, a...)
}
func colorFormat(format string, color int, a ...interface{}) string {
return c_format(color) + fmt.Sprintf(format, a...) + c_unformat()
}
func errorString(format string, a ...interface{}) string {
return c_format(BgHiMagenta, FgHiWhite) + fmt.Sprintf(format, a...) + c_unformat()
}