Skip to content

Commit

Permalink
Pad thread ID in log header to 7 spaces, to match C++ format.
Browse files Browse the repository at this point in the history
Add check in TestHeader to detect padding mismatch.
  • Loading branch information
dsymonds committed Nov 5, 2014
1 parent c741557 commit c56a6cb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 3 additions & 3 deletions glog.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,9 @@ func (l *loggingT) formatHeader(s severity, file string, line int) *buffer {
buf.tmp[14] = '.'
buf.nDigits(6, 15, now.Nanosecond()/1000, '0')
buf.tmp[21] = ' '
buf.nDigits(5, 22, pid, ' ') // TODO: should be TID
buf.tmp[27] = ' '
buf.Write(buf.tmp[:28])
buf.nDigits(7, 22, pid, ' ') // TODO: should be TID
buf.tmp[29] = ' '
buf.Write(buf.tmp[:30])
buf.WriteString(file)
buf.tmp[0] = ':'
n := buf.someDigits(1, line)
Expand Down
9 changes: 8 additions & 1 deletion glog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,17 @@ func TestHeader(t *testing.T) {
pid = 1234
Info("test")
var line int
n, err := fmt.Sscanf(contents(infoLog), "I0102 15:04:05.067890 1234 glog_test.go:%d] test\n", &line)
format := "I0102 15:04:05.067890 1234 glog_test.go:%d] test\n"
n, err := fmt.Sscanf(contents(infoLog), format, &line)
if n != 1 || err != nil {
t.Errorf("log format error: %d elements, error %s:\n%s", n, err, contents(infoLog))
}
// Scanf treats multiple spaces as equivalent to a single space,
// so check for correct space-padding also.
want := fmt.Sprintf(format, line)
if contents(infoLog) != want {
t.Errorf("log format error: got:\n\t%q\nwant:\t%q", contents(infoLog), want)
}
}

// Test that an Error log goes to Warning and Info.
Expand Down

0 comments on commit c56a6cb

Please sign in to comment.