Skip to content

Commit

Permalink
Remove -logfile flag.
Browse files Browse the repository at this point in the history
There was a lot of extra (and unnecessary) logic surrounding logging
various things to a file specified by the user. Removal of this flag is a
possibly breaking change.

The flag can be replicated entirely with file redirection.

rtlamr 2> rtlamr.log

rtlamr > rtlamr.log

rtlamr &> rtlamr.log

rtlamr >> rtlamr.log

rtlamr &> rtlamr.log

The -quiet flag was also removed as it no longer means anything.

The gob encoder was also removed for lack of interest/use.
  • Loading branch information
bemasher committed Oct 6, 2016
1 parent b2dba49 commit 3fef211
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 45 deletions.
43 changes: 8 additions & 35 deletions flags.go
Expand Up @@ -18,7 +18,6 @@ package main

import (
"bytes"
"encoding/gob"
"encoding/json"
"encoding/xml"
"flag"
Expand All @@ -32,9 +31,6 @@ import (
"github.com/bemasher/rtlamr/parse"
)

var logFilename = flag.String("logfile", "/dev/stdout", "log statement dump file")
var logFile *os.File

var sampleFilename = flag.String("samplefile", os.DevNull, "raw signal dump file")
var sampleFile *os.File

Expand All @@ -51,10 +47,8 @@ var meterType MeterTypeFilter
var unique = flag.Bool("unique", false, "suppress duplicate messages from each meter")

var encoder Encoder
var format = flag.String("format", "plain", "format to write log messages in: plain, csv, json, xml or gob")
var gobUnsafe = flag.Bool("gobunsafe", false, "allow gob output to stdout")
var format = flag.String("format", "plain", "format to write log messages in: plain, csv, json, or xml")

var quiet = flag.Bool("quiet", false, "suppress printing state information at startup")
var single = flag.Bool("single", false, "one shot execution, if used with -filterid, will wait for exactly one packet from each meter id")

var version = flag.Bool("version", false, "display build date and commit hash")
Expand All @@ -67,7 +61,6 @@ func RegisterFlags() {
flag.Var(meterType, "filtertype", "display only messages matching a type in a comma-separated list of types.")

rtlamrFlags := map[string]bool{
"logfile": true,
"samplefile": true,
"msgtype": true,
"symbollength": true,
Expand All @@ -76,12 +69,9 @@ func RegisterFlags() {
"filterid": true,
"filtertype": true,
"format": true,
"gobunsafe": true,
"quiet": true,
"unique": true,
"single": true,
"cpuprofile": true,
"fastmag": true,
"version": true,
}

Expand Down Expand Up @@ -125,16 +115,6 @@ func EnvOverride() {
func HandleFlags() {
var err error

if *logFilename == "/dev/stdout" {
logFile = os.Stdout
} else {
logFile, err = os.Create(*logFilename)
if err != nil {
log.Fatal("Error creating log file:", err)
}
}
log.SetOutput(logFile)

sampleFile, err = os.Create(*sampleFilename)
if err != nil {
log.Fatal("Error creating sample file:", err)
Expand All @@ -143,19 +123,13 @@ func HandleFlags() {
*format = strings.ToLower(*format)
switch *format {
case "plain":
encoder = PlainEncoder{*sampleFilename, logFile}
encoder = PlainEncoder{*sampleFilename}
case "csv":
encoder = csv.NewEncoder(logFile)
encoder = csv.NewEncoder(os.Stdout)
case "json":
encoder = json.NewEncoder(logFile)
encoder = json.NewEncoder(os.Stdout)
case "xml":
encoder = xml.NewEncoder(logFile)
case "gob":
encoder = gob.NewEncoder(logFile)
if !*gobUnsafe && *logFilename == "/dev/stdout" {
fmt.Println("Gob encoded messages are not stdout safe, specify non-stdout -logfile or use -gobunsafe.")
os.Exit(1)
}
encoder = xml.NewEncoder(os.Stdout)
}
}

Expand Down Expand Up @@ -227,14 +201,13 @@ func (uf UniqueFilter) Filter(msg parse.Message) bool {

type PlainEncoder struct {
sampleFilename string
logFile *os.File
}

func (pe PlainEncoder) Encode(msg interface{}) (err error) {
if pe.sampleFilename == os.DevNull {
_, err = fmt.Fprintln(pe.logFile, msg.(parse.LogMessage).StringNoOffset())
if m, ok := msg.(parse.LogMessage); ok && pe.sampleFilename == os.DevNull {
_, err = fmt.Println(m.StringNoOffset())
} else {
_, err = fmt.Fprintln(pe.logFile, msg.(parse.LogMessage))
_, err = fmt.Println(m)
}
return
}
14 changes: 4 additions & 10 deletions main.go
Expand Up @@ -87,14 +87,10 @@ func (rcvr *Receiver) NewReceiver() {
rcvr.SetGainMode(true)
}

if !*quiet {
rcvr.p.Log()
}
rcvr.p.Log()

// Tell the user how many gain settings were reported by rtl_tcp.
if !*quiet {
log.Println("GainCount:", rcvr.SDR.Info.GainCount)
}
log.Println("GainCount:", rcvr.SDR.Info.GainCount)

return
}
Expand Down Expand Up @@ -170,10 +166,9 @@ func (rcvr *Receiver) Run() {
log.Fatal("Error encoding message: ", err)
}

// The XML encoder doesn't write new lines after each
// element, add them.
// The XML encoder doesn't write new lines after each element, print them.
if _, ok := encoder.(*xml.Encoder); ok {
fmt.Fprintln(logFile)
fmt.Println()
}

pktFound = true
Expand Down Expand Up @@ -232,7 +227,6 @@ func main() {

rcvr.NewReceiver()

defer logFile.Close()
defer sampleFile.Close()
defer rcvr.Close()

Expand Down

0 comments on commit 3fef211

Please sign in to comment.