mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 09:18:34 +00:00
Refactor log (#3446)
* Refactor log * Add new log methods * Fix logger test * Change all logging code * Clean up pathObj * Rebase to latest main * Remove invoking method name after the dot
This commit is contained in:
parent
8320732743
commit
079d0bd8a9
291 changed files with 1837 additions and 2368 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
_ "unsafe"
|
||||
|
||||
"github.com/xtls/xray-core/common/ctx"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/features/routing"
|
||||
)
|
||||
|
@ -11,35 +12,19 @@ import (
|
|||
//go:linkname IndependentCancelCtx context.newCancelCtx
|
||||
func IndependentCancelCtx(parent context.Context) context.Context
|
||||
|
||||
type sessionKey int
|
||||
|
||||
const (
|
||||
idSessionKey sessionKey = iota
|
||||
inboundSessionKey
|
||||
outboundSessionKey
|
||||
contentSessionKey
|
||||
muxPreferedSessionKey
|
||||
sockoptSessionKey
|
||||
trackedConnectionErrorKey
|
||||
dispatcherKey
|
||||
timeoutOnlyKey
|
||||
allowedNetworkKey
|
||||
handlerSessionKey
|
||||
inboundSessionKey ctx.SessionKey = 1
|
||||
outboundSessionKey ctx.SessionKey = 2
|
||||
contentSessionKey ctx.SessionKey = 3
|
||||
muxPreferedSessionKey ctx.SessionKey = 4
|
||||
sockoptSessionKey ctx.SessionKey = 5
|
||||
trackedConnectionErrorKey ctx.SessionKey = 6
|
||||
dispatcherKey ctx.SessionKey = 7
|
||||
timeoutOnlyKey ctx.SessionKey = 8
|
||||
allowedNetworkKey ctx.SessionKey = 9
|
||||
handlerSessionKey ctx.SessionKey = 10
|
||||
)
|
||||
|
||||
// ContextWithID returns a new context with the given ID.
|
||||
func ContextWithID(ctx context.Context, id ID) context.Context {
|
||||
return context.WithValue(ctx, idSessionKey, id)
|
||||
}
|
||||
|
||||
// IDFromContext returns ID in this context, or 0 if not contained.
|
||||
func IDFromContext(ctx context.Context) ID {
|
||||
if id, ok := ctx.Value(idSessionKey).(ID); ok {
|
||||
return id
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func ContextWithInbound(ctx context.Context, inbound *Inbound) context.Context {
|
||||
return context.WithValue(ctx, inboundSessionKey, inbound)
|
||||
}
|
||||
|
|
|
@ -5,20 +5,18 @@ import (
|
|||
"context"
|
||||
"math/rand"
|
||||
|
||||
c "github.com/xtls/xray-core/common/ctx"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/common/protocol"
|
||||
"github.com/xtls/xray-core/common/signal"
|
||||
)
|
||||
|
||||
// ID of a session.
|
||||
type ID uint32
|
||||
|
||||
// NewID generates a new ID. The generated ID is high likely to be unique, but not cryptographically secure.
|
||||
// The generated ID will never be 0.
|
||||
func NewID() ID {
|
||||
func NewID() c.ID {
|
||||
for {
|
||||
id := ID(rand.Uint32())
|
||||
id := c.ID(rand.Uint32())
|
||||
if id != 0 {
|
||||
return id
|
||||
}
|
||||
|
@ -28,7 +26,7 @@ func NewID() ID {
|
|||
// ExportIDToError transfers session.ID into an error object, for logging purpose.
|
||||
// This can be used with error.WriteToLog().
|
||||
func ExportIDToError(ctx context.Context) errors.ExportOption {
|
||||
id := IDFromContext(ctx)
|
||||
id := c.IDFromContext(ctx)
|
||||
return func(h *errors.ExportOptionHolder) {
|
||||
h.SessionID = uint32(id)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue