refactor:优化日志打印方式

This commit is contained in:
Mmx
2021-05-03 14:30:15 +08:00
parent fb562f92c9
commit 7ceb7169e5
6 changed files with 36 additions and 32 deletions

View File

@@ -1,15 +1,15 @@
package Request package Request
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
) )
func Get(Url string, Query map[string]string) (string, error) { func Get(Url string, Query map[string]string) (string, error) {
req, err := http.NewRequest("GET", Url, nil) req, err := http.NewRequest("GET", Url, nil)
if err != nil { if err != nil {
fmt.Println(err) log.Println(err)
return "", err return "", err
} }
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36") req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36")
@@ -22,7 +22,7 @@ func Get(Url string, Query map[string]string) (string, error) {
} }
resp, err := (&http.Client{}).Do(req) resp, err := (&http.Client{}).Do(req)
if err != nil { if err != nil {
fmt.Println(err) log.Println(err)
return "", err return "", err
} }
defer resp.Body.Close() defer resp.Body.Close()

View File

@@ -1,15 +1,15 @@
package Util package Util
import ( import (
"fmt" "log"
"os" "os"
) )
func getbyte(a byte) int { func getbyte(a byte) int {
x := int(a) x := int(a)
if x > 255 { if x > 255 {
fmt.Println("INVALID_CHARACTER_ERR: DOM Exception 5") log.Println("INVALID_CHARACTER_ERR: DOM Exception 5")
os.Exit(3) os.Exit(1)
} }
return x return x
} }

View File

@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
) )
type file struct { type file struct {
@@ -35,5 +36,13 @@ func (*file) Write(path string, receiver interface{}) error {
if err != nil { if err != nil {
return err 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)
} }

View File

@@ -3,19 +3,13 @@ package Util
import ( import (
"Mmx/Global" "Mmx/Global"
"Mmx/Modles" "Mmx/Modles"
"fmt" "log"
"os" "os"
"path/filepath"
) )
func init() { func init() {
//配置文件初始化 //配置文件初始化
Path := "Config.json" Path := File.GetRootPath() + "/Config.json"
if t, err := os.Executable(); err != nil {
ErrHandler(err)
} else {
Path = filepath.Dir(t) + "/" + Path
}
if !File.Exists(Path) { if !File.Exists(Path) {
if err := File.Write(Path, &Modles.Config{ //默认值 if err := File.Write(Path, &Modles.Config{ //默认值
From: Modles.LoginForm{ From: Modles.LoginForm{
@@ -30,17 +24,17 @@ func init() {
Enc: "srun_bx1", Enc: "srun_bx1",
}, },
}); err != nil { }); err != nil {
fmt.Println("创建配置文件失败:\n", err.Error()) log.Println("创建配置文件失败:\n", err.Error())
os.Exit(3) os.Exit(1)
} }
fmt.Println("已生成配置文件,请编辑 'Config.json' 然后重试") log.Println("已生成配置文件,请编辑 'Config.json' 然后重试")
os.Exit(1) os.Exit(0)
} }
var c Modles.Config var c Modles.Config
if err := File.Read(Path, &c); err != nil { if err := File.Read(Path, &c); err != nil {
fmt.Println("读取配置文件失败:\n", err.Error()) log.Println("读取配置文件失败:\n", err.Error())
os.Exit(3) os.Exit(1)
} }
Global.Config = &c Global.Config = &c

View File

@@ -7,6 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"regexp" "regexp"
) )
@@ -50,11 +51,10 @@ func Sha1(content string) string {
func ErrHandler(err error) { func ErrHandler(err error) {
if err != nil { if err != nil {
fmt.Println("运行出错,状态异常") log.Println("运行出错,状态异常")
if Global.Config.Settings.DemoMode { if Global.Config.Settings.DemoMode {
panic(err) log.Fatalln(err)
} }
fmt.Println(err) os.Exit(1)
os.Exit(3)
} }
} }

15
main.go
View File

@@ -6,26 +6,27 @@ import (
"Mmx/Util" "Mmx/Util"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"time" "time"
) )
func main() { func main() {
fmt.Println("Step0: 检查状态…") log.Println("Step0: 检查状态…")
G := Global.Config.Generate() G := Global.Config.Generate()
if Global.Config.Settings.QuitIfNetOk && Util.Checker.NetOk(G.UrlCheckApi) { if Global.Config.Settings.QuitIfNetOk && Util.Checker.NetOk(G.UrlCheckApi) {
fmt.Println("网络正常,程序退出") log.Println("网络正常,程序退出")
return return
} }
fmt.Println("Step1: 正在获取客户端ip") log.Println("Step1: 正在获取客户端ip")
{ {
body, err := Request.Get(G.UrlLoginPage, nil) body, err := Request.Get(G.UrlLoginPage, nil)
Util.ErrHandler(err) Util.ErrHandler(err)
G.Ip, err = Util.GetIp(body) G.Ip, err = Util.GetIp(body)
Util.ErrHandler(err) Util.ErrHandler(err)
} }
fmt.Println("Step2: 正在获取Token") log.Println("Step2: 正在获取Token")
{ {
data, err := Request.Get(G.UrlGetChallengeApi, map[string]string{ data, err := Request.Get(G.UrlGetChallengeApi, map[string]string{
"callback": "jsonp1583251661367", "callback": "jsonp1583251661367",
@@ -36,7 +37,7 @@ func main() {
G.Token, err = Util.GetToken(data) G.Token, err = Util.GetToken(data)
Util.ErrHandler(err) Util.ErrHandler(err)
} }
fmt.Println("Step3: 执行登录…") log.Println("Step3: 执行登录…")
{ {
info, err := json.Marshal(map[string]string{ info, err := json.Marshal(map[string]string{
"username": G.Form.UserName, "username": G.Form.UserName,
@@ -75,9 +76,9 @@ func main() {
Util.ErrHandler(err) Util.ErrHandler(err)
G.LoginResult, err = Util.GetResult(res) G.LoginResult, err = Util.GetResult(res)
Util.ErrHandler(err) Util.ErrHandler(err)
fmt.Println("登录结果: " + G.LoginResult) log.Println("登录结果: " + G.LoginResult)
if Global.Config.Settings.DemoMode { if Global.Config.Settings.DemoMode {
fmt.Println(res) log.Println(res)
} }
} }
} }