-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,126 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package clock | ||
|
||
import "time" | ||
|
||
type Clock interface { | ||
Now() time.Time | ||
Since(time.Time) time.Duration | ||
} | ||
|
||
var Real Clock = realClock{} | ||
|
||
type realClock struct{} | ||
|
||
// Now returns the current time. | ||
func (realClock) Now() time.Time { | ||
return time.Now() | ||
} | ||
|
||
// Since returns time since the specified timestamp. | ||
func (realClock) Since(ts time.Time) time.Duration { | ||
return time.Since(ts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package testing | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type FakeClock struct { | ||
mu sync.RWMutex | ||
t time.Time | ||
} | ||
|
||
func NewFakeClock(t time.Time) *FakeClock { | ||
return &FakeClock{t: t} | ||
} | ||
|
||
// Now returns f's time. | ||
func (f *FakeClock) Now() time.Time { | ||
f.mu.RLock() | ||
defer f.mu.RUnlock() | ||
return f.t | ||
} | ||
|
||
// Since returns time since the time in f. | ||
func (f *FakeClock) Since(t time.Time) time.Duration { | ||
f.mu.RLock() | ||
defer f.mu.RUnlock() | ||
return f.t.Sub(t) | ||
} | ||
|
||
// SetTime sets the time on the FakeClock. | ||
func (f *FakeClock) SetTime(t time.Time) { | ||
f.mu.Lock() | ||
defer f.mu.Unlock() | ||
f.t = t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package log | ||
|
||
import ( | ||
"bytes" | ||
"sync" | ||
) | ||
|
||
var bufferPool = sync.Pool{New: func() any { return new([]byte) }} | ||
|
||
func getBuffer() *[]byte { | ||
p := bufferPool.Get().(*[]byte) | ||
*p = (*p)[:0] | ||
return p | ||
} | ||
|
||
func putBuffer(p *[]byte) { | ||
if cap(*p) > 64<<10 { | ||
*p = nil | ||
} | ||
bufferPool.Put(p) | ||
} | ||
|
||
var bytesBufferPool = sync.Pool{ | ||
New: func() any { | ||
return new(bytes.Buffer) | ||
}, | ||
} | ||
|
||
// Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding. | ||
func itoa(buf *[]byte, i int, wid int) { | ||
// Assemble decimal in reverse order. | ||
var b [20]byte | ||
bp := len(b) - 1 | ||
for i >= 10 || wid > 1 { | ||
wid-- | ||
q := i / 10 | ||
b[bp] = byte('0' + i - q*10) | ||
bp-- | ||
i = q | ||
} | ||
// i < 10 | ||
b[bp] = byte('0' + i) | ||
*buf = append(*buf, b[bp:]...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package log | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
const ( | ||
y1 = `0123456789` | ||
y2 = `0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789` | ||
y3 = `0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999` | ||
y4 = `0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789` | ||
mo1 = `000000000111` | ||
mo2 = `123456789012` | ||
d1 = `0000000001111111111222222222233` | ||
d2 = `1234567890123456789012345678901` | ||
h1 = `000000000011111111112222` | ||
h2 = `012345678901234567890123` | ||
mi1 = `000000000011111111112222222222333333333344444444445555555555` | ||
mi2 = `012345678901234567890123456789012345678901234567890123456789` | ||
s1 = `000000000011111111112222222222333333333344444444445555555555` | ||
s2 = `012345678901234567890123456789012345678901234567890123456789` | ||
ns1 = `0123456789` | ||
) | ||
|
||
const logTimeFormat = "2006-01-02 15:04:05.000" | ||
|
||
func formatTimeHeader(when time.Time, buf []byte) (int, int) { | ||
if len(buf) < 23 { | ||
return 0, 0 | ||
} | ||
|
||
y, mo, d := when.Date() | ||
h, mi, s := when.Clock() | ||
ns := when.Nanosecond() / 1000000 | ||
// len("2006/01/02 15:04:05.000") == 23 | ||
|
||
buf[0] = y1[y/1000%10] | ||
buf[1] = y2[y/100] | ||
buf[2] = y3[y-y/100*100] | ||
buf[3] = y4[y-y/100*100] | ||
buf[4] = '-' | ||
buf[5] = mo1[mo-1] | ||
buf[6] = mo2[mo-1] | ||
buf[7] = '-' | ||
buf[8] = d1[d-1] | ||
buf[9] = d2[d-1] | ||
buf[10] = ' ' | ||
buf[11] = h1[h] | ||
buf[12] = h2[h] | ||
buf[13] = ':' | ||
buf[14] = mi1[mi] | ||
buf[15] = mi2[mi] | ||
buf[16] = ':' | ||
buf[17] = s1[s] | ||
buf[18] = s2[s] | ||
buf[19] = '.' | ||
buf[20] = ns1[ns/100] | ||
buf[21] = ns1[ns%100/10] | ||
buf[22] = ns1[ns%10] | ||
|
||
return d, h | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package log | ||
|
||
var DefaultLogger = New() | ||
|
||
func Trace(args ...interface{}) { | ||
DefaultLogger.log(TraceLevel, "", args...) | ||
} | ||
|
||
func Debug(args ...interface{}) { | ||
DefaultLogger.log(DebugLevel, "", args...) | ||
} | ||
|
||
func Info(args ...interface{}) { | ||
DefaultLogger.log(InfoLevel, "", args...) | ||
} | ||
|
||
func Warn(args ...interface{}) { | ||
DefaultLogger.log(WarnLevel, "", args...) | ||
} | ||
|
||
func Error(args ...interface{}) { | ||
DefaultLogger.log(ErrorLevel, "", args...) | ||
} | ||
|
||
func Tracef(msg string, args ...interface{}) { | ||
DefaultLogger.log(TraceLevel, msg, args...) | ||
} | ||
|
||
func Debugf(msg string, args ...interface{}) { | ||
DefaultLogger.log(DebugLevel, msg, args...) | ||
} | ||
|
||
func Infof(msg string, args ...interface{}) { | ||
DefaultLogger.log(InfoLevel, msg, args...) | ||
} | ||
|
||
func Warnf(msg string, args ...interface{}) { | ||
DefaultLogger.log(WarnLevel, msg, args...) | ||
} | ||
|
||
func Errorf(msg string, args ...interface{}) { | ||
DefaultLogger.log(ErrorLevel, msg, args...) | ||
} |
Oops, something went wrong.