add logid

This commit is contained in:
admin 2022-12-04 18:58:35 +08:00
parent 71b00c8a37
commit 7400653848
8 changed files with 6666 additions and 16 deletions

19
log.go
View File

@ -154,17 +154,8 @@ func (log *Logger) Panicf(format string, v ...interface{}) {
log.logwrite(PANIC, 3, format, v...)
}
func GetLogid() string {
if logidGetter != nil {
return logidGetter()
}
return ""
}
func SetLogid(logid string) {
if logidSetter != nil {
logidSetter(logid)
}
func (log *Logger) GetLogidStr(format string) string {
return fmt.Sprintf(format, GetLogid())
}
func (log *Logger) logwrite(typ logLevel, calldepth int, format string, v ...interface{}) {
@ -172,11 +163,7 @@ func (log *Logger) logwrite(typ logLevel, calldepth int, format string, v ...int
return
}
var idstr = ""
if id := GetLogid(); id != "" {
idstr = "[" + id + "] "
}
var idstr = log.GetLogidStr("[%s] ")
format = strings.Trim(format, "\n")
switch typ {
case PANIC:

18
logid+glslogid.go Normal file
View File

@ -0,0 +1,18 @@
package log
import(
"github.com/jtolds/gls"
)
type glsLogid struct {
mgr *gls.ContextManager
idkey gls.ContextKey
}
func GetLogid() string {
return ""
}
func SetLogid(logid string) {
}

9
logid+nologid.go Normal file
View File

@ -0,0 +1,9 @@
package log
func GetLogid() string {
return ""
}
func SetLogid(logid string) {
}

45
logid+runtimelogid.go Normal file
View File

@ -0,0 +1,45 @@
package log
func GetLogid() string {
if logidGetter != nil {
return logidGetter()
}
return ""
}
func SetLogid(logid string) {
if logidSetter != nil {
logidSetter(logid)
}
}
// +build logid
package yk_logger
import (
"bytes"
"runtime"
"strconv"
)
// LogidFormatPart is a FormatPart of the full source code placeholder.
type LogidFormatPart struct{}
// Format writes the source file path and line number of the record to the buf.
func (p *LogidFormatPart) Format(r *Record, buf *bytes.Buffer) {
logid := p.GetLogid()
logidstr := strconv.FormatInt(logid, 10)
buf.WriteString(logidstr)
}
func (p *LogidFormatPart) GetLogid() int64 {
return runtime.GetLogid()
}
func (p *LogidFormatPart) SetLogid(newid int64) (oldid int64) {
return runtime.SetLogid(newid)
}
func init() {
LogidSupporter = &LogidFormatPart{}
}

View File

@ -0,0 +1,82 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package runtime
import (
"runtime/internal/atomic"
"unsafe"
)
// GOMAXPROCS sets the maximum number of CPUs that can be executing
// simultaneously and returns the previous setting. If n < 1, it does not
// change the current setting.
// The number of logical CPUs on the local machine can be queried with NumCPU.
// This call will go away when the scheduler improves.
func GOMAXPROCS(n int) int {
if GOARCH == "wasm" && n > 1 {
n = 1 // WebAssembly has no threads yet, so only one CPU is possible.
}
lock(&sched.lock)
ret := int(gomaxprocs)
unlock(&sched.lock)
if n <= 0 || n == ret {
return ret
}
stopTheWorld("GOMAXPROCS")
// newprocs will be processed by startTheWorld
newprocs = int32(n)
startTheWorld()
return ret
}
// NumCPU returns the number of logical CPUs usable by the current process.
//
// The set of available CPUs is checked by querying the operating system
// at process startup. Changes to operating system CPU allocation after
// process startup are not reflected.
func NumCPU() int {
return int(ncpu)
}
// NumCgoCall returns the number of cgo calls made by the current process.
func NumCgoCall() int64 {
var n int64
for mp := (*m)(atomic.Loadp(unsafe.Pointer(&allm))); mp != nil; mp = mp.alllink {
n += int64(mp.ncgocall)
}
return n
}
// NumGoroutine returns the number of goroutines that currently exist.
func NumGoroutine() int {
return int(gcount())
}
// GetLogid get a goroutine log, the logid is setted by SetLogid,
// or it is succesed from parents goroutine.
// add for test by q.bryant@live.com @ 2020.09.10
func GetLogid()int64{
_g_ := getg()
return _g_.logid
}
// SetLogid get a goroutine log, you can get the logid by GetLogid,
// it will return the old logid for trace call stack.
// add for test by q.bryant@live.com @ 2020.09.10
func SetLogid(newid int64)(oldid int64){
_g_ := getg()
oldid = _g_.logid
_g_.logid = newid
return oldid
}
//go:linkname debug_modinfo runtime/debug.modinfo
func debug_modinfo() string {
return modinfo
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,41 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package runtime_test
import (
"reflect"
"runtime"
"testing"
"unsafe"
)
// Assert that the size of important structures do not change unexpectedly.
func TestSizeof(t *testing.T) {
const _64bit = unsafe.Sizeof(uintptr(0)) == 8
var tests = []struct {
val interface{} // type as a value
_32bit uintptr // size on 32bit platforms
_64bit uintptr // size on 64bit platforms
}{
////// Add by q.bryant@live.com for logid @2020.09.10 ///////begain//////
// {runtime.G{}, 216, 376}, // g, but exported for testing
///////////////////// replace by ///////////////////////////
{runtime.G{}, 224, 384}, // g, but exported for testing
////// Add by q.bryant@live.com for logid @2020.09.10 ///////end/////////
}
for _, tt := range tests {
want := tt._32bit
if _64bit {
want = tt._64bit
}
got := reflect.TypeOf(tt.val).Size()
if want != got {
t.Errorf("unsafe.Sizeof(%T) = %d, want %d", tt.val, got, want)
}
}
}