mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 23:47:07 -05:00
migrate to new severity def
This commit is contained in:
parent
4146590d52
commit
742d9d25d1
@ -9,6 +9,7 @@ import (
|
||||
"v2ray.com/core/app/log/internal"
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/errors"
|
||||
"v2ray.com/core/common/log"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -51,15 +52,15 @@ func InitErrorLogger(file string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getLoggerAndPrefix(s errors.Severity) (internal.LogWriter, string) {
|
||||
func getLoggerAndPrefix(s log.Severity) (internal.LogWriter, string) {
|
||||
switch s {
|
||||
case errors.SeverityDebug:
|
||||
case log.Severity_Debug:
|
||||
return debugLogger, "[Debug]"
|
||||
case errors.SeverityInfo:
|
||||
case log.Severity_Info:
|
||||
return infoLogger, "[Info]"
|
||||
case errors.SeverityWarning:
|
||||
case log.Severity_Warning:
|
||||
return warningLogger, "[Warning]"
|
||||
case errors.SeverityError:
|
||||
case log.Severity_Error:
|
||||
return errorLogger, "[Error]"
|
||||
default:
|
||||
return infoLogger, "[Info]"
|
||||
|
@ -4,33 +4,24 @@ package errors
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"v2ray.com/core/common/log"
|
||||
"v2ray.com/core/common/serial"
|
||||
)
|
||||
|
||||
// Severity describes how severe the error is.
|
||||
type Severity int
|
||||
|
||||
const (
|
||||
SeverityDebug Severity = iota
|
||||
SeverityInfo
|
||||
SeverityWarning
|
||||
SeverityError
|
||||
)
|
||||
|
||||
type hasInnerError interface {
|
||||
// Inner returns the underlying error of this one.
|
||||
Inner() error
|
||||
}
|
||||
|
||||
type hasSeverity interface {
|
||||
Severity() Severity
|
||||
Severity() log.Severity
|
||||
}
|
||||
|
||||
// Error is an error object with underlying error.
|
||||
type Error struct {
|
||||
message []interface{}
|
||||
inner error
|
||||
severity Severity
|
||||
severity log.Severity
|
||||
path []string
|
||||
}
|
||||
|
||||
@ -59,19 +50,19 @@ func (v *Error) Base(err error) *Error {
|
||||
return v
|
||||
}
|
||||
|
||||
func (v *Error) atSeverity(s Severity) *Error {
|
||||
func (v *Error) atSeverity(s log.Severity) *Error {
|
||||
v.severity = s
|
||||
return v
|
||||
}
|
||||
|
||||
func (v *Error) Severity() Severity {
|
||||
func (v *Error) Severity() log.Severity {
|
||||
if v.inner == nil {
|
||||
return v.severity
|
||||
}
|
||||
|
||||
if s, ok := v.inner.(hasSeverity); ok {
|
||||
as := s.Severity()
|
||||
if as > v.severity {
|
||||
if as < v.severity {
|
||||
return as
|
||||
}
|
||||
}
|
||||
@ -81,22 +72,22 @@ func (v *Error) Severity() Severity {
|
||||
|
||||
// AtDebug sets the severity to debug.
|
||||
func (v *Error) AtDebug() *Error {
|
||||
return v.atSeverity(SeverityDebug)
|
||||
return v.atSeverity(log.Severity_Debug)
|
||||
}
|
||||
|
||||
// AtInfo sets the severity to info.
|
||||
func (v *Error) AtInfo() *Error {
|
||||
return v.atSeverity(SeverityInfo)
|
||||
return v.atSeverity(log.Severity_Info)
|
||||
}
|
||||
|
||||
// AtWarning sets the severity to warning.
|
||||
func (v *Error) AtWarning() *Error {
|
||||
return v.atSeverity(SeverityWarning)
|
||||
return v.atSeverity(log.Severity_Warning)
|
||||
}
|
||||
|
||||
// AtError sets the severity to error.
|
||||
func (v *Error) AtError() *Error {
|
||||
return v.atSeverity(SeverityError)
|
||||
return v.atSeverity(log.Severity_Error)
|
||||
}
|
||||
|
||||
// Path sets the path to the location where this error happens.
|
||||
@ -109,7 +100,7 @@ func (v *Error) Path(path ...string) *Error {
|
||||
func New(msg ...interface{}) *Error {
|
||||
return &Error{
|
||||
message: msg,
|
||||
severity: SeverityInfo,
|
||||
severity: log.Severity_Info,
|
||||
}
|
||||
}
|
||||
|
||||
@ -128,9 +119,9 @@ func Cause(err error) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func GetSeverity(err error) Severity {
|
||||
func GetSeverity(err error) log.Severity {
|
||||
if s, ok := err.(hasSeverity); ok {
|
||||
return s.Severity()
|
||||
}
|
||||
return SeverityInfo
|
||||
return log.Severity_Info
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
. "v2ray.com/core/common/errors"
|
||||
"v2ray.com/core/common/log"
|
||||
. "v2ray.com/ext/assert"
|
||||
)
|
||||
|
||||
@ -12,17 +13,17 @@ func TestError(t *testing.T) {
|
||||
assert := With(t)
|
||||
|
||||
err := New("TestError")
|
||||
assert(GetSeverity(err), Equals, SeverityInfo)
|
||||
assert(GetSeverity(err), Equals, log.Severity_Info)
|
||||
|
||||
err = New("TestError2").Base(io.EOF)
|
||||
assert(GetSeverity(err), Equals, SeverityInfo)
|
||||
assert(GetSeverity(err), Equals, log.Severity_Info)
|
||||
|
||||
err = New("TestError3").Base(io.EOF).AtWarning()
|
||||
assert(GetSeverity(err), Equals, SeverityWarning)
|
||||
assert(GetSeverity(err), Equals, log.Severity_Warning)
|
||||
|
||||
err = New("TestError4").Base(io.EOF).AtWarning()
|
||||
err = New("TestError5").Base(err)
|
||||
assert(GetSeverity(err), Equals, SeverityWarning)
|
||||
assert(GetSeverity(err), Equals, log.Severity_Warning)
|
||||
assert(err.Error(), HasSubstring, "EOF")
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user