blob: 81d1a4289109f43c7207e67592ec47c61b016b4f [file] [log] [blame]
Serge Bazanskiedf5c4f2020-11-25 13:45:31 +01001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package logtree
18
19import (
20 "fmt"
21 "strings"
22
23 "git.monogon.dev/source/nexantic.git/core/pkg/logbuffer"
24 apb "git.monogon.dev/source/nexantic.git/core/proto/api"
25)
26
27// LogEntry contains a log entry, combining both leveled and raw logging into a single stream of events. A LogEntry
28// will contain exactly one of either LeveledPayload or RawPayload.
29type LogEntry struct {
30 // If non-nil, this is a leveled logging entry.
31 Leveled *LeveledPayload
32 // If non-nil, this is a raw logging entry line.
33 Raw *logbuffer.Line
34 // DN from which this entry was logged.
35 DN DN
36}
37
38// String returns a canonical representation of this payload as a single string prefixed with metadata. If the entry is
39// a leveled log entry that originally was logged with newlines this representation will also contain newlines, with
40// each original message part prefixed by the metadata.
41// For an alternative call that will instead return a canonical prefix and a list of lines in the message, see Strings().
42func (l *LogEntry) String() string {
43 if l.Leveled != nil {
44 prefix, messages := l.Leveled.Strings()
45 res := make([]string, len(messages))
46 for i, m := range messages {
47 res[i] = fmt.Sprintf("%-32s %s%s", l.DN, prefix, m)
48 }
49 return strings.Join(res, "\n")
50 }
51 if l.Raw != nil {
52 return fmt.Sprintf("%-32s R %s", l.DN, l.Raw)
53 }
54 return "INVALID"
55}
56
57// Strings returns the canonical representation of this payload split into a prefix and all lines that were contained in
58// the original message. This is meant to be displayed to the user by showing the prefix before each line, concatenated
59// together - possibly in a table form with the prefixes all unified with a rowspan-like mechanism.
60//
61// For example, this function can return:
62// prefix = "root.foo.bar I1102 17:20:06.921395 0 foo.go:42] "
63// lines = []string{"current tags:", " - one", " - two"}
64//
65// With this data, the result should be presented to users this way in text form:
66// root.foo.bar I1102 17:20:06.921395 foo.go:42] current tags:
67// root.foo.bar I1102 17:20:06.921395 foo.go:42] - one
68// root.foo.bar I1102 17:20:06.921395 foo.go:42] - two
69//
70// Or, in a table layout:
71// .-------------------------------------------------------------------------------------.
72// | root.foo.bar I1102 17:20:06.921395 foo.go:42] : current tags: |
73// | :------------------|
74// | : - one |
75// | :------------------|
76// | : - two |
77// '-------------------------------------------------------------------------------------'
78//
79func (l *LogEntry) Strings() (prefix string, lines []string) {
80 if l.Leveled != nil {
81 prefix, messages := l.Leveled.Strings()
82 prefix = fmt.Sprintf("%-32s %s", l.DN, prefix)
83 return prefix, messages
84 }
85 if l.Raw != nil {
86 return fmt.Sprintf("%-32s R ", l.DN), []string{l.Raw.Data}
87 }
88 return "INVALID ", []string{"INVALID"}
89}
90
91// Convert this LogEntry to proto. Returned value may be nil if given LogEntry is invalid, eg. contains neither a Raw
92// nor Leveled entry.
93func (l *LogEntry) Proto() *apb.LogEntry {
94 p := &apb.LogEntry{
95 Dn: string(l.DN),
96 }
97 switch {
98 case l.Leveled != nil:
99 leveled := l.Leveled
100 p.Kind = &apb.LogEntry_Leveled_{
101 Leveled: leveled.Proto(),
102 }
103 case l.Raw != nil:
104 raw := l.Raw
105 p.Kind = &apb.LogEntry_Raw_{
106 Raw: raw.ProtoLog(),
107 }
108 default:
109 return nil
110 }
111 return p
112}
113
114// Parse a proto LogEntry back into internal structure. This can be used in log proto API consumers to easily print
115// received log entries.
116func LogEntryFromProto(l *apb.LogEntry) (*LogEntry, error) {
117 dn := DN(l.Dn)
118 if _, err := dn.Path(); err != nil {
119 return nil, fmt.Errorf("could not convert DN: %w", err)
120 }
121 res := &LogEntry{
122 DN: dn,
123 }
124 switch inner := l.Kind.(type) {
125 case *apb.LogEntry_Leveled_:
126 leveled, err := LeveledPayloadFromProto(inner.Leveled)
127 if err != nil {
128 return nil, fmt.Errorf("could not convert leveled entry: %w", err)
129 }
130 res.Leveled = leveled
131 case *apb.LogEntry_Raw_:
132 line, err := logbuffer.LineFromLogProto(inner.Raw)
133 if err != nil {
134 return nil, fmt.Errorf("could not convert raw entry: %w", err)
135 }
136 res.Raw = line
137 default:
138 return nil, fmt.Errorf("proto has neither Leveled nor Raw set")
139 }
140 return res, nil
141}