Skip to content

Commit

Permalink
write all commands including timestamp to logfile
Browse files Browse the repository at this point in the history
  • Loading branch information
julian59189 authored and deadprogram committed Nov 15, 2021
1 parent 06b6869 commit caf7a81
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 19 deletions.
27 changes: 14 additions & 13 deletions cmd/kd6ctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func main() {
rootFlagSet = flag.NewFlagSet("kd6ctl", flag.ExitOnError)
port = rootFlagSet.String("p", "/dev/corser/XtiumCLMX41_s0", "port of KD6RMX sensor to use")
logging = rootFlagSet.Bool("log", false, "turn on debug logging")
logFile = rootFlagSet.Bool("log-file", true, "turn on logging to file")
)

version := &ffcli.Command{
Expand All @@ -44,7 +45,7 @@ func main() {
return err
}

cis := kd6rmx.Sensor{Port: *port, Logging: *logging}
cis := kd6rmx.Sensor{Port: *port, Logging: *logging, FileLogging: *logFile}
return cis.LoadSettings(preset)
},
}
Expand All @@ -58,7 +59,7 @@ func main() {
return fmt.Errorf("needs test pattern value")
}

cis := kd6rmx.Sensor{Port: *port, Logging: *logging}
cis := kd6rmx.Sensor{Port: *port, Logging: *logging, FileLogging: *logFile}
switch args[0] {
case "on":
cis.TestPatternEnabled(true)
Expand Down Expand Up @@ -90,7 +91,7 @@ func main() {
return err
}

cis := kd6rmx.Sensor{Port: *port, Logging: *logging}
cis := kd6rmx.Sensor{Port: *port, Logging: *logging, FileLogging: *logFile}
return cis.SaveSettings(preset)
},
}
Expand All @@ -109,7 +110,7 @@ func main() {
return err
}

cis := kd6rmx.Sensor{Port: *port, Logging: *logging}
cis := kd6rmx.Sensor{Port: *port, Logging: *logging, FileLogging: *logFile}
return cis.OutputFrequency(float32(freq))
},
}
Expand Down Expand Up @@ -158,7 +159,7 @@ func main() {
return err
}

cis := kd6rmx.Sensor{Port: *port, Logging: *logging}
cis := kd6rmx.Sensor{Port: *port, Logging: *logging, FileLogging: *logFile}
return cis.PixelOutputFormat(bits, intf, conf, num)
},
}
Expand All @@ -182,7 +183,7 @@ func main() {
return fmt.Errorf("invalid interpolation, must be on or off")
}

cis := kd6rmx.Sensor{Port: *port, Logging: *logging}
cis := kd6rmx.Sensor{Port: *port, Logging: *logging, FileLogging: *logFile}
return cis.PixelInterpolation(on)
},
}
Expand All @@ -196,7 +197,7 @@ func main() {
return fmt.Errorf("dark correction requires a subcommand: 'on', 'off', or 'adjust'")
}

cis := kd6rmx.Sensor{Port: *port, Logging: *logging}
cis := kd6rmx.Sensor{Port: *port, Logging: *logging, FileLogging: *logFile}

switch args[0] {
case "on":
Expand All @@ -220,7 +221,7 @@ func main() {
return fmt.Errorf("white correction requires a subcommand: 'on', 'off', 'adjust', or 'target'")
}

cis := kd6rmx.Sensor{Port: *port, Logging: *logging}
cis := kd6rmx.Sensor{Port: *port, Logging: *logging, FileLogging: *logFile}

switch args[0] {
case "on":
Expand Down Expand Up @@ -283,7 +284,7 @@ func main() {
}
}

cis := kd6rmx.Sensor{Port: *port, Logging: *logging}
cis := kd6rmx.Sensor{Port: *port, Logging: *logging, FileLogging: *logFile}
return cis.LEDControl(leds, on, pulse)
},
}
Expand All @@ -307,7 +308,7 @@ func main() {
return err
}

