-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherror_buffer.go
More file actions
379 lines (328 loc) · 8.66 KB
/
error_buffer.go
File metadata and controls
379 lines (328 loc) · 8.66 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package insyra
import (
"fmt"
"sync"
"time"
"github.com/HazelnutParadise/insyra/internal/core"
)
type ErrPoppingMode int
// ErrPoppingMode defines the mode for popping errors.
const (
// ErrPoppingModeFIFO retrieves the first error in the buffer.
ErrPoppingModeFIFO ErrPoppingMode = iota
// ErrPoppingModeLIFO retrieves the last error in the buffer.
ErrPoppingModeLIFO
)
// ErrorInfo represents a structured error with context information.
// It is the public-facing error type returned by error retrieval functions.
type ErrorInfo struct {
Level LogLevel
PackageName string
FuncName string
Message string
Timestamp time.Time
}
// Error implements the error interface for ErrorInfo.
func (e ErrorInfo) Error() string {
return fmt.Sprintf("[%s] %s.%s: %s", e.Level.String(), e.PackageName, e.FuncName, e.Message)
}
// String returns a string representation of the LogLevel.
func (l LogLevel) String() string {
switch l {
case LogLevelDebug:
return "DEBUG"
case LogLevelInfo:
return "INFO"
case LogLevelWarning:
return "WARNING"
case LogLevelFatal:
return "FATAL"
default:
return "UNKNOWN"
}
}
type errorStruct struct {
packageName string
fnName string
errType LogLevel
message string
timestamp time.Time
}
var (
errRing = core.NewRing[errorStruct](1536)
errorChan = make(chan errorStruct, 1024)
errorMutex = sync.Mutex{}
)
func init() {
// Initialize the error channel
go func() {
for err := range errorChan {
if errHandlingFunc := Config.GetDefaultErrHandlingFunc(); errHandlingFunc != nil {
go errHandlingFunc(err.errType, err.packageName, err.fnName, err.message)
}
errorMutex.Lock()
errRing.Push(err)
errorMutex.Unlock()
}
}()
}
func pushError(errType LogLevel, packageName, fnName, errMes string) {
err := errorStruct{
errType: errType,
packageName: packageName,
fnName: fnName,
message: errMes,
timestamp: time.Now(),
}
errorChan <- err
}
// PopError retrieves and removes the first error from the buffer.
// If the buffer is empty, it returns an empty string and LogLevelInfo.
func PopError(mode ErrPoppingMode) (LogLevel, string) {
errorMutex.Lock()
defer errorMutex.Unlock()
if errRing.Len() == 0 {
return LogLevelInfo, ""
}
var err errorStruct
switch mode {
case ErrPoppingModeFIFO:
err, _ = errRing.PopFront()
case ErrPoppingModeLIFO:
err, _ = errRing.PopBack()
}
return err.errType, err.message
}
func PopErrorByPackageName(packageName string, mode ErrPoppingMode) (LogLevel, string) {
errorMutex.Lock()
defer errorMutex.Unlock()
if errRing.Len() == 0 {
return LogLevelInfo, ""
}
idxToPop := -1
switch mode {
case ErrPoppingModeFIFO:
// Find the first occurrence
for i := 0; i < errRing.Len(); i++ {
entry, ok := errRing.Get(i)
if ok && entry.packageName == packageName {
idxToPop = i
break
}
}
case ErrPoppingModeLIFO:
// Find the last occurrence
for i := errRing.Len() - 1; i >= 0; i-- {
entry, ok := errRing.Get(i)
if ok && entry.packageName == packageName {
idxToPop = i
break
}
}
}
if idxToPop != -1 {
err, _ := errRing.DeleteAt(idxToPop)
return err.errType, err.message
}
return LogLevelInfo, "" // No error found for the given package name
}
func PopErrorByFuncName(packageName, funcName string, mode ErrPoppingMode) (LogLevel, string) {
errorMutex.Lock()
defer errorMutex.Unlock()
if errRing.Len() == 0 {
return LogLevelInfo, ""
}
idxToPop := -1
switch mode {
case ErrPoppingModeFIFO:
// Find the first occurrence
for i := 0; i < errRing.Len(); i++ {
entry, ok := errRing.Get(i)
if ok && entry.packageName == packageName && entry.fnName == funcName {
idxToPop = i
break
}
}
case ErrPoppingModeLIFO:
// Find the last occurrence
for i := errRing.Len() - 1; i >= 0; i-- {
entry, ok := errRing.Get(i)
if ok && entry.packageName == packageName && entry.fnName == funcName {
idxToPop = i
break
}
}
}
if idxToPop != -1 {
err, _ := errRing.DeleteAt(idxToPop)
return err.errType, err.message
}
return LogLevelInfo, "" // No error found for the given package and function name
}
func PopErrorAndCallback(mode ErrPoppingMode, callback func(errType LogLevel, packageName string, funcName string, errMsg string)) {
errorMutex.Lock()
defer errorMutex.Unlock()
if errRing.Len() == 0 {
return
}
var err errorStruct
switch mode {
case ErrPoppingModeFIFO:
err, _ = errRing.PopFront()
case ErrPoppingModeLIFO:
err, _ = errRing.PopBack()
}
callback(err.errType, err.packageName, err.fnName, err.message)
}
func ClearErrors() {
errorMutex.Lock()
defer errorMutex.Unlock()
errRing.Clear()
}
func GetErrorCount() int {
errorMutex.Lock()
defer errorMutex.Unlock()
return errRing.Len()
}
// HasError returns true if there are any errors in the buffer.
// This is a non-destructive check that doesn't modify the error buffer.
func HasError() bool {
errorMutex.Lock()
defer errorMutex.Unlock()
return errRing.Len() > 0
}
// HasErrorAboveLevel returns true if there are any errors at or above the specified level.
// This is a non-destructive check that doesn't modify the error buffer.
func HasErrorAboveLevel(level LogLevel) bool {
errorMutex.Lock()
defer errorMutex.Unlock()
for i := 0; i < errRing.Len(); i++ {
entry, ok := errRing.Get(i)
if ok && entry.errType >= level {
return true
}
}
return false
}
// PeekError returns the error at the specified position without removing it.
// Returns nil if the buffer is empty or index is out of bounds.
// Mode determines whether to peek from the front (FIFO) or back (LIFO).
func PeekError(mode ErrPoppingMode) *ErrorInfo {
errorMutex.Lock()
defer errorMutex.Unlock()
if errRing.Len() == 0 {
return nil
}
var e errorStruct
switch mode {
case ErrPoppingModeFIFO:
e, _ = errRing.Get(0)
case ErrPoppingModeLIFO:
e, _ = errRing.Get(errRing.Len() - 1)
}
return &ErrorInfo{
Level: e.errType,
PackageName: e.packageName,
FuncName: e.fnName,
Message: e.message,
Timestamp: e.timestamp,
}
}
// GetAllErrors returns a copy of all errors in the buffer without removing them.
// The returned slice is ordered from oldest to newest (FIFO order).
func GetAllErrors() []ErrorInfo {
errorMutex.Lock()
defer errorMutex.Unlock()
errSlice := errRing.ToSlice()
result := make([]ErrorInfo, len(errSlice))
for i, err := range errSlice {
result[i] = ErrorInfo{
Level: err.errType,
PackageName: err.packageName,
FuncName: err.fnName,
Message: err.message,
Timestamp: err.timestamp,
}
}
return result
}
// GetErrorsByLevel returns all errors at the specified level without removing them.
func GetErrorsByLevel(level LogLevel) []ErrorInfo {
errorMutex.Lock()
defer errorMutex.Unlock()
var result []ErrorInfo
for i := 0; i < errRing.Len(); i++ {
entry, ok := errRing.Get(i)
if ok && entry.errType == level {
result = append(result, ErrorInfo{
Level: entry.errType,
PackageName: entry.packageName,
FuncName: entry.fnName,
Message: entry.message,
Timestamp: entry.timestamp,
})
}
}
return result
}
// GetErrorsByPackage returns all errors from the specified package without removing them.
func GetErrorsByPackage(packageName string) []ErrorInfo {
errorMutex.Lock()
defer errorMutex.Unlock()
var result []ErrorInfo
for i := 0; i < errRing.Len(); i++ {
entry, ok := errRing.Get(i)
if ok && entry.packageName == packageName {
result = append(result, ErrorInfo{
Level: entry.errType,
PackageName: entry.packageName,
FuncName: entry.fnName,
Message: entry.message,
Timestamp: entry.timestamp,
})
}
}
return result
}
// PopAllErrors retrieves and removes all errors from the buffer.
// The returned slice is ordered from oldest to newest (FIFO order).
func PopAllErrors() []ErrorInfo {
errorMutex.Lock()
defer errorMutex.Unlock()
errSlice := errRing.ToSlice()
result := make([]ErrorInfo, len(errSlice))
for i, err := range errSlice {
result[i] = ErrorInfo{
Level: err.errType,
PackageName: err.packageName,
FuncName: err.fnName,
Message: err.message,
Timestamp: err.timestamp,
}
}
errRing.Clear()
return result
}
// PopErrorInfo retrieves and removes an error with full context information.
// Returns nil if the buffer is empty.
func PopErrorInfo(mode ErrPoppingMode) *ErrorInfo {
errorMutex.Lock()
defer errorMutex.Unlock()
if errRing.Len() == 0 {
return nil
}
var e errorStruct
switch mode {
case ErrPoppingModeFIFO:
e, _ = errRing.PopFront()
case ErrPoppingModeLIFO:
e, _ = errRing.PopBack()
}
return &ErrorInfo{
Level: e.errType,
PackageName: e.packageName,
FuncName: e.fnName,
Message: e.message,
Timestamp: e.timestamp,
}
}