From 7867a99f3b04eedbe3daab81ef981e6aa93b805c Mon Sep 17 00:00:00 2001 From: Mmx233 Date: Tue, 1 Mar 2022 23:11:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=8B=AC=E7=AB=8Bdebug=E9=80=89?= =?UTF-8?q?=E9=A1=B9=EF=BC=8C=E6=94=B9=E5=96=84=E6=97=A5=E5=BF=97=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- global/config.go | 3 +++ models/config.go | 7 ++++++- util/log.go | 39 +++++++++++++++++---------------------- v1/login.go | 5 +++-- v1/transfer/login.go | 6 ++++-- 5 files changed, 33 insertions(+), 27 deletions(-) diff --git a/global/config.go b/global/config.go index 5857e8a..89cdf28 100644 --- a/global/config.go +++ b/global/config.go @@ -36,6 +36,9 @@ func readConfig() error { Guardian: srunModels.Guardian{ Duration: 300, }, + Debug: srunModels.Debug{ + Path: "./", + }, }) //生成配置文件 diff --git a/models/config.go b/models/config.go index e45f827..93f425b 100644 --- a/models/config.go +++ b/models/config.go @@ -15,7 +15,6 @@ type Guardian struct { type Basic struct { Timeout uint `json:"timeout"` Interfaces string `json:"interfaces"` - DemoMode bool `json:"demo_mode" yaml:"demo_mode"` SkipNetCheck bool `json:"skip_net_check" yaml:"skip_net_check"` } @@ -23,6 +22,12 @@ type Settings struct { Basic Basic `json:"basic"` Guardian Guardian `json:"guardian"` Daemon Daemon `json:"daemon"` + Debug Debug +} + +type Debug struct { + Enable bool `json:"enable"` + Path string `json:"path"` } type Config struct { diff --git a/util/log.go b/util/log.go index b27cbb0..56e9201 100644 --- a/util/log.go +++ b/util/log.go @@ -2,41 +2,38 @@ package util import ( "fmt" + "github.com/Mmx233/BitSrunLoginGo/global" "github.com/Mmx233/tool" "log" "os" - "reflect" "runtime" "time" ) type loG struct { timeStamp string - Demo bool + WriteFile bool + Debug bool OutPut bool } var Log loG func (c *loG) WriteLog(name string, a ...interface{}) { - if !c.Demo { + if !(c.Debug && c.WriteFile) { return } - for _, v := range a { - var t string - switch reflect.TypeOf(v).Kind() { - case reflect.String: - t = v.(string) - case reflect.Interface: - t = v.(error).Error() - default: - t = fmt.Sprint(v) - } - err := tool.File.Add(name, fmt.Sprintf(time.Now().Format("2006/01/02 15:04:05 "))+t, 700) - if err != nil { - log.Println("Log error: ", err) + var t string + for i, v := range a { + t += fmt.Sprint(v) + if i != len(a)-1 { + t += " " } } + err := tool.File.Add(global.Config.Settings.Debug.Path+name, fmt.Sprintf(time.Now().Format("2006/01/02 15:04:05 "))+t, 700) + if err != nil { + log.Println("Write log error: ", err) + } } func (c *loG) genTimeStamp() { @@ -48,19 +45,17 @@ func (c *loG) genTimeStamp() { func (c *loG) Println(a ...interface{}) { c.genTimeStamp() c.WriteLog("Login-"+c.timeStamp+".log", a...) - if !c.OutPut { - return + if c.OutPut { + log.Println(a...) } - log.Println(a...) } func (c *loG) Fatalln(a ...interface{}) { c.genTimeStamp() c.WriteLog("LoginError-"+c.timeStamp+".log", a...) - if !c.OutPut { - return + if c.OutPut { + log.Fatalln(a...) } - log.Fatalln(a...) } func (c *loG) CatchRecover() { diff --git a/v1/login.go b/v1/login.go index af2f06c..c621350 100644 --- a/v1/login.go +++ b/v1/login.go @@ -10,7 +10,8 @@ import ( ) func Login(c *srunTransfer.Login) error { - util.Log.Demo = c.Demo + util.Log.Debug = c.Debug + util.Log.WriteFile = c.WriteLog util.Log.OutPut = c.OutPut G := util.GenerateLoginInfo(c.LoginInfo.Form, c.LoginInfo.Meta) @@ -101,7 +102,7 @@ func Login(c *srunTransfer.Login) error { return e } else { util.Log.Println("登录结果: " + G.LoginResult) - if c.Demo { + if c.Debug { util.Log.Println(res) } } diff --git a/v1/transfer/login.go b/v1/transfer/login.go index dc387ca..c6b49de 100644 --- a/v1/transfer/login.go +++ b/v1/transfer/login.go @@ -22,8 +22,10 @@ type LoginInfo struct { } type Login struct { - //文件日志输出开关 - Demo bool + //Debug模式 + Debug bool + //输出日志文件 + WriteLog bool //控制台日志打印开关 OutPut bool //登陆前是否检查网络,只在离线时登录