cis := kd6rmx.Sensor{Port: *port, Logging: *logging}
cis := kd6rmx.Sensor{Port: *port, Logging: *logging, FileLogging: *logFile}
return cis.LEDDutyCycle(led, duty)
},
}
Expand All @@ -321,7 +322,7 @@ func main() {
return fmt.Errorf("adjust the gain number")
}

cis := kd6rmx.Sensor{Port: *port, Logging: *logging}
cis := kd6rmx.Sensor{Port: *port, Logging: *logging, FileLogging: *logFile}
switch args[0] {
case "on":
cis.GainAmplifierEnabled(true)
Expand All @@ -345,7 +346,7 @@ func main() {
ShortHelp: "Dump the register values of CIS.",
Exec: func(_ context.Context, args []string) error {

cis := kd6rmx.Sensor{Port: *port, Logging: *logging}
cis := kd6rmx.Sensor{Port: *port, Logging: *logging, FileLogging: *logFile}
cis.ReadRegister("BR")
cis.ReadRegister("OF")
cis.ReadRegister("OC")
Expand Down Expand Up @@ -378,7 +379,7 @@ func main() {
register := args[0]
command := args[1]

cis := kd6rmx.Sensor{Port: *port, Logging: *logging}
cis := kd6rmx.Sensor{Port: *port, Logging: *logging, FileLogging: *logFile}
cis.SendCommand(register, command)
return nil
},
Expand Down
47 changes: 41 additions & 6 deletions kd6rmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
Expand All @@ -12,8 +13,9 @@ import (

// Sensor is a wrapper for control functions for the KD6RMX contact image sensor.
type Sensor struct {
Port string
Logging bool
Port string
Logging bool
FileLogging bool
}

// CommunicationSpeed sets the communcation speed.
Expand Down Expand Up @@ -573,10 +575,27 @@ func (cis Sensor) SendCommand(cmd string, params string) (string, error) {
return "", fmt.Errorf("error opening control port: %v", err)
}
defer f.Close()

write_string := cmd + params + "\r"
dt_string := time.Now().Format("2006-01-02 15:04:05.000000000")

if cis.FileLogging {
f_log, err := os.OpenFile("kd6cmd.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return "", fmt.Errorf("cannot write to log file: %v", err)
}
defer f_log.Close()

if _, err := f_log.WriteString(dt_string + "\n"); err != nil {
log.Println(err)
}
if _, err := f_log.WriteString("send: " + write_string + "\n"); err != nil {
log.Println(err)
}
}

if cis.Logging {
dt := time.Now()
fmt.Printf("%s\n", dt.Format("2006-01-02 15:04:05.000000000"))
fmt.Printf("%s\n", dt_string)
fmt.Println("send: ", write_string)
}

Expand Down Expand Up @@ -607,10 +626,26 @@ func (cis Sensor) SendCommand(cmd string, params string) (string, error) {
switch {
case result[len(result)-1] == '\r':
result = strings.Replace(result, "\r", "", -1)

dt_string_rec := time.Now().Format("2006-01-02 15:04:05.000000000")
if cis.FileLogging {
f_log, err := os.OpenFile("kd6cmd.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return "", fmt.Errorf("cannot write to log file: %v", err)
}
defer f_log.Close()
if _, err := f_log.WriteString(dt_string_rec + "\n"); err != nil {
log.Println(err)
}
if _, err := f_log.WriteString("received: " + write_string + "\n\n"); err != nil {
log.Println(err)
}
}

if cis.Logging {
dt := time.Now()
fmt.Printf("%s\n", dt.Format("2006-01-02 15:04:05.000000000"))
fmt.Printf("%s\n", dt_string_rec)
fmt.Printf("received: %s\n\n", result)

}
return result, nil
case n == 0:
Expand Down

0 comments on commit caf7a81

Please sign in to comment.