newtmgr - Allow trace messages to log to console.

Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/c6ae94b4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/c6ae94b4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/c6ae94b4

Branch: refs/heads/develop
Commit: c6ae94b40d66d186543b20d5c307f80c38c82d07
Parents: efbbabb
Author: Christopher Collins <ccoll...@apache.org>
Authored: Wed Oct 12 19:28:32 2016 -0700
Committer: Christopher Collins <ccoll...@apache.org>
Committed: Wed Oct 12 19:28:32 2016 -0700

----------------------------------------------------------------------
 newtmgr/cli/commands.go         |  8 ++++++--
 newtmgr/nmutil/nmutil.go        | 28 ++++++++++++++++------------
 newtmgr/transport/connserial.go | 10 +++++-----
 3 files changed, 27 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/c6ae94b4/newtmgr/cli/commands.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/commands.go b/newtmgr/cli/commands.go
index 5e00eb5..58f8a9f 100644
--- a/newtmgr/cli/commands.go
+++ b/newtmgr/cli/commands.go
@@ -23,6 +23,7 @@ import (
        log "github.com/Sirupsen/logrus"
        "github.com/spf13/cobra"
 
+       "mynewt.apache.org/newt/newtmgr/nmutil"
        "mynewt.apache.org/newt/util"
 )
 
@@ -49,8 +50,11 @@ func Commands() *cobra.Command {
        nmCmd.PersistentFlags().StringVarP(&ConnProfileName, "conn", "c", "",
                "connection profile to use.")
 
-       nmCmd.PersistentFlags().StringVarP(&logLevelStr, "loglevel", "l", "",
-               "log level to use (default WARN.)")
+       nmCmd.PersistentFlags().StringVarP(&logLevelStr, "loglevel", "l", 
"info",
+               "log level to use (default INFO.)")
+
+       nmCmd.PersistentFlags().BoolVarP(&nmutil.TraceLogEnabled, "trace", "t",
+               false, "print all bytes transmitted and received")
 
        nmCmd.AddCommand(connProfileCmd())
        nmCmd.AddCommand(echoCmd())

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/c6ae94b4/newtmgr/nmutil/nmutil.go
----------------------------------------------------------------------
diff --git a/newtmgr/nmutil/nmutil.go b/newtmgr/nmutil/nmutil.go
index a95e95b..bc531ad 100644
--- a/newtmgr/nmutil/nmutil.go
+++ b/newtmgr/nmutil/nmutil.go
@@ -24,9 +24,12 @@ import (
        "fmt"
        "os"
        "time"
+
+       log "github.com/Sirupsen/logrus"
 )
 
 var PacketTraceDir string
+var TraceLogEnabled bool
 var traceFile *os.File
 
 // @return                      true if the file can be used;
@@ -53,22 +56,23 @@ func ensureTraceFileOpen() bool {
        return true
 }
 
-func LogIncoming(bytes []byte) {
-       if !ensureTraceFileOpen() {
-               return
+func traceText(text string) {
+       if ensureTraceFileOpen() {
+               fmt.Fprintf(traceFile, "%s\n", text)
+       }
+       if TraceLogEnabled {
+               log.Infof("%s", text)
        }
-
-       fmt.Fprintf(traceFile, "Incoming:\n%s\n", hex.Dump(bytes))
 }
 
-func LogOutgoing(bytes []byte) {
-       if !ensureTraceFileOpen() {
-               return
-       }
+func TraceIncoming(bytes []byte) {
+       traceText(fmt.Sprintf("Incoming:\n%s", hex.Dump(bytes)))
+}
 
-       fmt.Fprintf(traceFile, "Outgoing:\n%s\n", hex.Dump(bytes))
+func TraceOutgoing(bytes []byte) {
+       traceText(fmt.Sprintf("Outgoing:\n%s", hex.Dump(bytes)))
 }
 
-func LogMessage(msg string) {
-       fmt.Fprintf(traceFile, "Message: %s\n", msg)
+func TraceMessage(msg string) {
+       traceText(fmt.Sprintf("Message: %s\n", msg))
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/c6ae94b4/newtmgr/transport/connserial.go
----------------------------------------------------------------------
diff --git a/newtmgr/transport/connserial.go b/newtmgr/transport/connserial.go
index 6f7da9a..b7aaf4f 100644
--- a/newtmgr/transport/connserial.go
+++ b/newtmgr/transport/connserial.go
@@ -114,7 +114,7 @@ func (cs *ConnSerial) ReadPacket() (*Packet, error) {
        for scanner.Scan() {
                line := []byte(scanner.Text())
 
-               nmutil.LogIncoming(line)
+               nmutil.TraceIncoming(line)
 
                for {
                        if len(line) > 1 && line[0] == '\r' {
@@ -133,7 +133,7 @@ func (cs *ConnSerial) ReadPacket() (*Packet, error) {
 
                data, err := base64.StdEncoding.DecodeString(base64Data)
                if err != nil {
-                       nmutil.LogMessage("base64 decode error\n")
+                       nmutil.TraceMessage("base64 decode error\n")
                        return nil, util.NewNewtError(
                                fmt.Sprintf("Couldn't decode base64 string: 
%s\n"+
                                        "Packet hex dump:\n%s",
@@ -160,7 +160,7 @@ func (cs *ConnSerial) ReadPacket() (*Packet, error) {
                full := cs.currentPacket.AddBytes(data)
                if full {
                        if crc16.Crc16(cs.currentPacket.GetBytes()) != 0 {
-                               nmutil.LogMessage("CRC error\n")
+                               nmutil.TraceMessage("CRC error\n")
                                return nil, util.NewNewtError("CRC error")
                        }
 
@@ -178,7 +178,7 @@ func (cs *ConnSerial) ReadPacket() (*Packet, error) {
        if err == nil {
                // Scanner hit EOF, so we'll need to create a new one.  This 
only
                // happens on timeouts.
-               nmutil.LogMessage("Timeout reading from serial connection\n")
+               nmutil.TraceMessage("Timeout reading from serial connection\n")
                err = util.NewNewtError("Timeout reading from serial 
connection")
                cs.scanner = bufio.NewScanner(cs.serialChannel)
        }
@@ -186,7 +186,7 @@ func (cs *ConnSerial) ReadPacket() (*Packet, error) {
 }
 
 func (cs *ConnSerial) writeData(bytes []byte) {
-       nmutil.LogOutgoing(bytes)
+       nmutil.TraceOutgoing(bytes)
        log.Debugf("Writing %+v to data channel", bytes)
        cs.serialChannel.Write(bytes)
 }

Reply via email to