mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
use slog
This commit is contained in:
@@ -29,7 +29,6 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Printf("%+v\n", config)
|
|
||||||
router, err := showbridge.NewRouter(ctx, config)
|
router, err := showbridge.NewRouter(ctx, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
10
router.go
10
router.go
@@ -3,6 +3,8 @@ package showbridge
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Router struct {
|
type Router struct {
|
||||||
@@ -12,6 +14,14 @@ type Router struct {
|
|||||||
|
|
||||||
func NewRouter(ctx context.Context, config Config) (*Router, error) {
|
func NewRouter(ctx context.Context, config Config) (*Router, error) {
|
||||||
|
|
||||||
|
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||||
|
Level: slog.LevelDebug,
|
||||||
|
}))
|
||||||
|
|
||||||
|
slog.SetDefault(logger)
|
||||||
|
|
||||||
|
slog.Debug("creating router", "config", config)
|
||||||
|
|
||||||
router := Router{
|
router := Router{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
ModuleInstances: []Module{},
|
ModuleInstances: []Module{},
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package showbridge
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -49,7 +50,7 @@ func (ts TCPClient) Run(ctx context.Context) error {
|
|||||||
for {
|
for {
|
||||||
client, err := net.Dial("tcp", fmt.Sprintf(":%d", ts.Port))
|
client, err := net.Dial("tcp", fmt.Sprintf(":%d", ts.Port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
slog.Error(err.Error())
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 2)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -68,12 +69,12 @@ func (ts TCPClient) Run(ctx context.Context) error {
|
|||||||
byteCount, err := client.Read(buffer)
|
byteCount, err := client.Read(buffer)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("connection closed")
|
slog.Debug("connection closed")
|
||||||
break READ
|
break READ
|
||||||
}
|
}
|
||||||
|
|
||||||
if byteCount > 0 {
|
if byteCount > 0 {
|
||||||
fmt.Println(buffer[0:byteCount])
|
slog.Info(string(buffer[0:byteCount]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package showbridge
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ts TCPServer) HandleClient(ctx context.Context, client net.Conn) {
|
func (ts TCPServer) HandleClient(ctx context.Context, client net.Conn) {
|
||||||
fmt.Printf("handling connection %s\n", client.RemoteAddr())
|
slog.Info("handling connection", "remoteAddr", client.RemoteAddr().String())
|
||||||
|
|
||||||
buffer := make([]byte, 1024)
|
buffer := make([]byte, 1024)
|
||||||
for {
|
for {
|
||||||
@@ -43,13 +44,13 @@ func (ts TCPServer) HandleClient(ctx context.Context, client net.Conn) {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.Error() == "EOF" {
|
if err.Error() == "EOF" {
|
||||||
fmt.Println("connection closed")
|
slog.Debug("connection closed")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if byteCount > 0 {
|
if byteCount > 0 {
|
||||||
fmt.Println(buffer[0:byteCount])
|
slog.Info(string(buffer[0:byteCount]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -55,7 +56,7 @@ func (us UDPServer) Run(ctx context.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println(buffer[:numBytes])
|
slog.Info(string(buffer[:numBytes]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user