refactor:log包增加处理机制
This commit is contained in:
@@ -1,15 +1,9 @@
|
||||
package Util
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func getbyte(a byte) int {
|
||||
x := int(a)
|
||||
if x > 255 {
|
||||
log.Println("INVALID_CHARACTER_ERR: DOM Exception 5")
|
||||
os.Exit(1)
|
||||
Log.Fatalln("INVALID_CHARACTER_ERR: DOM Exception 5")
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
17
Util/file.go
17
Util/file.go
@@ -1,6 +1,7 @@
|
||||
package Util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@@ -24,7 +25,7 @@ func (*file) Exists(path string) bool {
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -36,7 +37,7 @@ func (*file) ReadJson(path string, receiver interface{}) error {
|
||||
}
|
||||
|
||||
func (a *file) Write(path string, data []byte) error {
|
||||
return ioutil.WriteFile(a.GetRootPath()+"/"+path, data, 700)
|
||||
return ioutil.WriteFile(a.GetRootPath()+path, data, 700)
|
||||
}
|
||||
|
||||
func (a *file) WriteJson(path string, receiver interface{}) error {
|
||||
@@ -52,5 +53,15 @@ func (*file) GetRootPath() string {
|
||||
if err != nil {
|
||||
ErrHandler(err)
|
||||
}
|
||||
return filepath.Dir(t)
|
||||
return filepath.Dir(t) + "/"
|
||||
}
|
||||
|
||||
func (a *file) Add(path string, c string) error {
|
||||
file, err := os.OpenFile(a.GetRootPath()+path, os.O_WRONLY|os.O_CREATE, 700)
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = bufio.NewWriter(file).WriteString(c + "\n")
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package Util
|
||||
import (
|
||||
"Mmx/Global"
|
||||
"Mmx/Modles"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
@@ -24,16 +23,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)
|
||||
}
|
||||
|
||||
|
||||
39
Util/log.go
Normal file
39
Util/log.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package Util
|
||||
|
||||
import (
|
||||
"Mmx/Global"
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type loG struct{}
|
||||
|
||||
var Log loG
|
||||
|
||||
func (loG) WriteLog(name string, a ...interface{}) {
|
||||
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)
|
||||
}
|
||||
_ = File.Add(name, t)
|
||||
}
|
||||
}
|
||||
|
||||
func (c loG) Println(a ...interface{}) {
|
||||
if Global.Config.Settings.DemoMode {
|
||||
c.WriteLog("Login.loG", a...)
|
||||
}
|
||||
log.Println(a...)
|
||||
}
|
||||
|
||||
func (c loG) Fatalln(a ...interface{}) {
|
||||
c.WriteLog("LoginError.loG", a...)
|
||||
log.Fatalln(a...)
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
)
|
||||
@@ -51,9 +50,9 @@ func Sha1(content string) string {
|
||||
|
||||
func ErrHandler(err error) {
|
||||
if err != nil {
|
||||
log.Println("运行出错,状态异常")
|
||||
Log.Println("运行出错,状态异常")
|
||||
if Global.Config.Settings.DemoMode {
|
||||
log.Fatalln(err)
|
||||
Log.Fatalln(err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user