This repository was archived by the owner on Jun 22, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
337 lines (254 loc) · 7.6 KB
/
Copy pathmain.go
File metadata and controls
337 lines (254 loc) · 7.6 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package codectrl
import (
"context"
"fmt"
"io"
"os"
"reflect"
"strings"
e "github.com/Authentura/codectrl-go-logger/error"
h "github.com/Authentura/codectrl-go-logger/hashbag"
b "github.com/Authentura/codectrl-go-protobufs/data/backtrace_data"
l "github.com/Authentura/codectrl-go-protobufs/data/log"
logsService "github.com/Authentura/codectrl-go-protobufs/logs_service"
"github.com/go-errors/errors"
grpc "google.golang.org/grpc"
)
type createLogParams struct {
surround uint32
functionName string
functionNameOccurences h.HashBag[string]
}
func createLog(message string, params ...createLogParams) (*l.Log, error) {
// function_name := ""
if len(params) > 0 {
params := params[0]
// if params.function_name != "" {
// function_name = params.function_name
// }
if params.surround == 0 {
params.surround = 3
}
}
parameters := params[0]
log := l.Log{
Uuid: "",
Stack: []*b.BacktraceData{},
LineNumber: 0,
FileName: "",
CodeSnippet: map[uint32]string{},
Message: message,
MessageType: reflect.TypeOf(message).String(),
Address: "",
Warnings: []string{},
Language: "Go",
}
stack, err := getStackTrace(&log)
if err != nil {
return nil, errors.Wrap(err, 0)
}
log.Stack = *stack
if !(len(log.GetStack()) > 0) {
return &log, nil
}
last := log.GetStack()[len(log.GetStack())-1]
if last != nil {
log.LineNumber = last.GetLineNumber()
log.FileName = last.GetFilePath()
snippet, err := getCodeSnippet(last.FilePath, &log, parameters.surround, "", nil)
if err != nil {
return nil, errors.Wrap(err, 0)
}
log.CodeSnippet = *snippet
}
return &log, nil
}
// Optional parameters for the Logger interface methods.
type LoggerParams struct {
surround uint32
host string
port string
}
// Creates a new LoggerParams using the given parameters.
func NewLoggerParams(surround uint32, host string, port string) LoggerParams {
return LoggerParams{surround: surround, host: host, port: port}
}
// Creates a new, empty LoggerParams.
func NewEmptyLoggerParams() LoggerParams {
return LoggerParams{}
}
// Main Logger struct
type Logger struct{}
// Creates a new Logger.
func NewLogger() Logger {
return Logger{}
}
// TODO: Add batch logging
type loggerInterface interface {
Log(message string, params ...LoggerParams) (*logsService.RequestResult, error)
LogIf(message string, condition func(params ...struct{}) bool, params ...LoggerParams) (*logsService.RequestResult, error)
LogWhenEnv(message string, params ...LoggerParams) (*logsService.RequestResult, error)
log(log l.Log, host string, port string) (*logsService.RequestResult, error)
}
// Main Log function, sends a log whenever this function is called, assuming
// the connection is valid.
func (logger Logger) Log(message string, params ...LoggerParams) (*logsService.RequestResult, error) {
host := "127.0.0.1"
port := "3002"
surround := uint32(3)
if len(params) > 0 {
params := params[0]
if params.host != "" {
host = params.host
}
if params.port != "" {
port = params.port
}
if params.surround != 0 {
surround = params.surround
}
}
log, err := createLog(message, createLogParams{surround: surround})
if err != nil {
return nil, errors.Wrap(err, 0)
}
return logger.log(*log, host, port)
}
// Log function that only connects and sends if the given `condition` function
// pointer resolves to true.
func (logger Logger) LogIf(message string, condition func(params ...struct{}) bool, params ...LoggerParams) (*logsService.RequestResult, error) {
if !condition() {
return nil, errors.Wrap("Condition was not true", 0)
}
return logger.Log(message, params...)
}
// Log function that only connects and sends when the "CODECTRL_DEBUG"
// environment variable is set.
func (logger Logger) LogWhenEnv(message string, params ...LoggerParams) (*logsService.RequestResult, error) {
_, present := os.LookupEnv("CODECTRL_DEBUG")
if !present {
return nil, errors.Wrap("Environment variable CODECTRL_DEBUG not set", 0)
}
return logger.Log(message, params...)
}
func (logger Logger) log(log l.Log, host string, port string) (*logsService.RequestResult, error) {
connection, err := grpc.Dial(fmt.Sprintf("%s:%s", host, port), grpc.WithInsecure())
if err != nil {
return nil, errors.Wrap(err, 0)
}
client := logsService.NewLogClientClient(connection)
resultChannel := make(chan logsService.RequestResult)
errorChannel := make(chan error)
go func() {
defer connection.Close()
r, err := client.SendLog(context.Background(), &log)
if r != nil {
resultChannel <- *r
errorChannel <- nil
} else if err != nil {
errorChannel <- err
}
}()
r := <-resultChannel
return &r, <-errorChannel
}
func deduplicateStack(stack []*b.BacktraceData) []*b.BacktraceData {
occurred := map[uint32]bool{}
result := []*b.BacktraceData{}
for x := range stack {
if occurred[stack[x].GetLineNumber()] != true {
occurred[stack[x].GetLineNumber()] = true
result = append(result, stack[x])
}
}
return result
}
// NOTE: For some reason, this seems to skip some layers when generating the
// stack trace. I am unsure whether this is down to the implementation in
// go-errors, or that the functions are being inlined in a way which makes it
// impossible for the stacktrace to generate for those lines.
func getStackTrace(log *l.Log) (*[]*b.BacktraceData, error) {
fakeError := errors.Wrap("fake error", 0)
stack := fakeError.StackFrames()
bstack := []*b.BacktraceData{}
for _, frame := range stack {
switch frame.Package {
case "runtime", "testing", "github.com/Authentura/codectrl-go-logger":
continue
default:
}
if strings.Contains(frame.File, os.Getenv("GOROOT")) {
continue
}
code, err := frame.SourceLine()
if err != nil {
codeResult, err := getCode(frame.File, uint32(frame.LineNumber))
if err != nil {
return nil, errors.Wrap(err, 0)
}
code = *codeResult
}
bstack = append(
[]*b.BacktraceData{
{
LineNumber: uint32(frame.LineNumber),
ColumnNumber: uint32(0),
FilePath: frame.File,
Name: frame.Name,
Code: code,
},
},
bstack...)
}
bstack = deduplicateStack(bstack)
return &bstack, nil
}
func getCode(filePath string, lineNumber uint32) (*string, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, errors.Wrap(err, 0)
}
defer file.Close()
contentBytes, err := io.ReadAll(file)
if err != nil {
return nil, errors.Wrap(err, 0)
}
content := string(contentBytes)
lines := strings.Split(content, "\n")
if len(lines) < int(lineNumber) {
return nil, errors.Wrap(e.New(e.LineNumTooLargeError, "Line number is too large for this file."), 0)
} else if int(lineNumber) <= 0 {
return nil, errors.Wrap(e.New(e.LineNumZeroError, "Line number is zero or negative."), 0)
}
line := lines[lineNumber-1]
return &line, nil
}
// TODO: Account for batch logging
func getCodeSnippet(filePath string, log *l.Log, surround uint32, functionName string, functionNameOccurences *h.HashBag[string]) (*map[uint32]string, error) {
file, err := os.Open(filePath)
lineNumber := int(log.LineNumber)
offsetLine := lineNumber - int(surround)
if offsetLine < 0 {
offsetLine = 0
}
if err != nil {
return nil, errors.Wrap(err, 0)
}
defer file.Close()
contentBytes, err := io.ReadAll(file)
if err != nil {
return nil, errors.Wrap(err, 0)
}
content := string(contentBytes)
lines := strings.Split(content, "\n")
snippet := map[uint32]string{}
for index, line := range lines {
if index < offsetLine {
continue
} else if index > lineNumber+int(surround) {
break
}
snippet[uint32(index+1)] = line
}
return &snippet, nil
}