From 598eb7a23f77e73d93fbbace51fb4b689560d977 Mon Sep 17 00:00:00 2001 From: gekigek99 <53654579+gekigek99@users.noreply.github.com> Date: Fri, 25 Nov 2022 01:47:06 +0100 Subject: [PATCH] errco: Log() can add trace to msh log --- lib/config/config.go | 8 ++--- lib/conn/conn.go | 12 +++---- lib/errco/errco.go | 62 +++++++++++++++++++++------------- lib/input/input.go | 8 ++--- lib/progmgr/progmgr.go | 2 +- lib/progmgr/sgmmgr.go | 8 ++--- lib/servctrl/servctrl-utils.go | 4 +-- lib/servctrl/servctrl.go | 4 +-- 8 files changed, 61 insertions(+), 47 deletions(-) diff --git a/lib/config/config.go b/lib/config/config.go index a54616ed..01af475b 100644 --- a/lib/config/config.go +++ b/lib/config/config.go @@ -91,7 +91,7 @@ func (c *Configuration) Save() *errco.MshLog { // escape unicode characters ("\u003c" to "<" and "\u003e" to ">") configData, logMsh := utility.UnicodeEscape(configData) if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() } // write to config file @@ -142,7 +142,7 @@ func (c *Configuration) loadDefault() *errco.MshLog { version, protocol, logMsh := c.getVersionInfo() if logMsh != nil { // just log it since ms version/protocol are not vital for the connection with clients - logMsh.AddTrace().Log() + logMsh.Log() } else if c.Server.Version != version || c.Server.Protocol != protocol { c.Server.Version = version c.Server.Protocol = protocol @@ -261,7 +261,7 @@ func (c *Configuration) loadRuntime(confdef *Configuration) *errco.MshLog { // initialize ip and ports for connection logMsh := c.loadIpPorts() if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() servstats.Stats.SetMajorError(logMsh) } else { errco.NewLogln(errco.TYPE_INF, errco.LVL_3, errco.ERROR_NIL, "msh proxy setup: %s:%d --> %s:%d", ListenHost, ListenPort, TargetHost, TargetPort) @@ -271,7 +271,7 @@ func (c *Configuration) loadRuntime(confdef *Configuration) *errco.MshLog { logMsh = c.loadIcon() if logMsh != nil { // log and continue (default icon is loaded by default) - logMsh.AddTrace().Log() + logMsh.Log() } return nil diff --git a/lib/conn/conn.go b/lib/conn/conn.go index ea010c34..ef8180cc 100644 --- a/lib/conn/conn.go +++ b/lib/conn/conn.go @@ -23,7 +23,7 @@ func HandleClientSocket(clientSocket net.Conn) { reqPacket, reqType, logMsh := getReqType(clientSocket) if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() return } @@ -46,7 +46,7 @@ func HandleClientSocket(clientSocket net.Conn) { // answer to client ping logMsh = getPing(clientSocket) if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() } case errco.CLIENT_REQ_JOIN: // log to msh console and answer to client with error @@ -91,7 +91,7 @@ func HandleClientSocket(clientSocket net.Conn) { // answer to client ping logMsh := getPing(clientSocket) if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() return } @@ -118,7 +118,7 @@ func HandleClientSocket(clientSocket net.Conn) { logMsh := config.ConfigRuntime.IsWhitelist(reqPacket, clientAddress) if logMsh != nil { // warn client with text in the loadscreen - logMsh.AddTrace().Log() + logMsh.Log() mes := buildMessage(reqType, "You don't have permission to warm this server") clientSocket.Write(mes) errco.NewLogln(errco.TYPE_BYT, errco.LVL_4, errco.ERROR_NIL, "%smsh --> client%s: %v", errco.COLOR_PURPLE, errco.COLOR_RESET, mes) @@ -129,7 +129,7 @@ func HandleClientSocket(clientSocket net.Conn) { logMsh = servctrl.WarmMS() if logMsh != nil { // warn client with text in the loadscreen - logMsh.AddTrace().Log() + logMsh.Log() mes := buildMessage(reqType, "An error occurred while warming the server: check the msh log") clientSocket.Write(mes) errco.NewLogln(errco.TYPE_BYT, errco.LVL_4, errco.ERROR_NIL, "%smsh --> client%s: %v", errco.COLOR_PURPLE, errco.COLOR_RESET, mes) @@ -148,7 +148,7 @@ func HandleClientSocket(clientSocket net.Conn) { logMsh = servctrl.WarmMS() if logMsh != nil { // warn client with text in the loadscreen - logMsh.AddTrace().Log() + logMsh.Log() mes := buildMessage(errco.MESSAGE_FORMAT_TXT, "An error occurred while warming the server: check the msh log") clientSocket.Write(mes) errco.NewLogln(errco.TYPE_BYT, errco.LVL_4, errco.ERROR_NIL, "%smsh --> client%s: %v", errco.COLOR_PURPLE, errco.COLOR_RESET, mes) diff --git a/lib/errco/errco.go b/lib/errco/errco.go index 1db24870..63acb207 100644 --- a/lib/errco/errco.go +++ b/lib/errco/errco.go @@ -61,66 +61,78 @@ func NewLogln(t LogTyp, l LogLvl, c LogCod, m string, a ...interface{}) *MshLog // // returns the original log for convenience. // returns nil if msh log struct is nil -func (logO *MshLog) Log() *MshLog { +func (log *MshLog) Log() *MshLog { + // return original log if it's nil + if log == nil { + return log + } + // ------- operations on original log ------- - // return original log if it's nil - if logO == nil { - return logO + // add trace if Log() was not called by NewLogln() + // 1) example() -> Log() -> trace(2) : example + // 2) example() -> NewLogln() -> trace(2) : example + // \> Log() -> trace(2) : NewLogln (!) + // example 2: + // - trace(2) from Log() results in "NewLogln", + // - trace(3) from Log() results in "example" (but it's wrong as NewLogln() already set the correct trace) + pc := trace(2) + if pc != LogOri("NewLogln") { + log.Ori = pc + LogOri(": ") + log.Ori } // return original log if log level is not high enough - if logO.Lvl > DebugLvl { - return logO + if log.Lvl > DebugLvl { + return log } // make a copy of original log - logC := *logO + logMod := *log // -------- operations on copied log -------- // set logC colors depending on logC level - switch logC.Lvl { + switch logMod.Lvl { case LVL_0: // make important logs more visible - logC.Mex = COLOR_CYAN + logC.Mex + COLOR_RESET + logMod.Mex = COLOR_CYAN + logMod.Mex + COLOR_RESET } // set log colors depending on log type var t string - switch logC.Typ { + switch logMod.Typ { case TYPE_INF: - t = COLOR_BLUE + string(logC.Typ) + COLOR_RESET + t = COLOR_BLUE + string(logMod.Typ) + COLOR_RESET case TYPE_SER: - t = COLOR_GRAY + string(logC.Typ) + COLOR_RESET - logC.Mex = COLOR_GRAY + logC.Mex + "\x00" + COLOR_RESET + t = COLOR_GRAY + string(logMod.Typ) + COLOR_RESET + logMod.Mex = COLOR_GRAY + logMod.Mex + "\x00" + COLOR_RESET case TYPE_BYT: - t = COLOR_PURPLE + string(logC.Typ) + COLOR_RESET + t = COLOR_PURPLE + string(logMod.Typ) + COLOR_RESET case TYPE_WAR: - t = COLOR_YELLOW + string(logC.Typ) + COLOR_RESET + t = COLOR_YELLOW + string(logMod.Typ) + COLOR_RESET case TYPE_ERR: - t = COLOR_RED + string(logC.Typ) + COLOR_RESET + t = COLOR_RED + string(logMod.Typ) + COLOR_RESET } - switch logC.Typ { + switch logMod.Typ { case TYPE_INF, TYPE_SER, TYPE_BYT: fmt.Printf("%s [%-16s %-4s] %s\n", time.Now().Format("2006/01/02 15:04:05"), t, - strings.Repeat("≡", 4-int(logC.Lvl)), - fmt.Sprintf(logC.Mex, logC.Arg...)) + strings.Repeat("≡", 4-int(logMod.Lvl)), + fmt.Sprintf(logMod.Mex, logMod.Arg...)) case TYPE_WAR, TYPE_ERR: fmt.Printf("%s [%-16s %-4s] %s %s %s\n", time.Now().Format("2006/01/02 15:04:05"), t, - strings.Repeat("≡", 4-int(logC.Lvl)), - LogOri(COLOR_YELLOW)+logC.Ori+":"+LogOri(COLOR_RESET), - fmt.Sprintf(logC.Mex, logC.Arg...), - fmt.Sprintf("[%08x]", logC.Cod)) + strings.Repeat("≡", 4-int(logMod.Lvl)), + LogOri(COLOR_YELLOW)+logMod.Ori+":"+LogOri(COLOR_RESET), + fmt.Sprintf(logMod.Mex, logMod.Arg...), + fmt.Sprintf("[%08x]", logMod.Cod)) } // return original log - return logO + return log } // AddTrace adds the caller function to the msh log trace @@ -131,12 +143,14 @@ func (log *MshLog) AddTrace() *MshLog { } log.Ori = trace(2) + LogOri(": ") + log.Ori + return log } // trace returns the function name the parent was called from // // skip == 2: example() -> NewLog() -> trace() +// // result: example func trace(skip int) LogOri { var o string = "?" diff --git a/lib/input/input.go b/lib/input/input.go index 3c64f03c..fd23d1ff 100644 --- a/lib/input/input.go +++ b/lib/input/input.go @@ -59,19 +59,19 @@ func GetInput() { case "start": logMsh := servctrl.WarmMS() if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() } case "freeze": // stop minecraft server forcefully logMsh := servctrl.FreezeMS(true) if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() } case "exit": // stop minecraft server forcefully logMsh := servctrl.FreezeMS(true) if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() } // exit msh errco.NewLogln(errco.TYPE_INF, errco.LVL_0, errco.ERROR_NIL, "issuing msh termination") @@ -97,7 +97,7 @@ func GetInput() { // pass the command to the minecraft server terminal _, logMsh := servctrl.Execute(strings.Join(lineSplit[1:], " "), "user input") if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() } // wrong target diff --git a/lib/progmgr/progmgr.go b/lib/progmgr/progmgr.go index c5d95ddc..f02ad5ce 100644 --- a/lib/progmgr/progmgr.go +++ b/lib/progmgr/progmgr.go @@ -53,7 +53,7 @@ func MshMgr() { // stop the minecraft server forcefully logMsh := servctrl.FreezeMS(true) if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() } // send last statistics before exiting diff --git a/lib/progmgr/sgmmgr.go b/lib/progmgr/sgmmgr.go index 1bdaea08..052f384a 100644 --- a/lib/progmgr/sgmmgr.go +++ b/lib/progmgr/sgmmgr.go @@ -92,7 +92,7 @@ func sgmMgr() { if sgm.push.verCheck != "" && servstats.Stats.PlayerCount > 0 { logMsh := servctrl.TellRaw("manager", sgm.push.verCheck, "sgmMgr") if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() } } @@ -100,7 +100,7 @@ func sgmMgr() { for _, m := range sgm.push.messages { logMsh := servctrl.TellRaw("message", m, "sgmMgr") if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() } } } @@ -110,7 +110,7 @@ func sgmMgr() { // send request res, logMsh := sendApi2Req(updAddr, buildApi2Req(false)) if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() sgm.prolong(10 * time.Minute) break mainselect } @@ -132,7 +132,7 @@ func sgmMgr() { // get server response into struct resJson, logMsh := readApi2Res(res) if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() break mainselect } diff --git a/lib/servctrl/servctrl-utils.go b/lib/servctrl/servctrl-utils.go index 28752b05..b3aaf2b0 100644 --- a/lib/servctrl/servctrl-utils.go +++ b/lib/servctrl/servctrl-utils.go @@ -29,9 +29,9 @@ func countPlayerSafe() int { errco.NewLogln(errco.TYPE_INF, errco.LVL_3, errco.ERROR_NIL, "retrieving player count...") - if playerCount, logMsh = getPlayersByServInfo(); logMsh.AddTrace().Log() == nil { + if playerCount, logMsh = getPlayersByServInfo(); logMsh.Log() == nil { method = "list command" - } else if playerCount, logMsh = getPlayersByListCom(); logMsh.AddTrace().Log() == nil { + } else if playerCount, logMsh = getPlayersByListCom(); logMsh.Log() == nil { method = "server info" } else { playerCount = servstats.Stats.PlayerCount diff --git a/lib/servctrl/servctrl.go b/lib/servctrl/servctrl.go index a08ad8af..10b0a329 100644 --- a/lib/servctrl/servctrl.go +++ b/lib/servctrl/servctrl.go @@ -172,7 +172,7 @@ func FreezeMSSchedule() { errco.NewLogln(errco.TYPE_INF, errco.LVL_1, errco.ERROR_NIL, "performing scheduled ms soft freeze") logMsh := FreezeMS(false) if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() } }, ) @@ -223,7 +223,7 @@ func killMSifOnlineAfterTimeout() { if config.ConfigRuntime.Msh.AllowSuspend { servstats.Stats.Suspended, logMsh = opsys.ProcTreeResume(uint32(ServTerm.cmd.Process.Pid)) if logMsh != nil { - logMsh.AddTrace().Log() + logMsh.Log() } }