Skip to content

Commit

Permalink
refactor: prints to log
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Nov 1, 2023
1 parent b7d603c commit 34394b4
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 37 deletions.
7 changes: 3 additions & 4 deletions app/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"darkbot/app/settings/logus"
"fmt"
"io"
"net/http"
"strings"
Expand All @@ -23,7 +22,7 @@ func FixtureTestWebServer() {
if body == "pong!" {
break
}
fmt.Println("sleeping to acquire server pong")
logus.Debug("sleeping to acquire server pong")
}
time.Sleep(50 * time.Millisecond)
}
Expand All @@ -43,7 +42,7 @@ func TestHomePage(t *testing.T) {
FixtureTestWebServer()
body := testQuery("/")

fmt.Println(body)
logus.Debug(body)

if !strings.Contains(body, "Not found") {
t.Error("")
Expand All @@ -54,7 +53,7 @@ func TestPlayers(t *testing.T) {
FixtureTestWebServer()
body := testQuery("/players")

fmt.Println(body)
logus.Debug(body)

if !strings.Contains(body, "Another page") {
t.Error("")
Expand Down
4 changes: 2 additions & 2 deletions app/api/web/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package web

import (
"fmt"
"darkbot/app/settings/logus"
"net/http"
)

Expand Down Expand Up @@ -31,6 +31,6 @@ func (s *Server) GetMux() *http.ServeMux {
for route, endpoint := range s.router {
mux.Handle(string(route), endpoint)
}
fmt.Println("started server")
logus.Info("started server")
return mux
}
5 changes: 3 additions & 2 deletions app/configurator/alerts_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package configurator

import (
"darkbot/app/settings/logus"
"darkbot/app/settings/types"
"darkbot/app/settings/utils"
"fmt"
Expand All @@ -17,7 +18,7 @@ func TestAlertTreshold(t *testing.T) {

cfg := NewCfgAlertNeutralPlayersGreaterThan(genericCfg)
status, _ := cfg.Status(channelID)
fmt.Println("status=", status)
logus.Debug(fmt.Sprintf("status=%v", status))
assert.Nil(t, status, "status is not Nil. failed aert")

cfg.Set(channelID, 5)
Expand All @@ -38,7 +39,7 @@ func TestAlertBool(t *testing.T) {

cfg := NewCfgAlertBaseIsUnderAttack(genericCfg)
status, _ := cfg.Status(channelID)
fmt.Println("status=", status)
logus.Debug(fmt.Sprintf("status=%t", status))
assert.False(t, status, "status is not true. failed aert")

cfg.Enable(channelID)
Expand Down
3 changes: 2 additions & 1 deletion app/configurator/channel_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package configurator

import (
"darkbot/app/settings/logus"
"darkbot/app/settings/types"
"fmt"
"testing"
Expand All @@ -18,7 +19,7 @@ func TestChannels(t *testing.T) {
cg.Add("3")

channels, _ := cg.List()
fmt.Println(channels)
logus.Debug("invoked List", logus.Items(channels, "channels"))
assert.Len(t, channels, 3)

cg.Remove("3")
Expand Down
4 changes: 2 additions & 2 deletions app/consoler/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"darkbot/app/consoler/commands/cmdgroup"
"darkbot/app/consoler/consoler_types"
"darkbot/app/settings"
"darkbot/app/settings/logus"
"darkbot/app/settings/types"
"fmt"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -186,7 +186,7 @@ func (r *rootCommands) CreatePing() {
Use: "ping",
Short: "Check stuff is working",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("ping called with args=", args)
logus.Debug("ping called with args=", logus.Items(args, "args"))
cmd.OutOrStdout().Write([]byte("Pong! from consoler"))
},
}
Expand Down
6 changes: 3 additions & 3 deletions app/listener/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func Run() {
logus.CheckFatal(err, "error opening connection,")
defer dg.Close()

fmt.Println("Bot is now running. Press CTRL-C to exit.")
logus.Info("Bot is now running. Press CTRL-C to exit.")
utils.SleepAwaitCtrlC()
fmt.Println("gracefully closed discord conn")
logus.Info("gracefully closed discord conn")
}

func allowedMessage(s *discordgo.Session, m *discordgo.MessageCreate) bool {
Expand Down Expand Up @@ -103,5 +103,5 @@ func consolerHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
if rendered != "" {
s.ChannelMessageSend(m.ChannelID, rendered)
}
fmt.Println("ChannelID=", m.ChannelID)
logus.Debug("consolerHandler finished", logus.ChannelID(channelID))
}
4 changes: 2 additions & 2 deletions app/management/anounce.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"darkbot/app/configurator"
"darkbot/app/discorder"
"darkbot/app/settings"
"darkbot/app/settings/logus"
"darkbot/app/viewer/templ"
"fmt"
"strings"

"github.com/spf13/cobra"
Expand All @@ -19,7 +19,7 @@ var amounceCmd = &cobra.Command{
Use: "anounce",
Short: "Anounce something",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Anounce is called with args=", args)
logus.Info("Anounce is called with args=", logus.Items(args, "args"))
dg := discorder.NewClient()

// go listener.Run()
Expand Down
4 changes: 2 additions & 2 deletions app/management/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package management

import (
"darkbot/app/discorder"
"fmt"
"darkbot/app/settings/logus"

"github.com/spf13/cobra"
)
Expand All @@ -15,7 +15,7 @@ var checkCmd = &cobra.Command{
Use: "check",
Short: "Experimental command",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("check called")
logus.Info("check called")
// fmt.Println("result=", consoler.Consoler{}.New("").Execute().String())

// db := configurator.GetConnection()
Expand Down
4 changes: 2 additions & 2 deletions app/management/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package management
import (
"darkbot/app/configurator"
"darkbot/app/settings"
"fmt"
"darkbot/app/settings/logus"

"github.com/spf13/cobra"
)
Expand All @@ -16,7 +16,7 @@ var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Migrate db",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("migrate called")
logus.Info("migrate called")
configurator.NewConfigurator(settings.Dbpath).AutoMigrateSchema()
},
}
Expand Down
10 changes: 7 additions & 3 deletions app/settings/logus/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,17 @@ func Regex(value types.RegExp) slogParam {
}
}

func Records[T any](value []T) slogParam {
func Items[T any](value []T, item_name string) slogParam {
return func(c *slogGroup) {
c.params["records"] = fmt.Sprintf("%v", value)
c.params["records_len"] = fmt.Sprintf("%d", len(value))
c.params[item_name] = fmt.Sprintf("%v", value)
c.params[fmt.Sprintf("%s_len", item_name)] = fmt.Sprintf("%d", len(value))
}
}

func Records[T any](value []T) slogParam {
return Items[T](value, "records")
}

func APIUrl(value types.APIurl) slogParam {
return func(c *slogGroup) {
c.params["api_url"] = string(value)
Expand Down
4 changes: 2 additions & 2 deletions app/settings/utils/sleep.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package utils

import (
"fmt"
"darkbot/app/settings/logus"
"os"
"os/signal"
"sync"
Expand All @@ -15,7 +15,7 @@ func SleepAwaitCtrlC() {
}

func SleepForever() {
fmt.Println("awaiting smth forever")
logus.Debug("awaiting smth forever in SleepForever")
mu := sync.Mutex{}
mu.Lock()
mu.Lock()
Expand Down
4 changes: 2 additions & 2 deletions app/settings/utils/tests.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package utils

import (
"fmt"
"darkbot/app/settings/logus"
"os"
)

func RegenerativeTest(callback func() error) error {
if os.Getenv("DARK_TEST_REGENERATE") != "true" {
fmt.Println("Skipping test data regenerative code")
logus.Debug("Skipping test data regenerative code")
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion app/viewer/templ/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestBaseViewerMocked(t *testing.T) {

render := NewTemplateBase(channelID, dbpath)
render.Render()
fmt.Println(render.main.Content)
logus.Debug("render.main.Content" + render.main.Content)

assert.NotEmpty(t, render.main.Content)
assert.Empty(t, render.AlertHealthLowerThan.Content)
Expand Down
19 changes: 10 additions & 9 deletions app/viewer/templ/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ func (t *PlayersTemplates) Render() {
regionTags, _ := t.API.Players.Regions.TagsList(t.API.ChannelID)
friendTags, _ := t.API.Players.Friends.TagsList(t.API.ChannelID)
enemyTags, _ := t.API.Players.Enemies.TagsList(t.API.ChannelID)
fmt.Println("systemTags=", systemTags)
fmt.Println("regionTags=", regionTags)
fmt.Println("friendTags=", friendTags)
fmt.Println("enemyTags=", enemyTags)
fmt.Println("record.List=", record.List)

logus.Debug(
"PlayerTemplatesRender next",
logus.Items(systemTags, "systemTags"),
logus.Items(friendTags, "friendTags"),
logus.Items(enemyTags, "enemyTags"),
logus.Items(record.List, "record.List"),
)
neutralPlayers := []player.Player{}
enemyPlayers := []player.Player{}
friendPlayers := []player.Player{}
Expand All @@ -117,9 +118,9 @@ func (t *PlayersTemplates) Render() {
neutralPlayers = append(neutralPlayers, player)
}

logus.Debug("friendPlayers=", logus.Records(friendPlayers))
logus.Debug("enemyPlayers=", logus.Records(enemyPlayers))
logus.Debug("neutralPlayers=", logus.Records(neutralPlayers))
logus.Debug("friendPlayers=", logus.Items(friendPlayers, "friendPlayers"))
logus.Debug("enemyPlayers=", logus.Items(enemyPlayers, "enemyPlayers"))
logus.Debug("neutralPlayers=", logus.Items(neutralPlayers, "neutralPlayers"))

if len(systemTags) > 0 || len(regionTags) > 0 {
t.neutral.MainTable.Content = utils.TmpRender(playerTemplate, TemplateRendrerPlayerInput{
Expand Down

0 comments on commit 34394b4

Please sign in to comment.