Refactor log

This commit is contained in:
世界 2022-07-12 15:17:29 +08:00
parent b47f3adbb3
commit 4fc763cfa2
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
46 changed files with 760 additions and 457 deletions

View file

@ -9,33 +9,25 @@ import (
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
)
func New(ctx context.Context, router adapter.Router, logger log.Logger, index int, options option.Inbound) (adapter.Inbound, error) {
func New(ctx context.Context, router adapter.Router, logger log.ContextLogger, options option.Inbound) (adapter.Inbound, error) {
if common.IsEmptyByEquals(options) {
return nil, E.New("empty inbound config")
}
var tag string
if options.Tag != "" {
tag = options.Tag
} else {
tag = F.ToString(index)
}
inboundLogger := logger.WithPrefix(F.ToString("inbound/", options.Type, "[", tag, "]: "))
switch options.Type {
case C.TypeDirect:
return NewDirect(ctx, router, inboundLogger, options.Tag, options.DirectOptions), nil
return NewDirect(ctx, router, logger, options.Tag, options.DirectOptions), nil
case C.TypeSocks:
return NewSocks(ctx, router, inboundLogger, options.Tag, options.SocksOptions), nil
return NewSocks(ctx, router, logger, options.Tag, options.SocksOptions), nil
case C.TypeHTTP:
return NewHTTP(ctx, router, inboundLogger, options.Tag, options.HTTPOptions), nil
return NewHTTP(ctx, router, logger, options.Tag, options.HTTPOptions), nil
case C.TypeMixed:
return NewMixed(ctx, router, inboundLogger, options.Tag, options.MixedOptions), nil
return NewMixed(ctx, router, logger, options.Tag, options.MixedOptions), nil
case C.TypeShadowsocks:
return NewShadowsocks(ctx, router, inboundLogger, options.Tag, options.ShadowsocksOptions)
return NewShadowsocks(ctx, router, logger, options.Tag, options.ShadowsocksOptions)
case C.TypeTun:
return NewTun(ctx, router, inboundLogger, options.Tag, options.TunOptions)
return NewTun(ctx, router, logger, options.Tag, options.TunOptions)
default:
return nil, E.New("unknown inbound type: ", options.Type)
}