Skip to content

Commit

Permalink
Custom Loggers
Browse files Browse the repository at this point in the history
Define LoggerFactory as part of the SettingEngine
  • Loading branch information
Hugo Arregui committed Apr 1, 2019
1 parent f1c3fb8 commit 96e902d
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 18 deletions.
8 changes: 8 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

package webrtc

import (
"github.com/pions/logging"
)

// API bundles the global funcions of the WebRTC and ORTC API.
// Some of these functions are also exported globally using the
// defaultAPI object. Note that the global version of the API
Expand All @@ -23,6 +27,10 @@ func NewAPI(options ...func(*API)) *API {
a.settingEngine = &SettingEngine{}
}

if a.settingEngine.LoggerFactory == nil {
a.settingEngine.LoggerFactory = logging.NewDefaultLoggerFactory()
}

if a.mediaEngine == nil {
a.mediaEngine = &MediaEngine{}
}
Expand Down
6 changes: 3 additions & 3 deletions datachannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ type DataChannel struct {

// A reference to the associated api object used by this datachannel
api *API
log *logging.LeveledLogger
log logging.LeveledLogger
}

// NewDataChannel creates a new DataChannel.
// This constructor is part of the ORTC API. It is not
// meant to be used together with the basic WebRTC API.
func (api *API) NewDataChannel(transport *SCTPTransport, params *DataChannelParameters) (*DataChannel, error) {
d, err := api.newDataChannel(params, logging.NewScopedLogger("ortc"))
d, err := api.newDataChannel(params, api.settingEngine.LoggerFactory.NewLogger("ortc"))
if err != nil {
return nil, err
}
Expand All @@ -73,7 +73,7 @@ func (api *API) NewDataChannel(transport *SCTPTransport, params *DataChannelPara

// newDataChannel is an internal constructor for the data channel used to
// create the DataChannel object before the networking is set up.
func (api *API) newDataChannel(params *DataChannelParameters, log *logging.LeveledLogger) (*DataChannel, error) {
func (api *API) newDataChannel(params *DataChannelParameters, log logging.LeveledLogger) (*DataChannel, error) {
// https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #5)
if len(params.Label) > 65535 {
return nil, &rtcerr.TypeError{Err: ErrStringSizeLimit}
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ module github.com/pions/webrtc
require (
github.com/pions/datachannel v1.2.1
github.com/pions/dtls v1.2.2
github.com/pions/ice v0.1.1
github.com/pions/logging v0.1.0
github.com/pions/ice v0.2.0
github.com/pions/logging v0.2.0
github.com/pions/quic v0.0.1
github.com/pions/rtcp v1.1.2
github.com/pions/rtp v1.1.0
github.com/pions/sctp v1.4.3
github.com/pions/sdp/v2 v2.1.0
github.com/pions/srtp v1.1.2
github.com/pions/stun v0.2.0 // indirect
github.com/pions/transport v0.5.0
github.com/stretchr/testify v1.3.0
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pions/datachannel v1.2.1 h1:zbSwtPqVcjqUM81A3VTR7lGfeAnQgxJhheXSfBk16SM=
github.com/pions/datachannel v1.2.1/go.mod h1:Vd79tKnghclBJLCYNqN0xQzw3u3P2N/7QHYoLCLh2+Q=
github.com/pions/dtls v1.0.2/go.mod h1:T22vu8VCOxNmIrbe3nnM1UdIo3m1Bx5CJNkHyehahLg=
github.com/pions/dtls v1.2.2 h1:izn/74bIBxVuHb+NdirkmWMH/yFNd8PBVXJgeH/ofGU=
github.com/pions/dtls v1.2.2/go.mod h1:5o0cLHyBEl8CvuA88enDDM6aBwn4SNo8md2dPhcynfc=
github.com/pions/ice v0.1.1 h1:SZouAvl9RnrNnCHxSnurOfzqK2+oD0ZeURSoxuOs0tI=
github.com/pions/ice v0.1.1/go.mod h1:vGnMrwYxOMmfR093luGSh2yMm1DDpPqj8lGRNsmo9VQ=
github.com/pions/ice v0.2.0 h1:1FQNPY3KZHoft9Qg3POnEQFAei2IM3KBfHbJ9Uo0apw=
github.com/pions/ice v0.2.0/go.mod h1:+WYLnnIPnit0VaD+pEx3zZrBoCAWErBYNmlF2bvrUP0=
github.com/pions/logging v0.1.0 h1:vr+vInmjaRI06CqPWErEQpywxaqoIZcUjQ2eN68jRbk=
github.com/pions/logging v0.1.0/go.mod h1:duuz9/Se8ujqvq7OPzbnPpRlha6A0fk1Ba2wrbn4zew=
github.com/pions/logging v0.2.0 h1:xSCkE+7+aebgVimtHBv7Y8b8waT8pgeyKku9Bo+eGdk=
github.com/pions/logging v0.2.0/go.mod h1:duuz9/Se8ujqvq7OPzbnPpRlha6A0fk1Ba2wrbn4zew=
github.com/pions/pkg v0.0.0-20181115215726-b60cd756f712/go.mod h1:r9wKZs+Xxv2acLspex4CHQiIhFjGK1zGP+nUm/8klXA=
github.com/pions/qtls-vendor-extracted v0.0.0-20190210024908-018998217c65 h1:skcEQZ2eUdm1WKlYu7y1y0HBzOwa1pgSAwvhG6PrI2s=
github.com/pions/qtls-vendor-extracted v0.0.0-20190210024908-018998217c65/go.mod h1:tSUehzG/8OAT3JvWvnovveLfRMM8NvgfN1LzwSrBX5s=
github.com/pions/quic v0.0.1 h1:SvloojnZl+wiaee/yKI88n/wQosFMCvatAKyxoRoiFQ=
Expand All @@ -52,19 +58,23 @@ github.com/pions/transport v0.4.0 h1:1N6fluzmj5W/16eFLDsCB18s/xEkjVTek0K4IJz75FU
github.com/pions/transport v0.4.0/go.mod h1:9gvUd8ZeyU4ZX7dhNuUq97mPoekopkd7eCJEyhKwVO0=
github.com/pions/transport v0.5.0 h1:/KGBCzjc8bvVh8P5NP8GiSGL/PbupzhqvEOEBSlZux0=
github.com/pions/transport v0.5.0/go.mod h1:9gvUd8ZeyU4ZX7dhNuUq97mPoekopkd7eCJEyhKwVO0=
github.com/pions/webrtc v1.2.0/go.mod h1:bih1dMY7qksVxZTG2XMjIA6J7D5b92+MJzXYe+G2kng=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2 h1:NwxKRvbkH5MsNkvOtPZi3/3kmI8CAzs3mtv+GLQMkNo=
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
Expand Down
1 change: 1 addition & 0 deletions icegatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (g *ICEGatherer) Gather() error {
PortMax: g.api.settingEngine.ephemeralUDP.PortMax,
ConnectionTimeout: g.api.settingEngine.timeout.ICEConnection,
KeepaliveInterval: g.api.settingEngine.timeout.ICEKeepalive,
LoggerFactory: g.api.settingEngine.LoggerFactory,
}

requestedNetworkTypes := g.api.settingEngine.candidates.ICENetworkTypes
Expand Down
15 changes: 12 additions & 3 deletions icetransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ type ICETransport struct {
conn *ice.Conn
mux *mux.Mux

log *logging.LeveledLogger
api *API

log logging.LeveledLogger
}

// func (t *ICETransport) GetLocalCandidates() []ICECandidate {
Expand Down Expand Up @@ -58,7 +60,8 @@ type ICETransport struct {
func (api *API) NewICETransport(gatherer *ICEGatherer) *ICETransport {
return &ICETransport{
gatherer: gatherer,
log: logging.NewScopedLogger("ortc"),
api: api,
log: api.settingEngine.LoggerFactory.NewLogger("ortc"),
}
}

Expand Down Expand Up @@ -126,7 +129,13 @@ func (t *ICETransport) Start(gatherer *ICEGatherer, params ICEParameters, role *
}

t.conn = iceConn
t.mux = mux.NewMux(t.conn, receiveMTU)

config := mux.Config{
Conn: t.conn,
BufferSize: receiveMTU,
LoggerFactory: t.api.settingEngine.LoggerFactory,
}
t.mux = mux.NewMux(config)

return nil
}
Expand Down
18 changes: 13 additions & 5 deletions internal/mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import (
// The maximum amount of data that can be buffered before returning errors.
const maxBufferSize = 1000 * 1000 // 1MB

// Config collects the arguments to mux.Mux construction into
// a single structure
type Config struct {
Conn net.Conn
BufferSize int
LoggerFactory logging.LoggerFactory
}

// Mux allows multiplexing
type Mux struct {
lock sync.RWMutex
Expand All @@ -19,17 +27,17 @@ type Mux struct {
bufferSize int
closedCh chan struct{}

log *logging.LeveledLogger
log logging.LeveledLogger
}

// NewMux creates a new Mux
func NewMux(conn net.Conn, bufferSize int) *Mux {
func NewMux(config Config) *Mux {
m := &Mux{
nextConn: conn,
nextConn: config.Conn,
endpoints: make(map[*Endpoint]MatchFunc),
bufferSize: bufferSize,
bufferSize: config.BufferSize,
closedCh: make(chan struct{}),
log: logging.NewScopedLogger("mux"),
log: config.LoggerFactory.NewLogger("mux"),
}

go m.readLoop()
Expand Down
18 changes: 16 additions & 2 deletions internal/mux/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/pions/logging"
"github.com/pions/transport/test"
)

Expand Down Expand Up @@ -47,7 +48,13 @@ func pipeMemory() (*Endpoint, net.Conn, func(*testing.T)) {
return true
}

m := NewMux(ca, 8192)
config := Config{
Conn: ca,
BufferSize: 8192,
LoggerFactory: logging.NewDefaultLoggerFactory(),
}

m := NewMux(config)
e := m.NewEndpoint(matchAll)
m.RemoveEndpoint(e)
e = m.NewEndpoint(matchAll)
Expand All @@ -73,7 +80,14 @@ func TestNoEndpoints(t *testing.T) {
if err != nil {
panic("Failed to close network pipe")
}
m := NewMux(ca, 8192)

config := Config{
Conn: ca,
BufferSize: 8192,
LoggerFactory: logging.NewDefaultLoggerFactory(),
}

m := NewMux(config)
err = m.dispatch(make([]byte, 1))
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions peerconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type PeerConnection struct {

// A reference to the associated API state used by this connection
api *API
log *logging.LeveledLogger
log logging.LeveledLogger
}

// NewPeerConnection creates a peerconnection with the default
Expand Down Expand Up @@ -109,7 +109,7 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection,
dataChannels: make(map[uint16]*DataChannel),

api: api,
log: logging.NewScopedLogger("pc"),
log: api.settingEngine.LoggerFactory.NewLogger("pc"),
}

var err error
Expand Down
2 changes: 2 additions & 0 deletions settingengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/pions/ice"
"github.com/pions/logging"
)

// SettingEngine allows influencing behavior in ways that are not
Expand All @@ -26,6 +27,7 @@ type SettingEngine struct {
candidates struct {
ICENetworkTypes []NetworkType
}
LoggerFactory logging.LoggerFactory
}

// DetachDataChannels enables detaching data channels. When enabled
Expand Down

0 comments on commit 96e902d

Please sign in to comment.