Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 1 | // 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 | |
| 17 | package logtree |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 21 | "strconv" |
| 22 | "strings" |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 23 | "time" |
Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 24 | |
Serge Bazanski | 31370b0 | 2021-01-07 16:31:14 +0100 | [diff] [blame] | 25 | apb "source.monogon.dev/metropolis/proto/api" |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 26 | ) |
| 27 | |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 28 | // 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 Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 31 | type LeveledPayload struct { |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 32 | // 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 Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 35 | // 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 Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 45 | // 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 Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 49 | func (p *LeveledPayload) String() string { |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 50 | 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 Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 57 | |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 58 | // 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 | // |
| 80 | func (p *LeveledPayload) Strings() (prefix string, lines []string) { |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 81 | _, month, day := p.timestamp.Date() |
| 82 | hour, minute, second := p.timestamp.Clock() |
| 83 | nsec := p.timestamp.Nanosecond() / 1000 |
| 84 | |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 85 | // Same format as in glog, but without treadid. |
| 86 | // Lmmdd hh:mm:ss.uuuuuu file:line] |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 87 | // TODO(q3k): rewrite this to printf-less code. |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 88 | 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 Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 94 | // Message returns the inner message lines of this entry, ie. what was passed to the actual logging method, but split by |
| 95 | // newlines. |
| 96 | func (p *LeveledPayload) Messages() []string { return p.messages } |
| 97 | |
| 98 | func (p *LeveledPayload) MessagesJoined() string { return strings.Join(p.messages, "\n") } |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 99 | |
| 100 | // Timestamp returns the time at which this entry was logged. |
Serge Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 101 | func (p *LeveledPayload) Timestamp() time.Time { return p.timestamp } |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 102 | |
| 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 Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 105 | func (p *LeveledPayload) Location() string { return fmt.Sprintf("%s:%d", p.file, p.line) } |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 106 | |
| 107 | // Severity returns the Severity with which this entry was logged. |
Serge Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 108 | func (p *LeveledPayload) Severity() Severity { return p.severity } |
Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 109 | |
| 110 | // Proto converts a LeveledPayload to protobuf format. |
| 111 | func (p *LeveledPayload) Proto() *apb.LogEntry_Leveled { |
| 112 | return &apb.LogEntry_Leveled{ |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 113 | Lines: p.Messages(), |
Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 114 | 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. |
| 121 | func 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 Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 136 | messages: p.Lines, |
Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 137 | timestamp: time.Unix(0, p.Timestamp), |
| 138 | severity: severity, |
| 139 | file: file, |
| 140 | line: line, |
| 141 | }, nil |
| 142 | } |