Skip to content

Commit

Permalink
added generic writer
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewI26 committed Dec 22, 2024
1 parent 75ff6f6 commit 962175c
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 274 deletions.
18 changes: 16 additions & 2 deletions canlink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,32 @@ during a test and dumps it into a file.

### Usage

1) Create an instance of Tracer with the _NewTracer()_ function.
1) Create an instance of Tracer with the `NewTracer()` function.
A can interface, directory and logger must be provided as arguments.
Functional options are available of type _TracerOption_ if required.
Functional options are available of type `TracerOption` if required.

```go
func main() {
writers := make([]writer.WriterIface, 0)
jsonWriter := writer.NewWriter(
logger,
".jsonl",
)
asciiWriter := writer.NewWriter(
logger,
".asc",
)
writers = append(writers, jsonWriter)
writers = append(writers, asciiWriter)

tracer := canlink.NewTracer(
"can0",
"/opt/traces",
logger,
connection,
canlink.WithBusName("PT"),
canlink.WithTimeout(5*time.Minute))
canlink.WithWriters(writers)
}
```
2) Open the Tracer using _Open()_
Expand Down
18 changes: 13 additions & 5 deletions canlink/canlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"go.einride.tech/can/pkg/socketcan"
"go.uber.org/zap"

"github.com/macformula/hil/canlink/tracewriters"
"github.com/macformula/hil/canlink/writer"
)

const (
Expand Down Expand Up @@ -48,17 +48,25 @@ func setup(t *testing.T) (*Tracer, *zap.Logger, func(*testing.T, *Tracer, *zap.L
t.Fatalf("Failed to create socket can connection. Error: %v", err)
}

writers := make([]tracewriters.TraceWriter, 0)
writers = append(writers, tracewriters.NewAsciiWriter(logger))
writers = append(writers, tracewriters.NewJsonWriter(logger))
writers := make([]writer.WriterIface, 0)
jsonWriter := writer.NewWriter(
logger,
".jsonl",
)
asciiWriter := writer.NewWriter(
logger,
".asc",
)
writers = append(writers, jsonWriter)
writers = append(writers, asciiWriter)

tracer := NewTracer(
_canIface,
_traceDir,
logger,
conn,
WithBusName(_busName),
WithTraceWriters(writers))
WithWriters(writers))

err = tracer.Open(ctx)
if err != nil {
Expand Down
28 changes: 14 additions & 14 deletions canlink/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/macformula/hil/utils"
"github.com/macformula/hil/canlink/tracewriters"
"github.com/macformula/hil/canlink/writer"
"github.com/pkg/errors"
"go.einride.tech/can/pkg/socketcan"
"go.uber.org/zap"
Expand Down Expand Up @@ -36,14 +36,14 @@ type TracerOption func(*Tracer)
type Tracer struct {
l *zap.Logger
stop chan struct{}
frameCh chan tracewriters.TimestampedFrame
cachedData []tracewriters.TimestampedFrame
frameCh chan writer.TimestampedFrame
cachedData []writer.TimestampedFrame
err *utils.ResettableError
isRunning bool
receiver *socketcan.Receiver

traceDir string
traceWriters []tracewriters.TraceWriter
writers []writer.WriterIface

canInterface string
timeout time.Duration
Expand All @@ -61,7 +61,7 @@ func NewTracer(

tracer := &Tracer{
l: l.Named(_loggerName),
cachedData: []tracewriters.TimestampedFrame{},
cachedData: []writer.TimestampedFrame{},
err: utils.NewResettaleError(),
timeout: _defaultTimeout,
canInterface: canInterface,
Expand Down Expand Up @@ -91,10 +91,10 @@ func WithBusName(name string) TracerOption {
}
}

// WithFiles sets different filetypes the tracer can dump CAN data to
func WithTraceWriters(traceWriters []tracewriters.TraceWriter) TracerOption {
// WithWriters sets the slice of writers which manage trace files
func WithWriters(writer []writer.WriterIface) TracerOption {
return func(t *Tracer) {
t.traceWriters = traceWriters
t.writers = writer
}
}

Expand All @@ -107,7 +107,7 @@ func (t *Tracer) Open(ctx context.Context) error {
t.l.Info("canlink receiver created")

// IMPORTANT: frameCh must be open before isRunning is set
t.frameCh = make(chan tracewriters.TimestampedFrame, _frameBufferLength)
t.frameCh = make(chan writer.TimestampedFrame, _frameBufferLength)

go t.fetchData(ctx)

Expand All @@ -121,7 +121,7 @@ func (t *Tracer) StartTrace(ctx context.Context) error {

t.stop = make(chan struct{})

for _, writer := range t.traceWriters {
for _, writer := range t.writers {
err := writer.CreateTraceFile(t.traceDir, t.busName)
if err != nil {
return errors.Wrap(err, "create trace file")
Expand Down Expand Up @@ -169,7 +169,7 @@ func (t *Tracer) Close() error {
return errors.Wrap(err, "close socketcan receiver")
}

for _, writer := range t.traceWriters {
for _, writer := range t.writers {
err = writer.CloseTraceFile()
if err != nil {
return errors.Wrap(err, "close trace file")
Expand All @@ -187,7 +187,7 @@ func (t *Tracer) Error() error {
// fetchData fetches CAN frames from the socket and sends them over a buffered channel
func (t *Tracer) fetchData(ctx context.Context) {

timeFrame := tracewriters.TimestampedFrame{}
timeFrame := writer.TimestampedFrame{}

for t.receiver.Receive() {
select {
Expand Down Expand Up @@ -234,8 +234,8 @@ func (t *Tracer) receiveData(ctx context.Context) {
}
}

func (t *Tracer) writeFrameToFile(frame *tracewriters.TimestampedFrame) error {
for _, writer := range t.traceWriters {
func (t *Tracer) writeFrameToFile(frame *writer.TimestampedFrame) error {
for _, writer := range t.writers {
err := writer.WriteFrameToFile(frame)
if err != nil {
return errors.Wrap(err, "write frame to file")
Expand Down
41 changes: 0 additions & 41 deletions canlink/tracewriters/README.md

This file was deleted.

97 changes: 0 additions & 97 deletions canlink/tracewriters/asciiwriter.go

This file was deleted.

Loading

0 comments on commit 962175c

Please sign in to comment.