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" |
| 21 | "time" |
| 22 | ) |
| 23 | |
Serge Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 24 | // LeveledPayload is a log entry for leveled logs (as per leveled.go). It contains not only the log message itself and |
| 25 | // its severity, but also additional metadata that would be usually seen in a text representation of a leveled log entry. |
| 26 | type LeveledPayload struct { |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 27 | // message is the log message, rendered from a leveled log call like Infof(), Warningf(), ... |
| 28 | message string |
| 29 | // timestamp is the time at which this message was emitted. |
| 30 | timestamp time.Time |
| 31 | // severity is the leveled Severity at which this message was emitted. |
| 32 | severity Severity |
| 33 | // file is the filename of the caller that emitted this message. |
| 34 | file string |
| 35 | // line is the line number within the file of the caller that emitted this message. |
| 36 | line int |
| 37 | } |
| 38 | |
Serge Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 39 | func (p *LeveledPayload) String() string { |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 40 | // Same format as in glog: |
| 41 | // Lmmdd hh:mm:ss.uuuuuu threadid file:line] |
| 42 | // Except, threadid is (currently) always zero. In the future this field might be used for something different. |
| 43 | |
| 44 | _, month, day := p.timestamp.Date() |
| 45 | hour, minute, second := p.timestamp.Clock() |
| 46 | nsec := p.timestamp.Nanosecond() / 1000 |
| 47 | |
| 48 | // TODO(q3k): rewrite this to printf-less code. |
| 49 | return fmt.Sprintf("%s%02d%02d %02d:%02d:%02d.%06d % 7d %s:%d] %s", p.severity, month, day, hour, minute, second, |
| 50 | nsec, 0, p.file, p.line, p.message) |
| 51 | } |
| 52 | |
| 53 | // Message returns the inner message of this entry, ie. what was passed to the actual logging method. |
Serge Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 54 | func (p *LeveledPayload) Message() string { return p.message } |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 55 | |
| 56 | // Timestamp returns the time at which this entry was logged. |
Serge Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 57 | func (p *LeveledPayload) Timestamp() time.Time { return p.timestamp } |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 58 | |
| 59 | // Location returns a string in the form of file_name:line_number that shows the origin of the log entry in the |
| 60 | // program source. |
Serge Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 61 | 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] | 62 | |
| 63 | // Severity returns the Severity with which this entry was logged. |
Serge Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 64 | func (p *LeveledPayload) Severity() Severity { return p.severity } |