diff --git a/box.go b/box.go index 8132e462..5a6e3d55 100644 --- a/box.go +++ b/box.go @@ -203,21 +203,23 @@ func (s *Box) Start() error { func (s *Box) preStart() error { for serviceName, service := range s.preServices { + s.logger.Trace("pre-start ", serviceName) err := adapter.PreStart(service) if err != nil { - return E.Cause(err, "pre-start ", serviceName) + return E.Cause(err, "pre-starting ", serviceName) } } for i, out := range s.outbounds { + var tag string + if out.Tag() == "" { + tag = F.ToString(i) + } else { + tag = out.Tag() + } if starter, isStarter := out.(common.Starter); isStarter { + s.logger.Trace("initializing outbound/", out.Type(), "[", tag, "]") err := starter.Start() if err != nil { - var tag string - if out.Tag() == "" { - tag = F.ToString(i) - } else { - tag = out.Tag() - } return E.Cause(err, "initialize outbound/", out.Type(), "[", tag, "]") } } @@ -231,24 +233,27 @@ func (s *Box) start() error { return err } for serviceName, service := range s.preServices { + s.logger.Trace("starting ", serviceName) err = service.Start() if err != nil { return E.Cause(err, "start ", serviceName) } } for i, in := range s.inbounds { + var tag string + if in.Tag() == "" { + tag = F.ToString(i) + } else { + tag = in.Tag() + } + s.logger.Trace("initializing inbound/", in.Type(), "[", tag, "]") err = in.Start() if err != nil { - var tag string - if in.Tag() == "" { - tag = F.ToString(i) - } else { - tag = in.Tag() - } return E.Cause(err, "initialize inbound/", in.Type(), "[", tag, "]") } } for serviceName, service := range s.postServices { + s.logger.Trace("starting ", service) err = service.Start() if err != nil { return E.Cause(err, "start ", serviceName) @@ -266,30 +271,36 @@ func (s *Box) Close() error { } var errors error for serviceName, service := range s.postServices { + s.logger.Trace("closing ", serviceName) errors = E.Append(errors, service.Close(), func(err error) error { return E.Cause(err, "close ", serviceName) }) } for i, in := range s.inbounds { + s.logger.Trace("closing inbound/", in.Type(), "[", i, "]") errors = E.Append(errors, in.Close(), func(err error) error { return E.Cause(err, "close inbound/", in.Type(), "[", i, "]") }) } for i, out := range s.outbounds { + s.logger.Trace("closing outbound/", out.Type(), "[", i, "]") errors = E.Append(errors, common.Close(out), func(err error) error { return E.Cause(err, "close outbound/", out.Type(), "[", i, "]") }) } + s.logger.Trace("closing router") if err := common.Close(s.router); err != nil { errors = E.Append(errors, err, func(err error) error { return E.Cause(err, "close router") }) } for serviceName, service := range s.preServices { + s.logger.Trace("closing ", serviceName) errors = E.Append(errors, service.Close(), func(err error) error { return E.Cause(err, "close ", serviceName) }) } + s.logger.Trace("closing log factory") if err := common.Close(s.logFactory); err != nil { errors = E.Append(errors, err, func(err error) error { return E.Cause(err, "close log factory") diff --git a/route/router.go b/route/router.go index 6dbb1922..380ec5e5 100644 --- a/route/router.go +++ b/route/router.go @@ -489,31 +489,56 @@ func (r *Router) Start() error { } func (r *Router) Close() error { - for _, rule := range r.rules { - err := rule.Close() - if err != nil { - return err - } + var err error + for i, rule := range r.rules { + r.logger.Trace("closing rule[", i, "]") + err = E.Append(err, rule.Close(), func(err error) error { + return E.Cause(err, "close rule[", i, "]") + }) } - for _, rule := range r.dnsRules { - err := rule.Close() - if err != nil { - return err - } + for i, rule := range r.dnsRules { + r.logger.Trace("closing dns rule[", i, "]") + err = E.Append(err, rule.Close(), func(err error) error { + return E.Cause(err, "close dns rule[", i, "]") + }) } - for _, transport := range r.transports { - err := transport.Close() - if err != nil { - return err - } + for i, transport := range r.transports { + r.logger.Trace("closing transport[", i, "] ") + err = E.Append(err, transport.Close(), func(err error) error { + return E.Cause(err, "close dns transport[", i, "]") + }) } - return common.Close( - common.PtrOrNil(r.geoIPReader), - r.interfaceMonitor, - r.networkMonitor, - r.packageManager, - r.timeService, - ) + if r.geositeReader != nil { + r.logger.Trace("closing geoip reader") + err = E.Append(err, common.Close(r.geoIPReader), func(err error) error { + return E.Cause(err, "close geoip reader") + }) + } + if r.interfaceMonitor != nil { + r.logger.Trace("closing interface monitor") + err = E.Append(err, r.interfaceMonitor.Close(), func(err error) error { + return E.Cause(err, "close interface monitor") + }) + } + if r.networkMonitor != nil { + r.logger.Trace("closing network monitor") + err = E.Append(err, r.networkMonitor.Close(), func(err error) error { + return E.Cause(err, "close network monitor") + }) + } + if r.packageManager != nil { + r.logger.Trace("closing package manager") + err = E.Append(err, r.packageManager.Close(), func(err error) error { + return E.Cause(err, "close package manager") + }) + } + if r.timeService != nil { + r.logger.Trace("closing time service") + err = E.Append(err, r.timeService.Close(), func(err error) error { + return E.Cause(err, "close time service") + }) + } + return err } func (r *Router) GeoIPReader() *geoip.Reader {