fix:修复bug若干

1、修复日志不追加的问题
2、修复未初始化时Demo mode读取panic问题
3、修复重构时文件名也被替换的问题
4、修复file包逻辑漏洞
5、log输出文件名新增时间戳
This commit is contained in:
Mmx
2021-05-05 10:52:17 +08:00
parent 4e4cdd35c2
commit f0c3df85da
3 changed files with 31 additions and 17 deletions

View File

@@ -8,8 +8,7 @@ import (
"path/filepath" "path/filepath"
) )
type file struct { type file struct{}
}
var File file var File file
@@ -28,12 +27,12 @@ func (a *file) Read(path string) ([]byte, error) {
return ioutil.ReadFile(a.GetRootPath() + path) return ioutil.ReadFile(a.GetRootPath() + path)
} }
func (*file) ReadJson(path string, receiver interface{}) error { func (a *file) ReadJson(path string, receiver interface{}) error {
file, err := ioutil.ReadFile(path) data, err := a.Read(path)
if err != nil { if err != nil {
return err return err
} }
return json.Unmarshal(file, receiver) return json.Unmarshal(data, receiver)
} }
func (a *file) Write(path string, data []byte) error { 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 { 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() defer file.Close()
if err != nil { if err != nil {
return err return err
} }
_, err = bufio.NewWriter(file).WriteString(c + "\n") w := bufio.NewWriter(file)
return err if _, err = w.WriteString(c + "\n"); err != nil {
return err
}
return w.Flush()
} }

View File

@@ -3,6 +3,7 @@ package Util
import ( import (
"Mmx/Global" "Mmx/Global"
"Mmx/Modles" "Mmx/Modles"
"log"
"os" "os"
) )
@@ -23,16 +24,16 @@ func init() {
Enc: "srun_bx1", Enc: "srun_bx1",
}, },
}); err != nil { }); err != nil {
Log.Println("创建配置文件失败:\n", err.Error()) log.Println("创建配置文件失败:\n", err.Error())
os.Exit(1) os.Exit(1)
} }
Log.Println("已生成配置文件,请编辑 'Config.json' 然后重试") log.Println("已生成配置文件,请编辑 'Config.json' 然后重试")
os.Exit(0) os.Exit(0)
} }
var c Modles.Config var c Modles.Config
if err := File.ReadJson(Path, &c); err != nil { if err := File.ReadJson(Path, &c); err != nil {
Log.Println("读取配置文件失败:\n", err.Error()) log.Println("读取配置文件失败:\n", err.Error())
os.Exit(1) os.Exit(1)
} }

View File

@@ -5,13 +5,16 @@ import (
"fmt" "fmt"
"log" "log"
"reflect" "reflect"
"time"
) )
type loG struct{} type loG struct {
timeStamp string
}
var Log loG var Log loG
func (loG) WriteLog(name string, a ...interface{}) { func (*loG) WriteLog(name string, a ...interface{}) {
for _, v := range a { for _, v := range a {
var t string var t string
switch reflect.TypeOf(v).Kind() { 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 { if Global.Config.Settings.DemoMode {
c.WriteLog("Login.loG", a...) c.WriteLog("Login-"+c.timeStamp+".log", a...)
} }
log.Println(a...) log.Println(a...)
} }
func (c loG) Fatalln(a ...interface{}) { func (c *loG) Fatalln(a ...interface{}) {
c.WriteLog("LoginError.loG", a...) c.genTimeStamp()
c.WriteLog("LoginError-"+c.timeStamp+".log", a...)
log.Fatalln(a...) log.Fatalln(a...)
} }