blob: 6338118e6e5358acc8347f3f7b914183b761f6eb [file] [log] [blame]
Serge Bazanski5faa2fc2020-09-07 14:09:30 +02001// 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"
Serge Bazanskib0272182020-11-02 18:39:44 +010021 "strconv"
22 "strings"
Serge Bazanski5faa2fc2020-09-07 14:09:30 +020023 "time"
Serge Bazanskib0272182020-11-02 18:39:44 +010024
25 apb "git.monogon.dev/source/nexantic.git/core/proto/api"
Serge Bazanski5faa2fc2020-09-07 14:09:30 +020026)
27
Serge Bazanski12971d62020-11-17 12:12:58 +010028// LeveledPayload is a log entry for leveled logs (as per leveled.go). It contains the input to these calls (severity and
29// message split into newline-delimited messages) and additional metadata that would be usually seen in a text
30// representation of a leveled log entry.
Serge Bazanski1bfa0c22020-10-14 16:45:07 +020031type LeveledPayload struct {
Serge Bazanski12971d62020-11-17 12:12:58 +010032 // messages is the list of messages contained in this payload. This list is built from splitting up the given message
33 // from the user by newline.
34 messages []string
Serge Bazanski5faa2fc2020-09-07 14:09:30 +020035 // timestamp is the time at which this message was emitted.
36 timestamp time.Time
37 // severity is the leveled Severity at which this message was emitted.
38 severity Severity
39 // file is the filename of the caller that emitted this message.
40 file string
41 // line is the line number within the file of the caller that emitted this message.
42 line int
43}
44
Serge Bazanski12971d62020-11-17 12:12:58 +010045// String returns a canonical representation of this payload as a single string prefixed with metadata. If the original
46// message was logged with newlines, this representation will also contain newlines, with each original message part
47// prefixed by the metadata.
48// For an alternative call that will instead return a canonical prefix and a list of lines in the message, see Strings().
Serge Bazanski1bfa0c22020-10-14 16:45:07 +020049func (p *LeveledPayload) String() string {
Serge Bazanski12971d62020-11-17 12:12:58 +010050 prefix, lines := p.Strings()
51 res := make([]string, len(p.messages))
52 for i, line := range lines {
53 res[i] = fmt.Sprintf("%s%s", prefix, line)
54 }
55 return strings.Join(res, "\n")
56}
Serge Bazanski5faa2fc2020-09-07 14:09:30 +020057
Serge Bazanski12971d62020-11-17 12:12:58 +010058// Strings returns the canonical representation of this payload split into a prefix and all lines that were contained in
59// the original message. This is meant to be displayed to the user by showing the prefix before each line, concatenated
60// together - possibly in a table form with the prefixes all unified with a rowspan-like mechanism.
61//
62// For example, this function can return:
63// prefix = "I1102 17:20:06.921395 foo.go:42] "
64// lines = []string{"current tags:", " - one", " - two"}
65//
66// With this data, the result should be presented to users this way in text form:
67// I1102 17:20:06.921395 foo.go:42] current tags:
68// I1102 17:20:06.921395 foo.go:42] - one
69// I1102 17:20:06.921395 foo.go:42] - two
70//
71// Or, in a table layout:
72// .-----------------------------------------------------------.
73// | I1102 17:20:06.921395 0 foo.go:42] : current tags: |
74// | :------------------|
75// | : - one |
76// | :------------------|
77// | : - two |
78// '-----------------------------------------------------------'
79//
80func (p *LeveledPayload) Strings() (prefix string, lines []string) {
Serge Bazanski5faa2fc2020-09-07 14:09:30 +020081 _, month, day := p.timestamp.Date()
82 hour, minute, second := p.timestamp.Clock()
83 nsec := p.timestamp.Nanosecond() / 1000
84
Serge Bazanski12971d62020-11-17 12:12:58 +010085 // Same format as in glog, but without treadid.
86 // Lmmdd hh:mm:ss.uuuuuu file:line]
Serge Bazanski5faa2fc2020-09-07 14:09:30 +020087 // TODO(q3k): rewrite this to printf-less code.
Serge Bazanski12971d62020-11-17 12:12:58 +010088 prefix = fmt.Sprintf("%s%02d%02d %02d:%02d:%02d.%06d %s:%d] ", p.severity, month, day, hour, minute, second, nsec, p.file, p.line)
89
90 lines = p.messages
91 return
Serge Bazanski5faa2fc2020-09-07 14:09:30 +020092}
93
Serge Bazanski12971d62020-11-17 12:12:58 +010094// Message returns the inner message lines of this entry, ie. what was passed to the actual logging method, but split by
95// newlines.
96func (p *LeveledPayload) Messages() []string { return p.messages }
97
98func (p *LeveledPayload) MessagesJoined() string { return strings.Join(p.messages, "\n") }
Serge Bazanski5faa2fc2020-09-07 14:09:30 +020099
100// Timestamp returns the time at which this entry was logged.
Serge Bazanski1bfa0c22020-10-14 16:45:07 +0200101func (p *LeveledPayload) Timestamp() time.Time { return p.timestamp }
Serge Bazanski5faa2fc2020-09-07 14:09:30 +0200102
103// Location returns a string in the form of file_name:line_number that shows the origin of the log entry in the
104// program source.
Serge Bazanski1bfa0c22020-10-14 16:45:07 +0200105func (p *LeveledPayload) Location() string { return fmt.Sprintf("%s:%d", p.file, p.line) }
Serge Bazanski5faa2fc2020-09-07 14:09:30 +0200106
107// Severity returns the Severity with which this entry was logged.
Serge Bazanski1bfa0c22020-10-14 16:45:07 +0200108func (p *LeveledPayload) Severity() Severity { return p.severity }
Serge Bazanskib0272182020-11-02 18:39:44 +0100109
110// Proto converts a LeveledPayload to protobuf format.
111func (p *LeveledPayload) Proto() *apb.LogEntry_Leveled {
112 return &apb.LogEntry_Leveled{
Serge Bazanski12971d62020-11-17 12:12:58 +0100113 Lines: p.Messages(),
Serge Bazanskib0272182020-11-02 18:39:44 +0100114 Timestamp: p.Timestamp().UnixNano(),
115 Severity: p.Severity().ToProto(),
116 Location: p.Location(),
117 }
118}
119
120// LeveledPayloadFromProto parses a protobuf message into the internal format.
121func LeveledPayloadFromProto(p *apb.LogEntry_Leveled) (*LeveledPayload, error) {
122 severity, err := SeverityFromProto(p.Severity)
123 if err != nil {
124 return nil, fmt.Errorf("could not convert severity: %w", err)
125 }
126 parts := strings.Split(p.Location, ":")
127 if len(parts) != 2 {
128 return nil, fmt.Errorf("invalid location, must be two :-delimited parts, is %d parts", len(parts))
129 }
130 file := parts[0]
131 line, err := strconv.Atoi(parts[1])
132 if err != nil {
133 return nil, fmt.Errorf("invalid location line number: %w", err)
134 }
135 return &LeveledPayload{
Serge Bazanski12971d62020-11-17 12:12:58 +0100136 messages: p.Lines,
Serge Bazanskib0272182020-11-02 18:39:44 +0100137 timestamp: time.Unix(0, p.Timestamp),
138 severity: severity,
139 file: file,
140 line: line,
141 }, nil
142}