From f0c3df85dafe683d8a78463a7980adbecafa7a7d Mon Sep 17 00:00:00 2001 From: Mmx <1624045573@qq.com> Date: Wed, 5 May 2021 10:52:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8Dbug=E8=8B=A5=E5=B9=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1、修复日志不追加的问题 2、修复未初始化时Demo mode读取panic问题 3、修复重构时文件名也被替换的问题 4、修复file包逻辑漏洞 5、log输出文件名新增时间戳 --- Util/file.go | 18 ++++++++++-------- Util/init.go | 7 ++++--- Util/log.go | 23 +++++++++++++++++------ 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/Util/file.go b/Util/file.go index 57381eb..736c458 100644 --- a/Util/file.go +++ b/Util/file.go @@ -8,8 +8,7 @@ import ( "path/filepath" ) -type file struct { -} +type file struct{} var File file @@ -28,12 +27,12 @@ func (a *file) Read(path string) ([]byte, error) { return ioutil.ReadFile(a.GetRootPath() + path) } -func (*file) ReadJson(path string, receiver interface{}) error { - file, err := ioutil.ReadFile(path) +func (a *file) ReadJson(path string, receiver interface{}) error { + data, err := a.Read(path) if err != nil { return err } - return json.Unmarshal(file, receiver) + return json.Unmarshal(data, receiver) } func (a *file) Write(path string, data []byte) error { @@ -57,11 +56,14 @@ func (*file) GetRootPath() string { } func (a *file) Add(path string, c string) error { - file, err := os.OpenFile(a.GetRootPath()+path, os.O_WRONLY|os.O_CREATE, 700) + file, err := os.OpenFile(a.GetRootPath()+path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 700) defer file.Close() if err != nil { return err } - _, err = bufio.NewWriter(file).WriteString(c + "\n") - return err + w := bufio.NewWriter(file) + if _, err = w.WriteString(c + "\n"); err != nil { + return err + } + return w.Flush() } diff --git a/Util/init.go b/Util/init.go index 1767228..21f67fa 100644 --- a/Util/init.go +++ b/Util/init.go @@ -3,6 +3,7 @@ package Util import ( "Mmx/Global" "Mmx/Modles" + "log" "os" ) @@ -23,16 +24,16 @@ func init() { Enc: "srun_bx1", }, }); err != nil { - Log.Println("创建配置文件失败:\n", err.Error()) + log.Println("创建配置文件失败:\n", err.Error()) os.Exit(1) } - Log.Println("已生成配置文件,请编辑 'Config.json' 然后重试") + log.Println("已生成配置文件,请编辑 'Config.json' 然后重试") os.Exit(0) } var c Modles.Config if err := File.ReadJson(Path, &c); err != nil { - Log.Println("读取配置文件失败:\n", err.Error()) + log.Println("读取配置文件失败:\n", err.Error()) os.Exit(1) } diff --git a/Util/log.go b/Util/log.go index 58103ad..34cf3a6 100644 --- a/Util/log.go +++ b/Util/log.go @@ -5,13 +5,16 @@ import ( "fmt" "log" "reflect" + "time" ) -type loG struct{} +type loG struct { + timeStamp string +} var Log loG -func (loG) WriteLog(name string, a ...interface{}) { +func (*loG) WriteLog(name string, a ...interface{}) { for _, v := range a { var t string switch reflect.TypeOf(v).Kind() { @@ -26,14 +29,22 @@ func (loG) WriteLog(name string, a ...interface{}) { } } -func (c loG) Println(a ...interface{}) { +func (c *loG) genTimeStamp() { + if c.timeStamp == "" { + c.timeStamp = time.Now().Format("2006.01.02-15:04:05") + } +} + +func (c *loG) Println(a ...interface{}) { + c.genTimeStamp() if Global.Config.Settings.DemoMode { - c.WriteLog("Login.loG", a...) + c.WriteLog("Login-"+c.timeStamp+".log", a...) } log.Println(a...) } -func (c loG) Fatalln(a ...interface{}) { - c.WriteLog("LoginError.loG", a...) +func (c *loG) Fatalln(a ...interface{}) { + c.genTimeStamp() + c.WriteLog("LoginError-"+c.timeStamp+".log", a...) log.Fatalln(a...) }