Add some logging
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.
This commit is contained in:
parent
cd34480bba
commit
fdbf39f00c
|
@ -12,6 +12,9 @@
|
||||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
*.out
|
*.out
|
||||||
|
|
||||||
|
# Log files
|
||||||
|
*.log
|
||||||
|
|
||||||
# GoCode debug file
|
# GoCode debug file
|
||||||
debug
|
debug
|
||||||
|
|
||||||
|
|
19
Makefile
19
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
|
all: fmt vet MovieNight MovieNight.exe static/main.wasm
|
||||||
|
|
||||||
|
setdev:
|
||||||
|
$(eval export TAGS=-tags "dev")
|
||||||
|
|
||||||
|
dev: setdev all
|
||||||
|
|
||||||
MovieNight.exe: *.go common/*.go
|
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
|
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
|
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:
|
clean:
|
||||||
-rm MovieNight.exe MovieNight ./static/main.wasm
|
-rm MovieNight.exe MovieNight ./static/main.wasm
|
||||||
|
@ -23,5 +30,5 @@ get:
|
||||||
go get golang.org/x/tools/cmd/goimports
|
go get golang.org/x/tools/cmd/goimports
|
||||||
|
|
||||||
vet:
|
vet:
|
||||||
go vet ./...
|
go vet $(TAGS) ./...
|
||||||
GOOS=js GOARCH=wasm go vet ./...
|
GOOS=js GOARCH=wasm go vet $(TAGS) ./...
|
||||||
|
|
|
@ -24,13 +24,13 @@ type Client struct {
|
||||||
func (cl *Client) NewMsg(data common.ClientData) {
|
func (cl *Client) NewMsg(data common.ClientData) {
|
||||||
switch data.Type {
|
switch data.Type {
|
||||||
case common.CdAuth:
|
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))
|
err := cl.SendChatData(common.NewChatHiddenMessage(data.Type, cl.CmdLevel))
|
||||||
if err != nil {
|
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:
|
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()
|
names := chat.GetNames()
|
||||||
idx := -1
|
idx := -1
|
||||||
|
@ -42,7 +42,7 @@ func (cl *Client) NewMsg(data common.ClientData) {
|
||||||
|
|
||||||
err := cl.SendChatData(common.NewChatHiddenMessage(data.Type, append(names[:idx], names[idx+1:]...)))
|
err := cl.SendChatData(common.NewChatHiddenMessage(data.Type, append(names[:idx], names[idx+1:]...)))
|
||||||
if err != nil {
|
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:
|
case common.CdMessage:
|
||||||
msg := html.EscapeString(data.Message)
|
msg := html.EscapeString(data.Message)
|
||||||
|
@ -68,7 +68,7 @@ func (cl *Client) NewMsg(data common.ClientData) {
|
||||||
common.CmdlUser,
|
common.CmdlUser,
|
||||||
common.MsgCommandResponse))
|
common.MsgCommandResponse))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error command results %v\n", err)
|
common.LogErrorf("Error command results %v\n", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ func (cl *Client) NewMsg(data common.ClientData) {
|
||||||
msg = msg[0:400]
|
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
|
// Enable links for mods and admins
|
||||||
if cl.CmdLevel >= common.CmdlMod {
|
if cl.CmdLevel >= common.CmdlMod {
|
||||||
|
|
|
@ -61,19 +61,19 @@ var commands = &CommandControl{
|
||||||
if settings.AdminPassword == pw {
|
if settings.AdminPassword == pw {
|
||||||
cl.CmdLevel = common.CmdlAdmin
|
cl.CmdLevel = common.CmdlAdmin
|
||||||
cl.belongsTo.AddModNotice(cl.name + " used the admin password")
|
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."
|
return "Admin rights granted."
|
||||||
}
|
}
|
||||||
|
|
||||||
if cl.belongsTo.redeemModPass(pw) {
|
if cl.belongsTo.redeemModPass(pw) {
|
||||||
cl.CmdLevel = common.CmdlMod
|
cl.CmdLevel = common.CmdlMod
|
||||||
cl.belongsTo.AddModNotice(cl.name + " used a mod password")
|
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."
|
return "Moderator privileges granted."
|
||||||
}
|
}
|
||||||
|
|
||||||
cl.belongsTo.AddModNotice(cl.name + " attempted to auth without success")
|
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."
|
return "Invalid password."
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -217,7 +217,7 @@ var commands = &CommandControl{
|
||||||
}
|
}
|
||||||
|
|
||||||
name := strings.TrimLeft(args[0], "@")
|
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)
|
return cl.belongsTo.Ban(name)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -229,7 +229,7 @@ var commands = &CommandControl{
|
||||||
return "missing name to unban."
|
return "missing name to unban."
|
||||||
}
|
}
|
||||||
name := strings.TrimLeft(args[0], "@")
|
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)
|
err := settings.RemoveBan(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -243,7 +243,7 @@ var commands = &CommandControl{
|
||||||
common.CNPurge.String(): Command{
|
common.CNPurge.String(): Command{
|
||||||
HelpText: "Purge the chat.",
|
HelpText: "Purge the chat.",
|
||||||
Function: func(cl *Client, args []string) string {
|
Function: func(cl *Client, args []string) string {
|
||||||
fmt.Println("[purge] clearing chat")
|
common.LogInfoln("[purge] clearing chat")
|
||||||
cl.belongsTo.AddCmdMsg(common.CmdPurgeChat, nil)
|
cl.belongsTo.AddCmdMsg(common.CmdPurgeChat, nil)
|
||||||
return ""
|
return ""
|
||||||
},
|
},
|
||||||
|
@ -282,12 +282,12 @@ var commands = &CommandControl{
|
||||||
cl.SendServerMessage("Reloading emotes")
|
cl.SendServerMessage("Reloading emotes")
|
||||||
num, err := common.LoadEmotes()
|
num, err := common.LoadEmotes()
|
||||||
if err != nil {
|
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)
|
return fmt.Sprintf("ERROR: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cl.belongsTo.AddModNotice(cl.name + " has reloaded emotes")
|
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)
|
return fmt.Sprintf("Emotes loaded: %d", num)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -302,17 +302,17 @@ var commands = &CommandControl{
|
||||||
},
|
},
|
||||||
|
|
||||||
common.CNIP.String(): Command{
|
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 {
|
Function: func(cl *Client, args []string) string {
|
||||||
cl.belongsTo.clientsMtx.Lock()
|
cl.belongsTo.clientsMtx.Lock()
|
||||||
fmt.Println("Clients:")
|
common.LogInfoln("Clients:")
|
||||||
for uuid, client := range cl.belongsTo.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 {
|
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()
|
cl.belongsTo.clientsMtx.Unlock()
|
||||||
return "see console for output"
|
return "see console for output"
|
||||||
|
@ -327,33 +327,33 @@ func (cc *CommandControl) RunCommand(command string, args []string, sender *Clie
|
||||||
|
|
||||||
// Look for user command
|
// Look for user command
|
||||||
if userCmd, ok := cc.user[cmd]; ok {
|
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)
|
return userCmd.Function(sender, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Look for mod command
|
// Look for mod command
|
||||||
if modCmd, ok := cc.mod[cmd]; ok {
|
if modCmd, ok := cc.mod[cmd]; ok {
|
||||||
if sender.CmdLevel >= common.CmdlMod {
|
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)
|
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"
|
return "You are not a mod Jebaited"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Look for admin command
|
// Look for admin command
|
||||||
if adminCmd, ok := cc.admin[cmd]; ok {
|
if adminCmd, ok := cc.admin[cmd]; ok {
|
||||||
if sender.CmdLevel == common.CmdlAdmin {
|
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)
|
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"
|
return "You are not the admin Jebaited"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command not found
|
// 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."
|
return "Invalid command."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -416,31 +416,31 @@ var cmdColor = Command{
|
||||||
if strings.HasPrefix(args[0], "@") {
|
if strings.HasPrefix(args[0], "@") {
|
||||||
name = strings.TrimLeft(args[0], "@")
|
name = strings.TrimLeft(args[0], "@")
|
||||||
color = args[1]
|
color = args[1]
|
||||||
fmt.Println("Found explicit name: ", name)
|
common.LogDebugln("[color:mod] Found explicit name: ", name)
|
||||||
} else if strings.HasPrefix(args[1], "@") {
|
} else if strings.HasPrefix(args[1], "@") {
|
||||||
name = strings.TrimLeft(args[1], "@")
|
name = strings.TrimLeft(args[1], "@")
|
||||||
color = args[0]
|
color = args[0]
|
||||||
fmt.Println("Found explicit name: ", name)
|
common.LogDebugln("[color:mod] Found explicit name: ", name)
|
||||||
|
|
||||||
// Check for explicit color
|
// Check for explicit color
|
||||||
} else if strings.HasPrefix(args[0], "#") {
|
} else if strings.HasPrefix(args[0], "#") {
|
||||||
name = strings.TrimPrefix(args[1], "@") // this shouldn't be needed, but just in case.
|
name = strings.TrimPrefix(args[1], "@") // this shouldn't be needed, but just in case.
|
||||||
color = args[0]
|
color = args[0]
|
||||||
fmt.Println("Found explicit color: ", color)
|
common.LogDebugln("[color:mod] Found explicit color: ", color)
|
||||||
} else if strings.HasPrefix(args[1], "#") {
|
} else if strings.HasPrefix(args[1], "#") {
|
||||||
name = strings.TrimPrefix(args[0], "@") // this shouldn't be needed, but just in case.
|
name = strings.TrimPrefix(args[0], "@") // this shouldn't be needed, but just in case.
|
||||||
color = args[1]
|
color = args[1]
|
||||||
fmt.Println("Found explicit color: ", color)
|
common.LogDebugln("[color:mod] Found explicit color: ", color)
|
||||||
|
|
||||||
// Guess
|
// Guess
|
||||||
} else if common.IsValidColor(args[0]) {
|
} else if common.IsValidColor(args[0]) {
|
||||||
name = strings.TrimPrefix(args[1], "@")
|
name = strings.TrimPrefix(args[1], "@")
|
||||||
color = args[0]
|
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]) {
|
} else if common.IsValidColor(args[1]) {
|
||||||
name = strings.TrimPrefix(args[0], "@")
|
name = strings.TrimPrefix(args[0], "@")
|
||||||
color = args[1]
|
color = args[1]
|
||||||
fmt.Println("Guessed name: ", name, " and color: ", color)
|
common.LogDebugln("[color:mod] Guessed name: ", name, " and color: ", color)
|
||||||
}
|
}
|
||||||
|
|
||||||
if name == "" {
|
if name == "" {
|
||||||
|
@ -451,12 +451,12 @@ var cmdColor = Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
if color == "" {
|
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"
|
return "Missing color"
|
||||||
}
|
}
|
||||||
|
|
||||||
if name == "" {
|
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"
|
return "Missing name"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -469,7 +469,7 @@ var cmdColor = Command{
|
||||||
// Don't allow an unprivilaged user to change their color if
|
// Don't allow an unprivilaged user to change their color if
|
||||||
// it was changed by a mod
|
// it was changed by a mod
|
||||||
if cl.IsColorForced {
|
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."
|
return "You are not allowed to change your color."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -484,7 +484,7 @@ var cmdColor = Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
cl.color = args[0]
|
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."
|
return "Color changed successfully."
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
32
chatroom.go
32
chatroom.go
|
@ -45,7 +45,7 @@ func newChatRoom() (*ChatRoom, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error loading emotes: %s", err)
|
return nil, fmt.Errorf("error loading emotes: %s", err)
|
||||||
}
|
}
|
||||||
fmt.Printf("Loaded %d emotes\n", num)
|
common.LogInfof("Loaded %d emotes\n", num)
|
||||||
|
|
||||||
//the "heartbeat" for broadcasting messages
|
//the "heartbeat" for broadcasting messages
|
||||||
go cr.Broadcast()
|
go cr.Broadcast()
|
||||||
|
@ -113,10 +113,10 @@ func (cr *ChatRoom) Join(name, uid string) (*Client, error) {
|
||||||
cr.clients[uid] = client
|
cr.clients[uid] = client
|
||||||
delete(cr.tempConn, uid)
|
delete(cr.tempConn, uid)
|
||||||
|
|
||||||
fmt.Printf("[join] %s %s\n", host, name)
|
common.LogChatf("[join] %s %s\n", host, name)
|
||||||
playingCommand, err := common.NewChatCommand(common.CmdPlaying, []string{cr.playing, cr.playingLink}).ToJSON()
|
playingCommand, err := common.NewChatCommand(common.CmdPlaying, []string{cr.playing, cr.playingLink}).ToJSON()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Unable to encode playing command on join: %s\n", err)
|
common.LogErrorf("Unable to encode playing command on join: %s\n", err)
|
||||||
} else {
|
} else {
|
||||||
client.Send(playingCommand)
|
client.Send(playingCommand)
|
||||||
}
|
}
|
||||||
|
@ -132,7 +132,7 @@ func (cr *ChatRoom) Leave(name, color string) {
|
||||||
|
|
||||||
client, suid, err := cr.getClient(name)
|
client, suid, err := cr.getClient(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("[leave] Unable to get client suid %v\n", err)
|
common.LogErrorf("[leave] Unable to get client suid %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
host := client.Host()
|
host := client.Host()
|
||||||
|
@ -141,7 +141,7 @@ func (cr *ChatRoom) Leave(name, color string) {
|
||||||
cr.delClient(suid)
|
cr.delClient(suid)
|
||||||
|
|
||||||
cr.AddEventMsg(common.EvLeave, name, color)
|
cr.AddEventMsg(common.EvLeave, name, color)
|
||||||
fmt.Printf("[leave] %s %s\n", host, name)
|
common.LogChatf("[leave] %s %s\n", host, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// kicked from the chatroom
|
// kicked from the chatroom
|
||||||
|
@ -168,7 +168,7 @@ func (cr *ChatRoom) Kick(name string) string {
|
||||||
cr.delClient(suid)
|
cr.delClient(suid)
|
||||||
|
|
||||||
cr.AddEventMsg(common.EvKick, name, color)
|
cr.AddEventMsg(common.EvKick, name, color)
|
||||||
fmt.Printf("[kick] %s %s has been kicked\n", host, name)
|
common.LogInfof("[kick] %s %s has been kicked\n", host, name)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -178,7 +178,7 @@ func (cr *ChatRoom) Ban(name string) string {
|
||||||
|
|
||||||
client, suid, err := cr.getClient(name)
|
client, suid, err := cr.getClient(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("[ban] Unable to get client for name %q\n", name)
|
common.LogErrorf("[ban] Unable to get client for name %q\n", name)
|
||||||
return "Cannot find that name"
|
return "Cannot find that name"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,7 +206,7 @@ func (cr *ChatRoom) Ban(name string) string {
|
||||||
|
|
||||||
err = settings.AddBan(host, names)
|
err = settings.AddBan(host, names)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("[BAN] Error banning %q: %s\n", name, err)
|
common.LogErrorf("[BAN] Error banning %q: %s\n", name, err)
|
||||||
cr.AddEventMsg(common.EvKick, name, color)
|
cr.AddEventMsg(common.EvKick, name, color)
|
||||||
} else {
|
} else {
|
||||||
cr.AddEventMsg(common.EvBan, name, color)
|
cr.AddEventMsg(common.EvBan, name, color)
|
||||||
|
@ -229,7 +229,7 @@ func (cr *ChatRoom) AddMsg(from *Client, isAction, isServer bool, msg string) {
|
||||||
select {
|
select {
|
||||||
case cr.queue <- common.NewChatMessage(from.name, from.color, msg, from.CmdLevel, t):
|
case cr.queue <- common.NewChatMessage(from.name, from.color, msg, from.CmdLevel, t):
|
||||||
default:
|
default:
|
||||||
fmt.Println("Unable to queue chat message. Channel full.")
|
common.LogErrorln("Unable to queue chat message. Channel full.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -237,7 +237,7 @@ func (cr *ChatRoom) AddCmdMsg(command common.CommandType, args []string) {
|
||||||
select {
|
select {
|
||||||
case cr.queue <- common.NewChatCommand(command, args):
|
case cr.queue <- common.NewChatCommand(command, args):
|
||||||
default:
|
default:
|
||||||
fmt.Println("Unable to queue command message. Channel full.")
|
common.LogErrorln("Unable to queue command message. Channel full.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,7 +245,7 @@ func (cr *ChatRoom) AddModNotice(message string) {
|
||||||
select {
|
select {
|
||||||
case cr.modqueue <- common.NewChatMessage("", "", message, common.CmdlUser, common.MsgNotice):
|
case cr.modqueue <- common.NewChatMessage("", "", message, common.CmdlUser, common.MsgNotice):
|
||||||
default:
|
default:
|
||||||
fmt.Println("Unable to queue notice. Channel full.")
|
common.LogErrorln("Unable to queue notice. Channel full.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,7 +253,7 @@ func (cr *ChatRoom) AddEventMsg(event common.EventType, name, color string) {
|
||||||
select {
|
select {
|
||||||
case cr.queue <- common.NewChatEvent(event, name, color):
|
case cr.queue <- common.NewChatEvent(event, name, color):
|
||||||
default:
|
default:
|
||||||
fmt.Println("Unable to queue event message. Channel full.")
|
common.LogErrorln("Unable to queue event message. Channel full.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -310,7 +310,7 @@ func (cr *ChatRoom) Broadcast() {
|
||||||
send := func(data common.ChatData, client *Client) {
|
send := func(data common.ChatData, client *Client) {
|
||||||
err := client.SendChatData(data)
|
err := client.SendChatData(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error sending data to client: %v\n", err)
|
common.LogErrorf("Error sending data to client: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -333,7 +333,7 @@ func (cr *ChatRoom) Broadcast() {
|
||||||
|
|
||||||
data, err := msg.ToJSON()
|
data, err := msg.ToJSON()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error converting ChatData to ChatDataJSON: %v\n", err)
|
common.LogErrorf("Error converting ChatData to ChatDataJSON: %v\n", err)
|
||||||
cr.clientsMtx.Unlock()
|
cr.clientsMtx.Unlock()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -342,7 +342,7 @@ func (cr *ChatRoom) Broadcast() {
|
||||||
go func(c *chatConnection, suid string) {
|
go func(c *chatConnection, suid string) {
|
||||||
err = c.WriteData(data)
|
err = c.WriteData(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error writing data to connection: %v\n", err)
|
common.LogErrorf("Error writing data to connection: %v\n", err)
|
||||||
delete(cr.tempConn, suid)
|
delete(cr.tempConn, suid)
|
||||||
}
|
}
|
||||||
}(conn, uuid)
|
}(conn, uuid)
|
||||||
|
@ -484,7 +484,7 @@ func (cr *ChatRoom) changeName(oldName, newName string, forced bool) error {
|
||||||
|
|
||||||
if currentClient != nil {
|
if currentClient != nil {
|
||||||
currentClient.name = newName
|
currentClient.name = newName
|
||||||
fmt.Printf("%q -> %q\n", oldName, newName)
|
common.LogDebugf("%q -> %q\n", oldName, newName)
|
||||||
|
|
||||||
if forced {
|
if forced {
|
||||||
cr.AddEventMsg(common.EvNameChangeForced, oldName+":"+newName, currentClient.color)
|
cr.AddEventMsg(common.EvNameChangeForced, oldName+":"+newName, currentClient.color)
|
||||||
|
|
|
@ -47,14 +47,15 @@ func LoadEmotes() (int, error) {
|
||||||
globbed_files := []string(emotePNGs)
|
globbed_files := []string(emotePNGs)
|
||||||
globbed_files = append(globbed_files, emoteGIFs...)
|
globbed_files = append(globbed_files, emoteGIFs...)
|
||||||
|
|
||||||
fmt.Println("Loading emotes...")
|
LogInfoln("Loading emotes...")
|
||||||
|
emInfo := []string{}
|
||||||
for _, file := range globbed_files {
|
for _, file := range globbed_files {
|
||||||
file = filepath.Base(file)
|
file = filepath.Base(file)
|
||||||
key := file[0 : len(file)-4]
|
key := file[0 : len(file)-4]
|
||||||
newEmotes[key] = file
|
newEmotes[key] = file
|
||||||
fmt.Printf("%s ", key)
|
emInfo = append(emInfo, key)
|
||||||
}
|
}
|
||||||
Emotes = newEmotes
|
Emotes = newEmotes
|
||||||
fmt.Println("")
|
LogInfoln(strings.Join(emInfo, " "))
|
||||||
return len(Emotes), nil
|
return len(Emotes), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,211 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var loglevel LogLevel
|
||||||
|
|
||||||
|
type LogLevel string
|
||||||
|
|
||||||
|
const (
|
||||||
|
LLError LogLevel = "error" // only log errors
|
||||||
|
LLChat LogLevel = "chat" // log chat and commands
|
||||||
|
LLInfo LogLevel = "info" // log info messages (not quite debug, but not chat)
|
||||||
|
LLDebug LogLevel = "debug" // log everything
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
logPrefixError string = "[ERROR] "
|
||||||
|
logPrefixChat string = "[CHAT] "
|
||||||
|
logPrefixInfo string = "[INFO] "
|
||||||
|
logPrefixDebug string = "[DEBUG] "
|
||||||
|
logPrefixDev string = "[DEV] "
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
logError *log.Logger
|
||||||
|
logChat *log.Logger
|
||||||
|
logInfo *log.Logger
|
||||||
|
logDebug *log.Logger
|
||||||
|
logDev *log.Logger
|
||||||
|
)
|
||||||
|
|
||||||
|
//func ParseLogLevel(input string) LogLevel {
|
||||||
|
// switch LogLevel(input) {
|
||||||
|
// case LLError, LLChat, LLInfo, LLDebug:
|
||||||
|
// return LogLevel(input)
|
||||||
|
// default:
|
||||||
|
// return LLError
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
func SetupLogging(level LogLevel, file string) error {
|
||||||
|
switch level {
|
||||||
|
case LLDebug:
|
||||||
|
if file == "" {
|
||||||
|
logError = log.New(os.Stderr, logPrefixError, log.LstdFlags)
|
||||||
|
logChat = log.New(os.Stdout, logPrefixChat, log.LstdFlags)
|
||||||
|
logDebug = log.New(os.Stdout, logPrefixDebug, log.LstdFlags)
|
||||||
|
logInfo = log.New(os.Stdout, logPrefixInfo, log.LstdFlags)
|
||||||
|
} else {
|
||||||
|
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to open log file for writing: %s", err)
|
||||||
|
}
|
||||||
|
logError = log.New(io.MultiWriter(os.Stderr, f), logPrefixError, log.LstdFlags)
|
||||||
|
logChat = log.New(io.MultiWriter(os.Stdout, f), logPrefixChat, log.LstdFlags)
|
||||||
|
logInfo = log.New(io.MultiWriter(os.Stdout, f), logPrefixInfo, log.LstdFlags)
|
||||||
|
logDebug = log.New(io.MultiWriter(os.Stdout, f), logPrefixDebug, log.LstdFlags)
|
||||||
|
}
|
||||||
|
case LLChat:
|
||||||
|
logDebug = nil
|
||||||
|
if file == "" {
|
||||||
|
logError = log.New(os.Stderr, logPrefixError, log.LstdFlags)
|
||||||
|
logChat = log.New(os.Stdout, logPrefixChat, log.LstdFlags)
|
||||||
|
logInfo = log.New(os.Stdout, logPrefixInfo, log.LstdFlags)
|
||||||
|
} else {
|
||||||
|
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to open log file for writing: %s", err)
|
||||||
|
}
|
||||||
|
logError = log.New(io.MultiWriter(os.Stderr, f), logPrefixError, log.LstdFlags)
|
||||||
|
logChat = log.New(io.MultiWriter(os.Stdout, f), logPrefixChat, log.LstdFlags)
|
||||||
|
logInfo = log.New(io.MultiWriter(os.Stdout, f), logPrefixInfo, log.LstdFlags)
|
||||||
|
}
|
||||||
|
|
||||||
|
case LLInfo:
|
||||||
|
logDebug = nil
|
||||||
|
logChat = nil
|
||||||
|
if file == "" {
|
||||||
|
logError = log.New(os.Stderr, logPrefixError, log.LstdFlags)
|
||||||
|
logInfo = log.New(os.Stdout, logPrefixInfo, log.LstdFlags)
|
||||||
|
} else {
|
||||||
|
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to open log file for writing: %s", err)
|
||||||
|
}
|
||||||
|
logError = log.New(io.MultiWriter(os.Stderr, f), logPrefixError, log.LstdFlags)
|
||||||
|
logInfo = log.New(io.MultiWriter(os.Stdout, f), logPrefixInfo, log.LstdFlags)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default to error
|
||||||
|
default:
|
||||||
|
logChat = nil
|
||||||
|
logDebug = nil
|
||||||
|
logInfo = nil
|
||||||
|
if file == "" {
|
||||||
|
logError = log.New(os.Stderr, logPrefixError, log.LstdFlags)
|
||||||
|
} else {
|
||||||
|
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to open log file for writing: %s", err)
|
||||||
|
}
|
||||||
|
logError = log.New(io.MultiWriter(os.Stderr, f), logPrefixError, log.LstdFlags)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogErrorf(format string, v ...interface{}) {
|
||||||
|
if logError == nil {
|
||||||
|
panic("Logging not setup!")
|
||||||
|
}
|
||||||
|
|
||||||
|
logError.Printf(format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogErrorln(v ...interface{}) {
|
||||||
|
if logError == nil {
|
||||||
|
panic("Logging not setup!")
|
||||||
|
}
|
||||||
|
|
||||||
|
logError.Println(v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogChatf(format string, v ...interface{}) {
|
||||||
|
// if logError isn't set to something, logging wasn't setup.
|
||||||
|
if logError == nil {
|
||||||
|
panic("Logging not setup!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// logging chat and commands is turned off.
|
||||||
|
if logChat == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logChat.Printf(format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogChatln(v ...interface{}) {
|
||||||
|
// if logError isn't set to something, logging wasn't setup.
|
||||||
|
if logError == nil {
|
||||||
|
panic("Logging not setup!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// logging chat and commands is turned off.
|
||||||
|
if logChat == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logChat.Println(v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogInfof(format string, v ...interface{}) {
|
||||||
|
// if logError isn't set to something, logging wasn't setup.
|
||||||
|
if logError == nil {
|
||||||
|
panic("Logging not setup!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// logging info is turned off.
|
||||||
|
if logInfo == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logInfo.Printf(format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogInfoln(v ...interface{}) {
|
||||||
|
// if logError isn't set to something, logging wasn't setup.
|
||||||
|
if logError == nil {
|
||||||
|
panic("Logging not setup!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// logging info is turned off.
|
||||||
|
if logInfo == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logInfo.Println(v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogDebugf(format string, v ...interface{}) {
|
||||||
|
// if logError isn't set to something, logging wasn't setup.
|
||||||
|
if logError == nil {
|
||||||
|
panic("Logging not setup!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// logging debug is turned off.
|
||||||
|
if logDebug == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logDebug.Printf(format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogDebugln(v ...interface{}) {
|
||||||
|
// if logError isn't set to something, logging wasn't setup.
|
||||||
|
if logError == nil {
|
||||||
|
panic("Logging not setup!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// logging debug is turned off.
|
||||||
|
if logDebug == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logDebug.Println(v...)
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
// +build dev
|
||||||
|
|
||||||
|
package common
|
||||||
|
|
||||||
|
func LogDevf(format string, v ...interface{}) {
|
||||||
|
if logError == nil {
|
||||||
|
panic("Logging not setup!")
|
||||||
|
}
|
||||||
|
|
||||||
|
logError.Printf(format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogDevln(v ...interface{}) {
|
||||||
|
if logError == nil {
|
||||||
|
panic("Logging not setup!")
|
||||||
|
}
|
||||||
|
|
||||||
|
logError.Println(v...)
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/zorchenhimer/MovieNight/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type chatConnection struct {
|
type chatConnection struct {
|
||||||
|
@ -31,7 +32,7 @@ func (cc *chatConnection) WriteData(data interface{}) error {
|
||||||
err := cc.WriteJSON(data)
|
err := cc.WriteJSON(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if operr, ok := err.(*net.OpError); ok {
|
if operr, ok := err.(*net.OpError); ok {
|
||||||
fmt.Println("OpError: " + operr.Err.Error())
|
common.LogDebugln("OpError: " + operr.Err.Error())
|
||||||
}
|
}
|
||||||
return fmt.Errorf("Error writing data to %s %s: %v", cc.clientName, cc.Host(), err)
|
return fmt.Errorf("Error writing data to %s %s: %v", cc.clientName, cc.Host(), err)
|
||||||
}
|
}
|
||||||
|
|
44
handlers.go
44
handlers.go
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -58,7 +57,7 @@ func wsStaticFiles(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
goodPath := r.URL.Path[8:len(r.URL.Path)]
|
goodPath := r.URL.Path[8:len(r.URL.Path)]
|
||||||
fmt.Printf("[static] serving %q from folder ./static/\n", goodPath)
|
common.LogDebugf("[static] serving %q from folder ./static/\n", goodPath)
|
||||||
|
|
||||||
http.ServeFile(w, r, "./static/"+goodPath)
|
http.ServeFile(w, r, "./static/"+goodPath)
|
||||||
}
|
}
|
||||||
|
@ -70,7 +69,7 @@ func wsWasmFile(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func wsImages(w http.ResponseWriter, r *http.Request) {
|
func wsImages(w http.ResponseWriter, r *http.Request) {
|
||||||
base := filepath.Base(r.URL.Path)
|
base := filepath.Base(r.URL.Path)
|
||||||
fmt.Println("[img] ", base)
|
common.LogDebugln("[img] ", base)
|
||||||
http.ServeFile(w, r, "./static/img/"+base)
|
http.ServeFile(w, r, "./static/img/"+base)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +90,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error upgrading to websocket:", err)
|
common.LogErrorln("Error upgrading to websocket:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,7 +106,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
uid, err := chat.JoinTemp(chatConn)
|
uid, err := chat.JoinTemp(chatConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("[handler] could not do a temp join, %v\n", err)
|
common.LogErrorf("[handler] could not do a temp join, %v\n", err)
|
||||||
conn.Close()
|
conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,7 +116,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
var data common.ClientData
|
var data common.ClientData
|
||||||
err := chatConn.ReadData(&data)
|
err := chatConn.ReadData(&data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("[handler] Client closed connection: %s\n", conn.RemoteAddr().String())
|
common.LogInfof("[handler] Client closed connection: %s\n", conn.RemoteAddr().String())
|
||||||
conn.Close()
|
conn.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -126,14 +125,14 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch err.(type) {
|
switch err.(type) {
|
||||||
case UserFormatError, UserTakenError:
|
case UserFormatError, UserTakenError:
|
||||||
fmt.Printf("[handler|%s] %v\n", errorName(err), err)
|
common.LogInfof("[handler|%s] %v\n", errorName(err), err)
|
||||||
case BannedUserError:
|
case BannedUserError:
|
||||||
fmt.Printf("[handler|%s] %v\n", errorName(err), err)
|
common.LogInfof("[handler|%s] %v\n", errorName(err), err)
|
||||||
// close connection since banned users shouldn't be connecting
|
// close connection since banned users shouldn't be connecting
|
||||||
conn.Close()
|
conn.Close()
|
||||||
default:
|
default:
|
||||||
// for now all errors not caught need to be warned
|
// for now all errors not caught need to be warned
|
||||||
fmt.Printf("[handler|uncaught] %v\n", err)
|
common.LogErrorf("[handler|uncaught] %v\n", err)
|
||||||
conn.Close()
|
conn.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -156,7 +155,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
func handleHelpTemplate(w http.ResponseWriter, r *http.Request) {
|
func handleHelpTemplate(w http.ResponseWriter, r *http.Request) {
|
||||||
t, err := template.ParseFiles("./static/base.html", "./static/help.html")
|
t, err := template.ParseFiles("./static/base.html", "./static/help.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error parsing template file, %v\n", err)
|
common.LogErrorf("Error parsing template file, %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,14 +181,14 @@ func handleHelpTemplate(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
err = t.Execute(w, data)
|
err = t.Execute(w, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error executing file, %v", err)
|
common.LogErrorf("Error executing file, %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleIndexTemplate(w http.ResponseWriter, r *http.Request) {
|
func handleIndexTemplate(w http.ResponseWriter, r *http.Request) {
|
||||||
t, err := template.ParseFiles("./static/base.html", "./static/main.html")
|
t, err := template.ParseFiles("./static/base.html", "./static/main.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error parsing template file, %v\n", err)
|
common.LogErrorf("Error parsing template file, %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -220,7 +219,7 @@ func handleIndexTemplate(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
err = t.Execute(w, data)
|
err = t.Execute(w, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error executing file, %v", err)
|
common.LogErrorf("Error executing file, %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -228,22 +227,22 @@ func handlePublish(conn *rtmp.Conn) {
|
||||||
streams, _ := conn.Streams()
|
streams, _ := conn.Streams()
|
||||||
|
|
||||||
l.Lock()
|
l.Lock()
|
||||||
fmt.Println("request string->", conn.URL.RequestURI())
|
common.LogDebugln("request string->", conn.URL.RequestURI())
|
||||||
urlParts := strings.Split(strings.Trim(conn.URL.RequestURI(), "/"), "/")
|
urlParts := strings.Split(strings.Trim(conn.URL.RequestURI(), "/"), "/")
|
||||||
fmt.Println("urlParts->", urlParts)
|
common.LogDebugln("urlParts->", urlParts)
|
||||||
|
|
||||||
if len(urlParts) > 2 {
|
if len(urlParts) > 2 {
|
||||||
fmt.Println("Extra garbage after stream key")
|
common.LogErrorln("Extra garbage after stream key")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(urlParts) != 2 {
|
if len(urlParts) != 2 {
|
||||||
fmt.Println("Missing stream key")
|
common.LogErrorln("Missing stream key")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if urlParts[1] != settings.GetStreamKey() {
|
if urlParts[1] != settings.GetStreamKey() {
|
||||||
fmt.Println("Due to key not match, denied stream")
|
common.LogErrorln("Stream key is incorrect. Denying stream.")
|
||||||
return //If key not match, deny stream
|
return //If key not match, deny stream
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,13 +258,13 @@ func handlePublish(conn *rtmp.Conn) {
|
||||||
}
|
}
|
||||||
l.Unlock()
|
l.Unlock()
|
||||||
if ch == nil {
|
if ch == nil {
|
||||||
fmt.Println("Unable to start stream, channel is nil.")
|
common.LogErrorln("Unable to start stream, channel is nil.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Stream started")
|
common.LogInfoln("Stream started")
|
||||||
avutil.CopyPackets(ch.que, conn)
|
avutil.CopyPackets(ch.que, conn)
|
||||||
fmt.Println("Stream finished")
|
common.LogInfoln("Stream finished")
|
||||||
|
|
||||||
l.Lock()
|
l.Lock()
|
||||||
delete(channels, streamPath)
|
delete(channels, streamPath)
|
||||||
|
@ -303,7 +302,8 @@ func handleDefault(w http.ResponseWriter, r *http.Request) {
|
||||||
avutil.CopyFile(muxer, cursor)
|
avutil.CopyFile(muxer, cursor)
|
||||||
} else {
|
} else {
|
||||||
if r.URL.Path != "/" {
|
if r.URL.Path != "/" {
|
||||||
fmt.Println("[http 404] ", r.URL.Path)
|
// not really an error for the server, but for the client.
|
||||||
|
common.LogInfoln("[http 404] ", r.URL.Path)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
} else {
|
} else {
|
||||||
handleIndexTemplate(w, r)
|
handleIndexTemplate(w, r)
|
||||||
|
|
20
main.go
20
main.go
|
@ -2,13 +2,13 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
"github.com/nareix/joy4/format"
|
"github.com/nareix/joy4/format"
|
||||||
"github.com/nareix/joy4/format/rtmp"
|
"github.com/nareix/joy4/format/rtmp"
|
||||||
|
"github.com/zorchenhimer/MovieNight/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -35,7 +35,7 @@ func main() {
|
||||||
// Load emotes before starting server.
|
// Load emotes before starting server.
|
||||||
var err error
|
var err error
|
||||||
if chat, err = newChatRoom(); err != nil {
|
if chat, err = newChatRoom(); err != nil {
|
||||||
fmt.Println(err)
|
common.LogErrorln(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,15 +43,17 @@ func main() {
|
||||||
addr = settings.ListenAddress
|
addr = settings.ListenAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
|
common.LogDevln("This should break things")
|
||||||
|
|
||||||
// A stream key was passed on the command line. Use it, but don't save
|
// A stream key was passed on the command line. Use it, but don't save
|
||||||
// it over the stream key in the settings.json file.
|
// it over the stream key in the settings.json file.
|
||||||
if sKey != "" {
|
if sKey != "" {
|
||||||
settings.SetTempKey(sKey)
|
settings.SetTempKey(sKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Stream key: ", settings.GetStreamKey())
|
common.LogInfoln("Stream key: ", settings.GetStreamKey())
|
||||||
fmt.Println("Admin password: ", settings.AdminPassword)
|
common.LogInfoln("Admin password: ", settings.AdminPassword)
|
||||||
fmt.Println("Listen and serve ", addr)
|
common.LogInfoln("Listen and serve ", addr)
|
||||||
|
|
||||||
go startServer()
|
go startServer()
|
||||||
go startRmtpServer()
|
go startRmtpServer()
|
||||||
|
@ -66,7 +68,8 @@ func startRmtpServer() {
|
||||||
}
|
}
|
||||||
err := server.ListenAndServe()
|
err := server.ListenAndServe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error trying to start server: %v\n", err)
|
// If the server cannot start, don't pretend we can continue.
|
||||||
|
panic("Error trying to start rtmp server: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +90,8 @@ func startServer() {
|
||||||
|
|
||||||
err := http.ListenAndServe(addr, nil)
|
err := http.ListenAndServe(addr, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error trying to start rmtp server: %v\n", err)
|
// If the server cannot start, don't pretend we can continue.
|
||||||
|
panic("Error trying to start chat/http server: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,7 +99,7 @@ func handleInterrupt(exit chan bool) {
|
||||||
ch := make(chan os.Signal)
|
ch := make(chan os.Signal)
|
||||||
signal.Notify(ch, os.Interrupt)
|
signal.Notify(ch, os.Interrupt)
|
||||||
<-ch
|
<-ch
|
||||||
fmt.Println("Closing server")
|
common.LogInfoln("Closing server")
|
||||||
if settings.StreamStats {
|
if settings.StreamStats {
|
||||||
stats.Print()
|
stats.Print()
|
||||||
}
|
}
|
||||||
|
|
18
settings.go
18
settings.go
|
@ -9,6 +9,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/zorchenhimer/MovieNight/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
var settings *Settings
|
var settings *Settings
|
||||||
|
@ -27,6 +29,8 @@ type Settings struct {
|
||||||
StreamKey string
|
StreamKey string
|
||||||
ListenAddress string
|
ListenAddress string
|
||||||
Bans []BanInfo
|
Bans []BanInfo
|
||||||
|
LogLevel common.LogLevel
|
||||||
|
LogFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
type BanInfo struct {
|
type BanInfo struct {
|
||||||
|
@ -45,8 +49,8 @@ func init() {
|
||||||
panic("Missing stream key is settings.json")
|
panic("Missing stream key is settings.json")
|
||||||
}
|
}
|
||||||
|
|
||||||
if settings.TitleLength <= 0 {
|
if err = common.SetupLogging(settings.LogLevel, settings.LogFile); err != nil {
|
||||||
settings.TitleLength = 50
|
panic("Unable to setup logger: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save admin password to file
|
// Save admin password to file
|
||||||
|
@ -79,8 +83,14 @@ func LoadSettings(filename string) (*Settings, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to generate admin password: %s", err)
|
return nil, fmt.Errorf("unable to generate admin password: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't use LogInfof() here. Log isn't setup yet when LoadSettings() is called from init().
|
||||||
fmt.Printf("Settings reloaded. New admin password: %s\n", s.AdminPassword)
|
fmt.Printf("Settings reloaded. New admin password: %s\n", s.AdminPassword)
|
||||||
|
|
||||||
|
if s.TitleLength <= 0 {
|
||||||
|
s.TitleLength = 50
|
||||||
|
}
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,7 +132,7 @@ func (s *Settings) AddBan(host string, names []string) error {
|
||||||
}
|
}
|
||||||
settings.Bans = append(settings.Bans, b)
|
settings.Bans = append(settings.Bans, b)
|
||||||
|
|
||||||
fmt.Printf("[BAN] %q (%s) has been banned.\n", strings.Join(names, ", "), host)
|
common.LogInfof("[BAN] %q (%s) has been banned.\n", strings.Join(names, ", "), host)
|
||||||
|
|
||||||
return settings.Save()
|
return settings.Save()
|
||||||
}
|
}
|
||||||
|
@ -136,7 +146,7 @@ func (s *Settings) RemoveBan(name string) error {
|
||||||
for _, b := range s.Bans {
|
for _, b := range s.Bans {
|
||||||
for _, n := range b.Names {
|
for _, n := range b.Names {
|
||||||
if n == name {
|
if n == name {
|
||||||
fmt.Printf("[ban] Removed ban for %s [%s]\n", b.IP, n)
|
common.LogInfof("[ban] Removed ban for %s [%s]\n", b.IP, n)
|
||||||
} else {
|
} else {
|
||||||
newBans = append(newBans, b)
|
newBans = append(newBans, b)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue