Uniq deprecated notes

This commit is contained in:
世界 2024-11-07 19:58:46 +08:00
parent 88099a304a
commit 21faadb992
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 22 additions and 13 deletions

View file

@ -67,5 +67,5 @@ func preRun(cmd *cobra.Command, args []string) {
if len(configPaths) == 0 && len(configDirectories) == 0 {
configPaths = append(configPaths, "config.json")
}
globalCtx = service.ContextWith(globalCtx, deprecated.NewEnvManager(log.StdLogger()))
globalCtx = service.ContextWith(globalCtx, deprecated.NewStderrManager(log.StdLogger()))
}

View file

@ -30,7 +30,7 @@ func check() error {
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(globalCtx)
instance, err := box.New(box.Options{
Context: ctx,
Options: options,

View file

@ -7,15 +7,23 @@ import (
"github.com/sagernet/sing/common/logger"
)
type envManager struct {
logger logger.Logger
type stderrManager struct {
logger logger.Logger
reported map[string]bool
}
func NewEnvManager(logger logger.Logger) Manager {
return &envManager{logger: logger}
func NewStderrManager(logger logger.Logger) Manager {
return &stderrManager{
logger: logger,
reported: make(map[string]bool),
}
}
func (f *envManager) ReportDeprecated(feature Note) {
func (f *stderrManager) ReportDeprecated(feature Note) {
if f.reported[feature.Name] {
return
}
f.reported[feature.Name] = true
if !feature.Impending() {
f.logger.Warn(feature.MessageWithLink())
return

View file

@ -4,27 +4,28 @@ import (
"sync"
"github.com/sagernet/sing-box/experimental/deprecated"
"github.com/sagernet/sing/common"
)
var _ deprecated.Manager = (*deprecatedManager)(nil)
type deprecatedManager struct {
access sync.Mutex
features []deprecated.Note
access sync.Mutex
notes []deprecated.Note
}
func (m *deprecatedManager) ReportDeprecated(feature deprecated.Note) {
m.access.Lock()
defer m.access.Unlock()
m.features = append(m.features, feature)
m.notes = common.Uniq(append(m.notes, feature))
}
func (m *deprecatedManager) Get() []deprecated.Note {
m.access.Lock()
defer m.access.Unlock()
features := m.features
m.features = nil
return features
notes := m.notes
m.notes = nil
return notes
}
var _ = deprecated.Note(DeprecatedNote{})