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{
Duration: 300,
},
Debug: srunModels.Debug{
Path: "./",
},
})
//生成配置文件

View File

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

View File

@@ -2,41 +2,38 @@ package util
import (
"fmt"
"github.com/Mmx233/BitSrunLoginGo/global"
"github.com/Mmx233/tool"
"log"
"os"
"reflect"
"runtime"
"time"
)
type loG struct {
timeStamp string
Demo bool
WriteFile bool
Debug bool
OutPut bool
}
var Log loG
func (c *loG) WriteLog(name string, a ...interface{}) {
if !c.Demo {
if !(c.Debug && c.WriteFile) {
return
}
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)
}
err := tool.File.Add(name, fmt.Sprintf(time.Now().Format("2006/01/02 15:04:05 "))+t, 700)
if err != nil {
log.Println("Log error: ", err)
var t string
for i, v := range a {
t += fmt.Sprint(v)
if i != len(a)-1 {
t += " "
}
}
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 {
log.Println("Write log error: ", err)
}
}
func (c *loG) genTimeStamp() {
@@ -48,19 +45,17 @@ func (c *loG) genTimeStamp() {
func (c *loG) Println(a ...interface{}) {
c.genTimeStamp()
c.WriteLog("Login-"+c.timeStamp+".log", a...)
if !c.OutPut {
return
if c.OutPut {
log.Println(a...)
}
log.Println(a...)
}
func (c *loG) Fatalln(a ...interface{}) {
c.genTimeStamp()
c.WriteLog("LoginError-"+c.timeStamp+".log", a...)
if !c.OutPut {
return
if c.OutPut {
log.Fatalln(a...)
}
log.Fatalln(a...)
}
func (c *loG) CatchRecover() {

View File

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

View File

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