mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 17:38:41 +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
|
@ -1,8 +1,10 @@
|
|||
package serial
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
creflect "github.com/xtls/xray-core/common/reflect"
|
||||
"github.com/xtls/xray-core/core"
|
||||
"github.com/xtls/xray-core/infra/conf"
|
||||
|
@ -18,20 +20,20 @@ func MergeConfigFromFiles(files []string, formats []string) (string, error) {
|
|||
if j, ok := creflect.MarshalToJson(c); ok {
|
||||
return j, nil
|
||||
}
|
||||
return "", newError("marshal to json failed.").AtError()
|
||||
return "", errors.New("marshal to json failed.").AtError()
|
||||
}
|
||||
|
||||
func mergeConfigs(files []string, formats []string) (*conf.Config, error) {
|
||||
cf := &conf.Config{}
|
||||
for i, file := range files {
|
||||
newError("Reading config: ", file).AtInfo().WriteToLog()
|
||||
errors.LogInfo(context.Background(), "Reading config: ", file)
|
||||
r, err := confloader.LoadConfig(file)
|
||||
if err != nil {
|
||||
return nil, newError("failed to read config: ", file).Base(err)
|
||||
return nil, errors.New("failed to read config: ", file).Base(err)
|
||||
}
|
||||
c, err := ReaderDecoderByFormat[formats[i]](r)
|
||||
if err != nil {
|
||||
return nil, newError("failed to decode config: ", file).Base(err)
|
||||
return nil, errors.New("failed to decode config: ", file).Base(err)
|
||||
}
|
||||
if i == 0 {
|
||||
*cf = *c
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package serial
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
|
@ -61,9 +61,9 @@ func DecodeJSONConfig(reader io.Reader) (*conf.Config, error) {
|
|||
pos = findOffset(jsonContent.Bytes(), int(tErr.Offset))
|
||||
}
|
||||
if pos != nil {
|
||||
return nil, newError("failed to read config file at line ", pos.line, " char ", pos.char).Base(err)
|
||||
return nil, errors.New("failed to read config file at line ", pos.line, " char ", pos.char).Base(err)
|
||||
}
|
||||
return nil, newError("failed to read config file").Base(err)
|
||||
return nil, errors.New("failed to read config file").Base(err)
|
||||
}
|
||||
|
||||
return jsonConfig, nil
|
||||
|
@ -77,7 +77,7 @@ func LoadJSONConfig(reader io.Reader) (*core.Config, error) {
|
|||
|
||||
pbConfig, err := jsonConfig.Build()
|
||||
if err != nil {
|
||||
return nil, newError("failed to parse json config").Base(err)
|
||||
return nil, errors.New("failed to parse json config").Base(err)
|
||||
}
|
||||
|
||||
return pbConfig, nil
|
||||
|
@ -88,17 +88,17 @@ func LoadJSONConfig(reader io.Reader) (*core.Config, error) {
|
|||
func DecodeTOMLConfig(reader io.Reader) (*conf.Config, error) {
|
||||
tomlFile, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, newError("failed to read config file").Base(err)
|
||||
return nil, errors.New("failed to read config file").Base(err)
|
||||
}
|
||||
|
||||
configMap := make(map[string]interface{})
|
||||
if err := toml.Unmarshal(tomlFile, &configMap); err != nil {
|
||||
return nil, newError("failed to convert toml to map").Base(err)
|
||||
return nil, errors.New("failed to convert toml to map").Base(err)
|
||||
}
|
||||
|
||||
jsonFile, err := json.Marshal(&configMap)
|
||||
if err != nil {
|
||||
return nil, newError("failed to convert map to json").Base(err)
|
||||
return nil, errors.New("failed to convert map to json").Base(err)
|
||||
}
|
||||
|
||||
return DecodeJSONConfig(bytes.NewReader(jsonFile))
|
||||
|
@ -112,7 +112,7 @@ func LoadTOMLConfig(reader io.Reader) (*core.Config, error) {
|
|||
|
||||
pbConfig, err := tomlConfig.Build()
|
||||
if err != nil {
|
||||
return nil, newError("failed to parse toml config").Base(err)
|
||||
return nil, errors.New("failed to parse toml config").Base(err)
|
||||
}
|
||||
|
||||
return pbConfig, nil
|
||||
|
@ -123,12 +123,12 @@ func LoadTOMLConfig(reader io.Reader) (*core.Config, error) {
|
|||
func DecodeYAMLConfig(reader io.Reader) (*conf.Config, error) {
|
||||
yamlFile, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, newError("failed to read config file").Base(err)
|
||||
return nil, errors.New("failed to read config file").Base(err)
|
||||
}
|
||||
|
||||
jsonFile, err := yaml.YAMLToJSON(yamlFile)
|
||||
if err != nil {
|
||||
return nil, newError("failed to convert yaml to json").Base(err)
|
||||
return nil, errors.New("failed to convert yaml to json").Base(err)
|
||||
}
|
||||
|
||||
return DecodeJSONConfig(bytes.NewReader(jsonFile))
|
||||
|
@ -142,7 +142,7 @@ func LoadYAMLConfig(reader io.Reader) (*core.Config, error) {
|
|||
|
||||
pbConfig, err := yamlConfig.Build()
|
||||
if err != nil {
|
||||
return nil, newError("failed to parse yaml config").Base(err)
|
||||
return nil, errors.New("failed to parse yaml config").Base(err)
|
||||
}
|
||||
|
||||
return pbConfig, nil
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue