feat: 独立debug选项,改善日志文件输出

This commit is contained in:
Mmx233
2022-03-01 23:11:17 +08:00
parent ccd1472f6f
commit 7867a99f3b
5 changed files with 33 additions and 27 deletions

View File

@@ -36,6 +36,9 @@ func readConfig() error {
Guardian: srunModels.Guardian{ Guardian: srunModels.Guardian{
Duration: 300, Duration: 300,
}, },
Debug: srunModels.Debug{
Path: "./",
},
}) })
//生成配置文件 //生成配置文件

View File

@@ -15,7 +15,6 @@ type Guardian struct {
type Basic struct { type Basic struct {
Timeout uint `json:"timeout"` Timeout uint `json:"timeout"`
Interfaces string `json:"interfaces"` Interfaces string `json:"interfaces"`
DemoMode bool `json:"demo_mode" yaml:"demo_mode"`
SkipNetCheck bool `json:"skip_net_check" yaml:"skip_net_check"` SkipNetCheck bool `json:"skip_net_check" yaml:"skip_net_check"`
} }
@@ -23,6 +22,12 @@ type Settings struct {
Basic Basic `json:"basic"` Basic Basic `json:"basic"`
Guardian Guardian `json:"guardian"` Guardian Guardian `json:"guardian"`
Daemon Daemon `json:"daemon"` Daemon Daemon `json:"daemon"`
Debug Debug
}
type Debug struct {
Enable bool `json:"enable"`
Path string `json:"path"`
} }
type Config struct { type Config struct {

View File

@@ -2,40 +2,37 @@ package util
import ( import (
"fmt" "fmt"
"github.com/Mmx233/BitSrunLoginGo/global"
"github.com/Mmx233/tool" "github.com/Mmx233/tool"
"log" "log"
"os" "os"
"reflect"
"runtime" "runtime"
"time" "time"
) )
type loG struct { type loG struct {
timeStamp string timeStamp string
Demo bool WriteFile bool
Debug bool
OutPut bool OutPut bool
} }
var Log loG var Log loG
func (c *loG) WriteLog(name string, a ...interface{}) { func (c *loG) WriteLog(name string, a ...interface{}) {
if !c.Demo { if !(c.Debug && c.WriteFile) {
return return
} }
for _, v := range a {
var t string var t string
switch reflect.TypeOf(v).Kind() { for i, v := range a {
case reflect.String: t += fmt.Sprint(v)
t = v.(string) if i != len(a)-1 {
case reflect.Interface: t += " "
t = v.(error).Error()
default:
t = fmt.Sprint(v)
} }
err := tool.File.Add(name, fmt.Sprintf(time.Now().Format("2006/01/02 15:04:05 "))+t, 700) }
err := tool.File.Add(global.Config.Settings.Debug.Path+name, fmt.Sprintf(time.Now().Format("2006/01/02 15:04:05 "))+t, 700)
if err != nil { if err != nil {
log.Println("Log error: ", err) log.Println("Write log error: ", err)
}
} }
} }
@@ -48,19 +45,17 @@ func (c *loG) genTimeStamp() {
func (c *loG) Println(a ...interface{}) { func (c *loG) Println(a ...interface{}) {
c.genTimeStamp() c.genTimeStamp()
c.WriteLog("Login-"+c.timeStamp+".log", a...) c.WriteLog("Login-"+c.timeStamp+".log", a...)
if !c.OutPut { if c.OutPut {
return
}
log.Println(a...) log.Println(a...)
}
} }
func (c *loG) Fatalln(a ...interface{}) { func (c *loG) Fatalln(a ...interface{}) {
c.genTimeStamp() c.genTimeStamp()
c.WriteLog("LoginError-"+c.timeStamp+".log", a...) c.WriteLog("LoginError-"+c.timeStamp+".log", a...)
if !c.OutPut { if c.OutPut {
return
}
log.Fatalln(a...) log.Fatalln(a...)
}
} }
func (c *loG) CatchRecover() { func (c *loG) CatchRecover() {

View File

@@ -10,7 +10,8 @@ import (
) )
func Login(c *srunTransfer.Login) error { func Login(c *srunTransfer.Login) error {
util.Log.Demo = c.Demo util.Log.Debug = c.Debug
util.Log.WriteFile = c.WriteLog
util.Log.OutPut = c.OutPut util.Log.OutPut = c.OutPut
G := util.GenerateLoginInfo(c.LoginInfo.Form, c.LoginInfo.Meta) G := util.GenerateLoginInfo(c.LoginInfo.Form, c.LoginInfo.Meta)
@@ -101,7 +102,7 @@ func Login(c *srunTransfer.Login) error {
return e return e
} else { } else {
util.Log.Println("登录结果: " + G.LoginResult) util.Log.Println("登录结果: " + G.LoginResult)
if c.Demo { if c.Debug {
util.Log.Println(res) util.Log.Println(res)
} }
} }

View File

@@ -22,8 +22,10 @@ type LoginInfo struct {
} }
type Login struct { type Login struct {
//文件日志输出开关 //Debug模式
Demo bool Debug bool
//输出日志文件
WriteLog bool
//控制台日志打印开关 //控制台日志打印开关
OutPut bool OutPut bool
//登陆前是否检查网络,只在离线时登录 //登陆前是否检查网络,只在离线时登录