1- // Copyright 2023 Blink Labs Software
1+ // Copyright 2024 Blink Labs Software
22//
33// Licensed under the Apache License, Version 2.0 (the "License");
44// you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ import (
1919 "bytes"
2020 "fmt"
2121 "io"
22+ "log/slog"
2223 "sync"
2324 "time"
2425
@@ -53,6 +54,7 @@ type ProtocolConfig struct {
5354 ProtocolId uint16
5455 ErrorChan chan error
5556 Muxer * muxer.Muxer
57+ Logger * slog.Logger
5658 Mode ProtocolMode
5759 Role ProtocolRole
5860 MessageHandlerFunc MessageHandlerFunc
@@ -85,6 +87,7 @@ const (
8587type ProtocolOptions struct {
8688 ConnectionId connection.ConnectionId
8789 Muxer * muxer.Muxer
90+ Logger * slog.Logger
8891 ErrorChan chan error
8992 Mode ProtocolMode
9093 // TODO: remove me
@@ -118,6 +121,7 @@ func New(config ProtocolConfig) *Protocol {
118121func (p * Protocol ) Start () {
119122 p .onceStart .Do (func () {
120123 // Register protocol with muxer
124+ p .Logger ().Debug ("registering protocol with muxer" )
121125 muxerProtocolRole := muxer .ProtocolRoleInitiator
122126 if p .config .Role == ProtocolRoleServer {
123127 muxerProtocolRole = muxer .ProtocolRoleResponder
@@ -156,6 +160,7 @@ func (p *Protocol) Start() {
156160func (p * Protocol ) Stop () {
157161 p .onceStop .Do (func () {
158162 // Unregister protocol from muxer
163+ p .Logger ().Debug ("unregistering protocol with muxer" )
159164 muxerProtocolRole := muxer .ProtocolRoleInitiator
160165 if p .config .Role == ProtocolRoleServer {
161166 muxerProtocolRole = muxer .ProtocolRoleResponder
@@ -167,6 +172,14 @@ func (p *Protocol) Stop() {
167172 })
168173}
169174
175+ // Logger returns the protocol logger
176+ func (p * Protocol ) Logger () * slog.Logger {
177+ if p .config .Logger == nil {
178+ return slog .New (slog .NewJSONHandler (io .Discard , nil ))
179+ }
180+ return p .config .Logger
181+ }
182+
170183// Mode returns the protocol mode
171184func (p * Protocol ) Mode () ProtocolMode {
172185 return p .config .Mode
0 commit comments