refactor:优化日志打印方式
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
package Util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func getbyte(a byte) int {
|
||||
x := int(a)
|
||||
if x > 255 {
|
||||
fmt.Println("INVALID_CHARACTER_ERR: DOM Exception 5")
|
||||
os.Exit(3)
|
||||
log.Println("INVALID_CHARACTER_ERR: DOM Exception 5")
|
||||
os.Exit(1)
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
11
Util/file.go
11
Util/file.go
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type file struct {
|
||||
@@ -35,5 +36,13 @@ func (*file) Write(path string, receiver interface{}) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(path, data, 777)
|
||||
return ioutil.WriteFile(path, data, 700)
|
||||
}
|
||||
|
||||
func (*file) GetRootPath() string {
|
||||
t, err := os.Executable()
|
||||
if err != nil {
|
||||
ErrHandler(err)
|
||||
}
|
||||
return filepath.Dir(t)
|
||||
}
|
||||
|
||||
22
Util/init.go
22
Util/init.go
@@ -3,19 +3,13 @@ package Util
|
||||
import (
|
||||
"Mmx/Global"
|
||||
"Mmx/Modles"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func init() {
|
||||
//配置文件初始化
|
||||
Path := "Config.json"
|
||||
if t, err := os.Executable(); err != nil {
|
||||
ErrHandler(err)
|
||||
} else {
|
||||
Path = filepath.Dir(t) + "/" + Path
|
||||
}
|
||||
Path := File.GetRootPath() + "/Config.json"
|
||||
if !File.Exists(Path) {
|
||||
if err := File.Write(Path, &Modles.Config{ //默认值
|
||||
From: Modles.LoginForm{
|
||||
@@ -30,17 +24,17 @@ func init() {
|
||||
Enc: "srun_bx1",
|
||||
},
|
||||
}); err != nil {
|
||||
fmt.Println("创建配置文件失败:\n", err.Error())
|
||||
os.Exit(3)
|
||||
log.Println("创建配置文件失败:\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("已生成配置文件,请编辑 'Config.json' 然后重试")
|
||||
os.Exit(1)
|
||||
log.Println("已生成配置文件,请编辑 'Config.json' 然后重试")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
var c Modles.Config
|
||||
if err := File.Read(Path, &c); err != nil {
|
||||
fmt.Println("读取配置文件失败:\n", err.Error())
|
||||
os.Exit(3)
|
||||
log.Println("读取配置文件失败:\n", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
Global.Config = &c
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
)
|
||||
@@ -50,11 +51,10 @@ func Sha1(content string) string {
|
||||
|
||||
func ErrHandler(err error) {
|
||||
if err != nil {
|
||||
fmt.Println("运行出错,状态异常")
|
||||
log.Println("运行出错,状态异常")
|
||||
if Global.Config.Settings.DemoMode {
|
||||
panic(err)
|
||||
log.Fatalln(err)
|
||||
}
|
||||
fmt.Println(err)
|
||||
os.Exit(3)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user