Skip to content

Commit

Permalink
Add logger to gobacnet object. Saves to file for now. Will make confi…
Browse files Browse the repository at this point in the history
…gurable in the future.
  • Loading branch information
alexbeltran committed Dec 22, 2017
1 parent 20d2534 commit d24c9f5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
21 changes: 16 additions & 5 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ package gobacnet
import (
"fmt"
"net"
"os"
"time"

"github.com/alexbeltran/gobacnet/tsm"
bactype "github.com/alexbeltran/gobacnet/types"
"github.com/alexbeltran/gobacnet/utsm"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)

const defaultStateSize = 20
Expand All @@ -52,6 +53,7 @@ type Client struct {
tsm *tsm.TSM
utsm *utsm.Manager
listener *net.UDPConn
log *logrus.Logger
}

// getBroadcast uses the given address with subnet to return the broadcast address
Expand Down Expand Up @@ -126,12 +128,21 @@ func NewClient(inter string, port int) (*Client, error) {
}

c.listener = conn
log.SetLevel(log.ErrorLevel)
c.log = logrus.New()
c.log.Formatter = &logrus.TextFormatter{}
c.log.SetLevel(logrus.DebugLevel)

// open a debug file
f, err := os.Create("gobacnet.log")
if err != nil {
return c, fmt.Errorf("Could not create a log file")
}
c.log.Out = f

// Print out relevant information
log.Debug(fmt.Sprintf("Broadcast Address: %v", c.broadcastAddress))
log.Debug(fmt.Sprintf("Local Address: %s", c.myAddress))
log.Debug(fmt.Sprintf("Port: %x", c.port))
c.log.Debug(fmt.Sprintf("Broadcast Address: %v", c.broadcastAddress))
c.log.Debug(fmt.Sprintf("Local Address: %s", c.myAddress))
c.log.Debug(fmt.Sprintf("Port: %x", c.port))
go c.listen()
return c, nil
}
Expand Down
25 changes: 14 additions & 11 deletions listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ package gobacnet

import (
"fmt"
"log"
"net"
"os"

"github.com/alexbeltran/gobacnet/encoding"
bactype "github.com/alexbeltran/gobacnet/types"
Expand All @@ -46,6 +46,9 @@ func (c *Client) Close() {
return
}
c.listener.Close()
if f, ok := c.log.Out.(*os.File); ok {
f.Close()
}
}

func (c *Client) handleMsg(src *net.UDPAddr, b []byte) {
Expand All @@ -56,7 +59,7 @@ func (c *Client) handleMsg(src *net.UDPAddr, b []byte) {
dec := encoding.NewDecoder(b)
err := dec.BVLC(&header)
if err != nil {
log.Print(err)
c.log.Error(err)
return
}

Expand All @@ -69,7 +72,7 @@ func (c *Client) handleMsg(src *net.UDPAddr, b []byte) {
}

if npdu.IsNetworkLayerMessage {
log.Print("Ignored Network Layer Message")
c.log.Debug("Ignored Network Layer Message")
return
}

Expand All @@ -83,7 +86,7 @@ func (c *Client) handleMsg(src *net.UDPAddr, b []byte) {
switch apdu.DataType {
case bactype.UnconfirmedServiceRequest:
if apdu.UnconfirmedService == bactype.ServiceUnconfirmedIAm {
log.Print("Received IAm Message")
c.log.Debug("Received IAm Message")
dec = encoding.NewDecoder(apdu.RawData)
var iam bactype.IAm

Expand All @@ -94,7 +97,7 @@ func (c *Client) handleMsg(src *net.UDPAddr, b []byte) {
src.IP = src.IP.To4()
iam.Addr = bactype.UDPToAddress(src)
if err != nil {
log.Print(err)
c.log.Error(err)
return
}
c.utsm.Publish(int(iam.ID.Instance), iam)
Expand All @@ -105,16 +108,16 @@ func (c *Client) handleMsg(src *net.UDPAddr, b []byte) {
// For now we are going to ignore who is request.
//log.WithFields(log.Fields{"low": low, "high": high}).Debug("WHO IS Request")
} else {
log.Printf("Unconfirmed: %d %v", apdu.UnconfirmedService, apdu.RawData)
c.log.Errorf("Unconfirmed: %d %v", apdu.UnconfirmedService, apdu.RawData)
}
case bactype.ComplexAck:
log.Print("Received Complex Ack")
c.log.Debug("Received Complex Ack")
err := c.tsm.Send(int(apdu.InvokeId), send)
if err != nil {
return
}
case bactype.ConfirmedServiceRequest:
log.Print("Received Confirmed Service Request")
c.log.Debug("Received Confirmed Service Request")
err := c.tsm.Send(int(apdu.InvokeId), send)
if err != nil {
return
Expand All @@ -123,7 +126,7 @@ func (c *Client) handleMsg(src *net.UDPAddr, b []byte) {
err := fmt.Errorf("Error Class %d Code %d", apdu.Error.Class, apdu.Error.Code)
err = c.tsm.Send(int(apdu.InvokeId), err)
if err != nil {
log.Printf("unable to send error to %d: %v", apdu.InvokeId, err)
c.log.Debug("unable to send error to %d: %v", apdu.InvokeId, err)
}
default:
// Ignore it
Expand All @@ -136,7 +139,7 @@ func (c *Client) handleMsg(src *net.UDPAddr, b []byte) {
// we will need to check it for any additional information we can gleam.
// NDPU has source
b = b[forwardHeaderLength:]
log.Print("Ignored NDPU Forwarded")
c.log.Debug("Ignored NDPU Forwarded")
}

}
Expand All @@ -155,7 +158,7 @@ func (c *Client) listen() error {
b := make([]byte, 1024)
i, adr, err = c.listener.ReadFromUDP(b)
if err != nil {
log.Println(err)
c.log.Error(err)
continue
}
go c.handleMsg(adr, b[:i])
Expand Down

0 comments on commit d24c9f5

Please sign in to comment.