1
- // Copyright 2023 Blink Labs Software
1
+ // Copyright 2024 Blink Labs Software
2
2
//
3
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
4
// you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ import (
19
19
"bytes"
20
20
"fmt"
21
21
"io"
22
+ "log/slog"
22
23
"sync"
23
24
"time"
24
25
@@ -53,6 +54,7 @@ type ProtocolConfig struct {
53
54
ProtocolId uint16
54
55
ErrorChan chan error
55
56
Muxer * muxer.Muxer
57
+ Logger * slog.Logger
56
58
Mode ProtocolMode
57
59
Role ProtocolRole
58
60
MessageHandlerFunc MessageHandlerFunc
@@ -85,6 +87,7 @@ const (
85
87
type ProtocolOptions struct {
86
88
ConnectionId connection.ConnectionId
87
89
Muxer * muxer.Muxer
90
+ Logger * slog.Logger
88
91
ErrorChan chan error
89
92
Mode ProtocolMode
90
93
// TODO: remove me
@@ -118,6 +121,7 @@ func New(config ProtocolConfig) *Protocol {
118
121
func (p * Protocol ) Start () {
119
122
p .onceStart .Do (func () {
120
123
// Register protocol with muxer
124
+ p .Logger ().Debug ("registering protocol with muxer" )
121
125
muxerProtocolRole := muxer .ProtocolRoleInitiator
122
126
if p .config .Role == ProtocolRoleServer {
123
127
muxerProtocolRole = muxer .ProtocolRoleResponder
@@ -156,6 +160,7 @@ func (p *Protocol) Start() {
156
160
func (p * Protocol ) Stop () {
157
161
p .onceStop .Do (func () {
158
162
// Unregister protocol from muxer
163
+ p .Logger ().Debug ("unregistering protocol with muxer" )
159
164
muxerProtocolRole := muxer .ProtocolRoleInitiator
160
165
if p .config .Role == ProtocolRoleServer {
161
166
muxerProtocolRole = muxer .ProtocolRoleResponder
@@ -167,6 +172,14 @@ func (p *Protocol) Stop() {
167
172
})
168
173
}
169
174
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
+
170
183
// Mode returns the protocol mode
171
184
func (p * Protocol ) Mode () ProtocolMode {
172
185
return p .config .Mode
0 commit comments