Add custom-logger example

Demonstrate how users can user their own logger via the
SettingEngine

Resolves #726
This commit is contained in:
Sean DuBois
2019-07-11 13:37:31 +01:00
parent 10d2ca3d88
commit 2228e7f61e
8 changed files with 131 additions and 7 deletions

View File

@@ -32,11 +32,11 @@ before_install:
- wget https://raw.githubusercontent.com/creationix/nvm/v0.31.0/nvm.sh -O ~/.nvm/nvm.sh
before_script:
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.14.0
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.17.1
script:
- golangci-lint run --build-tags quic ./...
- cd examples && golangci-lint run --build-tags quic ./... -D gochecknoinits
- cd examples && golangci-lint run --build-tags quic ./...
- cd ..
- rm -rf examples # Remove examples, no test coverage for them
- go test -tags quic -coverpkg=$(go list ./... | tr '\n' ',') -coverprofile=cover.out -v -race -covermode=atomic ./...

View File

@@ -22,6 +22,8 @@ For more full featured examples that use 3rd party libraries see our **[example-
* [ORTC QUIC](ortc-quic): Example ortc-quic shows how you an use the ORTC API for QUIC DataChannel communication.
* [Pion to Pion](pion-to-pion): Example pion-to-pion is an example of two pion instances communicating directly! It therefore has no corresponding web page.
#### Miscellaneous
* [Custom Logger](custom-logger) The custom-logger demonstrates how the user can override the logging and process messages instead of printing to stdout. It has no corresponding web page.
### Usage
We've made it easy to run the browser based examples on your local machine.

View File

@@ -0,0 +1,16 @@
# custom-logger
custom-logger is an example of how the Pion API provides an customizable
logging API. By default all Pion projects log to stdout, but we also allow
users to override this and process messages however they want.
## Instructions
### Download custom-logger
```
go get github.com/pion/webrtc/examples/custom-logger
```
### Run custom-logger
`custom-logger`
You should see messages from our customLogger, as two PeerConnections start a session

View File

@@ -0,0 +1,100 @@
package main
import (
"fmt"
"github.com/pion/logging"
"github.com/pion/webrtc/v2"
)
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
// customLogger satisfies the interface logging.LeveledLogger
// a logger is created per subsystem in Pion, so you can have custom
// behavior per subsystem (ICE, DTLS, SCTP...)
type customLogger struct {
}
// Print all messages except trace
func (c customLogger) Trace(msg string) {}
func (c customLogger) Tracef(format string, args ...interface{}) {}
func (c customLogger) Debug(msg string) { fmt.Printf("customLogger Debug: %s\n", msg) }
func (c customLogger) Debugf(format string, args ...interface{}) {
c.Debug(fmt.Sprintf(format, args...))
}
func (c customLogger) Info(msg string) { fmt.Printf("customLogger Info: %s\n", msg) }
func (c customLogger) Infof(format string, args ...interface{}) {
c.Trace(fmt.Sprintf(format, args...))
}
func (c customLogger) Warn(msg string) { fmt.Printf("customLogger Warn: %s\n", msg) }
func (c customLogger) Warnf(format string, args ...interface{}) {
c.Warn(fmt.Sprintf(format, args...))
}
func (c customLogger) Error(msg string) { fmt.Printf("customLogger Error: %s\n", msg) }
func (c customLogger) Errorf(format string, args ...interface{}) {
c.Error(fmt.Sprintf(format, args...))
}
// customLoggerFactory satisfies the interface logging.LoggerFactory
// This allows us to create different loggers per subsystem. So we can
// add custom behavior
type customLoggerFactory struct {
}
func (c customLoggerFactory) NewLogger(subsystem string) logging.LeveledLogger {
fmt.Printf("Creating logger for %s \n", subsystem)
return customLogger{}
}
func main() {
// Create a new API with a custom logger
// This SettingEngine allows non-standard WebRTC behavior
s := webrtc.SettingEngine{
LoggerFactory: customLoggerFactory{},
}
api := webrtc.NewAPI(webrtc.WithSettingEngine(s))
// Create a new RTCPeerConnection
offerPeerConnection, err := api.NewPeerConnection(webrtc.Configuration{})
if err != nil {
panic(err)
}
// Create a new RTCPeerConnection
answerPeerConnection, err := api.NewPeerConnection(webrtc.Configuration{})
if err != nil {
panic(err)
}
// Create an offer for the other PeerConnection
offer, err := offerPeerConnection.CreateOffer(nil)
if err != nil {
panic(err)
}
// SetLocalDescription, needed before remote gets offer
if err = offerPeerConnection.SetLocalDescription(offer); err != nil {
panic(err)
}
// Take offer from remote, answerPeerConnection is now able to contact
// the other PeerConnection
if err = answerPeerConnection.SetRemoteDescription(offer); err != nil {
panic(err)
}
// Create an Answer to send back to our originating PeerConnection
answer, err := answerPeerConnection.CreateAnswer(nil)
if err != nil {
panic(err)
}
// SetRemoteDescription on original PeerConnection, this finishes our signaling
// bother PeerConnections should be able to communicate with each other now
if err = offerPeerConnection.SetRemoteDescription(answer); err != nil {
panic(err)
}
select {}
}

View File

@@ -46,5 +46,11 @@
"link": "sfu-minimal",
"description": "The SFU example demonstrates how to broadcast a video to multiple peers. A broadcaster uploads the video once and the server forwards it to all other peers.",
"type": "browser"
},
{
"title": "Custom Logger",
"link": "#",
"description": "Example custom-logger demonstrates how the user can override the logging and process messages instead of printing to stdout. It has no corresponding web page.",
"type": "browser"
}
]

