log/删除所有的依赖库;
parent
4e1fe211cb
commit
1f39ef8c3a
@ -1,4 +0,0 @@
|
||||
git.blauwelle.com/go/crate/runtimehelper v0.1.0 h1:qNhtnt9YmHXNHKsGRbwD3AZ3pezpOwrbmX1o9Bz532I=
|
||||
git.blauwelle.com/go/crate/runtimehelper v0.1.0/go.mod h1:yVMA0GkO9AS7iuPmalHKeWyv9en0JWj25rY1vpTuHhk=
|
||||
git.blauwelle.com/go/crate/synchelper v0.1.0 h1:4yEXpshkklaws/57P94xN5bA3NmyyKGcZqYmzd6QIK4=
|
||||
git.blauwelle.com/go/crate/synchelper v0.1.0/go.mod h1:2JkfH+7sF0Q0wiIaDOqG42ZLO5JxpcMfSoyy7db4Y2g=
|
@ -0,0 +1,38 @@
|
||||
package logjson
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type BytesBufferPool interface {
|
||||
Get() *bytes.Buffer
|
||||
Put(buffer *bytes.Buffer)
|
||||
}
|
||||
|
||||
func newBytesBufferPool(initialSize, maximumSize int) *bytesBufferPool {
|
||||
return &bytesBufferPool{
|
||||
pool: sync.Pool{
|
||||
New: func() any {
|
||||
return bytes.NewBuffer(make([]byte, 0, initialSize))
|
||||
},
|
||||
},
|
||||
maximumSize: maximumSize,
|
||||
}
|
||||
}
|
||||
|
||||
type bytesBufferPool struct {
|
||||
pool sync.Pool
|
||||
maximumSize int
|
||||
}
|
||||
|
||||
func (pool *bytesBufferPool) Get() *bytes.Buffer {
|
||||
return pool.pool.Get().(*bytes.Buffer)
|
||||
}
|
||||
|
||||
func (pool *bytesBufferPool) Put(buf *bytes.Buffer) {
|
||||
if buf.Cap() > pool.maximumSize {
|
||||
return
|
||||
}
|
||||
pool.pool.Put(buf)
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package logjson
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func newSyncWriter(writer io.Writer) io.Writer {
|
||||
return &syncWriter{
|
||||
writer: writer,
|
||||
lock: sync.Mutex{},
|
||||
}
|
||||
}
|
||||
|
||||
type syncWriter struct {
|
||||
writer io.Writer
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func (w *syncWriter) Write(p []byte) (n int, err error) {
|
||||
w.lock.Lock()
|
||||
defer w.lock.Unlock()
|
||||
return w.writer.Write(p)
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package logsdk
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// Frame 调用相关信息
|
||||
type Frame struct {
|
||||
Function string `json:"func"`
|
||||
File string `json:"file"`
|
||||
Line int `json:"line"`
|
||||
}
|
||||
|
||||
// IsValid 表示是否有效
|
||||
func (frame Frame) IsValid() bool {
|
||||
return frame.Line > 0
|
||||
}
|
||||
|
||||
func getCaller(skip int) Frame {
|
||||
pc := make([]uintptr, 1)
|
||||
n := runtime.Callers(skip+2, pc)
|
||||
frame, _ := runtime.CallersFrames(pc[:n]).Next()
|
||||
if frame.PC == 0 {
|
||||
return Frame{}
|
||||
}
|
||||
return Frame{
|
||||
Function: frame.Function,
|
||||
File: frame.File,
|
||||
Line: frame.Line,
|
||||
}
|
||||
}
|
||||
|
||||
func getStack(skip, maximumFrames int) []Frame {
|
||||
pc := make([]uintptr, maximumFrames)
|
||||
n := runtime.Callers(skip+2, pc)
|
||||
stack := make([]Frame, 0, n)
|
||||
frames := runtime.CallersFrames(pc[:n])
|
||||
for {
|
||||
frame, more := frames.Next()
|
||||
if frame.PC != 0 {
|
||||
stack = append(stack, Frame{
|
||||
Function: frame.Function,
|
||||
File: frame.File,
|
||||
Line: frame.Line,
|
||||
})
|
||||
}
|
||||
if !more {
|
||||
break
|
||||
}
|
||||
}
|
||||
return stack
|
||||
}
|
Loading…
Reference in New Issue