Skip to content

Commit

Permalink
Reimplement CSV encoding of log messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
bemasher committed Aug 27, 2014
1 parent 99a0f27 commit 3c74572
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
36 changes: 36 additions & 0 deletions csv/csv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package csv

import (
"encoding/csv"
"errors"
"io"
)

// Produces a list of fields making up a record.
type Recorder interface {
Record() []string
}

// An Encoder writes CSV records to an output stream.
type Encoder struct {
w *csv.Writer
}

// NewEncoder returns a new encoder that writes to w.
func NewEncoder(w io.Writer) *Encoder {
return &Encoder{w: csv.NewWriter(w)}
}

// Encode writes a CSV record representing v to the stream followed by a
// newline character. Value given must implement the Recorder interface.
func (enc *Encoder) Encode(v interface{}) (err error) {
record, ok := v.(Recorder)
if !ok {
return errors.New("value does not satisfy Recorder interface")
}

err = enc.w.Write(record.Record())
enc.w.Flush()

return nil
}
4 changes: 4 additions & 0 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"os"
"strconv"
"strings"

"github.com/bemasher/rtlamr/csv"
)

var logFilename = flag.String("logfile", "/dev/stdout", "log statement dump file")
Expand Down Expand Up @@ -95,6 +97,8 @@ func HandleFlags() {
switch *format {
case "plain":
break
case "csv":
encoder = csv.NewEncoder(logFile)
case "json":
encoder = json.NewEncoder(logFile)
case "xml":
Expand Down
22 changes: 22 additions & 0 deletions recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import (
"log"
"os"
"os/signal"
"runtime/pprof"
"strconv"
"strings"
"time"

"github.com/bemasher/rtlamr/csv"
"github.com/bemasher/rtltcp"
)

Expand Down Expand Up @@ -178,6 +180,7 @@ type Message interface {
MsgType() string
MeterID() uint32
MeterType() uint8
csv.Recorder
}

type LogMessage struct {
Expand Down Expand Up @@ -206,10 +209,20 @@ func (msg LogMessage) String() string {
)
}

func (msg LogMessage) Record() (r []string) {
r = append(r, msg.Time.Format(time.RFC3339Nano))
r = append(r, strconv.FormatInt(msg.Offset, 10))
r = append(r, strconv.FormatInt(int64(msg.Length), 10))
r = append(r, msg.Body.Record()...)
return r
}

func init() {
log.SetFlags(log.Lshortfile | log.Lmicroseconds)
}

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file")

func main() {
rcvr.RegisterFlags()
RegisterFlags()
Expand All @@ -223,5 +236,14 @@ func main() {
defer sampleFile.Close()
defer rcvr.Close()

if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}

rcvr.Run()
}
11 changes: 11 additions & 0 deletions scm.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,14 @@ func (scm SCM) String() string {
scm.ID, scm.Type, scm.TamperPhy, scm.TamperEnc, scm.Consumption, scm.Checksum,
)
}

func (scm SCM) Record() (r []string) {
r = append(r, strconv.FormatUint(uint64(scm.ID), 10))
r = append(r, strconv.FormatUint(uint64(scm.Type), 10))
r = append(r, "0x"+strconv.FormatUint(uint64(scm.TamperPhy), 16))
r = append(r, "0x"+strconv.FormatUint(uint64(scm.TamperEnc), 16))
r = append(r, strconv.FormatUint(uint64(scm.Consumption), 10))
r = append(r, "0x"+strconv.FormatUint(uint64(scm.Checksum), 16))

return
}

0 comments on commit 3c74572

Please sign in to comment.