-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmd.go
57 lines (47 loc) · 965 Bytes
/
cmd.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
package main
import (
"flag"
"fmt"
"github.com/MaxSchaefer/macos-log-stream/pkg/mls"
"os"
"os/signal"
"syscall"
)
type Cmd struct {
fs *flag.FlagSet
Predicate string
}
func NewCmd() *Cmd {
cmd := &Cmd{
fs: flag.NewFlagSet("cmd", flag.ExitOnError),
}
cmd.fs.StringVar(&cmd.Predicate, "predicate", "", `
take a look at '> man log' or visit
https://eclecticlight.co/2016/10/17/log-a-primer-on-predicates/
`)
return cmd
}
func (c *Cmd) Exec(args []string) error {
if err := c.fs.Parse(args); err != nil {
return err
}
return c.run()
}
func (c *Cmd) run() error {
logs := mls.NewLogs()
logs.Predicate = c.Predicate
signalChannel := make(chan os.Signal)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalChannel
logs.StopGathering()
os.Exit(0)
}()
if err := logs.StartGathering(); err != nil {
panic(err)
}
for log := range logs.Channel {
fmt.Println(log.EventMessage)
}
return nil
}