Skip to content

Commit

Permalink
util/log: share more encoding code
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
knz committed Jun 13, 2023
1 parent f93a33d commit 5cfcccb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 122 deletions.
78 changes: 53 additions & 25 deletions pkg/util/log/format_crdb_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,25 +223,29 @@ func (f formatCrdbV1) formatEntry(entry logEntry) *buffer {
entry.header, f.showCounter, f.colorProfile, f.loc)
}

// formatEntryInternalV1 renders a log entry.
// Log lines are colorized depending on severity.
// It uses a newly allocated *buffer. The caller is responsible
// for calling putBuffer() afterwards.
func formatLogEntryInternalV1(
entry logpb.Entry, isHeader, showCounter bool, cp ttycolor.Profile, loc *time.Location,
) *buffer {
buf := getBuffer()
if entry.Line < 0 {
entry.Line = 0 // not a real line number, but acceptable to someDigits
func writeCrdbHeader(
buf *buffer,
cp ttycolor.Profile,
sev logpb.Severity,
ch logpb.Channel,
file string,
line int,
ts int64,
loc *time.Location,
gid int,
redactable bool,
) {
if line < 0 {
line = 0 // not a real line number, but acceptable to someDigits
}
if entry.Severity > severity.FATAL || entry.Severity <= severity.UNKNOWN {
entry.Severity = severity.INFO // for safety.
if sev > severity.FATAL || sev <= severity.UNKNOWN {
sev = severity.INFO // for safety.
}

tmp := buf.tmp[:len(buf.tmp)]
var n int
var prefix []byte
switch entry.Severity {
switch sev {
case severity.INFO:
prefix = cp[ttycolor.Cyan]
case severity.WARNING:
Expand All @@ -250,17 +254,17 @@ func formatLogEntryInternalV1(
prefix = cp[ttycolor.Red]
}
n += copy(tmp, prefix)
tmp[n] = severityChar[entry.Severity-1]
// Lyymmdd hh:mm:ss.uuuuuu file:line
tmp[n] = severityChar[sev-1]
n++

if loc == nil {
// Default time zone (UTC).
// Avoid Fprintf, for speed. The format is so simple that we can do it quickly by hand.
// It's worth about 3X. Fprintf is hard.
now := timeutil.Unix(0, entry.Time)
now := timeutil.Unix(0, ts)
year, month, day := now.Date()
hour, minute, second := now.Clock()
// Lyymmdd hh:mm:ss.uuuuuu file:line
if year < 2000 {
year = 2000
}
Expand All @@ -285,29 +289,32 @@ func formatLogEntryInternalV1(
// Slooooow path.
buf.Write(tmp[:n])
n = 0
now := timeutil.Unix(0, entry.Time).In(loc)
now := timeutil.Unix(0, ts).In(loc)
buf.WriteString(now.Format("060102"))
buf.Write(cp[ttycolor.Gray])
buf.WriteByte(' ')
buf.WriteString(now.Format("15:04:05.000000-070000"))
}

tmp[n] = ' '
n++
if entry.Goroutine > 0 {
n += buf.someDigits(n, int(entry.Goroutine))
if gid >= 0 {
n += buf.someDigits(n, gid)
tmp[n] = ' '
n++
}
if !isHeader && entry.Channel != 0 {

if ch != 0 {
// Prefix the filename with the channel number.
n += buf.someDigits(n, int(entry.Channel))
n += buf.someDigits(n, int(ch))
tmp[n] = '@'
n++
}

buf.Write(tmp[:n])
buf.WriteString(entry.File)
buf.WriteString(file)
tmp[0] = ':'
n = buf.someDigits(1, int(entry.Line))
n = buf.someDigits(1, line)
n++
// Reset the color to default.
n += copy(tmp[n:], cp[ttycolor.Reset])
Expand All @@ -316,7 +323,7 @@ func formatLogEntryInternalV1(
// If redaction is enabled, indicate that the current entry has
// markers. This indicator is used in the log parser to determine
// which redaction strategy to adopt.
if entry.Redactable {
if redactable {
copy(tmp[n:], redactableIndicatorBytes)
n += len(redactableIndicatorBytes)
}
Expand All @@ -327,6 +334,26 @@ func formatLogEntryInternalV1(
tmp[n] = ' '
n++
buf.Write(tmp[:n])
}

// formatEntryInternalV1 renders a log entry.
// Log lines are colorized depending on severity.
// It uses a newly allocated *buffer. The caller is responsible
// for calling putBuffer() afterwards.
func formatLogEntryInternalV1(
entry logpb.Entry, isHeader, showCounter bool, cp ttycolor.Profile, loc *time.Location,
) *buffer {
buf := getBuffer()
ch := entry.Channel
if isHeader {
ch = 0
}
gid := int(entry.Goroutine)
if gid == 0 {
// In the -v1 format, a goroutine ID of 0 is hidden.
gid = -1
}
writeCrdbHeader(buf, cp, entry.Severity, ch, entry.File, int(entry.Line), entry.Time, loc, gid, entry.Redactable)

// The remainder is variable-length and could exceed
// the static size of tmp. But we do have an upper bound.
Expand All @@ -353,7 +380,8 @@ func formatLogEntryInternalV1(

// Display the counter if set and enabled.
if showCounter && entry.Counter > 0 {
n = buf.someDigits(0, int(entry.Counter))
tmp := buf.tmp[:len(buf.tmp)]
n := buf.someDigits(0, int(entry.Counter))
tmp[n] = ' '
n++
buf.Write(tmp[:n])
Expand Down
100 changes: 3 additions & 97 deletions pkg/util/log/format_crdb_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ import (
"unicode/utf8"

"github.com/cockroachdb/cockroach/pkg/base/serverident"
"github.com/cockroachdb/cockroach/pkg/util/log/channel"
"github.com/cockroachdb/cockroach/pkg/util/log/logpb"
"github.com/cockroachdb/cockroach/pkg/util/log/severity"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
Expand Down Expand Up @@ -255,101 +253,8 @@ func (f formatCrdbV2) formatEntry(entry logEntry) *buffer {
// preserve cross-version compatibility with at least
// one version backwards.
buf := getBuffer()
if entry.line < 0 {
entry.line = 0 // not a real line number, but acceptable to someDigits
}
if entry.sev > severity.FATAL || entry.sev <= severity.UNKNOWN {
entry.sev = severity.INFO // for safety.
}

tmp := buf.tmp[:len(buf.tmp)]
var n int
var prefix []byte
cp := f.colorProfile
switch entry.sev {
case severity.INFO:
prefix = cp[ttycolor.Cyan]
case severity.WARNING:
prefix = cp[ttycolor.Yellow]
case severity.ERROR, severity.FATAL:
prefix = cp[ttycolor.Red]
}
n += copy(tmp, prefix)
// Lyymmdd hh:mm:ss.uuuuuu file:line
tmp[n] = severityChar[entry.sev-1]
n++

if f.loc == nil {
// Default time zone (UTC).
// Avoid Fprintf, for speed. The format is so simple that we can do it quickly by hand.
// It's worth about 3X. Fprintf is hard.
now := timeutil.Unix(0, entry.ts)
year, month, day := now.Date()
hour, minute, second := now.Clock()
if year < 2000 {
year = 2000
}
n += buf.twoDigits(n, year-2000)
n += buf.twoDigits(n, int(month))
n += buf.twoDigits(n, day)
n += copy(tmp[n:], cp[ttycolor.Gray]) // gray for time, file & line
tmp[n] = ' '
n++
n += buf.twoDigits(n, hour)
tmp[n] = ':'
n++
n += buf.twoDigits(n, minute)
tmp[n] = ':'
n++
n += buf.twoDigits(n, second)
tmp[n] = '.'
n++
n += buf.nDigits(6, n, now.Nanosecond()/1000, '0')
} else {
// Time zone was specified.
// Slooooow path.
buf.Write(tmp[:n])
n = 0
now := timeutil.Unix(0, entry.ts).In(f.loc)
buf.WriteString(now.Format("060102"))
buf.Write(cp[ttycolor.Gray])
buf.WriteByte(' ')
buf.WriteString(now.Format("15:04:05.000000-070000"))
}
tmp[n] = ' '
n++
n += buf.someDigits(n, int(entry.gid))
tmp[n] = ' '
n++
if entry.ch != channel.DEV {
// Prefix the filename with the channel number.
n += buf.someDigits(n, int(entry.ch))
tmp[n] = '@'
n++
}
buf.Write(tmp[:n])
buf.WriteString(entry.file)
tmp[0] = ':'
n = buf.someDigits(1, entry.line)
n++
// Reset the color to default.
n += copy(tmp[n:], cp[ttycolor.Reset])
tmp[n] = ' '
n++
// If redaction is enabled, indicate that the current entry has
// markers. This indicator is used in the log parser to determine
// which redaction strategy to adopt.
if entry.payload.redactable {
copy(tmp[n:], redactableIndicatorBytes)
n += len(redactableIndicatorBytes)
}
// Note: when the redactable indicator is not introduced
// there are two spaces next to each other. This is intended
// and should be preserved for backward-compatibility with
// 3rd party log parsers.
tmp[n] = ' '
n++
buf.Write(tmp[:n])
writeCrdbHeader(buf, cp, entry.sev, entry.ch, entry.file, entry.line, entry.ts, f.loc, int(entry.gid), entry.payload.redactable)

// The remainder is variable-length and could exceed
// the static size of tmp. But we do have a best-case upper bound.
Expand Down Expand Up @@ -379,7 +284,8 @@ func (f formatCrdbV2) formatEntry(entry logEntry) *buffer {

// Display the counter if set and enabled.
if entry.counter > 0 {
n = buf.someDigits(0, int(entry.counter))
tmp := buf.tmp[:len(buf.tmp)]
n := buf.someDigits(0, int(entry.counter))
buf.Write(cp[ttycolor.Cyan])
buf.Write(tmp[:n])
buf.Write(cp[ttycolor.Reset])
Expand Down

0 comments on commit 5cfcccb

Please sign in to comment.