Skip to content

Commit c56a6cb

Browse files
committed
Pad thread ID in log header to 7 spaces, to match C++ format.
Add check in TestHeader to detect padding mismatch.
1 parent c741557 commit c56a6cb

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

glog.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,9 @@ func (l *loggingT) formatHeader(s severity, file string, line int) *buffer {
574574
buf.tmp[14] = '.'
575575
buf.nDigits(6, 15, now.Nanosecond()/1000, '0')
576576
buf.tmp[21] = ' '
577-
buf.nDigits(5, 22, pid, ' ') // TODO: should be TID
578-
buf.tmp[27] = ' '
579-
buf.Write(buf.tmp[:28])
577+
buf.nDigits(7, 22, pid, ' ') // TODO: should be TID
578+
buf.tmp[29] = ' '
579+
buf.Write(buf.tmp[:30])
580580
buf.WriteString(file)
581581
buf.tmp[0] = ':'
582582
n := buf.someDigits(1, line)

glog_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,17 @@ func TestHeader(t *testing.T) {
180180
pid = 1234
181181
Info("test")
182182
var line int
183-
n, err := fmt.Sscanf(contents(infoLog), "I0102 15:04:05.067890 1234 glog_test.go:%d] test\n", &line)
183+
format := "I0102 15:04:05.067890 1234 glog_test.go:%d] test\n"
184+
n, err := fmt.Sscanf(contents(infoLog), format, &line)
184185
if n != 1 || err != nil {
185186
t.Errorf("log format error: %d elements, error %s:\n%s", n, err, contents(infoLog))
186187
}
188+
// Scanf treats multiple spaces as equivalent to a single space,
189+
// so check for correct space-padding also.
190+
want := fmt.Sprintf(format, line)
191+
if contents(infoLog) != want {
192+
t.Errorf("log format error: got:\n\t%q\nwant:\t%q", contents(infoLog), want)
193+
}
187194
}
188195

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

0 commit comments

Comments
 (0)