-
Notifications
You must be signed in to change notification settings - Fork 13
/
log.go
54 lines (43 loc) · 1.36 KB
/
log.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
package events
import (
"regexp"
"github.com/hpcloud/tail"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/process"
)
type EventLog struct {
out output.Outputer
cfg process.Configurable
}
type EventLogParams struct {
Follow bool
}
func NewLog(prime primeable) *EventLog {
return &EventLog{
prime.Output(),
prime.Config(),
}
}
func (e *EventLog) Run(params *EventLogParams) error {
pid := process.ActivationPID(e.cfg)
if pid == -1 {
return locale.NewInputError("err_eventlog_pid", "Could not find parent process ID, make sure you're running this command from inside an activated state (run '[ACTIONABLE]state activate[/RESET]' first).")
}
filepath := logging.FilePathFor(logging.FileNameFor(int(pid)))
tailer, err := tail.TailFile(filepath, tail.Config{Follow: params.Follow})
if err != nil {
return locale.WrapError(err, "err_tail_start", "Could not tail logging file at {{.V0}}.", logging.FilePath())
}
matcher, err := regexp.Compile(`(?:\s|^)(?:\w+-|)Event:`)
if err != nil {
return locale.NewError("err_invalid_rx", "Could not create regex matcher. Please contact support, this should not happen.")
}
for line := range tailer.Lines {
if matcher.MatchString(line.Text) {
e.out.Print(line.Text)
}
}
return nil
}