Skip to content

Commit

Permalink
Fix log format to space pad PID instead of zero pad.
Browse files Browse the repository at this point in the history
This now matches C++ and the code comments.
  • Loading branch information
dsymonds committed Nov 5, 2014
1 parent 5066b11 commit b83197c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
16 changes: 11 additions & 5 deletions glog.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,9 @@ func (l *loggingT) formatHeader(s severity, file string, line int) *buffer {
buf.tmp[11] = ':'
buf.twoDigits(12, second)
buf.tmp[14] = '.'
buf.nDigits(6, 15, now.Nanosecond()/1000)
buf.nDigits(6, 15, now.Nanosecond()/1000, '0')
buf.tmp[21] = ' '
buf.nDigits(5, 22, pid) // TODO: should be TID
buf.nDigits(5, 22, pid, ' ') // TODO: should be TID
buf.tmp[27] = ' '
buf.Write(buf.tmp[:28])
buf.WriteString(file)
Expand All @@ -596,12 +596,18 @@ func (buf *buffer) twoDigits(i, d int) {
buf.tmp[i] = digits[d%10]
}

// nDigits formats a zero-prefixed n-digit integer at buf.tmp[i].
func (buf *buffer) nDigits(n, i, d int) {
for j := n - 1; j >= 0; j-- {
// nDigits formats an n-digit integer at buf.tmp[i],
// padding with pad on the left.
// It assumes d >= 0.
func (buf *buffer) nDigits(n, i, d int, pad byte) {
j := n - 1
for ; j >= 0 && d > 0; j-- {
buf.tmp[i+j] = digits[d%10]
d /= 10
}
for ; j >= 0; j-- {
buf.tmp[i+j] = pad
}
}

// someDigits formats a zero-prefixed variable-width integer at buf.tmp[i].
Expand Down
9 changes: 5 additions & 4 deletions glog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,13 @@ func TestHeader(t *testing.T) {
defer logging.swap(logging.newBuffers())
defer func(previous func() time.Time) { timeNow = previous }(timeNow)
timeNow = func() time.Time {
return time.Date(2006, 1, 2, 15, 4, 5, .678901e9, time.Local)
return time.Date(2006, 1, 2, 15, 4, 5, .067890e9, time.Local)
}
pid = 1234
Info("test")
var line, pid int
n, err := fmt.Sscanf(contents(infoLog), "I0102 15:04:05.678901 %d glog_test.go:%d] test\n", &pid, &line)
if n != 2 || err != nil {
var line int
n, err := fmt.Sscanf(contents(infoLog), "I0102 15:04:05.067890 1234 glog_test.go:%d] test\n", &line)
if n != 1 || err != nil {
t.Errorf("log format error: %d elements, error %s:\n%s", n, err, contents(infoLog))
}
}
Expand Down

0 comments on commit b83197c

Please sign in to comment.