forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_logger.go
101 lines (85 loc) · 2.29 KB
/
event_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
package eventlogger
import (
"fmt"
"time"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
durationfmt "github.com/cloudfoundry/bosh-micro-cli/eventlogger/durationfmt"
bmui "github.com/cloudfoundry/bosh-micro-cli/ui"
)
type EventFilter interface {
Filter(*Event) error
}
type Event struct {
Time time.Time
Stage string
Task string
State EventState
Message string
}
type EventState string
const (
Started EventState = "started"
Finished EventState = "finished"
Failed EventState = "failed"
Skipped EventState = "skipped"
)
type EventLogger interface {
NewStage(string) Stage
AddEvent(event Event) error
StartStage(string)
FinishStage(string)
}
type eventLogger struct {
ui bmui.UI
startedTasks map[string]time.Time
filters []EventFilter
}
func NewEventLogger(ui bmui.UI) EventLogger {
return &eventLogger{
ui: ui,
startedTasks: make(map[string]time.Time),
filters: []EventFilter{},
}
}
func (e *eventLogger) NewStage(name string) Stage {
return NewStage(name, e)
}
func NewEventLoggerWithFilters(ui bmui.UI, filters []EventFilter) EventLogger {
return &eventLogger{
ui: ui,
startedTasks: make(map[string]time.Time),
filters: filters,
}
}
func (e *eventLogger) AddEvent(event Event) error {
if e.filters != nil && len(e.filters) > 0 {
for _, filter := range e.filters {
filter.Filter(&event)
}
}
key := fmt.Sprintf("%s > %s...", event.Stage, event.Task)
switch event.State {
case Started:
e.ui.Say(fmt.Sprintf("Started %s", key))
e.startedTasks[key] = event.Time
case Finished:
duration := event.Time.Sub(e.startedTasks[key])
e.ui.Sayln(fmt.Sprintf(" done. (%s)", durationfmt.Format(duration)))
case Failed:
duration := event.Time.Sub(e.startedTasks[key])
e.ui.Sayln(fmt.Sprintf(" failed (%s). (%s)", event.Message, durationfmt.Format(duration)))
case Skipped:
duration := event.Time.Sub(e.startedTasks[key])
e.ui.Sayln(fmt.Sprintf(" skipped (%s). (%s)", event.Message, durationfmt.Format(duration)))
default:
return bosherr.Errorf("Unsupported event state '%s'", event.State)
}
return nil
}
func (e *eventLogger) FinishStage(name string) {
e.ui.Sayln(fmt.Sprintf("Done %s", name))
}
func (e *eventLogger) StartStage(name string) {
e.ui.Sayln("")
e.ui.Sayln(fmt.Sprintf("Started %s", name))
}