Skip to content

Commit

Permalink
Add some logging
Browse files Browse the repository at this point in the history
Functions added:
- LogErrorf()
- LogErrorln()
- LogChatf()
- LogChatln()
- LogInfof()
- LogInfoln()
- LogDebugf()
- LogDebugln()
- LogDevf()
- LogDevln()

New settings configure the logging: LogLevel and LogFile.  LogLevel can
be set to one of: error, chat, info, or debug and will default to
error.  LogFile is an optional file to write to.  Providing a file will
not prevent output in the console.

LogDevf() and LogDevln() only compile when the "dev" flag passed to
build.  This will cause Travis-CI to fail the build if it finds any
calls to either function.
  • Loading branch information
zorchenhimer committed Mar 24, 2019
1 parent cd34480 commit fdbf39f
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 95 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -12,6 +12,9 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Log files
*.log

# GoCode debug file
debug

Expand Down
19 changes: 13 additions & 6 deletions Makefile
@@ -1,15 +1,22 @@
.PHONY: fmt vet get clean
TAGS=

.PHONY: fmt vet get clean dev setdev

all: fmt vet MovieNight MovieNight.exe static/main.wasm

setdev:
$(eval export TAGS=-tags "dev")

dev: setdev all

MovieNight.exe: *.go common/*.go
GOOS=windows GOARCH=amd64 go build -o MovieNight.exe
GOOS=windows GOARCH=amd64 go build -o MovieNight.exe $(TAGS)

MovieNight: *.go common/*.go
GOOS=linux GOARCH=386 go build -o MovieNight
GOOS=linux GOARCH=386 go build -o MovieNight $(TAGS)

static/main.wasm: wasm/*.go common/*.go
GOOS=js GOARCH=wasm go build -o ./static/main.wasm wasm/*.go
GOOS=js GOARCH=wasm go build -o ./static/main.wasm $(TAGS) wasm/*.go

clean:
-rm MovieNight.exe MovieNight ./static/main.wasm
Expand All @@ -23,5 +30,5 @@ get:
go get golang.org/x/tools/cmd/goimports

vet:
go vet ./...
GOOS=js GOARCH=wasm go vet ./...
go vet $(TAGS) ./...
GOOS=js GOARCH=wasm go vet $(TAGS) ./...
12 changes: 6 additions & 6 deletions chatclient.go
Expand Up @@ -24,13 +24,13 @@ type Client struct {
func (cl *Client) NewMsg(data common.ClientData) {
switch data.Type {
case common.CdAuth:
fmt.Printf("[chat|hidden] <%s> get auth level\n", cl.name)
LogChatf("[chat|hidden] <%s> get auth level\n", cl.name)
err := cl.SendChatData(common.NewChatHiddenMessage(data.Type, cl.CmdLevel))
if err != nil {
fmt.Printf("Error sending auth level to client: %v\n", err)
LogErrorf("Error sending auth level to client: %v\n", err)
}
case common.CdUsers:
fmt.Printf("[chat|hidden] <%s> get list of users\n", cl.name)
common.LogChatf("[chat|hidden] <%s> get list of users\n", cl.name)

names := chat.GetNames()
idx := -1
Expand All @@ -42,7 +42,7 @@ func (cl *Client) NewMsg(data common.ClientData) {

err := cl.SendChatData(common.NewChatHiddenMessage(data.Type, append(names[:idx], names[idx+1:]...)))
if err != nil {
fmt.Printf("Error sending users to client: %v\n", err)
common.LogErrorf("Error sending chat data: %v\n", err)
}
case common.CdMessage:
msg := html.EscapeString(data.Message)
Expand All @@ -68,7 +68,7 @@ func (cl *Client) NewMsg(data common.ClientData) {
common.CmdlUser,
common.MsgCommandResponse))
if err != nil {
fmt.Printf("Error command results %v\n", err)
common.LogErrorf("Error command results %v\n", err)
}
return
}
Expand All @@ -79,7 +79,7 @@ func (cl *Client) NewMsg(data common.ClientData) {
msg = msg[0:400]
}

fmt.Printf("[chat] <%s> %q\n", cl.name, msg)
common.LogChatf("[chat] <%s> %q\n", cl.name, msg)

// Enable links for mods and admins
if cl.CmdLevel >= common.CmdlMod {
Expand Down
58 changes: 29 additions & 29 deletions chatcommands.go
Expand Up @@ -61,19 +61,19 @@ var commands = &CommandControl{
if settings.AdminPassword == pw {
cl.CmdLevel = common.CmdlAdmin
cl.belongsTo.AddModNotice(cl.name + " used the admin password")
fmt.Printf("[auth] %s used the admin password\n", cl.name)
common.LogInfof("[auth] %s used the admin password\n", cl.name)
return "Admin rights granted."
}

if cl.belongsTo.redeemModPass(pw) {
cl.CmdLevel = common.CmdlMod
cl.belongsTo.AddModNotice(cl.name + " used a mod password")
fmt.Printf("[auth] %s used a mod password\n", cl.name)
common.LogInfof("[auth] %s used a mod password\n", cl.name)
return "Moderator privileges granted."
}

cl.belongsTo.AddModNotice(cl.name + " attempted to auth without success")
fmt.Printf("[auth] %s gave an invalid password\n", cl.name)
common.LogInfof("[auth] %s gave an invalid password\n", cl.name)
return "Invalid password."
},
},
Expand Down Expand Up @@ -217,7 +217,7 @@ var commands = &CommandControl{
}

name := strings.TrimLeft(args[0], "@")
fmt.Printf("[ban] Attempting to ban %s\n", name)
common.LogInfof("[ban] Attempting to ban %s\n", name)
return cl.belongsTo.Ban(name)
},
},
Expand All @@ -229,7 +229,7 @@ var commands = &CommandControl{
return "missing name to unban."
}
name := strings.TrimLeft(args[0], "@")
fmt.Printf("[ban] Attempting to unban %s\n", name)
common.LogInfof("[ban] Attempting to unban %s\n", name)

err := settings.RemoveBan(name)
if err != nil {
Expand All @@ -243,7 +243,7 @@ var commands = &CommandControl{
common.CNPurge.String(): Command{
HelpText: "Purge the chat.",
Function: func(cl *Client, args []string) string {
fmt.Println("[purge] clearing chat")
common.LogInfoln("[purge] clearing chat")
cl.belongsTo.AddCmdMsg(common.CmdPurgeChat, nil)
return ""
},
Expand Down Expand Up @@ -282,12 +282,12 @@ var commands = &CommandControl{
cl.SendServerMessage("Reloading emotes")
num, err := common.LoadEmotes()
if err != nil {
fmt.Printf("Unbale to reload emotes: %s\n", err)
common.LogErrorf("Unbale to reload emotes: %s\n", err)
return fmt.Sprintf("ERROR: %s", err)
}

cl.belongsTo.AddModNotice(cl.name + " has reloaded emotes")
fmt.Printf("Loaded %d emotes\n", num)
common.LogInfof("Loaded %d emotes\n", num)
return fmt.Sprintf("Emotes loaded: %d", num)
},
},
Expand All @@ -302,17 +302,17 @@ var commands = &CommandControl{
},

common.CNIP.String(): Command{
HelpText: "list users and IP in the server console",
HelpText: "List users and IP in the server console. Requires logging level to be set to info or above.",
Function: func(cl *Client, args []string) string {
cl.belongsTo.clientsMtx.Lock()
fmt.Println("Clients:")
common.LogInfoln("Clients:")
for uuid, client := range cl.belongsTo.clients {
fmt.Printf(" [%s] %s %s\n", uuid, client.name, client.conn.Host())
common.LogInfof(" [%s] %s %s\n", uuid, client.name, client.conn.Host())
}

fmt.Println("TmpConn:")
common.LogInfoln("TmpConn:")
for uuid, conn := range cl.belongsTo.tempConn {
fmt.Printf(" [%s] %s\n", uuid, conn.Host())
common.LogInfof(" [%s] %s\n", uuid, conn.Host())
}
cl.belongsTo.clientsMtx.Unlock()
return "see console for output"
Expand All @@ -327,33 +327,33 @@ func (cc *CommandControl) RunCommand(command string, args []string, sender *Clie

// Look for user command
if userCmd, ok := cc.user[cmd]; ok {
fmt.Printf("[user] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
common.LogInfof("[user] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return userCmd.Function(sender, args)
}

// Look for mod command
if modCmd, ok := cc.mod[cmd]; ok {
if sender.CmdLevel >= common.CmdlMod {
fmt.Printf("[mod] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
common.LogInfof("[mod] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return modCmd.Function(sender, args)
}

fmt.Printf("[mod REJECTED] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
common.LogInfof("[mod REJECTED] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return "You are not a mod Jebaited"
}

// Look for admin command
if adminCmd, ok := cc.admin[cmd]; ok {
if sender.CmdLevel == common.CmdlAdmin {
fmt.Printf("[admin] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
common.LogInfof("[admin] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return adminCmd.Function(sender, args)
}
fmt.Printf("[admin REJECTED] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
common.LogInfof("[admin REJECTED] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return "You are not the admin Jebaited"
}

// Command not found
fmt.Printf("[cmd] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
common.LogInfof("[cmd] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return "Invalid command."
}

Expand Down Expand Up @@ -416,31 +416,31 @@ var cmdColor = Command{
if strings.HasPrefix(args[0], "@") {
name = strings.TrimLeft(args[0], "@")
color = args[1]
fmt.Println("Found explicit name: ", name)
common.LogDebugln("[color:mod] Found explicit name: ", name)
} else if strings.HasPrefix(args[1], "@") {
name = strings.TrimLeft(args[1], "@")
color = args[0]
fmt.Println("Found explicit name: ", name)
common.LogDebugln("[color:mod] Found explicit name: ", name)

// Check for explicit color
} else if strings.HasPrefix(args[0], "#") {
name = strings.TrimPrefix(args[1], "@") // this shouldn't be needed, but just in case.
color = args[0]
fmt.Println("Found explicit color: ", color)
common.LogDebugln("[color:mod] Found explicit color: ", color)
} else if strings.HasPrefix(args[1], "#") {
name = strings.TrimPrefix(args[0], "@") // this shouldn't be needed, but just in case.
color = args[1]
fmt.Println("Found explicit color: ", color)
common.LogDebugln("[color:mod] Found explicit color: ", color)

// Guess
} else if common.IsValidColor(args[0]) {
name = strings.TrimPrefix(args[1], "@")
color = args[0]
fmt.Println("Guessed name: ", name, " and color: ", color)
common.LogDebugln("[color:mod] Guessed name: ", name, " and color: ", color)
} else if common.IsValidColor(args[1]) {
name = strings.TrimPrefix(args[0], "@")
color = args[1]
fmt.Println("Guessed name: ", name, " and color: ", color)
common.LogDebugln("[color:mod] Guessed name: ", name, " and color: ", color)
}

if name == "" {
Expand All @@ -451,12 +451,12 @@ var cmdColor = Command{
}

if color == "" {
fmt.Printf("[color:mod] %s missing color\n", cl.name)
common.LogInfof("[color:mod] %s missing color\n", cl.name)
return "Missing color"
}

if name == "" {
fmt.Printf("[color:mod] %s missing name\n", cl.name)
common.LogInfof("[color:mod] %s missing name\n", cl.name)
return "Missing name"
}

Expand All @@ -469,7 +469,7 @@ var cmdColor = Command{
// Don't allow an unprivilaged user to change their color if
// it was changed by a mod
if cl.IsColorForced {
fmt.Printf("[color] %s tried to change a forced color\n", cl.name)
common.LogInfof("[color] %s tried to change a forced color\n", cl.name)
return "You are not allowed to change your color."
}

Expand All @@ -484,7 +484,7 @@ var cmdColor = Command{
}

cl.color = args[0]
fmt.Printf("[color] %s new color: %s\n", cl.name, cl.color)
common.LogInfof("[color] %s new color: %s\n", cl.name, cl.color)
return "Color changed successfully."
},
}
Expand Down

0 comments on commit fdbf39f

Please sign in to comment.