View File

@@ -136,7 +136,7 @@ func TestConvertTypeFromICE(t *testing.T) {
t.Fatal("failed coverting ice.CandidateTypeHost")
}
if ct != ICECandidateTypeHost {
t.Fatal("should be coverted to ICECandidateTypeHost")
t.Fatal("should be converted to ICECandidateTypeHost")
}
})
t.Run("srflx", func(t *testing.T) {
@@ -145,7 +145,7 @@ func TestConvertTypeFromICE(t *testing.T) {
t.Fatal("failed coverting ice.CandidateTypeServerReflexive")
}
if ct != ICECandidateTypeSrflx {
t.Fatal("should be coverted to ICECandidateTypeSrflx")
t.Fatal("should be converted to ICECandidateTypeSrflx")
}
})
t.Run("prflx", func(t *testing.T) {
@@ -154,7 +154,7 @@ func TestConvertTypeFromICE(t *testing.T) {
t.Fatal("failed coverting ice.CandidateTypePeerReflexive")
}
if ct != ICECandidateTypePrflx {
t.Fatal("should be coverted to ICECandidateTypePrflx")
t.Fatal("should be converted to ICECandidateTypePrflx")
}
})
}

View File

@@ -513,7 +513,7 @@ func (pc *PeerConnection) createICETransport() *ICETransport {
t := pc.api.NewICETransport(pc.iceGatherer)
t.OnConnectionStateChange(func(state ICETransportState) {
cs := ICEConnectionStateNew
var cs ICEConnectionState
switch state {
case ICETransportStateNew:
cs = ICEConnectionStateNew

View File

@@ -387,7 +387,7 @@ func TestPeerConnection_satisfyTypeAndDirection(t *testing.T) {
want []*RTPTransceiver
}{
{
"Audio and Video Transceivers can not satifsy each other",
"Audio and Video Transceivers can not satisfy each other",
[]RTPCodecType{RTPCodecTypeVideo},
[]RTPTransceiverDirection{RTPTransceiverDirectionSendrecv},
[]*RTPTransceiver{createTransceiver(RTPCodecTypeAudio, RTPTransceiverDirectionSendrecv)},