-
Notifications
You must be signed in to change notification settings - Fork 16
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
1 parent
bb06cbf
commit 71a3b34
Showing
17 changed files
with
1,794 additions
and
467 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
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,56 @@ | ||
package log | ||
|
||
import "sync" | ||
|
||
type buffer struct { | ||
bytes *[]byte | ||
disableColor bool | ||
} | ||
|
||
var bufPool = sync.Pool{ | ||
New: func() any { | ||
buf := make([]byte, 0, 1024) | ||
return &buffer{ | ||
bytes: &buf, | ||
} | ||
}, | ||
} | ||
|
||
func newBuffer(handler *handler) *buffer { | ||
buf := bufPool.Get().(*buffer) | ||
buf.disableColor = handler.opts.DisableColor | ||
return buf | ||
} | ||
|
||
func (b *buffer) Free() { | ||
const maxBufferSize = 16 << 10 | ||
if cap(*b.bytes) <= maxBufferSize { | ||
*b.bytes = (*b.bytes)[:0] | ||
b.disableColor = false | ||
bufPool.Put(b) | ||
} | ||
} | ||
func (b *buffer) Write(bytes []byte) *buffer { | ||
*b.bytes = append(*b.bytes, bytes...) | ||
return b | ||
} | ||
|
||
func (b *buffer) WriteBytes(char ...byte) *buffer { | ||
*b.bytes = append(*b.bytes, char...) | ||
return b | ||
} | ||
|
||
func (b *buffer) WriteString(str string) *buffer { | ||
*b.bytes = append(*b.bytes, str...) | ||
return b | ||
} | ||
|
||
func (b *buffer) WriteColorString(str ...string) *buffer { | ||
for _, s := range str { | ||
_, isColor := colors[s] | ||
if !isColor || !b.disableColor { | ||
b.WriteString(s) | ||
} | ||
} | ||
return b | ||
} |
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,11 @@ | ||
package log | ||
|
||
import ( | ||
"path/filepath" | ||
"strconv" | ||
) | ||
|
||
// CallerBasicFormat 返回调用者的基本格式 | ||
func CallerBasicFormat(file string, line int) (repFile, refLine string) { | ||
return filepath.Base(file), strconv.Itoa(line) | ||
} |
Oops, something went wrong.