fix:修复bug若干
1、修复日志不追加的问题 2、修复未初始化时Demo mode读取panic问题 3、修复重构时文件名也被替换的问题 4、修复file包逻辑漏洞 5、log输出文件名新增时间戳
This commit is contained in:
16
Util/file.go
16
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")
|
||||
w := bufio.NewWriter(file)
|
||||
if _, err = w.WriteString(c + "\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
return w.Flush()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
23
Util/log.go
23
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...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user