Fix log format to space pad PID instead of zero pad.
This now matches C++ and the code comments.
diff --git a/glog.go b/glog.go
index da3c6a7..a70eb9f 100644
--- a/glog.go
+++ b/glog.go
@@ -571,9 +571,9 @@
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 @@
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].
diff --git a/glog_test.go b/glog_test.go
index d2e157a..6e09e1a 100644
--- a/glog_test.go
+++ b/glog_test.go
@@ -130,12 +130,13 @@
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))
}
}