Skip to content

Commit

Permalink
errco: Log() can add trace to msh log
Browse files Browse the repository at this point in the history
  • Loading branch information
gekigek99 committed Nov 25, 2022
1 parent 2e4622f commit 598eb7a
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 47 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.AddTrace().Log()
logMsh.Log()
}

// 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.AddTrace().Log()
logMsh.Log()
} 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.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)
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.AddTrace().Log()
logMsh.Log()
}

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.AddTrace().Log()
logMsh.Log()
return
}

Expand All @@ -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
Expand Down Expand Up @@ -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
}

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.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)
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.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)
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.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)
Expand Down
62 changes: 38 additions & 24 deletions lib/errco/errco.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = "?"
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.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")
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.AddTrace().Log()
logMsh.Log()
}

// 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.AddTrace().Log()
logMsh.Log()
}

// 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.AddTrace().Log()
logMsh.Log()
}
}

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.AddTrace().Log()
logMsh.Log()
}
}
}
Expand All @@ -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
}
Expand All @@ -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
}

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.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
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.AddTrace().Log()
logMsh.Log()
}
},
)
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.AddTrace().Log()
logMsh.Log()
}
}

Expand Down

0 comments on commit 598eb7a

Please sign in to comment.