diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index 057216f16..828b600a9 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -57,7 +57,9 @@ func New(file...string) *Config { if gfile.Exists(envPath) { _ = c.SetPath(envPath) } else { - glog.Errorf("Configuration directory path does not exist: %s", envPath) + if errorPrint() { + glog.Errorf("Configuration directory path does not exist: %s", envPath) + } } } else { // Dir path of working dir. @@ -97,7 +99,9 @@ func (c *Config) filePath(file...string) (path string) { } else { buffer.WriteString(fmt.Sprintf("[gcfg] cannot find config file \"%s\" with no path set/add", name)) } - glog.Error(buffer.String()) + if errorPrint() { + glog.Error(buffer.String()) + } } return path } @@ -133,13 +137,17 @@ func (c *Config) SetPath(path string) error { buffer.WriteString(fmt.Sprintf(`[gcfg] SetPath failed: path "%s" does not exist`, path)) } err := errors.New(buffer.String()) - glog.Error(err) + if errorPrint() { + glog.Error(err) + } return err } // Should be a directory. if !gfile.IsDir(realPath) { err := errors.New(fmt.Sprintf(`[gcfg] SetPath failed: path "%s" should be directory type`, path)) - glog.Error(err) + if errorPrint() { + glog.Error(err) + } return err } // Repeated path check. @@ -191,12 +199,16 @@ func (c *Config) AddPath(path string) error { buffer.WriteString(fmt.Sprintf(`[gcfg] AddPath failed: path "%s" does not exist`, path)) } err := errors.New(buffer.String()) - glog.Error(err) + if errorPrint() { + glog.Error(err) + } return err } if !gfile.IsDir(realPath) { err := errors.New(fmt.Sprintf(`[gcfg] AddPath failed: path "%s" should be directory type`, path)) - glog.Error(err) + if errorPrint() { + glog.Error(err) + } return err } // Repeated path check. @@ -275,11 +287,13 @@ func (c *Config) getJson(file...string) *gjson.Json { } return j } else { - if filePath != "" { - glog.Criticalf(`[gcfg] Load config file "%s" failed: %s`, filePath, err.Error()) - } else { - glog.Criticalf(`[gcfg] Load configuration failed: %s`, err.Error()) - } + if errorPrint() { + if filePath != "" { + glog.Criticalf(`[gcfg] Load config file "%s" failed: %s`, filePath, err.Error()) + } else { + glog.Criticalf(`[gcfg] Load configuration failed: %s`, err.Error()) + } + } } return nil }) diff --git a/g/os/gcfg/gcfg_error.go b/g/os/gcfg/gcfg_error.go new file mode 100644 index 000000000..b56a23ac6 --- /dev/null +++ b/g/os/gcfg/gcfg_error.go @@ -0,0 +1,22 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gcfg + +import ( + "github.com/gogf/gf/g/internal/cmdenv" +) + +const ( + // gERROR_PRINT_KEY is used to specify the key controlling error printing to stdout. + // This error is designed not to be returned by functions. + gERROR_PRINT_KEY = "gf.gcfg.errorprint" +) + +// errorPrint checks whether printing error to stdout. +func errorPrint() bool { + return cmdenv.Get(gERROR_PRINT_KEY, true).Bool() +} \ No newline at end of file diff --git a/g/os/gcfg/gcfg_z_unit_test.go b/g/os/gcfg/gcfg_z_unit_test.go index d4dc8a9b5..caadec849 100644 --- a/g/os/gcfg/gcfg_z_unit_test.go +++ b/g/os/gcfg/gcfg_z_unit_test.go @@ -19,6 +19,10 @@ import ( "testing" ) +func init() { + os.Setenv("GF_GCFG_ERRORPRINT", "false") +} + func Test_Basic(t *testing.T) { config := ` v1 = 1 @@ -379,7 +383,7 @@ func TestCfg_Get(t *testing.T) { gtest.Assert(c.GetTime("time").Format("2006-01-02"), "2019-06-12") gtest.Assert(c.GetGTime("time").Format("Y-m-d"), "2019-06-12") gtest.Assert(c.GetDuration("time").String(), "0s") - t.Log(c.GetString("person")) + //t.Log(c.GetString("person")) err := c.GetToStruct("person", &name) gtest.Assert(err, nil) gtest.Assert(name.Name, "gf") diff --git a/g/os/gview/gview.go b/g/os/gview/gview.go index 98957cef0..dad0fac09 100644 --- a/g/os/gview/gview.go +++ b/g/os/gview/gview.go @@ -69,7 +69,9 @@ func New(path...string) *View { if gfile.Exists(envPath) { view.SetPath(envPath) } else { - glog.Errorf("Template directory path does not exist: %s", envPath) + if errorPrint() { + glog.Errorf("Template directory path does not exist: %s", envPath) + } } } else { // Dir path of working dir. @@ -146,13 +148,17 @@ func (view *View) SetPath(path string) error { buffer.WriteString(fmt.Sprintf(`[gview] SetPath failed: path "%s" does not exist`, path)) } err := errors.New(buffer.String()) - glog.Error(err) + if errorPrint() { + glog.Error(err) + } return err } // Should be a directory. if !gfile.IsDir(realPath) { err := errors.New(fmt.Sprintf(`[gview] SetPath failed: path "%s" should be directory type`, path)) - glog.Error(err) + if errorPrint() { + glog.Error(err) + } return err } // Repeated path check. @@ -194,13 +200,17 @@ func (view *View) AddPath(path string) error { buffer.WriteString(fmt.Sprintf(`[gview] AddPath failed: path "%s" does not exist`, path)) } err := errors.New(buffer.String()) - glog.Error(err) + if errorPrint() { + glog.Error(err) + } return err } // realPath should be type of folder. if !gfile.IsDir(realPath) { err := errors.New(fmt.Sprintf(`[gview] AddPath failed: path "%s" should be directory type`, path)) - glog.Error(err) + if errorPrint() { + glog.Error(err) + } return err } // Repeated path check. diff --git a/g/os/gview/gview_doparse.go b/g/os/gview/gview_doparse.go index 1ae3d750e..7ea7239e6 100644 --- a/g/os/gview/gview_doparse.go +++ b/g/os/gview/gview_doparse.go @@ -88,7 +88,9 @@ func (view *View) searchFile(file string) (path string, folder string, err error } else { buffer.WriteString(fmt.Sprintf("[gview] cannot find template file \"%s\" with no path set/add", file)) } - glog.Error(buffer.String()) + if errorPrint() { + glog.Error(buffer.String()) + } err = errors.New(fmt.Sprintf(`template file "%s" not found`, file)) } return diff --git a/g/os/gview/gview_error.go b/g/os/gview/gview_error.go new file mode 100644 index 000000000..7e478c8f3 --- /dev/null +++ b/g/os/gview/gview_error.go @@ -0,0 +1,22 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gview + +import ( + "github.com/gogf/gf/g/internal/cmdenv" +) + +const ( + // gERROR_PRINT_KEY is used to specify the key controlling error printing to stdout. + // This error is designed not to be returned by functions. + gERROR_PRINT_KEY = "gf.gview.errorprint" +) + +// errorPrint checks whether printing error to stdout. +func errorPrint() bool { + return cmdenv.Get(gERROR_PRINT_KEY, true).Bool() +} \ No newline at end of file diff --git a/g/os/gview/gview_unit_test.go b/g/os/gview/gview_unit_test.go index a6795e16c..e9e78bbc7 100644 --- a/g/os/gview/gview_unit_test.go +++ b/g/os/gview/gview_unit_test.go @@ -11,6 +11,10 @@ import ( "testing" ) +func init() { + os.Setenv("GF_GVIEW_ERRORPRINT", "false") +} + func TestView_Basic(t *testing.T) { gtest.Case(t, func() { str := `hello {{.name}},version:{{.version}};hello {{GetName}},version:{{GetVersion}};{{.other}}`