From 8e82ed319b11293a50f6f4d94ee26f5f72e4b8c8 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Wed, 30 Mar 2016 14:31:16 +0800 Subject: [PATCH 1/4] beelog bug fixed --- logs/log.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logs/log.go b/logs/log.go index 695691b2..c43782f3 100644 --- a/logs/log.go +++ b/logs/log.go @@ -255,7 +255,7 @@ func (bl *BeeLogger) Write(p []byte) (n int, err error) { } func (bl *BeeLogger) writeMsg(logLevel int, msg string, v ...interface{}) error { - if !beeLogger.init { + if !bl.init { bl.lock.Lock() bl.setLogger(AdapterConsole) bl.lock.Unlock() From 99436a75b169fae6b29b2a7746a6cd4b8688da12 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Wed, 30 Mar 2016 14:31:28 +0800 Subject: [PATCH 2/4] format time header 5% faster than before --- logs/logger.go | 57 +++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/logs/logger.go b/logs/logger.go index b25bfaef..0d1125a8 100644 --- a/logs/logger.go +++ b/logs/logger.go @@ -36,43 +36,44 @@ func (lg *logWriter) println(when time.Time, msg string) { lg.Unlock() } +const y1 = `0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999` +const y2 = `0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789` +const mo1 = `000000000111` +const mo2 = `123456789012` +const d1 = `0000000001111111111222222222233` +const d2 = `1234567890123456789012345678901` +const h1 = `000000000011111111112222` +const h2 = `012345678901234567890123` +const mi1 = `000000000011111111112222222222333333333344444444445555555555` +const mi2 = `012345678901234567890123456789012345678901234567890123456789` +const s1 = `000000000011111111112222222222333333333344444444445555555555` +const s2 = `012345678901234567890123456789012345678901234567890123456789` + func formatTimeHeader(when time.Time) ([]byte, int) { y, mo, d := when.Date() h, mi, s := when.Clock() - //len(2006/01/02 15:03:04)==19 + //len("2006/01/02 15:03:04 ")==20 var buf [20]byte - t := 3 - for y >= 10 { - p := y / 10 - buf[t] = byte('0' + y - p*10) - y = p - t-- - } - buf[0] = byte('0' + y) + + buf[0] = '2' + buf[1] = '0' + buf[2] = y1[y-2000] + buf[3] = y2[y-2000] buf[4] = '/' - if mo > 9 { - buf[5] = '1' - buf[6] = byte('0' + mo - 9) - } else { - buf[5] = '0' - buf[6] = byte('0' + mo) - } + buf[5] = mo1[mo] + buf[6] = mo2[mo] buf[7] = '/' - t = d / 10 - buf[8] = byte('0' + t) - buf[9] = byte('0' + d - t*10) + buf[8] = d1[d] + buf[9] = d2[d] buf[10] = ' ' - t = h / 10 - buf[11] = byte('0' + t) - buf[12] = byte('0' + h - t*10) + buf[11] = h1[h] + buf[12] = h2[h] buf[13] = ':' - t = mi / 10 - buf[14] = byte('0' + t) - buf[15] = byte('0' + mi - t*10) + buf[14] = mi1[mi] + buf[15] = mi2[mi] buf[16] = ':' - t = s / 10 - buf[17] = byte('0' + t) - buf[18] = byte('0' + s - t*10) + buf[17] = s1[s] + buf[18] = s2[s] buf[19] = ' ' return buf[0:], d From 814b673e3ce24e5880f4686abaffaf4d4e037685 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Wed, 30 Mar 2016 14:34:52 +0800 Subject: [PATCH 3/4] add a comment to the future bug --- logs/logger.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/logs/logger.go b/logs/logger.go index 0d1125a8..2f47e569 100644 --- a/logs/logger.go +++ b/logs/logger.go @@ -52,19 +52,21 @@ const s2 = `012345678901234567890123456789012345678901234567890123456789` func formatTimeHeader(when time.Time) ([]byte, int) { y, mo, d := when.Date() h, mi, s := when.Clock() - //len("2006/01/02 15:03:04 ")==20 + //len("2006/01/02 15:04:05 ")==20 var buf [20]byte + //change to '3' after 984 years, LOL buf[0] = '2' + //change to '1' after 84 years, LOL buf[1] = '0' buf[2] = y1[y-2000] buf[3] = y2[y-2000] buf[4] = '/' - buf[5] = mo1[mo] - buf[6] = mo2[mo] + buf[5] = mo1[mo-1] + buf[6] = mo2[mo-1] buf[7] = '/' - buf[8] = d1[d] - buf[9] = d2[d] + buf[8] = d1[d-1] + buf[9] = d2[d-1] buf[10] = ' ' buf[11] = h1[h] buf[12] = h2[h] From 96a5d09ef0f9c19ecba5555ad06fe0f8708ad944 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Wed, 30 Mar 2016 15:13:01 +0800 Subject: [PATCH 4/4] add format header test --- logs/logger_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 logs/logger_test.go diff --git a/logs/logger_test.go b/logs/logger_test.go new file mode 100644 index 00000000..4627853a --- /dev/null +++ b/logs/logger_test.go @@ -0,0 +1,57 @@ +// Copyright 2016 beego Author. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package logs + +import ( + "testing" + "time" +) + +func TestFormatHeader_0(t *testing.T) { + tm := time.Now() + if tm.Year() >= 2100 { + t.FailNow() + } + dur := time.Second + for { + if tm.Year() >= 2100 { + break + } + h, _ := formatTimeHeader(tm) + if tm.Format("2006/01/02 15:04:05 ") != string(h) { + t.Log(tm) + t.FailNow() + } + tm = tm.Add(dur) + dur *= 2 + } +} + +func TestFormatHeader_1(t *testing.T) { + tm := time.Now() + year := tm.Year() + dur := time.Second + for { + if tm.Year() >= year+1 { + break + } + h, _ := formatTimeHeader(tm) + if tm.Format("2006/01/02 15:04:05 ") != string(h) { + t.Log(tm) + t.FailNow() + } + tm = tm.Add(dur) + } +}