Skip to content

Commit

Permalink
Merge pull request #21 from seqsense/control-debug-output
Browse files Browse the repository at this point in the history
Control debug output by Option.Debug
  • Loading branch information
at-wat authored Nov 8, 2018
2 parents c9969f3 + cd1cb10 commit 5f2b96d
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 17 deletions.
21 changes: 10 additions & 11 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package awsiotdev

import (
"log"
"time"

"github.com/seqsense/aws-iot-device-sdk-go/pubqueue"
Expand Down Expand Up @@ -70,11 +69,11 @@ func connectionHandler(c *DeviceClient) {
}

case <-c.stableTimerCh:
log.Print("Stable timer reached\n")
c.dbg.print("Stable timer reached\n")
c.stateUpdateCh <- stable

case state = <-c.stateUpdateCh:
log.Printf("State updated to %s\n", state.String())
c.dbg.printf("State updated to %s\n", state.String())

switch state {
case inactive:
Expand All @@ -83,7 +82,7 @@ func connectionHandler(c *DeviceClient) {
if c.reconnectPeriod > c.opt.MaximumReconnectTime {
c.reconnectPeriod = c.opt.MaximumReconnectTime
}
log.Printf("Trying to reconnect (%d ms)\n", c.reconnectPeriod/time.Millisecond)
c.dbg.printf("Trying to reconnect (%d ms)\n", c.reconnectPeriod/time.Millisecond)
if c.opt.OnConnectionLost != nil {
c.opt.OnConnectionLost(c.opt)
}
Expand All @@ -98,7 +97,7 @@ func connectionHandler(c *DeviceClient) {
psq.resubscribe()

case established:
log.Print("Processing queued operations\n")
c.dbg.print("Processing queued operations\n")
go func() {
time.Sleep(c.opt.MinimumConnectionTime)
c.stableTimerCh <- true
Expand All @@ -107,12 +106,12 @@ func connectionHandler(c *DeviceClient) {

case stable:
if statePrev == established {
log.Print("Connection is stable\n")
c.dbg.print("Connection is stable\n")
c.reconnectPeriod = c.opt.BaseReconnectTime
}

case terminating:
log.Print("Terminating connection\n")
c.dbg.print("Terminating connection\n")
c.cli.Disconnect(250)
return

Expand All @@ -128,7 +127,7 @@ func (s *pubSubQueues) publishOrEnqueue(d *pubqueue.Data) {
go func() {
token.Wait()
if token.Error() != nil {
log.Printf("Failed to publish (%s)\n", token.Error())
s.cli.dbg.printf("Failed to publish (%s)\n", token.Error())
// MQTT doesn't guarantee receive order; just append to the last
s.cli.publishCh <- d
}
Expand All @@ -139,7 +138,7 @@ func (s *pubSubQueues) subscribeOrEnqueue(d *subqueue.Subscription) {
go func() {
token.Wait()
if token.Error() != nil {
log.Printf("Failed to subscribe (%s)\n", token.Error())
s.cli.dbg.printf("Failed to subscribe (%s)\n", token.Error())
s.cli.subscribeCh <- d
} else {
s.activeSubs[d.Topic] = d
Expand All @@ -151,7 +150,7 @@ func (s *pubSubQueues) unsubscribeOrEnqueue(d *subqueue.Subscription) {
go func() {
token.Wait()
if token.Error() != nil {
log.Printf("Failed to unsubscribe (%s)\n", token.Error())
s.cli.dbg.printf("Failed to unsubscribe (%s)\n", token.Error())
s.cli.subscribeCh <- d
} else {
delete(s.activeSubs, d.Topic)
Expand Down Expand Up @@ -186,7 +185,7 @@ func (s *pubSubQueues) resubscribe() {
go func(d *subqueue.Subscription) {
token.Wait()
if token.Error() != nil {
log.Printf("Failed to subscribe (%s)\n", token.Error())
s.cli.dbg.printf("Failed to subscribe (%s)\n", token.Error())
s.cli.subscribeCh <- d
} else {
s.activeSubs[d.Topic] = d
Expand Down
34 changes: 34 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package awsiotdev

import (
"log"
)

var (
// Backend function of debug print output. This can be replaced by custom logger.
DebugPrintBackend func(...interface{}) = log.Print
// Backend function of debug printf output. This can be replaced by custom logger.
DebugPrintfBackend func(string, ...interface{}) = log.Printf
// Backend function of debug println output. This can be replaced by custom logger.
DebugPrintlnBackend func(...interface{}) = log.Println
)

type debugOut struct {
enable bool
}

func (s *debugOut) print(a ...interface{}) {
if s.enable {
DebugPrintBackend(a...)
}
}
func (s *debugOut) printf(format string, a ...interface{}) {
if s.enable {
DebugPrintfBackend(format, a...)
}
}
func (s *debugOut) println(a ...interface{}) {
if s.enable {
DebugPrintlnBackend(a...)
}
}
55 changes: 55 additions & 0 deletions debug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package awsiotdev

import (
"errors"
"fmt"
"log"
)

func Example_debugOut_print() {
DebugPrintBackend = func(a ...interface{}) {
fmt.Print(a...)
}

s := &debugOut{true}
s.print("Test1:", true)
s = &debugOut{false}
s.print("Test2:", false)

DebugPrintBackend = log.Print

// Output:
// Test1:true
}

func Example_debugOut_printf() {
DebugPrintfBackend = func(format string, a ...interface{}) {
fmt.Printf(format, a...)
}

s := &debugOut{true}
s.printf("Formatted value 1 (%d)", 10)
s = &debugOut{false}
s.printf("Formatted value 2 (%d)", 11)

DebugPrintfBackend = log.Printf

// Output:
// Formatted value 1 (10)
}

func Example_debugOut_println() {
DebugPrintlnBackend = func(a ...interface{}) {
fmt.Println(a...)
}

s := &debugOut{true}
s.println(errors.New("Error string 1"))
s = &debugOut{false}
s.println(errors.New("Error string 2"))

DebugPrintlnBackend = log.Println

// Output:
// Error string 1
}
11 changes: 6 additions & 5 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Package awsiotdev implements offline queueing and reconnecting features of MQTT
package awsiotdev

import (
"log"
"time"

mqtt "github.com/eclipse/paho.mqtt.golang"
Expand Down Expand Up @@ -47,11 +46,12 @@ type DeviceClient struct {
stableTimerCh chan bool
publishCh chan *pubqueue.Data
subscribeCh chan *subqueue.Subscription
dbg *debugOut
}

func New(opt *Options) *DeviceClient {
if opt.Protocol != "" || opt.Host != "" {
log.Printf("Options.Protocol and Options.Host is deprecated. Use Options.URL instead.")
(&debugOut{opt.Debug}).print("Options.Protocol and Options.Host are deprecated. Use Options.URL instead.")
opt.Url = opt.Protocol + "://" + opt.Host
}
p, err := awsiotprotocol.ByUrl(opt.Url)
Expand Down Expand Up @@ -80,14 +80,15 @@ func New(opt *Options) *DeviceClient {
stableTimerCh: make(chan bool),
publishCh: make(chan *pubqueue.Data, publishChCap),
subscribeCh: make(chan *subqueue.Subscription, subscribeChCap),
dbg: &debugOut{opt.Debug},
}

connectionLost := func(client mqtt.Client, err error) {
log.Printf("Connection lost (%s)\n", err.Error())
d.dbg.printf("Connection lost (%s)\n", err.Error())
d.stateUpdateCh <- inactive
}
onConnect := func(client mqtt.Client) {
log.Printf("Connection established\n")
d.dbg.printf("Connection established\n")
d.stateUpdateCh <- established
}

Expand Down Expand Up @@ -120,7 +121,7 @@ func (s *DeviceClient) connect() {
go func() {
token.Wait()
if token.Error() != nil {
log.Printf("Failed to connect (%s)\n", token.Error())
s.dbg.printf("Failed to connect (%s)\n", token.Error())
s.stateUpdateCh <- inactive
}
}()
Expand Down
2 changes: 1 addition & 1 deletion sample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func main() {
MinimumConnectionTime: time.Second * 2,
Keepalive: time.Second * 2,
Url: *url,
Debug: false,
Debug: true,
Qos: 1,
Retain: false,
Will: &awsiot.TopicPayload{Topic: "notification", Payload: "{\"status\": \"dead\"}"},
Expand Down

0 comments on commit 5f2b96d

Please sign in to comment.