-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdescribe_events.go
145 lines (128 loc) · 3.24 KB
/
describe_events.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
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
package main
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/fatih/color"
"github.com/nokia/ntt/internal/lnav"
"github.com/nokia/ntt/k3/log"
"github.com/spf13/cobra"
)
var (
DescribeEventsCommand = &cobra.Command{
Use: "describe-events",
Short: "Outputs a description of k3 runtime events to stdin",
Hidden: true,
RunE: describe,
}
formatLnav bool
)
func init() {
DescribeEventsCommand.Flags().BoolVar(&formatLnav, "lnav", false, "Output in lnav format")
RootCommand.AddCommand(DescribeEventsCommand)
}
type Event struct {
Name string `json:"name"`
Description string `json:"description"`
Fields []string `json:"fields,omitempty"`
log.Category `json:"-"`
}
func describe(cmd *cobra.Command, args []string) error {
var events []Event
for _, c := range log.Categories {
fields := strings.Split(c.String(), "|")
events = append(events, Event{
Name: fields[0],
Description: fields[1],
Fields: fields[2:],
Category: c,
})
}
sort.Slice(events, func(i, j int) bool {
return strings.ToLower(events[i].Name) < strings.ToLower(events[j].Name)
})
switch {
case formatLnav:
return OutputLnavSpec(events)
case Format() == "json":
return OutputJSON(events)
default:
return OutputText(events)
}
}
func OutputLnavSpec(events []Event) error {
regexes := make(map[string]lnav.Regex)
for _, e := range events {
pattern := fmt.Sprintf(`^(?<timestamp>\d{8}T\d{6}\.\d{6})\|(?<eventtype>%s)\|(?<component>(\?|\w+))(=(?<location>[^\|]*))?`, e.Name)
for range e.Fields {
pattern += fmt.Sprintf(`\|.*`)
}
pattern += "$"
regexes[e.Name] = lnav.Regex{
Pattern: pattern,
}
}
formats := map[string]lnav.Format{
"ttcn3_log": {
Title: "TTCN3 Log Format",
Description: "Format describing TTCN3 log files.",
URL: []string{"https://pkg.go.dev/github.com/nokia/ntt/k3/log#Category"},
TimestampFormat: []string{"%Y%m%dT%H%M%S.%f"},
OrderedByTime: true,
Regex: regexes,
OPidField: "eventtype",
LevelField: "verdict",
Level: map[string]string{
"error": "error|[A-Z]{4}|DIV0|UTF8|inconc",
"trace": "none",
"notice": "pass",
"warning": "fail",
},
Value: map[string]lnav.Value{
"eventtype": {
Kind: "string",
Identifier: true,
Hidden: false,
Description: "The k3r event type",
},
"component": {Kind: "string", Identifier: true},
"location": {Kind: "string", Identifier: false},
"timestamp": {Kind: "string", Identifier: false},
},
},
}
spec := lnav.Spec{
Schema: "https://lnav.org/schemas/format-v1.schema.json",
Formats: formats,
}
b, err := json.MarshalIndent(spec, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}
func OutputJSON(events []Event) error {
b, err := json.MarshalIndent(events, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
return nil
}
func OutputText(events []Event) error {
for _, e := range events {
c := color.New(color.Bold)
if e.IsError() {
c = color.New(color.Bold, color.FgRed)
}
c.Printf("%s", e.Name)
fmt.Printf(": %s\n", e.Description)
for i, f := range e.Fields {
fmt.Printf(" %d: %s\n", 4+i, f)
}
fmt.Println()
}
return nil
}