summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-06-10 12:27:03 +0300
committerPaul Buetow <paul@buetow.org>2023-06-10 12:27:03 +0300
commitcd91865979a5bbaaab79a703e36d1be729d95101 (patch)
treef27fc6cbc01636f896c870b871e01f002dc0ee66 /internal
parent5c43de068a5d44c77b77c56f9e5570b392549888 (diff)
refactor logging
Diffstat (limited to 'internal')
-rw-r--r--internal/client/client.go10
-rw-r--r--internal/client/tcpclient.go4
-rw-r--r--internal/config/config.go2
-rw-r--r--internal/quorum/quorum.go6
-rw-r--r--internal/server/server.go6
-rw-r--r--internal/server/tcpserver.go16
-rw-r--r--internal/utils/string_test.go2
-rw-r--r--internal/vote/vote.go2
8 files changed, 22 insertions, 26 deletions
diff --git a/internal/client/client.go b/internal/client/client.go
index f7ddb12..91d64e2 100644
--- a/internal/client/client.go
+++ b/internal/client/client.go
@@ -8,10 +8,8 @@ import (
"codeberg.org/snonux/gorum/internal/config"
)
-const packageStr = "client"
-
func Start(ctx context.Context, conf config.Config, liveNodesCh <-chan []string) {
- log.Println(packageStr, "starting")
+ log.Println("client: starting")
fanOut := make([]chan []string, len(conf.Nodes))
for i, node := range conf.Nodes {
@@ -28,7 +26,7 @@ func Start(ctx context.Context, conf config.Config, liveNodesCh <-chan []string)
for {
select {
case liveNodes := <-liveNodesCh:
- log.Printf("Notifying live nodes %v to all partner nodes", liveNodes)
+ log.Printf("client: notifying live nodes %v to all partner nodes", liveNodes)
for _, ch := range fanOut {
// First, clear previous element of the channel, if any
select {
@@ -50,9 +48,9 @@ func startConnection(ctx context.Context, node string) chan []string {
go func() {
for {
- log.Println(packageStr, "starting connection", node)
+ log.Println("client: starting connection", node)
if err := tcpClientRun(ctx, node, ch); err != nil {
- log.Println(packageStr, "not connected to node", node, "anymore:", err)
+ log.Println("client: not connected to node", node, "anymore:", err)
}
select {
diff --git a/internal/client/tcpclient.go b/internal/client/tcpclient.go
index 356d259..58004c7 100644
--- a/internal/client/tcpclient.go
+++ b/internal/client/tcpclient.go
@@ -23,7 +23,7 @@ func tcpClientRun(ctx context.Context, node string, ch <-chan []string) error {
}
message := strings.Join(votes, " ")
- log.Println(packageStr, "sending", message, "to node", node)
+ log.Println("tcpclient: sending", message, "to node", node)
_, err = conn.Write([]byte(message))
if err != nil {
return err
@@ -34,6 +34,6 @@ func tcpClientRun(ctx context.Context, node string, ch <-chan []string) error {
return err
}
- log.Println(packageStr, "received", string(response), "from node", node)
+ log.Println("tcpclient: received", string(response), "from node", node)
}
}
diff --git a/internal/config/config.go b/internal/config/config.go
index 09302b7..c404284 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -67,7 +67,7 @@ func (c Config) IsNodeWithLookup(remoteAddr string, lookupIP func(string) ([]net
for _, node := range c.Nodes {
ips, err := lookupIP(utils.StripPort(node))
if err != nil {
- log.Println(err)
+ log.Println("config:", err)
continue
}
diff --git a/internal/quorum/quorum.go b/internal/quorum/quorum.go
index fb1d69d..a0306aa 100644
--- a/internal/quorum/quorum.go
+++ b/internal/quorum/quorum.go
@@ -60,12 +60,12 @@ func (quo Quorum) Start(ctx context.Context) <-chan []string {
}
func (quo Quorum) Vote(v vote.Vote) {
- log.Printf("Queing vote %v", v)
+ log.Printf("quorum: queing vote %v", v)
quo.voteCh <- v
}
func (quo Quorum) vote(v vote.Vote) {
- log.Printf("Adding vote %v", v)
+ log.Printf("quorum: adding vote %v", v)
quo.votes[v.FromID] = v
}
@@ -97,7 +97,7 @@ func (quo Quorum) score() (scores []Score) {
return i_ < j_
})
- log.Println("Scores are ", scores)
+ log.Println("quorum: scores are ", scores)
return
}
diff --git a/internal/server/server.go b/internal/server/server.go
index 2a809fe..e9d62f0 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -10,14 +10,12 @@ import (
"codeberg.org/snonux/gorum/internal/vote"
)
-const packageStr = "Server"
-
func Start(ctx context.Context, conf config.Config, quo quorum.Quorum) {
go func() {
for {
- log.Println(packageStr, "starting")
+ log.Println("server: starting")
if err := runServer(ctx, conf, quo); err != nil {
- log.Println(packageStr, err)
+ log.Println("server:", err)
}
select {
diff --git a/internal/server/tcpserver.go b/internal/server/tcpserver.go
index 3853cdc..47d47ca 100644
--- a/internal/server/tcpserver.go
+++ b/internal/server/tcpserver.go
@@ -15,26 +15,26 @@ type handlerCb func(message string) string
func tcpServerRun(ctx context.Context, conf config.Config, cb handlerCb) error {
listener, err := net.Listen("tcp", conf.Address)
if err != nil {
- return fmt.Errorf("Error starting TCP server: %s", err.Error())
+ return fmt.Errorf("error starting TCP server: %s", err.Error())
}
defer listener.Close()
- log.Printf("TCP server started on %s\n", conf.Address)
+ log.Println("server: lisetning on", conf.Address)
for {
conn, err := listener.Accept()
if err != nil {
- log.Printf("Error accepting connection: %s\n", err.Error())
+ log.Println("server: error accepting connection:", err)
continue
}
if !conf.IsNodeWithLookup(conn.RemoteAddr().String(), net.LookupIP) {
- log.Printf("Denying connection, peer not a node: %v\n", conn.RemoteAddr().String())
+ log.Println("server: denying connection, peer not a node:", conn.RemoteAddr().String())
conn.Close()
continue
}
- log.Printf("Client connected: %s\n", conn.RemoteAddr().String())
+ log.Println("server: client connected:", conn.RemoteAddr().String())
go handleConnection(ctx, conn, cb)
}
}
@@ -50,16 +50,16 @@ func handleConnection(ctx context.Context, conn net.Conn, cb handlerCb) {
for {
select {
case <-ctx.Done():
- log.Printf("Server context done, disconnecting client %s\n", remoteAddr)
+ log.Println("server: context done, disconnecting client:", remoteAddr)
return
default:
message, err := reader.ReadString('\n')
if err != nil {
- log.Printf("Client %s disconnected: %s\n", remoteAddr, err.Error())
+ log.Println("server: client disconnected:", remoteAddr, err)
return
}
- log.Printf("Received message from %s: %s", remoteAddr, message)
+ log.Println("server: received message", remoteAddr, message)
response := cb(message)
conn.Write([]byte(response))
}
diff --git a/internal/utils/string_test.go b/internal/utils/string_test.go
index ca3c0dd..10a7f83 100644
--- a/internal/utils/string_test.go
+++ b/internal/utils/string_test.go
@@ -6,6 +6,6 @@ import (
func TestStripPort(t *testing.T) {
if "localhost" != StripPort("localhost:1234") {
- t.Errorf("Unable to split port from \"localhost:1234\"")
+ t.Errorf("unable to split port from \"localhost:1234\"")
}
}
diff --git a/internal/vote/vote.go b/internal/vote/vote.go
index d5781b5..7407848 100644
--- a/internal/vote/vote.go
+++ b/internal/vote/vote.go
@@ -24,7 +24,7 @@ func New(conf config.Config, message string) Vote {
for _, id := range strings.Split(strings.TrimSpace(message), " ") {
if !conf.IsNode(id) {
- log.Printf("%s is not a node, excluding from the vote", id)
+ log.Println("vote: is not a node, excluding from the vote", id)
continue
}
if fromID == "" {