Przeglądaj źródła

Fix log format to space pad PID instead of zero pad.

This now matches C++ and the code comments.
David Symonds 12 lat temu
rodzic
commit
b83197c33c
2 zmienionych plików z 16 dodań i 9 usunięć
  1. 11 5
      glog.go
  2. 5 4
      glog_test.go

+ 11 - 5
glog.go

@@ -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)
@@ -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].

+ 5 - 4
glog_test.go

@@ -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))
 	}
 }