Преглед изворни кода

Pad thread ID in log header to 7 spaces, to match C++ format.

Add check in TestHeader to detect padding mismatch.
David Symonds пре 11 година
родитељ
комит
c56a6cb113
2 измењених фајлова са 11 додато и 4 уклоњено
  1. 3 3
      glog.go
  2. 8 1
      glog_test.go

+ 3 - 3
glog.go

@@ -574,9 +574,9 @@ func (l *loggingT) formatHeader(s severity, file string, line int) *buffer {
 	buf.tmp[14] = '.'
 	buf.tmp[14] = '.'
 	buf.nDigits(6, 15, now.Nanosecond()/1000, '0')
 	buf.nDigits(6, 15, now.Nanosecond()/1000, '0')
 	buf.tmp[21] = ' '
 	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.WriteString(file)
 	buf.tmp[0] = ':'
 	buf.tmp[0] = ':'
 	n := buf.someDigits(1, line)
 	n := buf.someDigits(1, line)

+ 8 - 1
glog_test.go

@@ -180,10 +180,17 @@ func TestHeader(t *testing.T) {
 	pid = 1234
 	pid = 1234
 	Info("test")
 	Info("test")
 	var line int
 	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 {
 	if n != 1 || err != nil {
 		t.Errorf("log format error: %d elements, error %s:\n%s", n, err, contents(infoLog))
 		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.
 // Test that an Error log goes to Warning and Info.