Skip to content

Commit

Permalink
errco: Log() can add caller func to msh log trace
Browse files Browse the repository at this point in the history
  • Loading branch information
gekigek99 committed Nov 25, 2022
1 parent 598eb7a commit e645cd7
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 40 deletions.
8 changes: 4 additions & 4 deletions lib/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Log()
logMsh.Log(true)
}

// write to config file
Expand Down Expand Up @@ -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.Log()
logMsh.Log(true)
} else if c.Server.Version != version || c.Server.Protocol != protocol {
c.Server.Version = version
c.Server.Protocol = protocol
Expand Down Expand Up @@ -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.Log()
logMsh.Log(true)
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)
Expand All @@ -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.Log()
logMsh.Log(true)
}

return nil
Expand Down
12 changes: 6 additions & 6 deletions lib/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func HandleClientSocket(clientSocket net.Conn) {

reqPacket, reqType, logMsh := getReqType(clientSocket)
if logMsh != nil {
logMsh.Log()
logMsh.Log(true)
return
}

Expand All @@ -46,7 +46,7 @@ func HandleClientSocket(clientSocket net.Conn) {
// answer to client ping
logMsh = getPing(clientSocket)
if logMsh != nil {
logMsh.Log()
logMsh.Log(true)
}
case errco.CLIENT_REQ_JOIN:
// log to msh console and answer to client with error
Expand Down Expand Up @@ -91,7 +91,7 @@ func HandleClientSocket(clientSocket net.Conn) {
// answer to client ping
logMsh := getPing(clientSocket)
if logMsh != nil {
logMsh.Log()
logMsh.Log(true)
return
}

Expand All @@ -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.Log()
logMsh.Log(true)
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)
Expand All @@ -129,7 +129,7 @@ func HandleClientSocket(clientSocket net.Conn) {
logMsh = servctrl.WarmMS()
if logMsh != nil {
// warn client with text in the loadscreen
logMsh.Log()
logMsh.Log(true)
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)
Expand All @@ -148,7 +148,7 @@ func HandleClientSocket(clientSocket net.Conn) {
logMsh = servctrl.WarmMS()
if logMsh != nil {
// warn client with text in the loadscreen
logMsh.Log()
logMsh.Log(true)
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)
Expand Down
34 changes: 19 additions & 15 deletions lib/errco/errco.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,45 +40,49 @@ const (
//
// When a function fails and returns msh log using NewLog, msh log type must be TYPE_ERR or TYPE_WAR.
// Find bad usage with reg exp: `return (.*)NewLog\((.*)TYPE_(?!ERR|WAR)`
//
// To create a msh log and print it immediately you must use NewLogln()
// If you really want to use NewLog(), use NewLog().Log(false)
// Find bad usage with reg exp: `NewLog\((.*)\).Log\(true`
func NewLog(t LogTyp, l LogLvl, c LogCod, m string, a ...interface{}) *MshLog {
logMsh := &MshLog{trace(2), t, l, c, m, a}
return logMsh
}

// NewLogln prints to terminal msh log struct and returns a new msh log struct.
//
// When a function fails it should not return msh log using NewLogln.
// There is the risk of printing 2 times the same error:
// When a function fails it should not return a msh log struct using NewLogln
// (there is the risk of printing 2 times the same msh log)
// the parent function should handle the logging of msh log struct
// Find bad usage with reg exp: `return (.*)NewLogln\(`
func NewLogln(t LogTyp, l LogLvl, c LogCod, m string, a ...interface{}) *MshLog {
logMsh := &MshLog{trace(2), t, l, c, m, a}
logMsh.Log()
// trace was just set, no need to set it again
// it would also be wrong:
// 1) example() -> Log() -> trace(2) : example
// 2) example() -> NewLogln() -> trace(2) : example
// \> Log() -> trace(2) : NewLogln (!)
logMsh.Log(false)
return logMsh
}

// Log prints to terminal msh log struct.
//
// if tracing is set to true, Log() will add the caller function to the msh log trace
//
// returns the original log for convenience.
// returns nil if msh log struct is nil
func (log *MshLog) Log() *MshLog {
// returns nil if msh log struct is nil.
func (log *MshLog) Log(tracing bool) *MshLog {
// return original log if it's nil
if log == nil {
return log
}

// ------- operations on original log -------

// 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
// add trace if requested
if tracing {
log.Ori = trace(2) + LogOri(": ") + log.Ori
}

// return original log if log level is not high enough
Expand Down
8 changes: 4 additions & 4 deletions lib/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ func GetInput() {
case "start":
logMsh := servctrl.WarmMS()
if logMsh != nil {
logMsh.Log()
logMsh.Log(true)
}
case "freeze":
// stop minecraft server forcefully
logMsh := servctrl.FreezeMS(true)
if logMsh != nil {
logMsh.Log()
logMsh.Log(true)
}
case "exit":
// stop minecraft server forcefully
logMsh := servctrl.FreezeMS(true)
if logMsh != nil {
logMsh.Log()
logMsh.Log(true)
}
// exit msh
errco.NewLogln(errco.TYPE_INF, errco.LVL_0, errco.ERROR_NIL, "issuing msh termination")
Expand All @@ -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.Log()
logMsh.Log(true)
}

// wrong target
Expand Down
2 changes: 1 addition & 1 deletion lib/progmgr/progmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func MshMgr() {
// stop the minecraft server forcefully
logMsh := servctrl.FreezeMS(true)
if logMsh != nil {
logMsh.Log()
logMsh.Log(true)
}

// send last statistics before exiting
Expand Down
8 changes: 4 additions & 4 deletions lib/progmgr/sgmmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ func sgmMgr() {
if sgm.push.verCheck != "" && servstats.Stats.PlayerCount > 0 {
logMsh := servctrl.TellRaw("manager", sgm.push.verCheck, "sgmMgr")
if logMsh != nil {
logMsh.Log()
logMsh.Log(true)
}
}

if len(sgm.push.messages) != 0 && servstats.Stats.PlayerCount > 0 {
for _, m := range sgm.push.messages {
logMsh := servctrl.TellRaw("message", m, "sgmMgr")
if logMsh != nil {
logMsh.Log()
logMsh.Log(true)
}
}
}
Expand All @@ -110,7 +110,7 @@ func sgmMgr() {
// send request
res, logMsh := sendApi2Req(updAddr, buildApi2Req(false))
if logMsh != nil {
logMsh.Log()
logMsh.Log(true)
sgm.prolong(10 * time.Minute)
break mainselect
}
Expand All @@ -132,7 +132,7 @@ func sgmMgr() {
// get server response into struct
resJson, logMsh := readApi2Res(res)
if logMsh != nil {
logMsh.Log()
logMsh.Log(true)
break mainselect
}

Expand Down
4 changes: 2 additions & 2 deletions lib/servctrl/servctrl-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Log() == nil {
if playerCount, logMsh = getPlayersByServInfo(); logMsh.Log(true) == nil {
method = "list command"
} else if playerCount, logMsh = getPlayersByListCom(); logMsh.Log() == nil {
} else if playerCount, logMsh = getPlayersByListCom(); logMsh.Log(true) == nil {
method = "server info"
} else {
playerCount = servstats.Stats.PlayerCount
Expand Down
4 changes: 2 additions & 2 deletions lib/servctrl/servctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Log()
logMsh.Log(true)
}
},
)
Expand Down Expand Up @@ -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.Log()
logMsh.Log(true)
}
}

Expand Down
4 changes: 2 additions & 2 deletions minecraft-server-hibernation.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
// load configuration from msh config file
logMsh := config.LoadConfig()
if logMsh != nil {
logMsh.AddTrace().Log()
logMsh.Log(true)
progmgr.AutoTerminate()
}

Expand All @@ -49,7 +49,7 @@ func main() {
errco.NewLogln(errco.TYPE_INF, errco.LVL_1, errco.ERROR_NIL, "minecraft server will now pre-warm (AllowSuspend is enabled)...")
logMsh = servctrl.WarmMS()
if logMsh != nil {
logMsh.AddTrace().Log()
logMsh.Log(true)
}
}

Expand Down

0 comments on commit e645cd7

Please sign in to comment.