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"
)
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()
}