-
Notifications
You must be signed in to change notification settings - Fork 39
/
handle_error.go
52 lines (43 loc) · 960 Bytes
/
handle_error.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
package main
import (
"github.com/aler9/gomavlib"
"log"
"sync"
"time"
)
type errorHandler struct {
printErrorsSingularly bool
errorCount int
errorCountMutex sync.Mutex
}
func newErrorHandler(printErrorsSingularly bool) (*errorHandler, error) {
eh := &errorHandler{
printErrorsSingularly: printErrorsSingularly,
}
return eh, nil
}
func (eh *errorHandler) run() {
// print errors in group
if eh.printErrorsSingularly == false {
for {
time.Sleep(5 * time.Second)
func() {
eh.errorCountMutex.Lock()
defer eh.errorCountMutex.Unlock()
if eh.errorCount > 0 {
log.Printf("%d errors in the last 5 seconds", eh.errorCount)
eh.errorCount = 0
}
}()
}
}
}
func (eh *errorHandler) onEventError(evt *gomavlib.EventParseError) {
if eh.printErrorsSingularly == true {
log.Printf("ERR: %s", evt.Error)
return
}
eh.errorCountMutex.Lock()
defer eh.errorCountMutex.Unlock()
eh.errorCount++
}